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#

MockGPTSFTDatasetConfig

Synthetic variable-length text SFT samples for global-batch packing.

MockGPTSFTDataset

Random-token SFT samples of fixed per-index length, collated as unpacked global-batch packing samples.

MockGPTSFTDatasetBuilder

Build synthetic variable-length SFT splits from declarative settings.

Functions#

length_bucket_bounds

Return the inclusive length range of each bucket: CP1, CP2, CP4, … capacity of seq_length.

length_bucket_counts

Split num_samples over the buckets with largest remainders, so the total is exact.

_length_block

Return the lengths of samples [block * LENGTH_BLOCK_SIZE, (block + 1) * LENGTH_BLOCK_SIZE) of split.

mock_sft_sequence_lengths

Return the deterministic training length of every sample of split, in dataset order.

mock_sft_packed_rows

Pack mock samples offline into num_rows rows.

mock_gpt_sft_train_valid_test_datasets_provider

Build synthetic SFT splits through the canonical runtime builder; no tokenizer is used.

Data#

_SPLITS

LENGTH_BLOCK_SIZE

Samples per independently seeded block of drawn lengths.

OFFLINE_PACKING_CHUNK_SIZE

Samples packed together into offline rows; a multiple of LENGTH_BLOCK_SIZE.

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.DataloaderConfig

Synthetic 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_length budget split over 2 ** (n - 1) ranks, for n fractions: each bucket’s upper bound is twice the previous one’s, and the last one’s is seq_length. Three fractions give the seq_length // 4 and seq_length // 2 boundaries 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. Only packed_sequence_size (the row capacity, at least seq_length) and pad_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,
) → list[tuple[int, int]]#

Return the inclusive length range of each bucket: CP1, CP2, CP4, … capacity of seq_length.

The last bucket ends at seq_length and 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],
) → list[int]#

Split num_samples over 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,
) → numpy.ndarray#

Return the lengths of samples [block * LENGTH_BLOCK_SIZE, (block + 1) * LENGTH_BLOCK_SIZE) of split.

bridge.data.builders.mock_gpt_sft.mock_sft_sequence_lengths(
config: bridge.data.builders.mock_gpt_sft.MockGPTSFTDatasetConfig,
num_samples: int,
split: str = 'train',
) → numpy.ndarray#

Return the deterministic training length of every sample of split, in dataset order.

Each block of LENGTH_BLOCK_SIZE samples holds the exact bucket split, interleaved, so the first n lengths are the same whatever num_samples is.

Raises:

ValueError – If split is unknown or num_samples is 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',
) → tuple[numpy.ndarray, list[list[int]]]#

Pack mock samples offline into num_rows rows.

Packs consecutive chunks of OFFLINE_PACKING_CHUNK_SIZE samples first-fit decreasing, on lengths aligned to offline_packing_specs.pad_seq_to_mult, shuffles each chunk’s rows with its own seed, and appends chunks until there are num_rows rows. The first n rows are the same whatever num_rows is.

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_rows is 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.Dataset

Random-token SFT samples of fixed per-index length, collated as unpacked global-batch packing samples.

With offline packing, num_samples counts 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 + 1 token IDs for sample index; 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 row index.

collate_fn(
batch: list[dict[str, Any]],
) → list[dict[str, torch.Tensor]]#

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(
config: bridge.data.builders.mock_gpt_sft.MockGPTSFTDatasetConfig,
)#

Build synthetic variable-length SFT splits from declarative settings.

Initialization

build(
context: megatron.bridge.data.base.DatasetBuildContext,
) → tuple[bridge.data.builders.mock_gpt_sft.MockGPTSFTDataset | None, bridge.data.builders.mock_gpt_sft.MockGPTSFTDataset | None, bridge.data.builders.mock_gpt_sft.MockGPTSFTDataset | None]#

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,
) → tuple[bridge.data.builders.mock_gpt_sft.MockGPTSFTDataset | None, bridge.data.builders.mock_gpt_sft.MockGPTSFTDataset | None, bridge.data.builders.mock_gpt_sft.MockGPTSFTDataset | None]#

Build synthetic SFT splits through the canonical runtime builder; no tokenizer is used.