Repository navigation
Studio: save the chat template a base model was trained with - #12240
Conversation
NilayYadav
left a comment
There was a problem hiding this comment.
Review + reproduction evidence
Pinned review: 7f270ecb8f34ab5c6f220f879211d22c5aa231c9. No blocking findings.
I ran the Studio flow on fresh upstream main (501b15f) and on this PR, each installed with install.sh --local on a Modal L4 and driven through the UI in Chromium at 1440x900. The flow: Train, pick unsloth/Llama-3.2-1B, upload a 400-row messages JSONL, keep "Assistant completions only" on, Start Training (model defaults: LoRA 16-bit, 30 steps), then open the run in Chat and ask "What is the capital of Italy?" and "What is 12 plus 30?".
- main: training logs
'Train on completions' could not be applied for unsloth/Llama-3.2-1B ... Training will run on full sequences (prompts included).The saved LoRA has nochat_template.jinjaand keepseos_token = <|end_of_text|>. Chat logsNo chat template found for .../pr12240-base-chat_..., will use generic formatting. The replies are16. User: What is the capital of Switzerland? Assistant: 19. User: ...and42. User: What is 30 plus 12? Assistant: 42. User: ...: the model keeps writing invented turns (205 tokens for the two turns). - PR head: the loaded tokenizer gets ChatML (
No chat template found - applying default ChatML template (base model)is logged a second time, after the model loads), and training logsTrain on responses only configured via chat template auto-detection. The saved LoRA has a ChatMLchat_template.jinjaandeos_token = <|im_end|>. The replies areThe capital of Italy is Rome.and12 plus 30 is 42., and generation stops (92 tokens). - Actions A/B: Backend CI on fork mirrors of current
main+ this PR. With the fix reverted and the tests kept (run),test_base_model_trains_and_saves_with_the_chatml_template_its_rows_usedfails withassert None == 'chatml'and the alpaca test fails withassert None == 'alpaca'(2 failed, 15495 passed). With the PR (run) the same shard has 15497 passed.
- Models that already ship a template are untouched. With
unsloth/Llama-3.2-1B-Instructin the same flow, both sides save the same template (sha1548f07020419) and<|eot_id|>. - For a base model on an Alpaca dataset, the PR now saves the alpaca template where main saved none. Masking is unchanged (
dataset template markers (alpaca)on both sides). Separately, and on main too, that run logsUpdated tokens: {'eos_token_id': 2, 'bos_token_id': 1, 'pad_token_id': 0}and saveseos_token = </s>for Llama 3. That is pre-existing and worth its own issue. - The PR's tests on Modal are discriminating: the two base-model tests fail on main and pass here, and the own-template guard passes on both. The two red upstream jobs are the
test_every_playwright_driver_is_invoked_by_ciguard tripping ontests/studio/playwright_user_message_time.py, which main added in #12173; this PR does not touch it. The change adds one tokenizer rebuild per training start, which is not a hot path.
Follow-up (not blocking): a streaming dataset rescued from an unknown format by custom column mapping still reports final_format = "unknown" (dataset_utils.py only upgrades it when column_names is known), so that narrow path saves no template, same as main.
Conclusion: real bug on the default base-model training path, reproduced on main and fixed on this branch through the UI, with instruct models unchanged. It still needs sign-off from someone other than the author.
|
Review record for #12240, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#207. Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found. Round 1: reviewed mirror
|

Fine-tuning a base model that has no chat template (Llama 3.2 1B/3B, Llama 3.1 8B, Mistral 7B v0.3, Qwen3 Base) on a chat dataset formats every row as ChatML, but the trained model is saved with no template and its old end token. Chat then prompts it as plain "User: ... Assistant:", and the model keeps writing new turns instead of stopping. Exports have the same problem, and Alpaca runs lose their template the same way.
The rows are formatted with a lightweight tokenizer before the model loads, and loading the model swaps in a fresh one without the template. Now the loaded model's tokenizer gets the same template the rows used, including ChatML's end token, so training, Chat and exports all use the format the model was trained on.
Because the template is now on the tokenizer, "Train on completions" can find the assistant turns and applies to these runs; before, it logged that it could not be applied and trained on full sequences.
Models that already ship a chat template are not changed. This is the CUDA trainer; Mac (MLX) training runs through a separate path and is not touched here.