Tokenizer
×Encode each input string as a sequence of token IDs.
Why
128K tokens. The vocabulary has 128K tokens: 100K from the tiktoken tokenizer and 28K more for languages other than English. On a sample of English data it compresses 3.94 characters into a token, against 3.17 for the Llama 2 tokenizer, which the authors say lets the model “read” more text for the same training compute. Llama 3 paper §3.2, p. 7
Those 28K tokens improved both compression and downstream performance, the authors found, with no effect on how English is tokenized. Llama 3 paper §3.2, p. 7
- out
- [B batch, T positions]
Explanation
The pattern pat_str splits the text into pieces, and byte-pair merges ranked by the fixed table encode each piece (tokenizer.py 47, 147–155). By default the text of a special token, such as <|begin_of_text|> typed in a prompt, encodes as ordinary characters; only the bos flag adds the token itself (tokenizer.py 105–106, 156–157). There are 128,256 available token IDs: the BPE ranks, then the 256 special tokens (tokenizer.py 75–77).
The generate method copies the prompts into a buffer of total_len positions filled with pad_id = −1 (generation.py 156–161), and its first forward call reads only the first min_prompt_len positions, so that batch holds no padding; the rest of a longer prompt enters one position per call (153, 179–203).
Sources
In codeself.tokenizer.encode(x, bos=True, eos=False)
Direct evidence
The tokenizer loads fixed BPE ranks, combines them with a regex and special tokens, and returns token IDs with caller-controlled BOS/EOS insertion.
tokenizer.py 45–160 ↗ · meta-llama/llama3 @ a0940f9cf706
Excerpt
num_reserved_special_tokens = 256
pat_str = r"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+" # noqa: E501… lines 48–55 omitted …
assert os.path.isfile(model_path), model_path
mergeable_ranks = load_tiktoken_bpe(model_path)
num_base_tokens = len(mergeable_ranks)
special_tokens = [
"<|begin_of_text|>",
"<|end_of_text|>",
"<|reserved_special_token_0|>",
"<|reserved_special_token_1|>",
"<|reserved_special_token_2|>",
"<|reserved_special_token_3|>",
"<|start_header_id|>",
"<|end_header_id|>",
"<|reserved_special_token_4|>",
"<|eot_id|>", # end of turn
] + [
f"<|reserved_special_token_{i}|>"
for i in range(5, self.num_reserved_special_tokens - 5)
]
self.special_tokens = {
token: num_base_tokens + i for i, token in enumerate(special_tokens)
}
self.model = tiktoken.Encoding(
name=Path(model_path).name,
pat_str=self.pat_str,
mergeable_ranks=mergeable_ranks,
special_tokens=self.special_tokens,
)… lines 84–146 omitted …
t: List[int] = []
for substr in substrs:
t.extend(
self.model.encode(
substr,
allowed_special=allowed_special,
disallowed_special=disallowed_special,
)
)
if bos:
t.insert(0, self.bos_id)
if eos:
t.append(self.eos_id)
return tThe base text-completion caller inserts BOS and omits EOS when encoding each input prompt.
generation.py 260 ↗ · meta-llama/llama3 @ a0940f9cf706
Excerpt
prompt_tokens = [self.tokenizer.encode(x, bos=True, eos=False) for x in prompts]Meta’s original Llama 3 8B base catalogue entry supplies dimensions, head counts, vocabulary, MLP rounding settings, epsilon and unscaled RoPE settings.
sku_list.py 19–207 ↗ · meta-llama/llama-models @ 0e0b8c519242
Excerpt
LLAMA3_VOCAB_SIZE = 128256… lines 20–187 omitted …
def llama3_base_models() -> list[Model]:
return [
Model(
core_model_id=CoreModelId.llama3_8b,
description="Llama 3 8b model",
huggingface_repo="meta-llama/Llama-3-8B",
arch_args={
"dim": 4096,
"n_layers": 32,
"n_heads": 32,
"n_kv_heads": 8,
"vocab_size": LLAMA3_VOCAB_SIZE,
"ffn_dim_multiplier": 1.3,
"multiple_of": 1024,
"norm_eps": 1e-05,
"rope_theta": 500000.0,
"use_scaled_rope": False,
},
pth_file_count=1,
),