Token-Free and Byte-Level Language Models › Synthetic Grouping Experiments
Language AI HandbookAll chapters The claims above about patch structure are easy to state and hard to see. Let us build both a fixed-width grouper and a causal entropy grouper from scratch, run them on the mixed-script sample, and check the properties that matter.
The entropy grouper needs a next-byte distribution. Rather than train one, we will define a small synthetic conditional distribution that depends only on the previous byte's character class. It is an illustration of the mechanism, not an implementation of BLT, and the numbers it produces should not be read as measurements of anything.
PythonIn [26]Expand
def byte_class(b):
if b == 0x20:
return "space"
if 0x61 <= b <= 0x7A:
return "lower"
if 0x30 <= b <= 0x39:
return "digit"
return "other"
CLASS_OF = {b: byte_class(b) for b in range(256)}
WEIGHTS = {
"space": {ord(c): 1 for c in "etaoinshrdlucmfwypvbg"},
"lower": dict(
zip((ord(c) for c in "eainorstl"), (50, 20, 10, 5, 3, 2, 1, 1, 1))
),
"digit": dict(
zip((ord(c) for c in "0123456789"), (40, 20, 10, 5, 5, 3, 2, 1, 1, 1))
),
"other": dict(
zip(
(ord(c) for c in ".,\n-'\"()"),
(30, 25, 20, 10, 8, 5, 2, 2),
strict=True,
)
),
}
FLOOR = 1e-6
def next_byte_distribution(prev_byte):
weights = WEIGHTS[CLASS_OF[prev_byte]]
total = float(sum(weights.values()))
probs = np.full(256, FLOOR / 256.0)
for b, w in weights.items():
probs[b] += (1.0 - FLOOR) * w / total
return probs
def entropy_bits(probs):
p = probs[probs > 0.0]
return float(-(p * np.log2(p)).sum())The previous-space class puts almost all its mass uniformly over 21 letters. The previous-lowercase class has more concentrated weights. Compute each class’s entropy before choosing thresholds.
PythonIn [27]Expand
class_entropies = {}
for name in ["space", "lower", "digit", "other"]:
representative = {
"space": 0x20,
"lower": ord("e"),
"digit": ord("1"),
"other": ord("."),
}[name]
class_entropies[name] = entropy_bits(next_byte_distribution(representative))ConsoleOut [28]Expand
previous byte class space -> next-byte entropy 4.392 bits previous byte class lower -> next-byte entropy 2.021 bits previous byte class digit -> next-byte entropy 2.340 bits previous byte class other -> next-byte entropy 2.530 bits
At a threshold of 3.0 bits, only the previous-space class exceeds the cutoff. Beyond the initial boundary, the toy starts patches only after spaces. BLT evaluates a separate space-based rule. Its entropy threshold, applied to estimates from a separately trained model, can place a boundary inside a word. Byte Latent Transformer, §2.3 and Figure 4
A zero probability contributes zero to entropy by the limit of as tends to zero. Observing a zero-probability byte has infinite surprisal under that distribution. The toy mixes in a small uniform distribution to give each byte positive probability. Softmax with finite logits also assigns positive probabilities in exact arithmetic, though floating-point exponentiation can underflow to zero.
Now define the two groupers. The fixed grouper cuts every bytes. The entropy grouper starts a new patch at position when the entropy of the distribution over byte given byte exceeds a threshold. The entropy at is computed before byte is observed.
PythonIn [29]Expand
def fixed_patches(ids, width):
return [ids[i : i + width] for i in range(0, len(ids), width)]
def entropy_bounds(ids, threshold_bits):
if not ids:
return []
bounds = [0]
for t in range(1, len(ids)):
if entropy_bits(next_byte_distribution(ids[t - 1])) > threshold_bits:
bounds.append(t)
return bounds
def entropy_patches(ids, threshold_bits):
bounds = entropy_bounds(ids, threshold_bits) + [len(ids)]
return [ids[bounds[i] : bounds[i + 1]] for i in range(len(bounds) - 1)]Note that entropy_bounds reads ids[t - 1] when deciding about position t. Nothing at or after t is consulted. That is the causality property in code.
Run both groupers on the mixed-script sample and check that concatenating their patches reproduces the original bytes. For the entropy grouper, also check strict decoding, agreement on boundary starts strictly inside each truncated prefix, and whether changing the current byte affects a boundary decision when the preceding bytes stay fixed.
PythonIn [30]Expand
GROUP_TEXT = "caf\u00e9 \u4e2d\u6587 \U0001f600 the quick brown fox"
GROUP_BYTES = GROUP_TEXT.encode("utf-8")
GROUP_IDS = list(GROUP_BYTES)
FIXED_W = 8
THRESHOLDS = [2.1, 3.0, 4.5]
fixed_groups = fixed_patches(GROUP_IDS, FIXED_W)
entropy_groups = {thr: entropy_patches(GROUP_IDS, thr) for thr in THRESHOLDS}
def reconstruct(groups):
return b"".join(bytes(g) for g in groups)
fixed_ok = reconstruct(fixed_groups) == GROUP_BYTES
entropy_ok = {
thr: reconstruct(groups) == GROUP_BYTES
for thr, groups in entropy_groups.items()
}
decode_ok = {
thr: reconstruct(groups).decode("utf-8") == GROUP_TEXT
for thr, groups in entropy_groups.items()
}
prefix_ok = {}
for thr in THRESHOLDS:
full = entropy_bounds(GROUP_IDS, thr)
good = True
for k in range(1, len(GROUP_IDS) + 1):
pre = entropy_bounds(GROUP_IDS[:k], thr)
if pre != [b for b in full if b < k]:
good = False
break
prefix_ok[thr] = good
prediction_time_ok = {}
for thr in THRESHOLDS:
starts = set(entropy_bounds(GROUP_IDS, thr))
prediction_time_ok[thr] = all(
(t in starts)
== (t in entropy_bounds(GROUP_IDS[:t] + [replacement], thr))
for t in range(1, len(GROUP_IDS))
for replacement in (ord(" "), ord("a"), ord("1"), ord("."))
)ConsoleOut [31]Expand
text: café 中文 😀 the quick brown fox bytes: 37 fixed width 8: 5 patches, sizes [8, 8, 8, 8, 5] reconstruction ok: True entropy threshold 2.1 bits: 19 patches, sizes [4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 6, 6, 3] reconstruction ok: True decode ok: True prefix invariant: True changed current byte: True entropy threshold 3.0 bits: 7 patches, sizes [6, 7, 5, 4, 6, 6, 3] reconstruction ok: True decode ok: True prefix invariant: True changed current byte: True entropy threshold 4.5 bits: 1 patches, sizes [37] reconstruction ok: True decode ok: True prefix invariant: True changed current byte: True
As the cutoff rises on this sample, the router produces fewer patches and the mean width grows. At 3.0 bits, each nonfinal patch ends at a space, so some patches contain a whole word and its trailing space. Byte reconstruction, strict decoding, prefix agreement, and the current-byte perturbation check all pass at every cutoff.
If concatenating the patches fails to reproduce the input bytes, the grouper has changed the byte sequence. A likelihood computed from that altered sequence would score different bytes.
The prefix check compares existing starts after truncation. The second check changes the current byte while holding prior bytes fixed. This tests current-byte dependence directly for the chosen replacements.
Let us plot the groupings as spans over byte positions so the difference in structure is visible.
PythonIn [32]Expand
def spans(groups):
out, start = [], 0
for g in groups:
out.append((start, len(g)))
start += len(g)
return out
fixed_spans = spans(fixed_groups)
entropy_spans = {thr: spans(entropy_groups[thr]) for thr in THRESHOLDS}VisualizationOut [33]Expand

Fixed-width patches contain eight bytes until the shorter final patch. With the 2.1-bit threshold, the toy router starts patches after spaces and bytes in its other class. At 3 bits only spaces trigger breaks. At 4.5 bits the whole example stays in one patch.
The toy illustrates how a threshold changes the placement and number of boundaries. In one BLT comparison, its entropy patcher averaged about 4.5 bytes per patch on the pretraining data mix. BLT also evaluates other average widths by changing the cutoff. Byte Latent Transformer, §§4.4, 5.2 and 5.3
Wider patches leave fewer global positions to process. The byte count and local byte positions stay the same across the rows.
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!