Data preparation guide#
Primus routes training through Megatron-LM, TorchTitan, and MaxText. Each backend expects its own data format and preprocessing pipeline. This guide summarizes how to prepare data, how to use mock data for smoke tests, and which environment variables commonly apply.
Scripts referenced below live under the Primus repository root, for example:
examples/megatron/preprocess_data.pyexamples/megatron/prepare.pyexamples/megatron/prepare_bookcorpus_megatron_dataset.pyexamples/torchtitan/prepare.py
1. Overview#
Backend |
Format |
Typical entry |
|---|---|---|
Megatron |
Indexed |
|
TorchTitan |
Hugging Face datasets + local assets |
|
MaxText |
TFDS / Hugging Face / Grain / synthetic |
|
All backends support synthetic or mock data for configuration and scaling tests without large downloads.
2. Mock data (testing)#
Megatron#
Set in the trainer module:
mock_data: true
Default in primus/configs/modules/megatron/trainer_base.yaml is false. When true, training uses generated data matching configured dimensions so you can validate YAML, parallelism, and throughput without real corpora.
TorchTitan#
training:
mock_data: true
Default in primus/configs/modules/torchtitan/pre_trainer.yaml is true (useful for quick runs; set false and supply real datasets for production).
MaxText#
Use dataset_type: synthetic (or other synthetic paths in MaxText configs). See third_party/maxtext/src/MaxText/configs/base.yml and model YAMLs under third_party/maxtext/src/MaxText/configs/.
3. Megatron data pipeline#
Inputs#
Raw JSON or JSONL text (one JSON object per line for JSONL).
Optional sentence splitting via NLTK when
--split-sentencesis used (requires NLTK data; see Environment variables).
Preprocessing: examples/megatron/preprocess_data.py#
The script tokenizes input and writes Megatron indexed datasets (.bin + .idx). It uses build_tokenizer from Primus’s Megatron tokenizer integration and accepts tokenizer flags from _add_tokenizer_args.
Important arguments (from the script’s argparse):
Argument |
Description |
|---|---|
|
Path to input JSON (required). |
|
Keys to read (default |
|
Output path without suffix; produces `{prefix}{key}{document |
|
Number of worker processes (required). |
|
Split input for parallel preprocessing (default |
|
Run NLTK sentence splitting before encode. |
|
Append end-of-document token. |
Example (mirrors examples/megatron/prepare.py for BookCorpus-style flows):
python3 examples/megatron/preprocess_data.py \
--input /path/to/train.json \
--tokenizer-type HuggingFaceTokenizer \
--tokenizer-model /path/to/tokenizer \
--output-prefix /path/to/out/bookcorpus_train \
--workers "$(nproc)" \
--split-sentences \
--partitions 2
Configuring training runs#
Parameter |
Notes |
|---|---|
|
Single path or weighted blend: |
|
Separate splits when used |
|
Train/valid/test ratio string, e.g. |
|
Megatron dataloader type; default in |
BookCorpus example scripts#
examples/megatron/prepare_bookcorpus_megatron_dataset.py—downloads BookCorpus to JSON via Hugging Facedatasets, optional--out-dir.examples/megatron/prepare.py—orchestrates download, train/valid split, and callspreprocess_data.pywith tokenizer settings from Primus config; respectsTOKENIZED_TRAIN_DATA_PATH/TOKENIZED_EVAL_DATA_PATHfor output locations.
Tokenizers#
Tokenizer type and model path are set on the model preset (for example tokenizer_type, tokenizer_model in primus/configs/models/megatron/language_model.yaml comments list Llama2Tokenizer, HuggingFaceTokenizer, etc.).
4. TorchTitan data pipeline#
TorchTitan uses Hugging Face datasets style identifiers and local paths.
Key |
Default ( |
Description |
|---|---|---|
|
|
Dataset identifier for TorchTitan loaders. |
|
|
Local directory for dataset assets when needed. |
Tokenizer and model assets are resolved from model.hf_assets_path (or equivalent in your model preset). The preparation script examples/torchtitan/prepare.py:
Resolves the TorchTitan checkout path.
Runs
scripts/download_hf_assets.pyinside TorchTitan to fetch tokenizer assets for a givenrepo_id.Uses
HF_TOKENwhen the model or dataset is gated.
5. MaxText data pipeline#
MaxText configuration is defined in upstream YAML (for example third_party/maxtext/src/MaxText/configs/base.yml).
Parameter |
Meaning |
|---|---|
|
One of |
|
Hugging Face pipeline inputs when |
|
Batch sizing on each device. |
|
Sequence packing for efficiency (default |
See MaxText’s data input documentation for Grain and TFDS specifics.
6. Environment variables for data#
Variable |
Usage |
|---|---|
|
Tokenized dataset locations for Megatron hooks and examples (see |
|
Override output paths in |
|
General data root used in scripts and CI-style launches. |
|
Required for gated Hugging Face models and some datasets (TorchTitan |
|
Hugging Face cache directory (used in |
|
NLTK tokenizer data directory for sentence splitting in |
Summary#
Use mock or synthetic data to validate configs and performance before investing in large preprocessing jobs.
For Megatron, convert JSON/JSONL to
.bin/.idxwithpreprocess_data.pyand pointdata_pathor split paths at the outputs.For TorchTitan, set
training.dataset/dataset_pathand runexamples/torchtitan/prepare.pyto fetch tokenizer assets.For MaxText, configure
dataset_typeandper_device_batch_sizeper upstreambase.ymland model YAMLs.