Token-Free and Byte-Level Language Models › The Byte Interface
Language AI HandbookAll chapters Encode the text as UTF-8 and map each byte to an input ID. The content alphabet contains all 256 byte values. Models may reserve distinct IDs for boundaries and padding.
Strict UTF-8 gives every sequence of Unicode scalar values a reversible encoding. A Python string can contain an unpaired surrogate, which the strict encoder rejects.
Some subword tokenizers use a byte fallback for text outside their learned pieces. A direct byte model uses the byte representation for every input and leaves grouping to subsequent model layers.
UTF-8 in Four Forms
RFC 3629 defines UTF-8 as a variable-length encoding of Unicode scalar values into sequences of one to four bytes. A scalar value is any integer from U+0000 to U+10FFFF except the surrogate range U+D800 to U+DFFF. The encoding table is short enough to memorize.
| Scalar value range | Byte pattern | Bytes |
|---|---|---|
| U+0000 to U+007F | 0xxxxxxx | 1 |
| U+0080 to U+07FF | 110xxxxx 10xxxxxx | 2 |
| U+0800 to U+FFFF, excluding U+D800 to U+DFFF | 1110xxxx 10xxxxxx 10xxxxxx | 3 |
| U+10000 to U+10FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx | 4 |
The table is worth reading carefully because the bit patterns are doing two jobs at once. The leading bits of the first byte encode how many bytes the sequence contains, and the leading bits of every subsequent byte mark it as a continuation. That design is what lets a decoder know, from the first byte alone, how many bytes to consume.
Every continuation byte matches 10xxxxxx, which means a lead byte can never be mistaken for a continuation and vice versa. That is the self-synchronizing property. If you land in the middle of a stream, you can scan forward until you find a byte that is not a continuation byte and resynchronize.
In an otherwise valid stream, a cut inside a scalar value can leave at most three continuation bytes before the next boundary or the end of the stream.
RFC 3629 also imposes three constraints that separate strict UTF-8 from the looser encodings of the 1990s.
- Shortest form only. A scalar value must use the shortest sequence that can represent it. The two-byte encoding of U+002F is illegal.
- No surrogates. Code points U+D800 to U+DFFF are excluded, so CESU-8 style encodings are rejected.
- Nothing above U+10FFFF. The original five- and six-byte forms were removed.
Each constraint closes a security or correctness hole. Overlong encodings let the same character be written in multiple ways, which defeats naive string comparison and has been used to bypass filters. Surrogates are reserved for UTF-16 and have no meaning as standalone scalar values. The five- and six-byte forms could represent values outside the Unicode range, which made decoders disagree about what a stream contained. Strict UTF-8 is the subset that avoids all three ambiguities.
A byte string is strict UTF-8 when it decodes under all three constraints of RFC 3629 with no error. Python's default bytes.decode("utf-8") implements strict UTF-8. The distinction matters because a byte language model can emit byte strings that are not strict UTF-8, and a decoder that silently repairs them can hide real failures.
Three Different Counts
Three quantities get mixed up constantly in discussion of token-free models. Keep them separate.
- Unicode scalar values. Python’s string length counts code points. For a string of Unicode scalar values, that is also its scalar count.
- UTF-8 bytes. The number of bytes after encoding. This is
len(s.encode("utf-8")). - Model positions. A direct byte model has one content position per byte. A subword model’s count depends on its tokenizer and input. A patched model has separate positions for bytes and patches. Count padding and special markers separately.
The scalar count and byte count agree for ASCII. A byte count grows faster when UTF-8 uses more than one byte per scalar value.
Let us compute all of this on a mixed-script example that contains ASCII, a two-byte accented letter, its combining form, CJK, and an emoji.
PythonIn [3]Expand
SAMPLES = {
"ascii": "cafe",
"precomposed": "caf\u00e9",
"decomposed": "cafe\u0301",
"cjk": "\u4e2d\u6587",
"emoji": "\U0001f600",
}
rows = []
for name, text in SAMPLES.items():
raw = text.encode("utf-8")
rows.append(
{
"name": name,
"text": text,
"scalars": len(text),
"bytes": len(raw),
"codepoints": " ".join(f"U+{ord(ch):04X}" for ch in text),
"hex": raw.hex(" "),
}
)ConsoleOut [4]Expand
sample scalars bytes hex scalar values --------------------------------------------------------------------- ascii 4 4 63 61 66 65 U+0063 U+0061 U+0066 U+0065 precomposed 4 5 63 61 66 c3 a9 U+0063 U+0061 U+0066 U+00E9 decomposed 5 6 63 61 66 65 cc 81 U+0063 U+0061 U+0066 U+0065 U+0301 cjk 2 6 e4 b8 ad e6 96 87 U+4E2D U+6587 emoji 1 4 f0 9f 98 80 U+1F600
Read the first three rows together. The ASCII string cafe is four scalar values and four bytes. The precomposed café is four scalar values and five bytes, because U+00E9 needs two bytes. The decomposed café is five scalar values and six bytes, because it is e followed by U+0301 COMBINING ACUTE ACCENT, and U+0301 also needs two bytes.
Two strings that render almost identically occupy different numbers of model positions under a byte interface. That is the first practical consequence of going token-free, and it is entirely a property of the encoding rather than of the model.
Each of the two CJK scalar values in this example needs three bytes, for six bytes total. Supplementary CJK scalar values such as U+20000 need four bytes. The sampled emoji also needs four bytes. Compare byte lengths on the actual texts. A translation can use a different number of scalar values, so the sample costs do not give a fixed multiplier across languages.
The chart compares Unicode scalar and UTF-8 byte counts for the mixed-script examples. For a direct byte model, the byte count also gives the number of content positions.
PythonIn [5]Expand
import numpy as np
sample_names = [r["name"] for r in rows]
scalar_counts = np.array([r["scalars"] for r in rows])
byte_counts = np.array([r["bytes"] for r in rows])
x_positions = np.arange(len(sample_names))
bar_width = 0.38
scalar_positions = x_positions - bar_width / 2
byte_positions = x_positions + bar_width / 2VisualizationOut [6]Expand

PythonIn [7]Expand
roundtrip = []
for r in rows:
decoded = r["text"].encode("utf-8").decode("utf-8")
roundtrip.append((r["name"], decoded == r["text"]))ConsoleOut [8]Expand
text -> bytes -> text is lossless for every sample: True
byte lengths: {'ascii': 4, 'precomposed': 5, 'decomposed': 6, 'cjk': 6, 'emoji': 4}The round trip holds for every sample. That is worth stating precisely, because it is easy to overclaim.
UTF-8 maps Unicode scalar sequences bijectively to well-formed UTF-8 byte strings. Validity depends on the whole sequence. At length one, 128 of the 256 possible strings are valid, all of them ASCII. At length two, of the strings are valid, either as two ASCII bytes or as one two-byte scalar. These counts give the rates under uniform byte draws at those lengths. A trained model has its own distribution. RFC 3629, §4
What Lives in the Vocabulary
A byte vocabulary maps the 256 possible byte values to distinct IDs. An implementation can reserve additional nonoverlapping markers for padding and sequence boundaries. This example keeps byte values as IDs and places special markers above them.
PythonIn [9]Expand
SPECIALS = ["<pad>", "<eos>", "<bos>"]
BYTE_TO_ID = {b: b for b in range(256)}
SPECIAL_IDS = {name: 256 + i for i, name in enumerate(SPECIALS)}
VOCAB_SIZE = 256 + len(SPECIALS)
collision_free = all(SPECIAL_IDS[s] >= 256 for s in SPECIALS)ConsoleOut [10]Expand
byte identifiers: 0 .. 255
special identifiers: {'<pad>': 256, '<eos>': 257, '<bos>': 258}
total vocabulary size: 259
special ids disjoint from byte ids: True
input embedding parameters at width 4,096: 1,060,864At embedding width 4,096, the input matrix has one row per vocabulary entry.
| Vocabulary rows | Input embedding parameters | Weight storage at two bytes per parameter |
|---|---|---|
| 259 | 1,060,864 | 2,121,728 bytes |
| 128,000 | 524,288,000 | 1,048,576,000 bytes |
An input lookup reads the selected row. A dense output projection that scores every symbol does work proportional to vocabulary size times the projection width at each scored position. Weight tying can share storage between these matrices when the input and output vocabularies and widths match. The share of total model memory depends on the other parameters and their storage format. Press and Wolf, §3
The trade is not free in the other direction. A byte vocabulary means the model must learn, from data, that certain byte pairs and triples co-occur and what they mean. A subword vocabulary hands it those groupings for free. That is the central tension of the chapter, and the patching architectures we examine later are all attempts to recover the grouping inside the model rather than outside it.
With the same fitted tokenizer, preprocessing, and deterministic encoding settings, two models receive the same subword segmentation for the same text. Byte models receive the same initial byte sequence under the same encoding policy, but the groupings they use inside the model can differ. For example, MEGABYTE groups fixed-width spans while BLT can assign boundaries from next-byte entropy. MEGABYTE, §2.1 · Byte Latent Transformer, §§2.1 and 2.3
Normalization Is a Policy
Because the byte interface is a faithful encoding of whatever string you give it, it inherits every inconsistency in how that string was produced. Unicode has canonical equivalences. café written as U+00E9 and café written as e plus U+0301 are canonically equivalent, and a strict byte model sees two different sequences. There is no automatic resolution.
This makes normalization a first-class decision, not an implementation detail. The usual options are:
- NFC canonically decomposes the input, then recomposes sequences where Unicode's composition rules permit.
- NFD applies canonical decomposition without recomposition.
- NFKC and NFKD additionally apply compatibility mappings, which are lossy. The ligature
fibecomesfi, superscript digits become plain digits, and full-width Latin letters become ASCII. - None leaves the input scalar sequence unchanged before UTF-8 encoding.
A change in normalization can change both the encoded sequence and its length. Record the policy used in training and evaluation. Unicode Normalization Forms
PythonIn [11]Expand
import unicodedata
norm_rows = []
for form in ["NFC", "NFD", "NFKC", "NFKD"]:
normalized = unicodedata.normalize(form, "caf\u00e9")
encoded = normalized.encode("utf-8")
norm_rows.append(
(form, normalized, len(normalized), len(encoded), encoded.hex(" "))
)
ligature = "\ufb01"
compat_rows = []
for form in ["NFC", "NFKC"]:
normalized = unicodedata.normalize(form, ligature)
compat_rows.append((form, normalized, len(normalized)))ConsoleOut [12]Expand
NFC scalars=4 bytes=5 63 61 66 c3 a9 NFD scalars=5 bytes=6 63 61 66 65 cc 81 NFKC scalars=4 bytes=5 63 61 66 c3 a9 NFKD scalars=5 bytes=6 63 61 66 65 cc 81 U+FB01 under NFC -> 'fi' (1 scalar values) U+FB01 under NFKC -> 'fi' (2 scalar values)
For this word, NFC and NFKC both use the precomposed letter. NFD and NFKD both use the base letter and combining accent. NFKC maps U+FB01 to two ASCII letters, which matters when a task needs to reproduce the original typography.
Compatibility normalization is a good example of a preprocessing choice that looks like a cleanup and is actually a modeling decision. If you apply NFKC, you are telling the model that fi and fi are the same string, which is true for many search and retrieval tasks and false for tasks that need to reproduce a document exactly. The byte interface does not make that decision for you, and it does not hide it. The bytes you feed are the bytes you chose.
The rule that follows is unglamorous and important. Pick one normalization form, apply it identically at corpus construction, at training, and at inference, and record the choice in the model card. A byte model does not remove the need for this decision. It removes the excuse for leaving it implicit, because the decision now shows up directly in the position count.
The same argument applies to case folding, whitespace collapsing, and Unicode-aware cleanup. Those are preprocessing choices, not tokenizer choices, and going token-free does not make them disappear.
Default Unicode lowercasing maps İ (U+0130, LATIN CAPITAL LETTER I WITH DOT ABOVE) to i (U+0069) followed by U+0307 COMBINING DOT ABOVE. The UTF-8 length grows from two bytes to three. Whitespace collapsing can also change the byte count, which changes the loss denominator. Document which preprocessing operations you apply. Unicode 17.0, SpecialCasing
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!