Fall back to a built-in chat template by model_type#2
Open
lixiangnlp wants to merge 1 commit into
Open
Conversation
When a tokenizer has no chat_template (no `chat_template` field in tokenizer_config.json, no chat_template.json/.jinja, and no `chat_template_type` set), look up the model's `model_type` from config.json and use a built-in template module if registered. Currently maps `deepseek_v4` and `deepseek_v32` -> `deepseek_v32`. Behavior is unchanged when either `chat_template` or `chat_template_type` is set, or when `model_type` is not in the registry. This unblocks chat against quantized DeepSeek-V4 checkpoints whose upload omitted the chat template, e.g. huggingface.co/Thump604/DeepSeek-V4-Flash-MLX-Q2-mixed-gs128-affine. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a checkpoint's tokenizer ships without a chat template (no
`chat_template` in `tokenizer_config.json`, no `chat_template.json` /
`chat_template.jinja` file, and no `chat_template_type` set), look up
`model_type` from `config.json` and use a built-in template module if
registered.
Currently registered:
Behavior is unchanged when either `tokenizer.chat_template` or
`chat_template_type` is set, or when `model_type` is not in the registry.
Motivation
`Thump604/DeepSeek-V4-Flash-MLX-Q2-mixed-gs128-affine`
ships a `tokenizer_config.json` without any `chat_template` and no
external `chat_template.jinja`, so `mlx_lm.chat` raises before generation:
```
ValueError: Cannot use chat template functions because tokenizer.chat_template
is not set and no template argument was passed!
```
Adding the entry to the upload would also fix it on the data side, but the
mlx_lm-side fallback is useful for any future V4 / V3.2 quant that's
distributed without one — and the registry is the natural place for the
maintainer's own internal `deepseek_v32` Python template to be reused
beyond `chat_template_type` opt-in.
Changes
`mlx_lm/tokenizer_utils.py`.
`config.json` and looks up `model_type`.
the helper only when `tokenizer.chat_template is None` and the tokenizer
config has no explicit `chat_template_type`.
Tests
Adds two cases to `tests/test_tokenizers.py`:
picks up the `deepseek_v32` template; `tokenizer.has_chat_template` is True.
`model_type` leaves the tokenizer with no chat template (no behavior change).
```
$ python -m unittest tests.test_tokenizers.TestTokenizers.test_chat_template_falls_back_for_known_model_type \
tests.test_tokenizers.TestTokenizers.test_chat_template_no_fallback_for_unknown_model_type \
tests.test_tokenizers.TestTokenizers.test_unknown_model_config_tokenizer_fallback
...
Ran 3 tests in 0.004s
OK
```
Dependency
Pairs with #1 — without that, the wrapper's auto-injected `enable_thinking`
kwarg makes the registered template raise `TypeError` once it actually fires.
The tests in this PR avoid that path by stopping at `tokenizer._chat_template`
identity, so this PR is mergeable independently, but real chat use needs both.