fix(llama): distinguish zephyr/tinyllama from phi chat template - #1284
Conversation
TinyLlama/Zephyr templates share <|user|>/<|assistant|> with Phi but end turns with eos (</s>), not <|end|>. Require <|end|> for phi detection and add a zephyr apply path so TinyLlama is not fed Phi separators. Signed-off-by: Dundy Pasupuleti <dundysm@gmail.com>
|
@yifeif-nv @chaofengw-nv friendly ping for review when you have a moment; this is a small llama chat-template detection fix for #1271 (tinyllama misclassified as phi). no local gpu here so the unit tests are cpu-only; would appreciate a gpu ci / a40 pass on |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: Your plan provides up to 12 included reviews per hour; 10 remain after this review. 📝 SummarySummary
Architecture impact
HUMAN REVIEW REQUIRED — Complete GPU validation before merge. WalkthroughThe Llama runtime now detects Zephyr templates separately from Phi templates and renders Zephyr turns with ChangesZephyr chat-template support
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to The reviewed change adds the intended Zephyr template detection and regression coverage without a supported unresolved merge-blocking risk. 🚥 Pre-merge checks | ✅ 7 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (7 passed)
Full details: Linked Issues checkExplanation For [ Comment |
Hello, thanks for the contribution. I will take a look today and trigger the CI for you if it all looks good. Sorry I missed that PR earlier! |
Looks like a legitimate fix. I'm triggering the CI for you! Thanks for the contribution, and looking forward to future contributions as well! |
Signed-off-by: Dundy Pasupuleti <dundysm@gmail.com>
|
@yifeif-nv thanks for taking a look and kicking ci; really appreciate it. pushed a small follow-up for the source-quality fail ( |
Background
Fixes #1271.
families/llama/tests/manifests/tinyllama-1.1b.jsone2e fails because the nativeruntime's first decode step predicts EOS immediately (
token_ids: [2], emptytext). HF reference greedy generation for the same prompt and chat template
returns a real answer (
token_ids: [12415, 2410, 29901, 3681, 2], textCapital: Paris).root cause (confirmed via template + detection/render comparison, not GPU
logits):
llama_detect_chat_template_formattreated any jinja containing<|user|>/<|assistant|>as phi. TinyLlama-1.1B-Chat (and Zephyr) usethose role tags but end turns with
eos_token(</s>), not Phi's literal<|end|>.apply_phi()therefore injected<|end|>separators the model wasnever trained on, which matches the observed out-of-distribution first-step EOS.
unrelated to open PR #1269 (eos_token_id list); that work does not touch
chat-template detection/rendering.
Exit Criteria
eos_token, no<|end|>) detects aszephyr, notphi.<|end|>) still detects asphi.llama_apply_chat_template("zephyr", ...)renders<|user|>\n{prompt}</s>\n<|assistant|>\n(matches HFapply_chat_templatewith
trim_blocks/lstrip_blocks).maintainers/CI GPU to confirm the e2e harness.
Implementation
in
families/llama/runtime/chat_templates.cpp:<|end|>(in addition to role tags) to classify phi.<|end|>as zephyr.apply_zephyr()that uses</s>turn ends instead of<|end|>.regression coverage in
families/llama/tests/cpp/test_llama_chat_template.cpp(detect + apply + e2e prompt string; asserts no
<|end|>in zephyr render).affected: llama family chat-template detection/apply only. no public API, ABI,
or bundle format change (format string is internal; bundles still store the
same
chat_template.jinjasection).Change categories
Validation
Commands and Results
also verified detect/apply on the real TinyLlama and Phi-3 jinja strings:
zephyr<|end|>present) ->phi<|user|>\nWhat is the capital of France? Answer in one word.</s>\n<|assistant|>\n<|user|>\nWhat is the capital of France? Answer in one word.<|end|>\n<|assistant|>\nHF-equivalent jinja render (jinja2
trim_blocks=True,lstrip_blocks=True,same as transformers) matches the zephyr apply string above.
Hardware, Environment, and Revisions
fix/tinyllama-chat-template-format)trtmc run/pytest families/llama/tests/test_e2e.py --e2e-model=tinyllama-1.1bTinyLlama/TinyLlama-1.1B-Chat-v1.0tokenizer_configon HF
main; Phi comparison frommicrosoft/Phi-3-mini-4k-instructNot Run / Remaining Gaps
tinyllama-1.1b(build bundle + native vs HF reference) wasnot run locally. please confirm on community GPU CI / a machine with CUDA
that
pytest families/llama/tests/test_e2e.py --e2e-model=tinyllama-1.1bnow produces non-empty text aligned with the HF reference cited in Bug: TinyLlama-1.1B-Chat's e2e case produces empty output — first decode step predicts EOS immediately #1271.
unchanged aside from requiring
<|end|>for phi classification).Contributor Self-Review
Notes For Future Readers
chat_templates.cpp, thenapply_zephyr, then new unit tests.<|user|>with a non-</s>eos, detection may need aricher signal; TinyLlama/Zephyr eos is
</s>and matches this apply path.Risk level
Risk rationale: surgical heuristic tightening plus one new format path; phi
behavior preserved when
<|end|>is present; covered by non-GPU unit tests.