Skip to content

Propagate fp8 block_size before the early return in get_lora_parameters_bias - #7189

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
vineethsaivs:fix/lora-bias-fp8-block-size
Jul 17, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
vineethsaivs:fix/lora-bias-fp8-block-size

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

Summary

get_lora_parameters_bias in unsloth/kernels/utils.py propagates the fp8 block_size onto W / W_quant only after its disable_adapters / merged early return:

    if getattr(proj, "disable_adapters", True) or proj.merged:
        return W, W_quant, None, None, None, base_layer.bias

    if getattr(base_layer, "quant_method", None) == "fp8":
        W.block_size = getattr(base_layer, "block_size", [128, 128])
        W_quant.block_size = W.block_size

So on the merged / disabled path (merged inference, DPO reference model) the block is dead code: a block-fp8 weight never gets its real block_size attached, and the downstream fp8 kernels (fp8.py, which read getattr(weight_scale, "block_size", [128, 128])) silently fall back to [128, 128]. A block-fp8 checkpoint whose real block size is not [128, 128] is then dequantized with the wrong block size.

The non-bias sibling get_lora_parameters already sets block_size before its early return, so the two helpers disagree for the same layer.

Fix

Move the fp8 block_size block ahead of the early return, so get_lora_parameters_bias matches the long-standing ordering of get_lora_parameters. Common [128, 128] fp8 models are unaffected; the change only restores the correct block size for non-[128, 128] block-fp8 checkpoints on the bias/merged path.

Test

Added tests/python/test_get_lora_parameters_bias_fp8_block_size.py, a CPU-only test (auto-discovered by pytest tests/, no GPU). It AST-extracts get_lora_parameters_bias (unsloth needs a GPU to import) and, for a merged/disabled block-fp8 projection shipping block_size = [64, 128], asserts that value survives onto the returned weight scale. It fails before this change (the block size drops to [128, 128]) and passes after.

…rs_bias

get_lora_parameters_bias set the fp8 block_size on W/W_quant only after the
disable_adapters/merged early return, so on the merged or disabled path (merged
inference, DPO reference model) a block-fp8 weight lost its real block_size and
downstream fp8 kernels fell back to [128, 128]. The non-bias sibling
get_lora_parameters already sets block_size before its early return; move the
block so both behave the same.

@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: e389aaceb1

ℹ️ 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 thread unsloth/kernels/utils.py Outdated
if getattr(base_layer, "quant_method", None) == "fp8":
# we need to somehow store and pass this information :)
W.block_size = getattr(base_layer, "block_size", [128, 128])
W_quant.block_size = W.block_size

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Guard missing fp8 quant state before setting block_size

On the disabled/merged path this block now runs before the early return for every layer whose base_layer.quant_method == "fp8". For decompressed compressed-tensors layers the preceding code intentionally leaves W_quant as None when the live weight is bf16, as noted in the comment above, so this line raises AttributeError before fast_linear_forward can fall back to the plain matmul path; previously those disabled/merged layers returned successfully with W_quant is None. Please only attach block_size when a scale/quant state was actually found.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, this was a real regression and it is fixed in 6ec2121.

You are right about the mechanism: a decompressed compressed-tensors layer keeps quant_method == "fp8" while its weight is back to bf16, so the W.dtype in _FP8_WEIGHT_DTYPES guard above deliberately leaves W_quant as None. Moving the block ahead of the early return made W_quant.block_size run on the merged/disabled path for those layers, which raised AttributeError before fast_linear_forward could reach its if W_quant is None plain-matmul fallback. Reproduced against both revisions: upstream returns cleanly with W_quant is None, the first revision of this PR raised.

The block size is now only attached when a quant state was actually found:

if getattr(base_layer, "quant_method", None) == "fp8":
    W.block_size = getattr(base_layer, "block_size", [128, 128])
    if W_quant is not None:
        W_quant.block_size = W.block_size

That keeps the original fix (a merged block-fp8 weight now carries its real [64, 128] instead of falling back to [128, 128]) while restoring the W_quant is None return for decompressed layers. Added a second regression test covering that path: it fails on the previous revision with the AttributeError you describe and passes now.

Note the sibling get_lora_parameters has the same unguarded W_quant.block_size on its own pre-return path, so it is exposed to this for a decompressed layer with an active adapter. I have left it alone to keep this PR focused, but happy to fix it here or in a follow-up if you prefer.

@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 ensures that the FP8 block size is correctly propagated even when adapters are disabled or merged in the get_lora_parameters_bias function. By moving the block size assignment logic before the early return, the code ensures that downstream kernels receive the correct configuration. A new regression test has been added to verify this behavior. I have no further feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

A decompressed compressed-tensors layer keeps quant_method == "fp8" while its
weight is back to bf16, so it has no quant state and get_lora_parameters_bias
must still return W_quant None for fast_linear_forward to fall back to a plain
matmul. Only attach block_size when a quant state was actually found.

@ErenAta16 ErenAta16 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.

Confirmed the actual bug: the fp8 block_size propagation ran AFTER the disable_adapters or merged early return, so for the merged/inference-disabled path (the common case after merge_and_unload()), the function returned before ever setting W.block_size/W_quant.block_size. Since downstream fp8 kernels default to [128, 128] when the attribute is missing, any checkpoint with a non-default block size would silently use the wrong one on that path. Moving the propagation above the early return fixes it for both branches.

The if W_quant is not None: guard is a genuine second bug, not just defensive padding, a decompressed compressed-tensors layer keeps quant_method == "fp8" in its metadata but has no live quant state once it's back to bf16, so the old unconditional W_quant.block_size = ... would raise AttributeError on that layer instead of correctly falling through to W_quant is None.

Nice approach on the test file too, extracting just the target function via ast instead of importing the full package, keeps it runnable without a GPU/torch/bitsandbytes. Both scenarios (non-default block size surviving the merged path, and the decompressed-layer None case) are covered directly.

…g quant state

Mirror the get_lora_parameters_bias guard so a decompressed compressed-tensors
layer (quant_method fp8, bf16 weight, no quant state) does not raise
AttributeError on the fused-LoRA path. Add a CPU-only regression test.
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed this is a real fix, not a cosmetic one.

get_lora_parameters_bias was attaching the block-fp8 block_size after the disable_adapters/merged early return, so on the merged / DPO / fast-decode inference path a non-[128,128] block-fp8 weight lost its block size and FP8BlockQuantLinear.forward fell back to getattr(..., "block_size", [128,128]). On most shapes that raises a ValueError (scale-grid mismatch); when the fallback grid coincidentally matches the real grid shape (e.g. [96,128]) it silently dequantizes with the wrong block boundaries.

Validation I ran before merging:

  • CPU differential: the added test fails on the merge-base (2 failed) and passes at head. The intermediate "propagate before the early return" commit crashes on the decompressed-layer case, which the if W_quant is not None guard then fixes, so both commits are load-bearing.
  • GPU (B200) with real block-fp8 weights across [128,128], [64,128], [64,64], [1,256], [96,128]: the common [128,128] path is unchanged (fallback == correct), and every non-default size is either a hard ValueError or silently wrong under the old fallback and matches an explicit dequant reference once the block size is carried through. Driving it through get_lora_parameters_bias end to end: base returns without the block size and the forward raises, head propagates it and the forward is correct.
  • Exhaustive state sweep (2400 layer states across quant_method / dtype / quant-state / scale / block-size / adapter-state): the fix never crashes and the two helpers now agree on every shared field.

Backwards compatibility: the common [128,128] fp8 path, the 4-bit bitsandbytes path (quant_method != "fp8"), and plain bf16 are all untouched.

I pushed one follow-up commit (85d0d5e): the sibling get_lora_parameters had the same unguarded W_quant.block_size = W.block_size, reachable on the fused-LoRA MLP/QKV/O path for a decompressed compressed-tensors layer (quant_method == "fp8", bf16 weight, no quant state). It now carries the same if W_quant is not None guard plus a mirrored CPU test, so the two helpers stay in parity.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 85d0d5e385

ℹ️ 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 a14b032 into unslothai:main Jul 17, 2026
46 of 47 checks passed
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.

3 participants