code-switch-mtp
Does multi-token prediction (MTP) get harder because of tokenizer fertility (how many tokens it takes to write a word) or because of morphological complexity (how much grammar is packed into each token)?
Comparing two languages cannot answer this, because switching language changes both at once. This project varies fertility while holding morphology fixed, by romanizing Hindi: same words, same grammar, different spelling system, and therefore different fertility under the same tokenizer.
Start here: docs/REPORT.md β the full write-up,
explained from scratch, with every number, decision, and caveat.
What is in this repository
| Path | Contents |
|---|---|
src/data/ |
corpus building, ITRANS romanization, Dakshina parsing |
src/measure/ |
fertility, word-boundary alignment, entropy, D_indep |
src/models/ |
Medusa-1 style MTP heads |
src/train/ |
MTP + NTP training stack |
scripts/ |
one entry point per experiment phase |
configs/ |
YAML config per phase |
results/ |
measurement CSVs (gitignored; see results/manifest.md) |
docs/ |
report, experiment log, hardware notes |
tests/ |
91 unit tests |
Headline results
- The originally specified Phase 3 architecture (frozen backbone + independent
per-head blocks) gives
D = BPB(MTP) - BPB(NTP) = 0.000000by construction β verified analytically and by ablation. MTP's cost needs shared trainable capacity to exist at all. - Byte-level spans are far closer to independent than subword spans
(
D_indepat k=3: 2.94 and 1.01 for byte vs 8.7β10.8 for all subword conditions), consistent across Hindi and English. - Fertility does not move
D_indepwithin a model; model identity mattered ~20x more than script. - Boundary-completion rate (this project's novel metric) separates conditions
far more sharply than
Ddoes, is stable across seeds, and tracks fertility rather than script.
Hypotheses H2 (script-switch discontinuity) and H3 (orthographic ambiguity) were not resolved; see the limitations section of the report.
Stage 2: draft heads that speed up decoding (phases 4 and 5)
Stage 2 uses the look-ahead heads as drafters for lossless draft-and-verify
decoding on a frozen meta-llama/Llama-3.2-1B, on English and on Hindi written
in four ways: Devanagari and three romanizations (ITRANS, IAST, ISO). Every
drafted token is checked against the frozen model, so the output is identical
to greedy decoding.
Tokens per backbone pass (maximum 4), mean of 3 seeds, 10M training tokens:
| Drafter | English | Devanagari | ITRANS | IAST | ISO |
|---|---|---|---|---|---|
| Independent heads (vanilla MTP) | 1.33 | 1.69 | 1.43 | 1.59 | 1.65 |
| Dependent heads | 1.66 | 2.09 | 1.68 | 1.85 | 1.90 |
| Dependent heads, backbone targets | 1.75 | 2.20 | 1.68 | 1.87 | 1.89 |
- End-to-end speed against greedy decoding on one GTX 1080 Ti: vanilla MTP heads give 0.83x (English) to 1.08x (Devanagari). Dependent heads with backbone targets and a 4,352-token draft shortlist give 1.43x (English) and 1.86x (Devanagari), with identical output on 48 of 48 documents.
- Where drafts fail in Hindi is set by the word group, not the word. With dependent heads and backbone targets in Devanagari, the first draft is right 73% of the time inside a word, 76% at a function word that continues a group (postposition, auxiliary, particle), and 36% at the start of a new group. The same split appears in all three romanizations.
- With word groups from the Stanza Hindi tagger instead of the word list
(
scripts/phase5_stanza_split.py; the two agree on 85% of word starts), the first draft in Devanagari is right 83% of the time at auxiliaries, 77% at postpositions, 55% at light verbs, 33% at the second noun of a compound and 36% at group starts. - At Hindi-English switches on real text, the first draft is right 8% of the time when entering English, 43% when returning to Hindi with a function word and 6% when returning with a content word.
- Using word position to choose draft-tree candidates does not beat the head's own confidence. Reweighting the loss by acceptance, masking look-ahead targets at word boundaries and gradient surgery did not help either.
- Reweighting look-ahead targets at group starts during training (weights 0,
0.25 and 3 against 1; 2.3M tokens; 2 seeds;
scripts/phase5_group_pilot.sh) lowers tokens per pass by 0.4% to 4.3% in Devanagari and ITRANS.
None of the drafting components is new. Extra heads on a frozen model follow
Medusa, dependent heads follow Hydra, backbone targets follow the
self-distillation of Medusa-2 and Hydra++, the frequency shortlist follows
FR-Spec, and the tree baselines follow Sequoia and EAGLE-2. The word group is
taken from Karthika et al., "Semantically Cohesive Word Grouping in Indian
Languages" (arXiv 2501.03988), and is approximated here with a closed-class
word list (scripts/phase5_word_groups.py). The contribution of this stage is
the measurements.
Entry points: scripts/phase5_queue.sh (train, then evaluate one condition
and seed), scripts/phase5_aggregate.py (tokens-per-pass tables),
scripts/phase5_timing_all.sh (end-to-end speed),
scripts/phase5_tree_draft.py and scripts/phase5_tree_aggregate.py
(position classes and draft trees), scripts/phase5_switch_points.py
(Hindi-English switches), scripts/phase5_stanza_split.py (tagger-based
word groups). Results are in results/phase5_dependent/.
Data: Hindi from the verified split of ai4bharat/sangraha, English from
wikimedia/wikipedia, human-typed romanized Hindi from Dakshina. Corpora and
trained head checkpoints are not part of this repository.
Llama 3.2 is licensed under the Llama 3.2 Community License, Copyright (c) Meta
Platforms, Inc. All Rights Reserved. See NOTICE.
Setup
Requires Python 3.10 and NVIDIA GPUs. The torch pin is deliberate β Pascal
(sm_61) support depends on it, see docs/hardware-notes.md.
python3 -m venv .venv && source .venv/bin/activate
pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 \
--index-url https://download.pytorch.org/whl/cu121
pip install -r requirements.txt
pytest tests/ -v
Set CUDA_DEVICE_ORDER=PCI_BUS_ID before importing torch in anything you add;
without it PyTorch orders GPUs fastest-first and its indices will not match
nvidia-smi.
Gated models need a Hugging Face token with "read access to public gated
repos" enabled. Put it in .env as HF_TOKEN=... (gitignored).
results/manifest.md documents every output CSV and the command that
regenerates it.
Model tree for Anupam-Rawat-IITB/code-switch-mtp
Base model
meta-llama/Llama-3.2-1B