Skip to content

Re-apply #4939: updated models template mappers - #4950

Merged
danielhanchen merged 13 commits into
mainfrom
feature/apply-mapping-updates
Apr 15, 2026
Merged

danielhanchen merged 13 commits into
mainfrom
feature/apply-mapping-updates

Conversation

@rolandtannous

@rolandtannous rolandtannous commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

This re-applies template mapping changes for gemma4, lfm2.5 and lfm2

also adds lfm2.5 to transformers 5.3.0 white list

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for several new models, including Gemma 4 (standard and thinking variants) and LFM 2/2.5, by updating model mappings, template configurations, and version requirements. A review comment suggests including the 4-bit quantized version of the Gemma 4 31B model in the Ollama template mappers to maintain consistency with other model groups.

"unsloth/gemma-4-E4B-it",
"unsloth/gemma-4-E4B",
"unsloth/gemma-4-E4B-it-unsloth-bnb-4bit",
"unsloth/gemma-4-31B-it",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency, unsloth/gemma-4-31B-it-unsloth-bnb-4bit should probably be included here. It's part of the gemma-4-thinking group in model_mappings.py, and its float counterpart unsloth/gemma-4-31B-it is already present.

Suggested change
"unsloth/gemma-4-31B-it",
"unsloth/gemma-4-31B-it",
"unsloth/gemma-4-31B-it-unsloth-bnb-4bit",

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 30c9e7fd5b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

"gemma4": (
"unsloth/gemma-4-E2B-it",
"unsloth/gemma-4-E2B",
"unsloth/gemma-4-E2B-it-unsloth-bnb-4bit",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep Gemma-4 base IDs mapped to Ollama template

Replacing the base Gemma-4 IDs with only *-it-unsloth-bnb-4bit variants drops template detection for models like unsloth/gemma-4-E2B, ...-E4B, ...-31B, and ...-26B-A4B. create_ollama_modelfile() does an exact lookup in MODEL_TO_OLLAMA_TEMPLATE_MAPPER and returns None when a model ID is missing, so GGUF/Ollama exports for those still-supported base IDs now skip Modelfile generation and lose the prompt template wiring.

Useful? React with 👍 / 👎.

…namespace for PR #4950

- Add unsloth/gemma-4-31B-it-unsloth-bnb-4bit to __INT_TO_FLOAT_MAPPER so
  the int-to-float resolution works for this model (already listed in
  TEMPLATE_TO_MODEL_MAPPER but had no mapper entry).
- Add LiquidAI/LFM2.5-1.2B-Instruct to lfm-2.5 TEMPLATE_TO_MODEL_MAPPER
  entry so the canonical upstream namespace is mapped consistently with lfm-2.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 864dab5ac0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +422 to +424
"lfm-2.5": (
"unsloth/LFM2.5-1.2B-Instruct",
"LiquidAI/LFM2.5-1.2B-Instruct",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Map LFM2.5 models to a defined chat template

Adding unsloth/LFM2.5-1.2B-Instruct under the new "lfm-2.5" template key makes Studio resolve that model to lfm-2.5, but unsloth.chat_templates.get_chat_template() only has lfm-2 and raises on unknown keys. In the chat-formatting path this means the Unsloth template application always fails for LFM2.5 and silently falls back, which can produce incorrect/empty formatted text when the tokenizer itself has no chat template. Please map this model to an existing template key or add an lfm-2.5 template alias in unsloth/chat_templates.py.

Useful? React with 👍 / 👎.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Apr 12, 2026
…plate files

1. Add missing gemma-4-31B-it-unsloth-bnb-4bit to Ollama gemma4 tuple
2. Remove redundant gemma-4-31B-it self-map from __INT_TO_FLOAT_MAPPER
   (bnb-4bit entry already covers FLOAT_TO_INT and MAP_TO_UNSLOTH_16bit)
3. Add CHAT_TEMPLATES["lfm-2.5"] alias to chat_templates.py
   (template key was mapped but never registered, causing silent fallback)
4. Widen lfm2.5-vl-450m to lfm2.5-vl in transformers 5.3.0 allowlist
   (covers future VL variants without false-positiving on text models)
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Apr 12, 2026
…ai#4950

1. Include <|channel>thought\n<channel|> in gemma-4-thinking response
   marker, consistent with qwen3-thinking and glm patterns. The
   thinking template always injects channel tokens for model turns;
   marking them as structural avoids wasting training loss on fixed
   tokens.
2. Remove stale "Phi-3" comment from lfm-2 DEFAULT_SYSTEM_MESSAGE.
…emplate alias

- Add unsloth/gemma-4-31B-it-unsloth-bnb-4bit to OLLAMA_TEMPLATE_TO_MODEL_MAPPER
  so Ollama export works for this model (E2B-it and E4B-it bnb-4bit variants were
  already present, 31B-it was inconsistently omitted)
- Register CHAT_TEMPLATES["lfm-2.5"] as alias of the lfm-2 template to prevent
  KeyError when Studio resolves LFM2.5 models through MODEL_TO_TEMPLATE_MAPPER
@danielhanchen danielhanchen added the auto-reviewing Auto-review in progress label Apr 12, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Apr 13, 2026
…hinking response prefix for PR unslothai#4950

- Fix gemma4 Ollama template to map assistant role to model (was outputting <|turn>assistant instead of <|turn>model)
- Restore dropped base model IDs (E2B, E4B, 31B, 26B-A4B) in Ollama template mapper to prevent export regression
- Update gemma-4-thinking response prefix to include <|channel>thought<channel|> header, consistent with qwen3-thinking pattern
- Remove redundant gemma-4-31B-it self-referencing entry from INT_TO_FLOAT_MAPPER (already covered by bnb-4bit entry)
- Fix stale Phi-3 comment on lfm-2 system message line
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Apr 13, 2026
…meter for PR unslothai#4950

- Gemma-4 has no native system role token; the HF chat template folds system
  messages into the first user turn. Hardcode <|turn>user for both user and
  system messages, matching the gemma3n Ollama template pattern.
- Add PARAMETER stop "<turn|>" so Ollama halts generation at turn boundaries.
@danielhanchen danielhanchen removed the auto-reviewing Auto-review in progress label Apr 13, 2026
@danielhanchen danielhanchen added the auto-reviewing Auto-review in progress label Apr 13, 2026
unsloth/LFM2-1.2B-unsloth-bnb-4bit is referenced in model_mappings.py
but had no mapper.py entry, so model resolution would fail when users
load that variant with load_in_4bit=False or when the float name is
used with load_in_4bit=True.
1. ollama_template_mappers.py: Restore dropped Gemma-4 base model IDs
   (E2B, E4B, 31B, 26B-A4B) and add missing google/ upstream IDs to
   the gemma4 Ollama mapper for consistency with other gemma entries.

2. mapper.py: Remove self-mapping non-bnb-4bit entries from
   __INT_TO_FLOAT_MAPPER that were polluting FLOAT_TO_INT_MAPPER with
   lowercase 16-bit names, causing load_in_4bit=True to return bad
   model names. Add direct MAP_TO_UNSLOTH_16bit entries to preserve
   the google->unsloth 16-bit redirects.

3. mapper.py: Add LFM2.5 MAP_TO_UNSLOTH_16bit redirect so
   LiquidAI/LFM2.5-1.2B-Instruct resolves to its unsloth mirror.
danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Apr 13, 2026
danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Apr 13, 2026
@danielhanchen danielhanchen removed the auto-reviewing Auto-review in progress label Apr 13, 2026
@danielhanchen danielhanchen added the auto-review-failed Auto-review rejected the PR label Apr 13, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Auto-review verdict: Changes requested

Reason: no verdict parsed; defaulting based on commit history

These test_*.py files were added at the repo root rather than under tests/.
Removing them from this PR; the production mapper changes remain.
Adds unsloth/gemma-4-26B-A4B-it to __INT_TO_FLOAT_MAPPER as a 2-tuple so
google/gemma-4-26B-A4B-it routes to unsloth/gemma-4-26B-A4B-it across
INT_TO_FLOAT_MAPPER, FLOAT_TO_INT_MAPPER, and MAP_TO_UNSLOTH_16bit.

The 26B-A4B (MoE) model has no bnb-4bit variant, so the key uses the
plain unsloth name rather than the -unsloth-bnb-4bit suffix.

Removes the now-redundant standalone _add_with_lower call for the -it
variant; the 16bit mapping is registered via the dict loop.
Adds E2B, E4B, 31B base unsloth-bnb-4bit entries to __INT_TO_FLOAT_MAPPER.
The 26B-A4B (MoE) base has no bnb-4bit variant on HF, so it stays on the
standalone _add_with_lower line for the 16bit-only routing.

Removes the redundant _add_with_lower lines for E2B, E4B, 31B base since
the dict loop now registers the same google->unsloth route through the
2-tuple entries, plus full FLOAT_TO_INT and INT_TO_FLOAT coverage.
@danielhanchen
danielhanchen merged commit 800ddc9 into main Apr 15, 2026
5 checks passed
@danielhanchen
danielhanchen deleted the feature/apply-mapping-updates branch April 15, 2026 14:52
@jonahsamost

jonahsamost commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

I believe this causes a bug.

NotImplementedError: Unsloth: google/gemma-4-31B-it is not supported in your current Unsloth version! Please update Unsloth via
...
> uv pip list|grep -i unsloth
unsloth                                  2026.4.4
unsloth-zoo                              2026.4.3

with:

     model_name = 'google/gemma-4-31B-it'
     ...
    common_kwargs = dict(
        model_name=model_name,
        max_seq_length=max_seq_length,
        load_in_4bit=False,
        load_in_16bit=load_in_16bit,
        full_finetuning=False,
        device_map={'': local_rank},
        # device_map="balanced",
    )
    peft_kwargs = dict(
        r=LORA_R,
        lora_alpha=LORA_ALPHA,
        lora_dropout=0,
        bias="none",
        use_gradient_checkpointing="unsloth",
        random_state=3407,
        exclude_modules=".*vision_tower.*",
    )

    model, tokenizer = FastVisionModel.from_pretrained(**common_kwargs)

i see

[rank1]:   File "<frozen runpy>", line 198, in _run_module_as_main
[rank1]:   File "<frozen runpy>", line 88, in _run_code
[rank1]:   File "/root/foo-llm-finetune/src/foo_rl/train/src/phase1/unsloth_train_sft.py", line 243, in <module>
[rank1]:     main()
[rank1]:   File "/root/foo-llm-finetune/src/foo_rl/train/src/phase1/unsloth_train_sft.py", line 107, in main
[rank1]:     model, tokenizer = _load_unsloth_model_and_tokenizer(MODEL_NAME, max_seq_length=max_seq_length)
[rank1]:                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank1]:   File "/root/foo-llm-finetune/src/foo_rl/train/src/phase1/unsloth_train_sft.py", line 75, in _load_unsloth_model_and_tokenizer
[rank1]:     model, tokenizer = FastVisionModel.from_pretrained(**common_kwargs)
[rank1]:                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank1]:   File "/root/foo-llm-finetune/.venv/lib/python3.12/site-packages/unsloth/models/loader.py", line 998, in from_pretrained
[rank1]:     new_model_name = get_model_name(
[rank1]:                      ^^^^^^^^^^^^^^^
[rank1]:   File "/root/foo-llm-finetune/.venv/lib/python3.12/site-packages/unsloth/models/loader_utils.py", line 264, in get_model_name
[rank1]:     raise NotImplementedError(
[rank1]: NotImplementedError: Unsloth: google/gemma-4-31B-it is not supported in your current Unsloth version! Please update Unsloth via:

editing in unsloth/models/loader_utils.py fixed it. this was the commit before this code was introduced

def _get_new_mapper():
    try:
        import requests
        new_mapper = "https://raw.githubusercontent.com/unslothai/unsloth/7c5464ad71f93365773f68bb9469c9d55a859ad9/unsloth/models/mapper.py"
        with requests.get(new_mapper, timeout = 3) as new_mapper:
            new_mapper = new_mapper.text
      ```

@rolandtannous

Copy link
Copy Markdown
Contributor Author

@jonahsamost you need to update your installed version of studio. we just made a release and you need to update to that release or else your older version will pull the updated mapper file from the repo but not have the other necessary changes.
Please install curl -fsSL https://unsloth.ai/install.sh | sh

or

update : https://unsloth.ai/docs/get-started/install/updating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-review-failed Auto-review rejected the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants