Data Loaders

Dataset

class mammoth.inputters.dataset.ParallelCorpus(src_file, tgt_file, src_vocab, tgt_vocab, transforms, device='cpu', stride=None, offset=None, is_train=False, task=None, max_length=None, line_idx_restore=None, model_max_seq_len=None, verbose_dataloader=False, device_rank=0)[source]

Bases: IterableDataset

Torch-style dataset

Data loading

class mammoth.inputters.dataloader.DynamicDatasetIter(task_queue_manager, opts, corpora_info, transforms_cls, vocabs_dict, is_train, batch_type, batch_size, batch_size_multiple, max_look_ahead_sentences=20480, lookahead_minibatches=4, line_idx_restore=None)[source]

Bases: object

Yield batch from (multiple) plain text corpus.

Parameters:
  • corpora (dict[str, ParallelCorpus]) – collections of corpora to iterate;

  • corpora_info (dict[str, dict]) – corpora infos correspond to corpora;

  • transforms (dict[str, Transform]) – transforms may be used by corpora;

  • fields (dict[str, Field]) – fields dict for convert corpora into Tensor;

  • is_train (bool) – True when generate data for training;

  • batch_type (str) – batching type to count on, choices=[tokens, sents];

  • batch_size (int) – numbers of examples in a batch;

  • batch_size_multiple (int) – make batch size multiply of this;

  • max_look_ahead_sentences (int) – accum this number of examples in a dynamic dataset;

  • skip_empty_level (str) – security level when encouter empty line;

  • stride (int) – iterate data files with this stride;

  • offset (int) – iterate data files with this offset.

Variables:
  • dataset_adapter (DatasetAdapter) – organize raw corpus to tensor adapt;

  • mixer (MixingStrategy) – the strategy to iterate corpora.

classmethod from_opts(task_queue_manager, transforms_cls, vocabs_dict, opts, is_train, line_idx_restore)[source]

Initilize DynamicDatasetIter with options parsed from opts.

class mammoth.inputters.dataloader.SimpleLookAheadBucketing(dataset, max_look_ahead_sentences, lookahead_minibatches, batch_size, score_fn=None)[source]

Bases: object

Parameters:
  • dataset – mammoth.inputters.ParallelCorpus

  • max_look_ahead_sentences – The maximum number of sentence pairs to read before yielding minibatches. Limits the time spent looping if there is a corpus with unexpectedly short sentences.

  • lookahead_minibatches – The number of minibatches that will be yielded once bucketing is complete. Recommended value: same as accum_count, or at least a multiple of it. Setting lookahead_minibatches == accum_count means that each accumulated batch uses up the whole buffer. All tasks stay in sync concerning the length sorting: each task begins with the smallest minibatch and ends with the largest just before accumulation ends.

  • batch_size – The maximum size of each minibatch in tokens. Note that the maximum batch size can not be guaranteed if the data contains examples that exceed the limit on their own. Use filtertoolong to avoid such examples.

  • score_fn – Compute the size estimate (single integer) for sorting examples.

class mammoth.inputters.dataloader.InferenceBatcher(dataset, batch_size)[source]

Bases: object

Iterator for inference

Vocab

class mammoth.inputters.vocab.HFTokenizerVocab(tokenizer_path, tag='', decoder_start_with_eos=False)[source]

Bases: object

Wrapper for HuggingFace tokenizers that provides MAMMOTH Vocab interface.

decode_token(token_id)[source]

Decode a single token ID to its string representation.

For BPE tokenizers, this returns the raw token string (e.g., ‘Ġhello’). For proper text decoding, use tokenizer.decode([ids]) instead.

Parameters:

token_id – Integer token ID

Returns:

Token string or ‘<unk>’ if ID not in vocabulary

decode_tokens(token_ids, skip_special_tokens=True)[source]

Decode a sequence of token IDs to text using the tokenizer’s decoder.

This properly handles BPE merging and special token removal.

Parameters:
  • token_ids – List of integer token IDs

  • skip_special_tokens – Whether to remove special tokens from output

Returns:

Decoded text string

tokenize(text, is_train=False)[source]

Tokenize text using the HuggingFace tokenizer.

Parameters:
  • text – Input text string or list of words

  • is_train – Training mode flag (for future regularization support)

Returns:

List of token strings

tokenize_example(example, is_train=False)[source]

Tokenize both src and tgt in an example dict.

Parameters:
  • example – Dict with ‘src’ and ‘tgt’ keys containing text

  • is_train – Training mode flag

Returns:

Modified example dict with tokenized src and tgt

class mammoth.inputters.vocab.Vocab(path, items=None, tag='', size=None, specials=[])[source]

Bases: object

classmethod merge(*vocabs, size=None)[source]

Merge vocabs.