Skip to content

[BUGFIX] Validate save methods before selecting export paths - #18710

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
DimitriChrysafis:fix-save-method-validation
Nov 23, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
DimitriChrysafis:fix-save-method-validation

Conversation

@DimitriChrysafis

Copy link
Copy Markdown
Contributor

Problem: In unsloth_generic_save, an unrecognized save_method on a non-PEFT model silently fell through to full-checkpoint save_pretrained, and the forced_merged_4bit alias was accepted at merge dispatch without being folded alongside merged_4bit_forced in unsloth_generic_save and unsloth_save_model.

Fix: Added up-front save_method validation in unsloth_generic_save before routing or Hub pushes, and normalized both merged_4bit_forced and forced_merged_4bit consistently in unsloth_generic_save and unsloth_save_model.

Tests: Expanded tests/saving/test_save_method_lora_routing.py for invalid save_method rejection on PEFT, non-PEFT, and Hub-push paths plus alias normalization; 162 passed across tests/saving/test_save_method_lora_routing.py and tests/test_merged_hub_destination.py, ruff check clean.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-23T13:40:55.533935Z
We couldn’t complete this review. Comment @codex review to try again.
f55e928 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: b0bab7037e

ℹ️ 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".

danielhanchen added a commit to unslothai/unsloth-zoo that referenced this pull request Nov 23, 2026
…n-module heads (#2706)

## Summary

Every `Core zoo` leg of the unsloth CI is red against current zoo `main`
(seen on unslothai/unsloth#18710), and the same tests fail locally on
`main`:

```
tests/test_fused_lm_head_peft_adapter.py::test_ast_rewritten_forward_trains_the_head_adapter[False]
tests/test_fused_lm_head_peft_adapter.py::test_ast_rewritten_forward_trains_the_head_adapter[True]
E   assert 1728 == 0          (out.logits.numel())
tests/test_per_device_gpu_detection.py::test_the_shared_adapter_skips_inductor_for_a_head_on_an_old_card
tests/test_per_device_gpu_detection.py::test_the_shared_adapter_checks_the_head_after_fsdp_unshards_it
E   AttributeError: 'types.SimpleNamespace' object has no attribute 'modules'
```

Both already fail at the merge commit of #2697.

## Root cause

#2688 and #2697 each added a way to keep a live PEFT adapter on
`lm_head` out of the fused kernel, and they landed an hour apart:

- #2688 put a guard in the AST rewriter's condition (`base_layer is not
None and merged == disable_adapters`), sending such a head down the
stock path, which returns the full `[tokens, vocab]` logits.
- #2697 made `unsloth_fused_lm_head_loss` itself detect a live adapter
(`lm_head_needs_module_forward`) and score the head's own forward, then
return `EMPTY_LOGITS` like the compiler path. Its test of the AST path
expects that route, but #2688's guard catches the head first.
- `lm_head_needs_module_forward` walks `lm_head.modules()`, which a head
object that is not an `nn.Module` (the shared adapter tests pass a
`SimpleNamespace` head) does not have.

## Fix

- `fused_losses/ast_rewriter.py`: drop the AST guard.
`_lm_head_module_reason` covers both states it caught (merged and
disabled, unmerged and enabled) and more: it walks every tuner in the
head, and `_module_head_ce_loss` applies the same bias, scale and
softcap as the kernel. One route for live adapters, and the AST and
compiler paths now both return `EMPTY_LOGITS` under
`UNSLOTH_RETURN_LOGITS=0`.
- `fused_losses/cross_entropy_loss.py`: `lm_head_needs_module_forward`
returns `False` for a head that is not an `nn.Module`; only a module can
carry a PEFT tuner.
- Tests: `test_fused_lm_head_coverage.py` pinned the old condition text,
and `test_merged_then_disabled_peft_head_takes_the_exact_path` used
non-empty logits as its route probe. Both now check the new route; the
latter still asserts the base loss and that the head was unmerged.

## Testing

- The four failing tests above pass, plus the whole fused-loss family
(`tests/test_fused_*.py`, the GRPO fused route tests,
`test_trl_fused_head_contract.py`, `test_per_device_gpu_detection.py`,
`test_inkling2_*.py`): 826 passed, 204 skipped.
- Sabotage: making `lm_head_needs_module_forward` always return `False`
fails 43 tests in `test_fused_lm_head_peft_adapter.py` and
`test_inkling2_fused_softcap_ce.py`, so the single route is guarded.

Not covered here: on the `HF=latest` leg (transformers 5.24)
`test_inkling_forwards_fuse_with_the_module_softcap` also fails because
the Inkling 2 forward is not routed to the AST rewriter at all; that is
a separate transformers 5.24 port.
@danielhanchen
danielhanchen marked this pull request as ready for review November 23, 2026 13:40
@danielhanchen
danielhanchen self-requested a review as a code owner November 23, 2026 13:40
@danielhanchen
danielhanchen merged commit 2f11f81 into unslothai:main Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants