bridge.models.ernie.ernie55_data#

SFT data for the ERNIE 5.5 training step: text or pre-tokenized JSONL, or synthetic documents, packed into THD rows.

Every dataset item is one THD row: a list of documents, each a pair of equal-length token and label lists with labels[t] the target of position t (-100 for none). The collate function concatenates the rows of a micro-batch into the batch contract of :mod:~megatron.bridge.models.ernie.ernie55_step (tokens, labels, cu_seqlens and pad_token_id); the step pads every document to a multiple of 2 * CP and slices the context-parallel rank’s rows.

Sources:

  • path: a JSONL file with one sample per line, either pre-tokenized, {"input_ids": [...], "labels": [...]} (labels follow the Hugging Face convention, labels[t] == input_ids[t] where supervised, the shift happens here, unless labels_are_shifted says they are already aligned), or text in a format of Megatron-Bridge’s text-only SFT tutorial: chat rows {"messages": [...]} or prompt/completion rows {"input": ..., "output": ...}. Text rows need hf_tokenizer; they are encoded once, when the dataset is built, with the reference’s chat template (:mod:~megatron.bridge.models.ernie.ernie55_chat), and kept in memory. For large corpora pre-tokenize with examples/models/ernie55/convert_sft_jsonl.py instead.

  • path=None: synthetic samples with deterministic random tokens. Every row holds documents of mock_document_lengths tokens whose first mock_prompt_fraction carries no loss.

  • valid_path / test_path: JSONL files, in the same formats as path, for the validation and test splits. Without them the splits read path — the same rows, packed the same way — so a validation loss then measures the fit to the training rows, not generalization.

A sample longer than seq_length is split into seq_length chunks (truncation="split", every chunk its own sample, like the reference’s --truncation_strategy split) or cut (truncation="right"). pack=True bins the samples into rows of at most seq_length padded tokens (first fit, longest first; the reference’s --packing), pack=False keeps one sample per row (the reference’s production setting). Items past the end of the rows cycle through them again.

Module Contents#

Classes#

Ernie55SFTDataset

Packed ERNIE 5.5 SFT rows (see the module docstring).

Ernie55SFTDatasetConfig

Packed SFT rows for :mod:~megatron.bridge.models.ernie.ernie55_step (see the module docstring).

Functions#

_shift

Hugging Face labels to aligned targets: the target of position t is labels[t + 1].

_chunks

collate_ernie55_rows

Concatenate the documents of a micro-batch’s rows into one packed row.

Data#

API#

bridge.models.ernie.ernie55_data.__all__#

[‘Ernie55SFTDataset’, ‘Ernie55SFTDatasetConfig’, ‘collate_ernie55_rows’]

bridge.models.ernie.ernie55_data._IGNORE#

None

bridge.models.ernie.ernie55_data.Document#

None

bridge.models.ernie.ernie55_data._shift(
input_ids: list[int],
labels: list[int],
) → bridge.models.ernie.ernie55_data.Document#

Hugging Face labels to aligned targets: the target of position t is labels[t + 1].

bridge.models.ernie.ernie55_data._chunks(
input_ids: list[int],
labels: list[int],
*,
seq_length: int,
truncation: str,
) → list[bridge.models.ernie.ernie55_data.Document]#
bridge.models.ernie.ernie55_data.collate_ernie55_rows(
rows: collections.abc.Sequence[list[bridge.models.ernie.ernie55_data.Document]],
*,
pad_token_id: int,
) → dict[str, Any]#

Concatenate the documents of a micro-batch’s rows into one packed row.

Parameters:
  • rows – The micro-batch’s rows, each a list of (input_ids, labels) documents.

  • pad_token_id – Token the training step pads documents with.

Returns:

tokens and labels [1, T] int64, cu_seqlens [n + 1] int64 and pad_token_id.

class bridge.models.ernie.ernie55_data.Ernie55SFTDataset(
config: bridge.models.ernie.ernie55_data.Ernie55SFTDatasetConfig,
*,
num_items: int,
seed_offset: int,
)#

Bases: torch.utils.data.Dataset

Packed ERNIE 5.5 SFT rows (see the module docstring).

Initialization

__len__() → int#
_encode_text_file() → list[dict[str, list[int]]]#

Every sample of the JSONL file as {"input_ids", "labels"}, encoding text rows with the tokenizer.

_chunk_lengths(key: int, length: int) → list[tuple[int, int, int]]#

(key, chunk index, chunk length) of the chunks of one length-token sample.

_read_lengths() → list[tuple[int, int, int]]#

(key, chunk index, chunk length) of every sample chunk; the key is the byte offset of the sample in the JSONL file, or its index in the encoded samples.

_plan_rows(
chunks: list[tuple[int, int, int]],
) → list[list[tuple[int, int]]]#

Group the sample chunks into rows: one per row, or first fit longest first into seq_length rows.

_read_document(
key: int,
chunk: int,
) → bridge.models.ernie.ernie55_data.Document#
_synthetic_row(
index: int,
) → list[bridge.models.ernie.ernie55_data.Document]#
__getitem__(
index: int,
) → list[bridge.models.ernie.ernie55_data.Document]#
collate_fn(
rows: collections.abc.Sequence[list[bridge.models.ernie.ernie55_data.Document]],
) → dict[str, Any]#

Collate a micro-batch for the ERNIE 5.5 step (:func:collate_ernie55_rows).

class bridge.models.ernie.ernie55_data.Ernie55SFTDatasetConfig#

Bases: megatron.bridge.data.base.DatasetProvider

Packed SFT rows for :mod:~megatron.bridge.models.ernie.ernie55_step (see the module docstring).

seq_length: int#

None

Longest row (and longest sample chunk), in tokens.

pad_token_id: int#

None

Token the training step pads documents with (the tokenizer’s pad token).

vocab_size: int | None#

None

The model’s vocabulary size; set, every token id and label of a sample must be below it (an id past the embedding fails on the GPU only, as a device-side assert). The recipe sets it from the model config.

path: str | None#

None

JSONL file of pre-tokenized or text samples (see the module docstring); None for synthetic documents.

valid_path: str | None#

None

JSONL file of the validation split, same formats as path; None reads path (the training rows themselves, so the validation loss is not held out).

test_path: str | None#

None

JSONL file of the test split, same formats as path; None reads path.

hf_tokenizer: str | None#

None

Hugging Face tokenizer (directory or Hub id) that encodes text rows; ERNIE 5.5’s own for real runs.

chat_template: str | None#

None

Chat template file for text rows; None uses the reference’s ERNIE 5.5 SFT template.

chat_template_kwargs: dict[str, Any]#

‘field(…)’

Extra template variables for text rows, such as {"enable_thinking": False}.

prompt_column: str#

‘input’

Prompt column of prompt/completion text rows.

completion_column: str#

‘output’

Completion column of prompt/completion text rows.

loss: Literal[assistant, all]#

‘assistant’

Supervised tokens of text rows: the template’s assistant spans (the reference’s default), or all.

labels_are_shifted: bool#

False

Whether the JSONL labels are already aligned (labels[t] is the target of position t).

truncation: Literal[split, right]#

‘split’

What happens to samples longer than seq_length: split into chunks, or cut.

pack: bool#

False

Bin several samples into one row of at most seq_length padded tokens.

pad_to_multiple_of: int#

1

Padding multiple of every document (2 * context_parallel_size), used to size packed rows.

mock_document_lengths: tuple[int, ...]#

‘field(…)’

Document lengths of every synthetic row.

mock_prompt_fraction: float#

0.0

Leading fraction of every synthetic document without a loss.

mock_token_range: tuple[int, int]#

(1, 32000)

Half-open range of synthetic token ids.

seed: int#

1234

Seed of the synthetic tokens and of the packed-row order.

dataloader_type: Literal[single, cyclic, batch, external] | None#

‘single’

num_workers: int#

0

persistent_workers: bool#

False

finalize() → None#

Validate the settings.

Raises:

ValueError – On an empty or overlong synthetic row, or a non-positive length or padding multiple.

build_datasets(
context: megatron.bridge.data.base.DatasetBuildContext,
) → tuple[bridge.models.ernie.ernie55_data.Ernie55SFTDataset, bridge.models.ernie.ernie55_data.Ernie55SFTDataset | None, bridge.models.ernie.ernie55_data.Ernie55SFTDataset | None]#

Build the train, validation and test datasets.

Parameters:

context – Requested sample counts.

Returns:

Train, validation (when requested) and test (when requested) datasets.

_split(
path: str | None,
) → bridge.models.ernie.ernie55_data.Ernie55SFTDatasetConfig#

This configuration reading path instead of the training file (None keeps the training file).