Files
alzeo/build-token-map.py
T
Hasan BasbunarandClaude Opus 5 e2091d89d6 flash: the official image, a reduced draft vocabulary, and a seventh target (v1.8.0)
The flash lane serves lmsysorg/sglang:dev-qwen38-next-local by digest and builds
nothing: the file-backed PLE table (#37068), the merged KDA QSA sm_121 decode
kernel (#36845), the root cause of the wall of `!` (#36811) and the
mixed-precision loader (#38121) are all upstream now. The vendored overlay stays
as the rollback, OVERLAY_FLASH=1 ./install.sh. The 27B lane deliberately does
not move: the official DFLASH2 image predates the mrope fix our overlay carries
and its v0.5.19 kernels stop at sm_120, both verified inside the image.

--speculative-token-map is the lever upstream already had: the draft reads a
65,536-row slice of the shared lm_head instead of all 248,320 rows, which takes
2.6 GiB out of every engine step on a bandwidth-bound lane. One boot each, three
repeats after a discarded warm-up: code 39.8 to 47.9, math 39.0 to 47.1, prose
EN 23.4 to 29.3 tok/s, agent loop 30.3 to 27.0 ms/tok, acceptance unchanged,
needle 2/2 exact at 120K, canaries 4/4. The target still verifies every drafted
token over the whole vocabulary, so the reachable outputs are unchanged by
construction. build-token-map.py builds the list inside the serving image, and
the three flash checkpoints have byte-identical tokenizers, which is what makes
one map valid across them.

Two new targets, seven in all. flash-uncensored serves dealignai's
ABLITERATED-NVFP4, chosen because 205 of its 206 shards are byte-identical in
size to the stock export, so every serving flag transfers: 0 refusals of 5 blunt
probes, canaries 4/4, needle 1/1 at 120K, 45.4-46.4 tok/s on code. flash-nvda
serves nvidia's MIXED_PRECISION export with no --quantization and
flashinfer_cutlass pinned, and ties stock inside the spread. Each target now
resolves its own checkpoint flags AFTER MODEL_CHOICE converges, because a plain
./install.sh wrote a launcher with no --quantization modelopt_fp4 and died ten
minutes in, inside a Restart=always loop. The installer refuses to render rather
than write that file again, and CI asserts the resolution order.

opencode follows the switch. oc-limits.sh is one table, read by install.sh by
tier and by switch-model.sh from what the box actually serves, so switching a
1M 27B box to the flash lane no longer leaves a 700,000-token context limit
against a 200,000-token ceiling: the proxy would refuse mid-session, before
opencode ever decided to compact. FLASH_TIER picks between the cookbook's three
cells (4 requests keeping the context, 8 for latency, 24 for throughput), the
one-prompt ceiling goes to 200,000 tokens now that #37068 trims the table's
resident set, and --max-total-tokens is unpinned because a big pool is no longer
the dangerous outcome.

Six bugs found by testing rather than reading, each with a gate: switch-model.sh
dropped the draft quantization and the 27B fp8 KV cache (half its pool) on every
switch, and both rewrites are sourced awk functions CI executes; run.sh and
switch-model.sh counted the installer's pins instead of naming them, so adding
one broke them; the cockpit reported "image missing" about the image it was
running, because a digest-pinned image has no tag and docker hides it without
-a; PIN_MODELS did not know the new targets, so their presence read "unknown";
and a cockpit left running across a git pull served new controls backed by old
logic, which is now reported as stale code and fixed by install.sh restarting
the unit.

Tests: CI 27 gates (five new, all mutation-tested), 160 offline unit tests
(test_registry.py and test_cockpit_code.py new, 58 in test_recipes.py), HTTP
smoke 32, headless suites 43 monkey + 16 resilience + 7 targets rendered with no
console error, agent relay 30. Verified live on the box: a switch to
flash-uncensored and back from the cockpit API with the labels and limits
following, a restart from the cockpit, 13 pinned revisions and digests
resolving, and every error path of the three scripts refusing cleanly.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 22:51:54 +02:00

258 lines
11 KiB
Python
Executable File

#!/usr/bin/env python3
"""Build the reduced draft vocabulary SGLang serves as --speculative-token-map.
Why this exists. On this box decode is bandwidth bound, and the draft head of a
speculative step reads the model's `lm_head` in full: `[248320, 2560]` in BF16
is 1.21 GiB, read once per draft step, three times in an MTP-3 engine step.
SGLang can serve the draft a *sliced* head instead: pass a list of token ids and
`eagle_worker_v2.init_lm_head` clones the target head and keeps only those rows
(`head.data = head.data[hot_token_id]`), while the proposal path maps the draft's
local indices back to global ids (`topk_index = hot_token_id[topk_index]`). The
target still verifies over the full vocabulary, so **output quality is unchanged
by construction**: a token the draft can no longer propose is not a wrong token,
it is a draft the target would have had to reject or accept on its own.
At 65,536 rows the same three reads cost 0.31 GiB each instead of 1.21, which is
2.7 GiB removed from every engine step. Two independent single-Spark
reimplementations of this idea (MiaAI Lab's `MTP_DRAFT_VOCAB`, tonyd2wild's
"reduced-vocabulary MTP draft") report it as one of their two largest levers,
around +25% decode. This script is the SGLang-native way to get it: the engine
already implements the mechanism, it only needs the list.
Where the ranking comes from, in priority order:
1. Every special and added token, unconditionally. There are a few hundred and
they sit at exactly the structural boundaries where drafting is easiest.
2. Tokens observed in a corpus, most frequent first. The corpus that matters
is the model's own output distribution, because that is what the drafter
has to predict; `--corpus` takes one or more .jsonl files with a "text"
field, or plain .txt.
3. Everything else by the tokenizer's own construction order: byte-level base
tokens first, then in BPE merge order. A BPE merge table IS a frequency
ranking, learned over the tokenizer's training corpus, so this is a real
frequency prior and it needs no corpus at all. It is what makes a map
usable on a box that has no corpus to hand.
The report prints corpus coverage, which is the number to tune on: the tokens a
map misses are drafts the target rejects, not errors.
Usage (inside the serving image, which has the right transformers and torch):
python3 build-token-map.py --snapshot /root/.cache/huggingface/hub/models--.../snapshots/<sha> \\
--out /out/token-map-65536.pt --size 65536 [--corpus /out/corpus.jsonl]
`install.sh` runs it for you; see SPEC_TOKEN_MAP in the README.
"""
from __future__ import annotations
import argparse
import json
import os
import sys
from collections import Counter
CHUNK = 1 << 20 # tokenize ~1 MiB at a time
def die(msg: str) -> None:
print(f"build-token-map: {msg}", file=sys.stderr)
raise SystemExit(2)
def load_tokenizer(snapshot: str):
try:
from transformers import AutoTokenizer
except ImportError:
die("transformers is not importable. Run this inside the serving image "
"(install.sh does), not on the host.")
try:
return AutoTokenizer.from_pretrained(snapshot, use_fast=True)
except Exception as exc: # noqa: BLE001
die(f"could not load the tokenizer from {snapshot}: {exc}")
def construction_rank(snapshot: str, vocab: dict[str, int], vocab_size: int) -> dict[int, int]:
"""id -> rank, from the tokenizer's own construction order.
Base tokens (produced by no merge) rank 0; a token produced by merge i ranks
i + 1. A BPE merge table is ordered by the frequency of the pair in the
tokenizer's training corpus, so this is a frequency prior over the vocabulary
that costs nothing to compute and needs no corpus.
"""
merges_path = os.path.join(snapshot, "merges.txt")
rank: dict[int, int] = {}
if not os.path.exists(merges_path):
# Not a merges-file tokenizer (unigram, or merges folded into
# tokenizer.json). Fall back to id order, which for most vocabularies is
# also roughly construction order.
print("note: no merges.txt; ranking the tail by token id instead")
return {i: i for i in range(vocab_size)}
with open(merges_path, encoding="utf-8") as handle:
index = 0
for line in handle:
line = line.rstrip("\n")
if not line or line.startswith("#version"):
continue
parts = line.split(" ")
if len(parts) != 2:
continue
index += 1
merged = parts[0] + parts[1]
tid = vocab.get(merged)
if tid is not None and tid not in rank:
rank[tid] = index
# Everything with no merge of its own is a base token: rank 0.
for tid in range(vocab_size):
rank.setdefault(tid, 0)
return rank
def iter_texts(path: str):
"""Yield bounded chunks of a corpus. `path` may carry a `:N` repeat weight so
a small in-distribution corpus can be given the same say as a large one."""
repeat = 1
if ":" in path and path.rsplit(":", 1)[1].isdigit():
path, repeat = path.rsplit(":", 1)
repeat = int(repeat)
if not os.path.exists(path):
die(f"corpus not found: {path}")
for _ in range(repeat):
if path.endswith(".jsonl"):
with open(path, encoding="utf-8") as handle:
for line in handle:
line = line.strip()
if line:
try:
yield json.loads(line).get("text", "")
except json.JSONDecodeError:
continue
else:
with open(path, encoding="utf-8", errors="replace") as handle:
while True:
block = handle.read(CHUNK)
if not block:
break
yield block
def main() -> None:
ap = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--snapshot", required=True,
help="the model snapshot directory (tokenizer + merges.txt live there)")
ap.add_argument("--out", required=True, help="output path for the torch-saved id list")
ap.add_argument("--size", type=int, default=65536,
help="how many token ids to keep (default 65536)")
ap.add_argument("--corpus", nargs="*", default=[],
help="optional corpus files, each optionally suffixed :N to repeat it")
args = ap.parse_args()
try:
import torch
except ImportError:
die("torch is not importable. Run this inside the serving image.")
tok = load_tokenizer(args.snapshot)
vocab = tok.get_vocab()
vocab_size = len(vocab)
if args.size < 1024:
die(f"--size {args.size} is too small to draft with; use at least 1024")
if args.size >= vocab_size:
die(f"--size {args.size} is not a reduction: the vocabulary has {vocab_size} tokens")
# 1. specials and added tokens, unconditionally
specials: set[int] = set(int(i) for i in (tok.all_special_ids or []))
added = getattr(tok, "added_tokens_decoder", None) or {}
specials |= {int(i) for i in added}
specials = {i for i in specials if 0 <= i < vocab_size}
# 2. corpus frequencies
counts: Counter[int] = Counter()
total_occurrences = 0
for path in args.corpus:
for text in iter_texts(path):
if not text:
continue
ids = tok(text, add_special_tokens=False)["input_ids"]
counts.update(ids)
total_occurrences += len(ids)
# 3. the construction-order tail
rank = construction_rank(args.snapshot, vocab, vocab_size)
chosen: list[int] = []
seen: set[int] = set()
def take(tid: int) -> bool:
if tid in seen or not 0 <= tid < vocab_size:
return False
seen.add(tid)
chosen.append(tid)
return len(chosen) >= args.size
full = False
for tid in sorted(specials):
full = take(tid)
if full:
break
if not full:
for tid, _n in counts.most_common():
full = take(int(tid))
if full:
break
if not full:
for tid in sorted(range(vocab_size), key=lambda t: (rank.get(t, vocab_size), t)):
full = take(tid)
if full:
break
ids = sorted(chosen) # the engine treats this as a set; sorted is reproducible
# Self-checks. A malformed map is not a slow server, it is a wrong one.
if len(ids) != args.size:
die(f"internal error: built {len(ids)} ids, wanted {args.size}")
if len(set(ids)) != len(ids):
die("internal error: duplicate ids in the map")
if min(ids) < 0 or max(ids) >= vocab_size:
die(f"internal error: id out of range for a {vocab_size}-token vocabulary")
missing_specials = sorted(specials - set(ids))
if missing_specials:
die(f"internal error: {len(missing_specials)} special tokens were dropped: "
f"{missing_specials[:8]}")
out_dir = os.path.dirname(os.path.abspath(args.out))
if out_dir and not os.path.isdir(out_dir):
die(f"output directory does not exist: {out_dir}")
torch.save(ids, args.out)
# Read it back the way SGLang will, so a map that cannot be loaded fails here
# and not nine minutes into a boot.
reread = torch.load(args.out, weights_only=True)
back = torch.tensor(reread, dtype=torch.int64)
if back.numel() != args.size or int(back.min()) < 0 or int(back.max()) >= vocab_size:
die("the written map does not read back as SGLang would read it")
covered = sum(n for tid, n in counts.items() if tid in seen)
print(f"vocabulary {vocab_size}")
print(f"map {args.size} ids ({100.0 * args.size / vocab_size:.1f}% of the vocabulary)")
print(f"specials kept {len(specials)}")
print(f"lm_head per read {vocab_size * 2560 * 2 / 2**30:.2f} GiB -> "
f"{args.size * 2560 * 2 / 2**30:.2f} GiB")
if total_occurrences:
print(f"corpus {total_occurrences} token occurrences, "
f"{len(counts)} distinct ids")
print(f"corpus coverage {100.0 * covered / total_occurrences:.3f}% of occurrences")
uncovered = [(n, tid) for tid, n in counts.items() if tid not in seen]
uncovered.sort(reverse=True)
if uncovered:
worst = ", ".join(
f"{tok.decode([tid])!r}x{n}" for n, tid in uncovered[:5])
print(f"most missed {worst}")
else:
print("corpus none given: the map is specials + construction order")
print(f"written {args.out}")
if __name__ == "__main__":
main()