nemo_automodel.components.datasets.llm.seq2seq
nemo_automodel.components.datasets.llm.seq2seq
Seq2seq (encoder-decoder) fine-tuning dataset for AutoModelForSeq2SeqLM.
Encoder-decoder models such as T5 and BART differ from decoder-only models in how the training batch is laid out:
- The encoder reads
input_ids(the source) with its ownattention_mask. labelsare the target tokens, kept at full length and not shifted. The model’s loss alignslogits[i]withlabels[i]directly.decoder_input_idsis the right-shifted copy oflabels(teacher forcing). HuggingFace builds this internally whenlabelsis passed, but the training loop popslabelsbefore calling the model, so we build it here and put it in the batch so it survives.
This is the opposite of the causal SFT path (see
formatting_utils._package_tokenized_example), which concatenates prompt and
answer into one stream and pre-shifts input_ids/labels by one position.
The produced per-sample dict reuses the ___PAD_TOKEN_IDS___ convention so
that utils.default_collater pads each field with the right value
(labels -> -100, the rest -> the pad id / 0).
Module Contents
Functions
Data
API
Pull a target string out of a dataset field.
Supports plain strings and the SQuAD answers layout
({"text": [...], "answer_start": [...]}).
Turn one raw example into the seq2seq batch fields.
Returns a dict with input_ids, attention_mask, labels (unshifted)
and decoder_input_ids (right-shifted labels), plus the
___PAD_TOKEN_IDS___ metadata used by default_collater.
Right-shift target tokens to form decoder inputs (teacher forcing).
Mirrors HuggingFace shift_tokens_right / T5 _shift_right: prepend
decoder_start_token_id and drop the final token, so position i of the
result is the input that should predict token_ids[i].
Parameters:
The (unshifted) target token ids.
The token the decoder starts from.
Returns:
A list of the same length as token_ids.
Load and preprocess a dataset for encoder-decoder (seq2seq) fine-tuning.
Each example is tokenized into an encoder source (input_ids +
attention_mask) and a decoder target. The target becomes the unshifted
labels; decoder_input_ids is its right-shifted copy. default_collater
pads labels with -100 and the id fields with the pad id.
Parameters:
A HuggingFace tokenizer (injected by the recipe). Must support
the text_target argument for target-side tokenization.
If set, truncate source and target to this length.
If set, only load this many examples from the split.
Dataset split to load (e.g. “train”, “validation”).
HuggingFace dataset identifier. Defaults to SQuAD, framed as a question+context -> answer seq2seq task.
str.format template applied to each
example to build the source text. If None, source_key is used
verbatim.
Field used as the source when source_template is
None.
Field holding the target. Supports plain strings and
the SQuAD answers dict layout.
Token the decoder starts from. If None, defaults to the tokenizer’s pad id (correct for T5/mT5). Models with a different convention (e.g. BART uses eos) should set this explicitly.
Whether to truncate to seq_length when it is set.
Returns:
A LazyMappedDataset yielding the per-sample seq2seq fields.