Skip to content

DeepSeek-V4.1 (transformers port): load, place and train it with LoRA / QLoRA - #17625

Merged
danielhanchen merged 1 commit into
mainfrom
deepseek-v41-lora
Nov 12, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
deepseek-v41-lora

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Why

DeepSeek-V4.1-Flash is only in the community transformers port (malaiwah/transformers@add-deepseek-v41). On a tiny copy of the real config, Unsloth could not load or train it:

  • Load: fails with Unrecognized configuration class ... AutoModelForImageTextToText. The composite config has a vision_config, but the port only maps a causal LM.
  • Placement: the whole model stayed on the CPU. The port's _no_placement_params = ["weight", "weight_scale_inv"] are bare names that transformers reads relative to a _no_split_modules module, but Unsloth suffix-matched them, so they caught every weight.
  • 4-bit: breaks o_a_proj. It is a grouped nn.Linear subclass, and bitsandbytes swaps it for a plain Linear4bit.
  • Packing: padding-free packing would leak across sequences, because the engram n-gram hash and the compressor pool over the whole row.
  • Region compile: crashes in Inductor's backward on Min(index_topk, T) (torch 2.14). Unsloth's model compile also gave a compiled-vs-recompute mismatch (CheckpointError) under gradient checkpointing.

Change

  • loader.py:
    • When no image-text class resolves for a vision-config checkpoint, fall back to AutoModelForCausalLM.
    • For deepseek_v41, load with UNSLOTH_COMPILE_DISABLE=partial, as csm already does.
  • loader_utils.py: new no_placement_matcher, also used in vision.py:
    • a bare name matches only a direct tensor of a no-split module;
    • a dotted name, such as Qwen4Exp's, keeps the suffix match.
  • vision.py: keep o_a_proj in 16-bit under 4-bit loads.
  • packing.py: refuse padding-free packing for this model.
  • _block_compile.py: do not region-compile DeepseekV41DecoderLayer.

Everything except the matcher, which only fixes bare names, is keyed on the port's classes or model type, so other models are untouched.

Evidence

  • CPU:
    • tests/test_deepseek_v41_lora_coverage.py: 6 passed; the new tests fail on main.
    • tests/test_block_compile.py plus the related suites: 89 passed.
  • GPU (Colab L4): tiny DeepSeek-V4.1 from the real config and tokenizer, with the companion zoo PR (DeepSeek-V4.1 (transformers port): LoRA / QLoRA on every linear with gradient checkpointing unsloth-zoo#2483).
    • 16-bit LoRA and 4-bit QLoRA both train: losses 11.787 to 11.188, and 11.803 to 11.191.
    • All 65 adapters get a gradient and change: attention, compressor, shared and routed experts, engram wkv.
    • On main, loading fails.

Not covered

  • Speed: model compile is off for this model until the recompute mismatch is root-caused.
  • Real fp4 / fp8 weights and multi-GPU.

…ram tables by transformers' no-placement rule, keep o_a_proj 16-bit under 4-bit, refuse padding-free, no region compile
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T19:51:00.429148Z 162c7dc Manual request
ℹ️ 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. More of your lovely PRs please.

Reviewed commit: 162c7dc332

ℹ️ 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
danielhanchen merged commit 3221ed0 into main Nov 12, 2026
@danielhanchen
danielhanchen deleted the deepseek-v41-lora branch November 12, 2026 20:10
meefs pushed a commit to meefs/unsloth that referenced this pull request Nov 13, 2026
…point recompute match (unslothai#17660)

## Why

Since unslothai#17625, loading DeepSeek-V4.1 (community port) sets
`UNSLOTH_COMPILE_DISABLE=partial`, because a compiled helper inside a
checkpointed layer did not match its recompute. The model has trained
with only the loss compiled since then.

unslothai/unsloth-zoo#2488 fixes the mismatch and adds
`prepare_compiled_checkpoint()`. With it, the non-reentrant checkpointed
layers run compiled helpers in both passes.

## What this changes

`unsloth/models/loader.py`, the `deepseek_v41` branch:

* It calls
`unsloth_zoo.temporary_patches.deepseek_v41.prepare_compiled_checkpoint()`.
* It still sets `UNSLOTH_COMPILE_DISABLE=partial` when the import fails
(an unsloth_zoo without the fix) or the call returns False. Either side
can update first without breaking training.

`tests/test_deepseek_v41_lora_coverage.py`: the source-level branch test
now checks the call and the fallback.

## Evidence

Colab L4, torch 2.14.1, the port (malaiwah/transformers@62d7ebd), this
branch on main 624ddb3 plus unsloth-zoo#2488. Tiny DeepSeek-V4.1,
Unsloth LoRA / QLoRA on every linear, unsloth gradient checkpointing,
engram tables on disk, 12 steps:

| run | step ms (median after 3) | graphs / breaks | max rel loss diff
vs eager |
|---|---|---|---|
| 16-bit compiled | 379, repeat 293 | 35 / 0 | 1.7e-4 |
| 16-bit eager | 611, repeat 486 | 0 / 0 | |
| 4-bit compiled | 380 | 39 / 9 | 3.1e-4 |
| 4-bit eager | 527 | 0 / 0 | |
| 16-bit compiled + offload_layers=2 | 297 | 35 / 0 | 0 vs 16-bit
compiled |
| 4-bit compiled + offload_layers=2 | 380 | 39 / 9 | 2.2e-4 vs 4-bit
compiled |

* Per 16-bit step, the profiler counts 38,321 ops compiled vs 86,824
eager, and 34.8 ms GPU time vs 66.7 ms.
* The 4-bit breaks are all `_forward_generic_experts_eagerly`, the bnb
4-bit expert path, which is disabled on purpose.
* Before this change, forcing compile on failed in the first backward
with `CheckpointError`.

The zoo PR has the failed alternatives (LRU cache off, automatic dynamic
off, raised recompile limit) and the root cause.

## Not covered

* Real-size speed. The tiny model is overhead bound.
* Multi-GPU. Not run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant