Integrate Your Own Text Dataset
This guide shows you how to integrate your own dataset into NeMo AutoModel for training. You will learn about two main dataset types: completion datasets for language modeling (such as HellaSwag) and instruction datasets for question-answering tasks (such as SQuAD). This guide covers how to create custom datasets by implementing the required methods and preprocessing functions, and finally shows you how to specify your own data logic using YAML configuration with file paths, allowing you to define custom dataset processing without modifying the main codebase.
Quick Start Summary
Types of Supported Datasets
NeMo AutoModel supports a variety of datasets, depending on the task.
Completion Datasets
Completion datasets are single text sequences designed for language modeling where the model learns to predict the next token given a context. These datasets typically contain a context (prompt) and a target (completion) that the model should learn to generate.
Example: HellaSwag
The HellaSwag dataset is a popular completion dataset used for commonsense reasoning. It contains situations with multiple-choice endings where the model must choose the most plausible continuation.
HellaSwag dataset structure:
- Context (
ctx): A situation or scenario description - Endings: Multiple possible completions (four options)
- Label: Index of the correct ending
Example:
Preprocess with SFTSingleTurnPreprocessor
NeMo AutoModel provides the SFTSingleTurnPreprocessor class to handle completion datasets. This processor:
- Extracts context and target using
get_context()andget_target(). - Tokenizes and cleans the context and target separately.
- Concatenates the context and target into one sequence.
- Creates a loss mask with
-100for the context and target IDs for the target. - Pads the sequences to an equal length.
Create Your Own Completion Dataset
To adapt your dataset into this format, define a class such as the following:
Instruction Datasets
Instruction datasets are question-answer pairs where the model learns to respond to specific instructions or questions. These datasets are structured as context-question pairs with corresponding answers, making them ideal for teaching models to follow instructions and provide accurate responses.
Example: SQuAD
The SQuAD (Stanford Question Answering Dataset) is a popular instruction dataset for reading comprehension. It contains questions based on Wikipedia articles along with their answers.
SQuAD dataset structure:
- Context: A paragraph of text from Wikipedia
- Question: A question about the context
- Answers: The correct answer with its position in the context
Create Your Own Instruction Dataset
The squad.py file contains the implementation for processing the SQuAD dataset into a format suitable for instruction tuning. It defines a dataset class and preprocessing functions that extract the context, question, and answer fields, concatenate them into a prompt-completion format, and apply tokenization, padding, and loss masking. This serves as a template for building custom instruction datasets by following a similar structure and adapting the extraction logic to your dataset’s schema.
Based on the SQuAD implementation in squad.py, you can create your own instruction dataset using the make_squad_dataset pattern:
YAML-Based Custom Dataset Configuration
NeMo AutoModel supports YAML-based dataset specification using the _target_ key. This allows you to reference dataset-building classes or functions using either of the following methods:
- Python Dotted Path
- File Path and Function Name
where:
<file-path>: The absolute path to a Python file containing your dataset function<function-name>: The name of the function to call from that file
This calls build_my_dataset() from the specified file with the other keys (such as num_blocks) as arguments. This approach allows you to integrate custom datasets using configuration alone, with no need to alter the codebase or package structure.
Packed Sequence Support in NeMo AutoModel
NeMo AutoModel supports packed sequences, which is a technique to optimize training with variable-length sequences (such as text) by minimizing padding.
What Is a Packed Sequence?
Instead of padding each sequence to a fixed length, which wastes computation on [PAD] tokens, packed sequences perform the following actions:
- Concatenate short sequences into a single continuous sequence.
- Separate sequences with special tokens (such as
[EOS]). - Track lengths using an “attention mask” to prevent cross-sequence information leakage.
Benefits
Packed sequences offer the following benefits:
- They reduce redundant computation on padding tokens, which leads to faster training.
- They enable larger effective batch sizes, which leads to better GPU utilization.
- They are especially useful for language modeling and text datasets.
Enable Packed Sequences in NeMo AutoModel
To enable packed sequences, add these keys to your recipe’s YAML config:
The packed_sequence block accepts the following settings:
- packed_sequence_size: Defines the total token length of each packed sequence. Higher values require more GPU memory.
- packing_strategy: Selects
thd(the default sequence-length-aware format) orneat. Withthd, AutoModel disables packing when the model does not supportseq_lens. - prepacked: If
true, treats the dataset as already packed and skips recipe-side packing. - max_packs: Optionally caps the number of packed samples created.
- drop_long_samples: For
neat, drops samples longer thanpacked_sequence_sizewhentrue. Otherwise, the packer raises an error. - num_proc: Specifies the number of worker processes used to tokenize the dataset before packing. Packing first runs one full pass over the dataset, which tokenizes every sample serially. Setting
num_proc > 1front-loads that tokenization across processes, producing identical, order-preserving output. This defaults to1(serial). The speedup grows with the per-sample tokenization cost (such as long multi-turn chats and heavy chat templates) and available CPU cores. This runs independently on each data-parallel rank, so configure it so thatlocal_ranks * num_procdoes not oversubscribe the node’s CPUs.
Troubleshooting Tips
- Tokenization Mismatch? Ensure your tokenizer aligns with the model’s expected inputs.
- Dataset too large? Use
limit_dataset_samplesin your YAML config to load a subset, which is useful for quick debugging. - Loss not decreasing? Verify that your loss mask correctly ignores prompt tokens.