After editing the Jinja chat template, your fine-tuned Llama 3 prints raw special-token text. What broke?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Click any words you think contain an error. Click again to unmark.
Two bugs: template strings like '<|start_header_id|>' can tokenize as text without the special-token map, AND fine-tune data used a different template than deployed. Decode + diff against training.
Imagine you teach a child a code language for marking sections in their homework: 'BEGIN-MATH' means a math problem starts. Now you change the rule to 'BEGINMATH' (one word) without telling the child. The child keeps writing 'BEGIN-MATH' from old habit, because that is what they learned. The same happens with chat templates and fine-tuning. The model was trained on the original template. You modified the template later for inference. Some of the role-marker strings tokenize differently now, or they were stored as text instead of as the single special-id they should have been. The model occasionally outputs the raw text it learned, because that is what its training data showed. The fix is to keep the training-time and inference-time formats identical, byte by byte.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Open with the framing that template and training data are coupled. Walk the two bugs: special-token handling (the string vs id distinction, controlled by add_special_tokens and the special-token map) and training/inference template drift (modify template after fine-tune means re-tune). Cover the diagnostic: render template, tokenize, decode, diff against training data. Close on the production discipline of CI tests that assert special-token ids appear at expected positions in training data and golden inference fixtures.
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Modifying the chat template after fine-tuning, so the training-time and inference-time formats diverge; or rendering templates to text without the special-token map active, so the role-marker strings tokenize as ordinary text.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.