bridge.models.ernie.ernie55_chat#

Text SFT rows to ERNIE 5.5 training samples, encoded as the reference implementation does.

Accepted rows, one JSON object per line (the text formats of Megatron-Bridge’s text-only SFT tutorial):

  • chat: {"messages": [{"role": ..., "content": ...}, ...]}, optionally with "tools";

  • prompt/completion: {"input": "...", "output": "..."} (column names configurable), encoded as one user turn and one assistant turn.

func:

encode_ernie55_sft_row returns {"input_ids", "labels"} with Hugging Face labels (labels[t] == input_ids[t] where supervised, -100 elsewhere), the pre-tokenized format of

class:

~megatron.bridge.models.ernie.ernie55_data.Ernie55SFTDatasetConfig.

The encoding mirrors the reference’s Ernie5_5Template._encode: it renders ernie55_chat_template.jinja (the reference’s ernie5_5.jinja: the checkpoint’s chat template with {% generation %} spans around what SFT supervises) through tokenizer.apply_chat_template(..., return_assistant_tokens_mask=True), supervises exactly the generation spans and never the first token. String tool-call arguments are decoded to JSON objects, tool_call messages are folded into the preceding assistant turn, and a trailing assistant message without content is dropped. The template is the reference’s file with its special tags in the released tokenizer’s names (sha256 b23eda7a…): <|ernie:im_start|>, <|ernie:im_end|>, <ernie:system_setting>, <ernie:global_setting>, <ernie:tool_list>, <ernie:tool_output> and <ernie:think> (and their closing tags) for the reference’s <|im_start|>, …, <think>, with the same token ids; nothing else differs. Per the reference’s documentation, <ernie:think> belongs to the prompt, "loss": false on a history assistant message keeps it out of the loss, and enable_thinking switches thinking; reasoning inlined in message content is read between <ernie:think> and </ernie:think>.

The tokenizer must be the released ERNIE 5.5 checkpoint’s (tokenizer.json and tokenizer_config.json): with any other tokenizer the token ids, and the splitting of ERNIE’s special tokens, differ from the reference. A tokenizer with the reference’s tag names needs the reference’s template (chat_template).

Module Contents#

Functions#

load_ernie55_chat_template

The chat template text: path, or the reference’s template when None.

ernie55_sft_messages

The row’s conversation as messages, normalized as the reference does.

encode_ernie55_sft_row

One text row as an ERNIE 5.5 training sample.

Data#

__all__

ERNIE55_CHAT_TEMPLATE_PATH

ERNIE 5.5’s SFT chat template: the reference implementation’s ernie5_5.jinja in the released tokenizer’s tag names.

_IGNORE

API#

bridge.models.ernie.ernie55_chat.__all__#

[‘ERNIE55_CHAT_TEMPLATE_PATH’, ‘encode_ernie55_sft_row’, ‘ernie55_sft_messages’, ‘load_ernie55_chat_…

bridge.models.ernie.ernie55_chat.ERNIE55_CHAT_TEMPLATE_PATH#

‘with_name(…)’

ERNIE 5.5’s SFT chat template: the reference implementation’s ernie5_5.jinja in the released tokenizer’s tag names.

bridge.models.ernie.ernie55_chat._IGNORE#

None

bridge.models.ernie.ernie55_chat.load_ernie55_chat_template(path: str | None = None) → str#

The chat template text: path, or the reference’s template when None.

bridge.models.ernie.ernie55_chat.ernie55_sft_messages(
row: dict[str, Any],
*,
prompt_column: str = 'input',
completion_column: str = 'output',
) → list[dict[str, Any]]#

The row’s conversation as messages, normalized as the reference does.

Parameters:
  • row – A chat row (messages) or a prompt/completion row.

  • prompt_column – Prompt column of prompt/completion rows.

  • completion_column – Completion column of prompt/completion rows.

Returns:

The messages to render.

Raises:

ValueError – If the row is neither format, or the conversation is empty.

bridge.models.ernie.ernie55_chat.encode_ernie55_sft_row(
tokenizer: Any,
row: dict[str, Any],
*,
chat_template: str | None = None,
chat_template_kwargs: dict[str, Any] | None = None,
prompt_column: str = 'input',
completion_column: str = 'output',
loss: Literal[assistant, all] = 'assistant',
) → dict[str, list[int]]#

One text row as an ERNIE 5.5 training sample.

Parameters:
  • tokenizer – A Hugging Face tokenizer (ERNIE 5.5’s own for real runs).

  • row – A chat row or a prompt/completion row (see the module docstring).

  • chat_template – Template text; None uses the reference’s template.

  • chat_template_kwargs – Extra template variables, such as enable_thinking.

  • prompt_column – Prompt column of prompt/completion rows.

  • completion_column – Completion column of prompt/completion rows.

  • loss – "assistant" supervises the template’s generation spans (the reference’s default); "all" every token but the first.

Returns:

{"input_ids": [...], "labels": [...]} with Hugging Face labels.