Repository navigation
MegatronDataset: Megatron Core indexed dataset (.bin/.idx) token IDs - #2013
Merged
Merged
Conversation
Reads the MMIDIDX version 1 format of Megatron Core (IndexedDatasetBuilder) with NumPy only, no megatron-core, PyTorch or tokenizer needed. Data file and index file are separate options, so another index can refer to an unchanged data file. One seq per index entry, data keys data (sparse, vocab_size required) and document_idx. Both files are memory-mapped lazily, also after pickling. Seq ordering, partition_epoch and sharding via the generic seq order. Integer dtype codes only, malformed or truncated index files and invalid spans raise.
data_file and index_file can be lists, one index file per data file, so a sub epoch of DistributeFilesDataset with several shards is one MegatronDataset. Seq tags doc-<d>/seq-<j> (document over all files, entry within the document), instead of the separate document_idx data key.
albertz
marked this pull request as ready for review
October 16, 2026 20:20
albertz
marked this pull request as draft
October 17, 2026 12:24
Seq tags <name>/doc-<d>/seq-<j>, with name the index file base name without .idx and the document within that index file. Before, documents were counted over the file list, so the tag of a seq changed with the other files and their order, e.g. per sub epoch of DistributeFilesDataset. Index file base names must be unique.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
New dataset
MegatronDatasetfor the Megatron Core indexed dataset format (MMIDIDXversion 1),as written by Megatron's
IndexedDatasetBuilder(reference).
A
.binfile holds the token IDs, its.idxfile the seq lengths, byte pointers and document boundaries.This format is used by many LM corpora (e.g. Nemotron), not specific to one corpus.
data_fileandindex_file: a file or a list of files, one index file per data file.Separate options, so another index (e.g. a resegmentation) can refer to an unchanged data file.
A list makes a sub epoch of
DistributeFilesDatasetwith several shards oneMegatronDataset.data: token IDs as stored (incl. EOD and special IDs), sparse, int32.<name>/doc-<d>/seq-<j>: entryjof documentdof the index file,<name>its base name without.idx.So a tag does not depend on the other files or their order (e.g. per sub epoch of
DistributeFilesDataset).Unique (document indices are checked, index file base names must be unique), and the document is directly in the tag.
The format has no other identifiers (no document IDs or names, Megatron's preprocessing drops them).
vocab_sizeis required (the index does not store it, and the payload is not scanned for it).Optional
vocabmust havevocab_sizelabels. Token IDs outside[0, vocab_size)raise when the seq is loaded.Float codes, unknown codes, wrong magic or version, truncated or multimodal index files,
document indices not from 0 to num seqs or not monotonic, negative lengths and spans outside the data file raise
ValueError.The checks are vectorized and chunked, once on the first access.
partition_epochand sharding (_num_shards/set_sharding) via the genericget_seq_order_for_epoch(a NumPy permutation over the seqs of this dataset, no Python lists).
GPTDataset.Tests (
tests/test_Dataset.py, CPU, py3.12, torch 2.12 env at RWTH):test_MegatronDataset: uint16 (with ID 65535) and int32, empty document, zero-length seq, pickling before and after the first access,tags and
seq_list(also invalid tags), the same seqs under the same tags over different file lists and orders,duplicate index base names, a separately written split index over the unchanged data file
(sorted ordering, documents reassemble losslessly).
test_MegatronDataset_invalid: all the error cases above, the vocab size mismatch, and mismatching file list lengths.test_MegatronDataset_partition_epoch_sharding: 3 sub-epochs x 2 shards cover every seq exactly once, deterministic, new order next epoch.test_MegatronDataset_DistributeFilesDataset: 6 shard files over 3 sub-epochs, 2 files per sub epoch dataset.Dataset class 'MegatronDataset' not found).Full
test_Dataset.py: 49 passed, 9 skipped.IndexedDatasetBuilder(add_document,add_item,end_document, empty document, zero-length seq),uint16 and int32: IDs, lengths, pointers and document indices equal megatron-core's
IndexedDataset, tags match its document indices.