Framework
Model
- class mammoth.models.NMTModel(encoder, decoder, attention_bridge)[source]
Bases:
BaseModelCore trainable object in OpenNMT. Implements a trainable interface for a simple, generic encoder + decoder model. :param encoder: an encoder object :type encoder: mammoth.modules.layer_stack.StackXcoder :param decoder: a decoder object :type decoder: mammoth.modules.layer_stack.StackXcoder
- count_parameters()[source]
Count and log number of parameters in model.
- Returns:
encoder side parameter count
decoder side parameter count
- Return type:
(int, int)
- forward(src, decoder_input, src_mask, metadata=None)[source]
Forward propagate a src and tgt pair for training. Possible initialized with a beginning decoder state.
- Parameters:
src (Tensor) – A source sequence passed to encoder. typically for inputs this will be a padded LongTensor of size
(len, batch, features). However, may be an image or other generic input depending on encoder.tgt (LongTensor) – A target sequence passed to decoder. Size
(tgt_len, batch, features).lengths (LongTensor) – The src lengths, pre-padding
(batch,).bptt (Boolean) – A flag indicating if truncated bptt is set. If reset then init_state
- Returns:
decoder output
(tgt_len, batch, hidden)dictionary attention dists of
(tgt_len, batch, src_len)
- Return type:
(FloatTensor, dict[str, FloatTensor])
Model building
- mammoth.model_builder.build_model(model_opts, opts, vocabs_dict, task_queue_manager, single_task=None)[source]
Build a model from opts.
- Parameters:
model_opts – the option loaded from checkpoint. It’s important that the opts have been updated and validated. See
mammoth.utils.parse.ArgumentParser.opts – overriding options.
vocabs_dict (dict[str, mammoth.inputters.Vocab]) – Vocab objects for the model.
task_queue_manager – TaskQueueManager
single_task – corpus_id of task, to create a single-task model
- Returns:
the NMTModel.
- mammoth.model_builder.build_xcoder(side: Side, model_opts, vocabs_dict: Dict[Tuple[str, str], Vocab], device, task_queue_manager, single_task: str | None = None, token_embs: Dict[str, Embedding] | None = None) StackXcoder[source]
Build a StackXcoder for use as either Encoder or Decoder. side: a Side enum from distributed components model_opts: options vocabs_dict: A dict mapping (‘src’|’tgt’, lang) to a Vocab. device: torch.device task_queue_manager: TaskQueueManager single_task: if a task_id string is given, the built model contains only the components necessary for that task. token_embs: to tie encoder and decoder embeddings, pass existing embeddings here.
- mammoth.model_builder.freeze_model_components(model, opts, task_queue_manager, frame_checkpoint_path=None)[source]
Freeze model components based on configuration options. Must be called after model initialization but before optimizer creation, on every rank (all ranks must agree on what is frozen, see WorldGroupGradientSync).
This function implements granular freezing for parameter-efficient fine-tuning, allowing independent control over encoder, decoder, cross-attention, embeddings, and attention bridge freezing. On top of that, freeze_components / train_components select whole DistributedComponents by name (see resolve_frozen_components). The result is the union of both mechanisms.
- Parameters:
model – The NMTModel instance
opts – Configuration options containing freeze flags
task_queue_manager – TaskQueueManager for distributed component access
frame_checkpoint_path – the train_from frame file, needed for
@pretrained
Notes
Adapters are never frozen, unless a freeze_components pattern names them
Frozen parameters are excluded from optimizer automatically
Freezing happens before distributed initialization to ensure consistency
- mammoth.model_builder.resolve_frozen_components(opts, task_queue_manager, checkpoint_prefix=None) Set[str][source]
Names of the DistributedComponents to freeze, from opts.freeze_components / opts.train_components.
Patterns are fnmatch wildcards over component names (= checkpoint shard names, e.g. ‘encoder_0_all’, ‘src_embeddings_sme’, ‘decoder_wrapper_eng’).
@pretrainedselects every component that has a shard in the train_from checkpoint (checkpoint_prefix).freeze_components: freeze the matched components, train the rest.
train_components: train only the matched components, freeze the rest.
Adapters stay trainable in both modes, unless a freeze_components pattern contains ‘adapter’. Matching uses the global component list, so every rank gets the same answer. Every pattern must match something, to catch typos.
Trainer
- class mammoth.Trainer(model, loss_functions, optim, norm_method='sents', accum_count=[1], accum_steps=[0], device_context=None, gpu_verbose_level=0, report_manager=None, model_saver=None, average_decay=0, average_every=1, model_dtype='fp32', earlystopper=None, dropout=[0.3], dropout_steps=[0], task_queue_manager=None, report_stats_from_parameters=False, report_training_accuracy=False, valid_metrics=None, valid_max_length=None, valid_max_batches=None, valid_timeout=None, valid_decode_timeout=None, valid_start=0, vocabs_dict=None, beam_size=1, max_length=100, block_ngram_repeat=0, world_group_sync=None, flops_config=None, report_tflops=True)[source]
Bases:
objectClass that controls the training process.
- Parameters:
model (
mammoth.models.model.NMTModel) – translation model to trainloss_functions – ModelDict containing loss functions
optim (
mammoth.utils.optimizers.Optimizer) – the optimizer responsible for updatedata_type (string) – type of the source input: [text]
norm_method (string) – normalization methods: [sents|tokens]
accum_count (list) – accumulate gradients this many times.
accum_steps (list) – steps for accum gradients changes.
report_manager (
mammoth.utils.ReportMgrBase) – the object that creates reports, or Nonemodel_saver (
mammoth.models.ModelSaverBase) – the saver is used to save a checkpoint. Thus nothing will be saved if this parameter is None
- train(train_iter, train_steps, save_checkpoint_steps=5000, valid_iter=None, valid_steps=10000, device_context=None)[source]
The main training loop by iterating over train_iter and possibly running validation on valid_iter.
- Parameters:
train_iter – A generator that returns the next training batch.
train_steps – Run training for this many iterations.
save_checkpoint_steps – Save a checkpoint every this many iterations.
valid_iter – A generator that returns the next validation batch.
valid_steps – Run evaluation every this many iterations.
- Returns:
The gathered statistics.
- validate(valid_iter, moving_average=None, task=None)[source]
Validate model using autoregressive generation.
- Parameters:
valid_iter – validate data iterator
moving_average – optional moving average parameters
task – optional task specification
- Returns:
validation loss statistics
- Return type:
nmt.Statistics
- class mammoth.utils.Statistics(loss=0, n_words=0, n_correct=None)[source]
Bases:
objectAccumulator for loss statistics. Currently calculates:
accuracy
perplexity
elapsed time
- accumulate_device(loss, n_words, nan_flag, n_src_words=None, metadata=None)[source]
Accumulate one window’s statistics as GPU tensors, WITHOUT syncing.
Everything stays on the GPU; nothing is copied to the CPU here, so this adds no .item() stall to the training loop. Call materialize() to read the running totals back (a single GPU->CPU sync) when they are needed.
- Parameters:
loss – scalar GPU tensor, summed loss for this window.
n_words – scalar GPU tensor, summed target (non-padding) tokens.
nan_flag – scalar GPU bool tensor, True if any loss in the window was NaN.
n_src_words – optional scalar GPU tensor, summed source tokens.
metadata – optional task metadata; if given, the window loss is also tracked per task (src_lang_tgt_lang).
- static all_gather_stats(stat, max_size=4096)[source]
Gather a Statistics object accross multiple process/nodes
- Parameters:
stat( – obj:Statistics): the statistics object to gather accross all processes/nodes
max_size (int) – max buffer size to use
- Returns:
Statistics, the update stats object
- static all_gather_stats_list(stat_list, max_size=4096)[source]
Gather a Statistics list accross all processes/nodes
- Parameters:
stat_list (list([Statistics])) – list of statistics objects to gather accross all processes/nodes
max_size (int) – max buffer size to use
- Returns:
list of updated stats
- Return type:
our_stats(list([Statistics]))
- classmethod from_loss_logits_target(loss: float, logits, target, padding_idx)[source]
Alternate constructor for computing the stats from loss, model prediction logits, and target indices. Note that this is heavy. Only use for validation / debug purposes.
- log_tensorboard(prefix, writer, learning_rate, patience, step)[source]
display statistics to tensorboard
- materialize()[source]
Fold the device-side accumulators into the CPU fields (one sync).
This is the ONLY place the deferred statistics touch the CPU. All pending GPU scalars are stacked into a single tensor and read back with one .tolist(), so a whole reporting interval costs exactly one GPU->CPU synchronisation instead of one per step. No-op (and idempotent) when nothing is pending.
- output(step, num_steps, learning_rate, start, metadata=None)[source]
Write out statistics to stdout.
- Parameters:
step (int) – current step
n_batch (int) – total batches
start (int) – start time of step.
Optimizer
Optimizers class
- class mammoth.utils.optimizers.AdaFactorFairSeq(params, lr=None, eps=(1e-30, 0.001), clip_threshold=1.0, decay_rate=-0.8, beta1=None, weight_decay=0.0, scale_parameter=True, relative_step=True, warmup_init=False)[source]
Bases:
OptimizerImplements Adafactor algorithm.
This implementation is based on: Adafactor: Adaptive Learning Rates with Sublinear Memory Cost (see https://arxiv.org/abs/1804.04235)
Note that this optimizer internally adjusts the learning rate depending on the scale_parameter, relative_step and warmup_init options. To use a manual (external) learning rate schedule you should set scale_parameter=False and relative_step=False.
- Parameters:
params (iterable) – iterable of parameters to optimize or dicts defining parameter groups
lr (float, optional) – external learning rate (default: None)
eps (tuple[float, float]) – regularization constans for square gradient and parameter scale respectively (default: (1e-30, 1e-3))
clip_threshold (float) – threshold of root mean square of final gradient update (default: 1.0)
decay_rate (float) – coefficient used to compute running averages of square gradient (default: -0.8)
beta1 (float) – coefficient used for computing running averages of gradient (default: None)
weight_decay (float, optional) – weight decay (L2 penalty) (default: 0)
scale_parameter (bool) – if True, learning rate is scaled by root mean square of parameter (default: True)
relative_step (bool) – if True, time-dependent learning rate is computed instead of external learning rate (default: True)
warmup_init (bool) – time-dependent learning rate computation depends on whether warm-up initialization is being used (default: False)
- class mammoth.utils.optimizers.MultipleOptimizer(suboptimizers, grad_scaler=None)[source]
Bases:
objectSeparate sub-optimizers for each distributed component. Handles creation of multiple optimizers, grad scaling, restoring from checkpoint, backward, zero_grad, deciding which suboptimizers to step, and reporting.
- property amp
True if use torch amp mix precision training.
- property training_step
Global training step, incremented for each minibatch. There may not be any actual parameters that have been trained this number of steps, or any optimizer stepped this number of times.
- class mammoth.utils.optimizers.SubOptimizer(optimizer, learning_rate, learning_rate_decay_fn, max_grad_norm=0, grad_scaler=None)[source]
Bases:
objectWraps a base optimizer (torch.optim.Optimizer). Handles learning rate scheduling and grad clipping.
- property training_step
The current training step.
- mammoth.utils.optimizers.exponential_decay(step, rate, decay_steps, start_step=0)[source]
A standard exponential decay, scaling the learning rate by
rateeverydecay_stepssteps.
- mammoth.utils.optimizers.get_base_optimizer(opts)[source]
Builds the PyTorch optimizer factory.
We use the default parameters for Adam that are suggested by the original paper https://arxiv.org/pdf/1412.6980.pdf These values are also used by other established implementations, e.g. https://www.tensorflow.org/api_docs/python/tf/compat/v1/train/AdamOptimizer https://keras.io/optimizers/ Recently there are slightly different values used in the paper “Attention is all you need” https://arxiv.org/pdf/1706.03762.pdf, particularly the value beta2=0.98 was used there however, beta2=0.999 is still arguably the more established value, so we use that here as well
- Parameters:
options. (opts. The dictionary of)
- Returns:
A callable that returns
torch.optim.Optimizerinstances.
- mammoth.utils.optimizers.make_learning_rate_decay_fn(opts)[source]
Returns the learning decay function from options.
- mammoth.utils.optimizers.noam_decay(step, warmup_steps, model_dim)[source]
Learning rate schedule described in https://arxiv.org/pdf/1706.03762.pdf.