Skip to content

QLoRA quant-state fallback: gate per weight, not a process-global flag - #14231

Merged
danielhanchen merged 1 commit into
mainfrom
dh/fsdp2-qlora-guard
Oct 19, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/fsdp2-qlora-guard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-up to the #13880 review.

  1. Process-global QLoRA flag. prepare_fsdp2_qlora set unsloth.kernels.utils._FSDP2_QLORA_PREPARED = True, and the LoRA getters and fast_lora_forward read it before falling back to the Linear4bit's own quant_state. Dynamo guards on a global it reads. Once one FSDP2 QLoRA model had been prepared, every compiled dense LoRA forward in the process compiled a second graph. The fallback is now gated on the weight itself: only an FSDP2 all-gathered 4-bit weight loses quant_state, and its packed bytes are (N, 1). The flag and its setter are removed.
  2. Block compile and non-reentrant checkpointing. A non-reentrant checkpoint ends a recompute by raising _StopRecomputationError through the forward. The block compile wrapper treated that as a compile failure and put the stock forward back in every block. It now re-raises it.

Without FSDP2 the getters take the same path as before: getattr(W, "quant_state") first, and the shape check only runs when that is None.

Tests

tests/python/test_get_lora_parameters_fsdp2_quant_state.py:

Case main this PR
a compiled dense LoRA forward, before and after an FSDP2 QLoRA model is prepared 2 graphs 1 graph
the getters read no process-global flag fails passes

With the existing fp8 getter tests, tests/test_block_compile.py, tests/test_fsdp2_qlora.py and tests/test_fsdp2_qlora_prepare_order.py: 59 passed.

Under a 2-rank launch with Unsloth's block compile, Llama, Qwen3, Qwen3-MoE, Gemma3, Gemma4 and Qwen2.5-VL show 0 graph breaks and nothing compiling from step 2 on, LoRA and QLoRA.

Not covered here

…al flag; block compile passes checkpoint's stop-recompute through
@danielhanchen danielhanchen self-assigned this Oct 19, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T10:52:13.955458Z b57a7df Manual request
🔒 Security Review ⚠️ Failed 2026-10-19T09:22:56.344690Z b57a7df PR opened
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: b57a7dfcff

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

Copy link
Copy Markdown
Member Author

The Core zoo (HF=4.57.6 + TRL<1) red here is not from this PR. The run resolved unsloth-zoo at f9c2fb21, where test_only_the_verify_forward_is_built_as_a_verify_block fails with 'SpeculativeEngine' object has no attribute '_invariant' (zoo #1871). unslothai/unsloth-zoo#1877 fixed it on zoo main. The zoo commit is resolved once per run, so this needs a full re-run of the workflow or a new push, not "re-run failed jobs".

@danielhanchen
danielhanchen merged commit 2ff041b into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the dh/fsdp2-qlora-guard branch October 19, 2026 17:21
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 21, 2026
…yer (unslothai#14065)

Part of unslothai#14063.

## Summary

The fix this PR originally made (re-raise torch's
`_StopRecomputationError` from the block-compile trial loop instead of
demoting the layer) landed on main separately in unslothai#14231, so the code
change here is dropped after merging main. What main does not have is a
test for it, so this PR now adds only that:

- `test_stop_recomputation_does_not_demote` makes the compiled block
raise `torch.utils.checkpoint._StopRecomputationError` and asserts it
propagates with the layer still on its first mode and not marked
checked. Demoting on it is what made the recompute run the stock forward
and fail `check_recomputed_tensors_match` on distributed runs.

## Verification

- With main's re-raise reverted (`if False:`), the new test fails; with
main as is, it passes.
- `tests/test_block_compile.py` on CPU: the new test passes; the one
failure there (`test_sm_count_is_read_outside_the_trace`) needs a CUDA
device and fails the same way on main without one.

The remaining Muse Glimmer route in unslothai#14063 (65 vs 64 with no demotion
logged, plus the slowdown) is fixed in unslothai/unsloth-zoo#1895.

## CI fixes pulled in with main

Merging main brought in CPU CI failures unrelated to block compile; they
are fixed here (tests only, merge base to head touches only `tests/`):

- `tests/test_gpt_oss_*`: skip guards read `has_real_cuda()` instead of
the spoofable `torch.cuda.is_available()`, and the Hub stub writes with
`encoding="utf-8"`.
- `tests/test_ddp_comm_defaults.py`: torch refuses `bf16_compress_hook`
on builds without CUDA/HIP and NCCL 2.10+
(`DistributedDataParallel._check_comm_hook`), which sank the shared
ranks fixture on CPU wheels. The bf16 case now runs only where that same
build check passes; the fp16 case still runs on every host.

---------

Co-authored-by: Daniel Han <[email protected]>
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