Skip to navigation

nemo_automodel.components.models.ergo.tokenization

View as MarkdownOpen in Claude

SentencePiece and token-level chat formatting for Ergo.

The chat protocol implemented here (speaker labels, the default system prompt, the CONTROL token boundaries, tool definitions and tool calls) is the one of the chat template published with the weights: Amazon-FMR/Ergo-bf16 at revision f47e685ec4b5095a0880e05cc514a4026859f9ed, https://huggingface.co/Amazon-FMR/Ergo-bf16/tree/f47e685ec4b5095a0880e05cc514a4026859f9ed Original files: chat_template.jinja (the protocol) and tokenizer.model (the SentencePiece model this tokenizer loads), under the Amazon Ergo Open-Weights License (LICENSE.md of that repository).

CONTROL boundaries are inserted by ID; encoding literal message text never creates these boundaries.

Module Contents

Classes

NameDescription
ErgoTokenizerHF tokenizer using explicit CONTROL IDs and exact assistant token masks.

Functions

Data

CHAT_PROTOCOL

DEFAULT_SYSTEM_PROMPT

JsonValue

SPEAKERS

API

class nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer(
vocab_file: str,
add_bos_token: bool = False,
add_eos_token: bool = False,
kwargs: typing.Any = {}
)

Bases: PreTrainedTokenizer

HF tokenizer using explicit CONTROL IDs and exact assistant token masks.

additional_stop_token_ids
= {self.control_ids['<ctrl100>']}
control_ids
model_input_names
= ['input_ids', 'attention_mask']
sp_model
= spm.SentencePieceProcessor(model_file=vocab_file)
vocab_files_names
= {'vocab_file': 'tokenizer.model'}
vocab_size
int

Return the unmodified SentencePiece vocabulary size.

nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer._convert_id_to_token(
index: int
) -> str
nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer._convert_token_to_id(
token: str
) -> int
nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer._decode(
token_ids: list[int],
skip_special_tokens: bool = False,
kwargs: typing.Any = {}
) -> str
nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer._encode_chat(
messages: collections.abc.Sequence[collections.abc.Mapping[str, typing.Any]],
tools: collections.abc.Sequence[collections.abc.Mapping[str, typing.Any]] | None,
add_generation_prompt: bool,
thinking: bool,
continue_final_message: bool
) -> tuple[list[int], list[int]]
nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer._tokenize(
text: str,
kwargs: typing.Any = {}
) -> list[str]
nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer.apply_chat_template(
conversation: list[dict[str, typing.Any]] | list[list[dict[str, typing.Any]]],
tools: list[dict[str, typing.Any]] | None = None,
tokenize: bool = True,
add_generation_prompt: bool = False,
continue_final_message: bool = False,
thinking: bool = True,
padding: bool | str = False,
truncation: bool = False,
max_length: int | None = None,
return_tensors: str | None = None,
return_dict: bool = False,
return_assistant_tokens_mask: bool = False,
kwargs: typing.Any = {}
) -> typing.Any

Serialize chats by ID, preserving assistant masks through padding.

Text rendering cannot represent this CONTROL protocol: tokenize=False is rejected so callers cannot accidentally train on literal delimiters. The shared SFT formatter consumes the IDs and owns the next-token shift.

nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer.build_inputs_with_special_tokens(
token_ids_0: list[int],
token_ids_1: list[int] | None = None
) -> list[int]

Add the begin and end tokens to plain text only as the tokenizer settings say.

Parameters:

token_ids_0
list[int]

Token IDs of the text, of any length.

token_ids_1
list[int] | NoneDefaults to None

Optional token IDs of a second text, appended to the first.

Returns: list[int]

The IDs of both texts, with bos_token_id in front when add_bos_token is set and

nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer.convert_tokens_to_string(
tokens: list[str]
) -> str

Decode ordinary SentencePiece pieces.

nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer.get_vocab() -> dict[str, int]

Return SentencePiece’s vocabulary plus any explicitly added tokens.

nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer.save_vocabulary(
save_directory: str,
filename_prefix: str | None = None
) -> tuple[str]

Save the exact SentencePiece binary for lossless tokenizer reload.

nemo_automodel.components.models.ergo.tokenization.ErgoTokenizer.tokenize(
text: str,
kwargs: typing.Any = {}
) -> list[str]

Encode ordinary text without HF’s special-token string matching.

nemo_automodel.components.models.ergo.tokenization._ordered_messages(
messages: collections.abc.Sequence[collections.abc.Mapping[str, typing.Any]]
) -> list[dict[str, typing.Any]]
nemo_automodel.components.models.ergo.tokenization._text(
content: str | list[str | collections.abc.Mapping[str, str]] | None
) -> str
nemo_automodel.components.models.ergo.tokenization._tool_definitions(
tools: collections.abc.Sequence[collections.abc.Mapping[str, typing.Any]]
) -> str
nemo_automodel.components.models.ergo.tokenization.CHAT_PROTOCOL = '{# Ergo token-ID protocol; reasoning_content #}{% generation %}{% endgeneration...
nemo_automodel.components.models.ergo.tokenization.DEFAULT_SYSTEM_PROMPT = 'You are Ergo, a model built by Amazon. You are a helpful agent that is good at ...
nemo_automodel.components.models.ergo.tokenization.JsonValue: TypeAlias = str | int | float | bool | None | list['JsonValue'] | dict[str, 'JsonValue']
nemo_automodel.components.models.ergo.tokenization.SPEAKERS = {'system': 'System', 'developer': 'System', 'user': 'User', 'assistant': 'Assist...