[BUG] ByteLevelTokenizer cannot decode configured BOS, EOS or PAD IDs above 255
Describe the bug
ByteLevelTokenizer accepts BOS, EOS and PAD IDs in its configured vocabulary, but ids_to_text() raises ValueError: bytes must be in range(0, 256) when one of those IDs is outside the byte range.
For example, with the default vocab_size=512 and _eos_id=256, decoding [65, tokenizer.eos_id] fails. The constructor accepts this configuration and registers the EOS ID in special_token_to_id. Decoding filters only IDs at or above special_start, which is 512, so the EOS ID reaches bytes() as though it were a text byte.
The same failure occurs with _bos_id=257 and _pad_id=258. All three IDs are inside the configured vocabulary and do not overlap ordinary UTF-8 byte values.
Steps/Code to reproduce bug
In a configured Megatron environment:
from megatron.core.tokenizers.text.libraries.bytelevel_tokenizer import ByteLevelTokenizer
for special in ("eos", "bos", "pad"):
token_id = {"eos": 256, "bos": 257, "pad": 258}[special]
tokenizer = ByteLevelTokenizer(vocab_size=512, **{f"_{special}_id": token_id})
ids = [token_id, 65] if special == "bos" else [65, token_id]
print(special, tokenizer.ids_to_text(ids))
Each case raises:
ValueError: bytes must be in range(0, 256)
For a CPU-only reproduction without importing the GPU package initialization, the actual two tokenizer source modules can be loaded directly from the repository root:
import importlib.util
import sys
from pathlib import Path
root = Path("megatron/core/tokenizers/text/libraries")
for name in ("abstract_tokenizer", "bytelevel_tokenizer"):
module_name = "tokenizer_repro." + name
spec = importlib.util.spec_from_file_location(module_name, root / (name + ".py"))
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
tokenizer = module.ByteLevelTokenizer(vocab_size=512, _eos_id=256)
print(tokenizer.ids_to_text([65, tokenizer.eos_id]))
Expected behavior
Configured special IDs should be handled as special tokens during decoding. With the method's existing special-token removal behavior, these examples should decode to "A", as an additional token supplied through special_tokens already does.
Validation
I ran the actual tokenizer class using the isolated source-module loading above on 581248531984920e92f076946ad4629d359442a4, Python 3.12.3, Ubuntu 24.04.5:
| Case | Result |
|---|---|
Ordinary bytes for Hello |
Hello |
special_tokens=["<extra>"], decode [65, 512] |
A |
Configured EOS 256 |
ValueError |
Configured BOS 257 |
ValueError |
Configured PAD 258 |
ValueError |
| Configured EOS plus an additional special token | ValueError |
No tokenizer methods were mocked or modified. These are CPU checks of the tokenizer class, not results from the distributed unit suite or model inference.
Additional context
I checked #5775, which adds tokenizer offsets. Its current diff does not change the special-token filtering in ids_to_text().
@NVIDIA/mcore-oncall, I can prepare a focused fix with regression tests for the configured BOS/EOS/PAD IDs, additional special tokens, and ordinary UTF-8 decoding if this scope is welcome.