bridge.data.packing.gpt_sft#

Runtime dataset for legacy NumPy GPT SFT packed artifacts.

Module Contents#

Classes#

Functions#

_safe_load_packed_npy

Load a packed NumPy artifact with the restricted object unpickler.

Data#

API#

bridge.data.packing.gpt_sft.logger#

‘getLogger(…)’

bridge.data.packing.gpt_sft._safe_load_packed_npy(file_path: str | pathlib.Path) → numpy.ndarray#

Load a packed NumPy artifact with the restricted object unpickler.

class bridge.data.packing.gpt_sft.GPTSFTPackedDataset(
file_path: str,
tokenizer: megatron.bridge.training.tokenizers.tokenizer.MegatronTokenizer,
return_cu_seqlen: bool = True,
pad_cu_seqlens: bool = False,
pad_seq_to_mult: int = 1,
pack_metadata_file_path: str | None = None,
**kwargs,
)#

Bases: megatron.bridge.data.datasets.gpt_sft.GPTSFTDataset

Initialization

file_path: See file_path in the parent class. tokenizer: See tokenizer in the parent class. return_cu_seqlen: Whether to return cu_seqlen to pass to the model. Having cu_seqlen in the model input enables THD attention kernel, which is the correct format for training with packed sequence to prevent cross-sequence attention. This flag should be True unless you have a specific use case. pad_seq_to_mult: The multiple used for padding sequences during packing. When > 1, both logical and physically padded cumulative sequence offsets are returned for THD CP. pack_metadata_file_path: Path to the metadata JSON file used to stabilize cu_seqlens shapes.

__getitem__(idx)#
_load_dataset()#
_build_samples_mapping()#
_build_loss_mask(processed_example)#
_maybe_cast_to_list(x)#
_logical_sequence_length(
tokens: numpy.ndarray,
loss_mask: numpy.ndarray,
) → int#

Return the length of one packed sequence without its alignment padding.

Offline packing with pad_seq_to_mult > 1 stores each sequence padded with zero-loss EOS tokens. A supervised terminal EOS is part of the sequence and is kept.

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

Split packed rows back into one unpacked scheduler sample per sequence.

For global-batch packing, Megatron-Core’s sequence-packing scheduler re-bins the sequences of the whole global batch, and under dynamic context parallelism picks a CP group for each bin. Offline packing then only decides which sequences share a step: the global batch counts packed rows. Each sequence keeps its own next-token labels and loss mask, and is padded to the global-batch alignment multiple instead of the offline pad_seq_to_mult.

collate_fn(batch)#

Collates a list of packed sequence samples into a batch for the model.

This method is specifically designed for GPTSFTPackedDataset. It takes a list of packed sequence items (as returned by __getitem__) and prepares a batch of tensors. This includes handling cu_seqlens which are crucial for the efficient processing of packed sequences with kernels like THD attention.

Parameters:

batch (List[dict]) – A list of packed sequence samples.

Returns:

A dictionary of batched tensors, including ‘tokens’, ‘labels’, ‘loss_mask’, ‘position_ids’, and the MCore/TE packed-sequence fields if return_cu_seqlen is True. With global-batch packing, a list of one unpacked sample per packed sequence instead.

Return type:

dict