bridge.data.builders.mock_gpt_sft#
Serializable config and runtime builder for synthetic variable-length text SFT samples.
The samples exercise global-batch packing and dynamic context parallelism without a dataset or tokenizer. Each sample
is random token IDs whose length is drawn from buckets defined by the context-parallel capacity it needs. With the
default three buckets: at most seq_length // 4 tokens (fits one rank of a CP4 group), at most seq_length // 2
(CP2), or up to seq_length (CP4); four buckets start at seq_length // 8 for a CP8 group. Lengths are uniform
within each bucket, and only the final completion_fraction of each sample is supervised.
Lengths are drawn in blocks of LENGTH_BLOCK_SIZE samples, each with the exact bucket split and its own seed, so a
sample’s length and tokens depend only on its index, not on how many samples a run requests.
With enable_offline_packing, the samples are first packed offline into rows of at most
offline_packing_specs.packed_sequence_size tokens, and each dataset item is one row that yields its samples, so the
global batch counts packed rows as in offline packed SFT. Rows are packed first-fit decreasing over consecutive chunks of
OFFLINE_PACKING_CHUNK_SIZE samples, so they too do not depend on how many rows a run requests.
Module Contents#
Classes#
Synthetic variable-length text SFT samples for global-batch packing. |
|
Random-token SFT samples of fixed per-index length, collated as unpacked global-batch packing samples. |
|
Build synthetic variable-length SFT splits from declarative settings. |
Functions#
Return the inclusive length range of each bucket: CP1, CP2, CP4, … capacity of |
|
Split |
|
Return the lengths of samples |
|
Return the deterministic training length of every sample of |
|
Pack mock samples offline into |
|
Build synthetic SFT splits through the canonical runtime builder; no tokenizer is used. |
Data#
Samples per independently seeded block of drawn lengths. |
|
Samples packed together into offline rows; a multiple of |
API#
- bridge.data.builders.mock_gpt_sft._SPLITS#
(‘train’, ‘validation’, ‘test’)
- bridge.data.builders.mock_gpt_sft.LENGTH_BLOCK_SIZE#
1024
Samples per independently seeded block of drawn lengths.
- bridge.data.builders.mock_gpt_sft.OFFLINE_PACKING_CHUNK_SIZE#
None
Samples packed together into offline rows; a multiple of
LENGTH_BLOCK_SIZE.
- class bridge.data.builders.mock_gpt_sft.MockGPTSFTDatasetConfig#
Bases:
megatron.bridge.data.base.DataloaderConfigSynthetic variable-length text SFT samples for global-batch packing.
- Parameters:
seq_length – Maximum training length of one sample, in tokens after the next-token shift.
vocab_size – Token IDs are drawn from
[1, vocab_size).length_bucket_fractions – Fractions of samples whose length needs at most CP1, CP2, CP4, … capacity of a
seq_lengthbudget split over2 ** (n - 1)ranks, fornfractions: each bucket’s upper bound is twice the previous one’s, and the last one’s isseq_length. Three fractions give theseq_length // 4andseq_length // 2boundaries of a CP4 group.completion_fraction – Share of each sample’s final tokens that is supervised.
random_seed – Seed for lengths and token IDs; each split derives its own stream.
enable_global_batch_packing – Must be
True; samples are yielded unpacked for the global-batch scheduler.global_batch_packing_pad_to_multiple_of – Per-sequence CP alignment, set by
ConfigContainer.validate.fold_alignment_padding –
Report alignment padding as part of each sequence, as for
- class:
~megatron.bridge.data.builders.GPTSFTDatasetConfig.
enable_offline_packing – Pack the samples offline into rows before training; each dataset item is one row and the global batch counts rows.
offline_packing_specs – Row settings for
enable_offline_packing. Onlypacked_sequence_size(the row capacity, at leastseq_length) andpad_seq_to_mult(alignment of each sample’s length when packing) apply.
- seq_length: int#
None
- vocab_size: int#
None
- length_bucket_fractions: tuple[float, ...]#
(0.5, 0.3, 0.2)
- completion_fraction: float#
0.2
- random_seed: int#
1234
- enable_global_batch_packing: bool#
True
- global_batch_packing_pad_to_multiple_of: int#
1
- fold_alignment_padding: bool#
False
- dataloader_type: Literal[single, cyclic] | None#
‘single’
- enable_offline_packing: bool#
False
- offline_packing_specs: megatron.bridge.data.packing.config.PackedSequenceSpecs | None#
None
- property yields_unpacked_samples: bool#
Whether the built datasets yield the per-sample dicts global-batch packing consumes.
- validate() None#
Validate synthetic data settings.
- finalize() None#
Finalize dataloader settings and validate this config.
- bridge.data.builders.mock_gpt_sft.length_bucket_bounds(
- seq_length: int,
- num_buckets: int = 3,
Return the inclusive length range of each bucket: CP1, CP2, CP4, … capacity of
seq_length.The last bucket ends at
seq_lengthand each earlier one at half the next one’s end.
- bridge.data.builders.mock_gpt_sft.length_bucket_counts(
- num_samples: int,
- fractions: collections.abc.Sequence[float],
Split
num_samplesover the buckets with largest remainders, so the total is exact.
- bridge.data.builders.mock_gpt_sft._length_block(
- config: bridge.data.builders.mock_gpt_sft.MockGPTSFTDatasetConfig,
- split: str,
- block: int,
Return the lengths of samples
[block * LENGTH_BLOCK_SIZE, (block + 1) * LENGTH_BLOCK_SIZE)ofsplit.
- bridge.data.builders.mock_gpt_sft.mock_sft_sequence_lengths(
- config: bridge.data.builders.mock_gpt_sft.MockGPTSFTDatasetConfig,
- num_samples: int,
- split: str = 'train',
Return the deterministic training length of every sample of
split, in dataset order.Each block of
LENGTH_BLOCK_SIZEsamples holds the exact bucket split, interleaved, so the firstnlengths are the same whatevernum_samplesis.- Raises:
ValueError – If
splitis unknown ornum_samplesis negative.
- bridge.data.builders.mock_gpt_sft.mock_sft_packed_rows(
- config: bridge.data.builders.mock_gpt_sft.MockGPTSFTDatasetConfig,
- num_rows: int,
- split: str = 'train',
Pack mock samples offline into
num_rowsrows.Packs consecutive chunks of
OFFLINE_PACKING_CHUNK_SIZEsamples first-fit decreasing, on lengths aligned tooffline_packing_specs.pad_seq_to_mult, shuffles each chunk’s rows with its own seed, and appends chunks until there arenum_rowsrows. The firstnrows are the same whatevernum_rowsis.- Returns:
The lengths of the samples drawn and, per row, the indices of its samples into them.
- Raises:
ValueError – If offline packing is not enabled or
num_rowsis negative.
- class bridge.data.builders.mock_gpt_sft.MockGPTSFTDataset(
- config: bridge.data.builders.mock_gpt_sft.MockGPTSFTDatasetConfig,
- num_samples: int,
- split: str,
Bases:
torch.utils.data.DatasetRandom-token SFT samples of fixed per-index length, collated as unpacked global-batch packing samples.
With offline packing,
num_samplescounts packed rows and each item is the samples of one row.Initialization
- __len__() int#
Return the number of samples, or of packed rows with offline packing.
- _input_ids(index: int) torch.Tensor#
Return
length + 1token IDs for sampleindex; the same index always yields the same tokens.
- __getitem__(index: int) dict[str, Any]#
Return the token IDs of sample
index, or of every sample in packed rowindex.
- collate_fn(
- batch: list[dict[str, Any]],
Shift once, supervise the completion, and pad each sequence to the CP alignment multiple.
Packed rows contribute one sample per sequence they hold.
- class bridge.data.builders.mock_gpt_sft.MockGPTSFTDatasetBuilder( )#
Build synthetic variable-length SFT splits from declarative settings.
Initialization
- build(
- context: megatron.bridge.data.base.DatasetBuildContext,
Build the train, validation, and test splits requested by the schedule.
- bridge.data.builders.mock_gpt_sft.mock_gpt_sft_train_valid_test_datasets_provider(
- train_val_test_num_samples: list[int],
- dataset_config: bridge.data.builders.mock_gpt_sft.MockGPTSFTDatasetConfig,
- tokenizer: Any | None = None,
- pg_collection: Any | None = None,
Build synthetic SFT splits through the canonical runtime builder; no tokenizer is used.