Skip to content

Fix fast inference crash on compressed-tensors FP8 models - #7025

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
danielhanchen:fix-fast-gemv-compressed-tensors
Jul 9, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
danielhanchen:fix-fast-gemv-compressed-tensors

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Loading a compressed-tensors FP8 checkpoint (for example unsloth/Llama-3.2-1B-Instruct-FP8-Block) with fast_inference=False and running generation crashes:

AttributeError: 'Parameter' object has no attribute 'absmax'
  in unsloth/kernels/utils.py, fast_gemv

Root cause

A compressed-tensors CompressedLinear exposes an already-dequantized bf16 weight at forward time, while still holding a weight_scale Parameter. get_lora_parameters_bias falls back to that weight_scale as the quant state. Then fast_linear_forward (and matmul_lora) see a non-None quant state on a non-fp8 weight and route into the bitsandbytes fast_gemv / fast_dequantize path, which assumes the quant state is a bitsandbytes QuantState with .absmax. A weight_scale Parameter has no .absmax, so it crashes.

Fix

Only take the bitsandbytes path when the quant state is actually a bitsandbytes 4bit QuantState (has absmax, or the legacy list form). Otherwise the weight is already a usable bf16 tensor, so matmul it directly. New helper _is_bnb_quant_state gates both fast_linear_forward and matmul_lora.

Verification

  • unsloth/Llama-3.2-1B-Instruct-FP8-Block (compressed-tensors), fast_inference=False: before this change it crashes; after, it generates coherently ("The capital of France is Paris. The Eiffel Tower is located in Paris...").
  • No regression, all coherent:
    • bitsandbytes 4bit (load_in_4bit=True) still uses fast_gemv and works.
    • block FP8 (unsloth/Qwen3-8B -> Qwen3-8B-FP8) uses the fp8 path, unchanged.
    • plain bf16 unchanged.
  • New CPU unit test tests/test_fast_gemv_dispatch.py covers _is_bnb_quant_state (Parameter / tensor / None -> not bnb; QuantState-with-absmax / list / tuple -> bnb).

Files

  • unsloth/kernels/utils.py - _is_bnb_quant_state helper and the two dispatch guards.
  • tests/test_fast_gemv_dispatch.py - new test.

@danielhanchen
danielhanchen requested a review from Datta0 as a code owner July 9, 2026 06:30
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: 6c6e1f850f

ℹ️ 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
elif W_quant is not None and not _is_bnb_quant_state(W_quant):
# compressed-tensors style: weight already dequantized to bf16 with a non-bnb
# `weight_scale`; matmul directly instead of the bnb fast_dequantize path.
out = torch_matmul(X, W.t(), out = out)

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 Preserve bias on compressed prefill matmuls

When this new compressed-tensors fallback is reached through fast_linear_forward for q_len != 1, fast_linear_forward returns matmul_lora(...) before its later if bias is not None: out += bias block runs. That means compressed-tensors layers whose live weight is dequantized but whose projection has a bias will now run during multi-token prompt/prefill, but silently compute X @ W.T without the bias, corrupting logits/KV cache for those architectures. The fallback needs to keep the bias path reachable or apply the bias before returning from the prefill path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed by resolving this at the source instead of at dispatch. The scale fallback in get_lora_parameters/get_lora_parameters_bias is now gated on the fp8 weight dtype, so a decompressed bf16 weight gets W_quant=None and flows through the normal bf16 path, which preserves bias in prefill.

Comment thread unsloth/kernels/utils.py Outdated
Comment on lines +1108 to +1111
elif W_quant is not None and not _is_bnb_quant_state(W_quant):
# compressed-tensors style: weight already dequantized to bf16 with a non-bnb
# `weight_scale`; matmul directly instead of the bnb fast_dequantize path.
out = torch_matmul(X, W.t(), out = out)

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 Avoid saving non-bnb quant states for LoRA backward

When LoRA adapters are active on a compressed-tensors model, this new direct-matmul path lets the forward pass succeed while leaving W_quant as the layer's weight_scale Parameter. The custom LoRA backward paths saved from this forward later call fast_dequantize(..., W_quant) directly (for example in fast_lora.py's MLP/QKV backwards), and fast_dequantize still treats any non-list quant state as bitsandbytes and reads .absmax, so the first backward pass raises the same AttributeError. The non-bnb state needs to be cleared/normalized for these forwards or handled in fast_dequantize as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed. Because a decompressed bf16 weight now resolves to W_quant=None, no non-bnb scale is stored for the LoRA backward, so fast_dequantize is never entered on it. Real fp8 weights still keep their scale.

Comment thread tests/test_fast_gemv_dispatch.py Outdated
Comment on lines +26 to +30
try:
from unsloth.kernels.utils import _is_bnb_quant_state
_IMPORT_OK = True
except Exception:
_IMPORT_OK = False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P3 Badge Don't let helper import failures pass the test

Because this catches every exception and the test later just returns, the new coverage is reported as passing whenever unsloth.kernels.utils fails to import for any reason, including a broken _is_bnb_quant_state definition or a bitsandbytes API error in this module. In CPU/no-bitsandbytes jobs this should be an explicit skip, but unrelated import failures should fail so the regression test can actually catch breakage in the dispatch helper.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed. The test now uses pytest.importorskip("bitsandbytes") to skip only on missing kernel deps, then imports get_lora_parameters_bias and _FP8_WEIGHT_DTYPES directly so a real import break surfaces as a failure.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

Loading a compressed-tensors FP8 checkpoint (for example
unsloth/Llama-3.2-1B-Instruct-FP8-Block) with fast_inference=False and
running a forward crashed with 'Parameter object has no attribute absmax'
inside fast_gemv.

A compressed-tensors CompressedLinear exposes an already dequantized bf16
weight at forward time while keeping a weight_scale Parameter. The quant
state resolution in get_lora_parameters/get_lora_parameters_bias fell back
to that weight_scale, so a bf16 weight was routed into the bitsandbytes
fast_gemv/fast_dequantize path, which expects a bitsandbytes QuantState
with an absmax attribute.

Only fall back to weight_scale_inv/weight_scale when the weight is still
fp8. A decompressed bf16 weight then resolves to no quant state and flows
through the normal bf16 path, which already handles bias and the LoRA
backward. Real fp8 and bitsandbytes 4bit weights are unchanged.
@danielhanchen
danielhanchen force-pushed the fix-fast-gemv-compressed-tensors branch from 8fe1961 to b70f03f Compare July 9, 2026 08:55
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

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

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


import pytest
import torch

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 Move the bitsandbytes skip before importing Unsloth

In environments that do not install bitsandbytes (the CPU-only case this test says it wants to skip), this import runs before pytest.importorskip("bitsandbytes"). import unsloth loads unsloth.save -> .kernels -> unsloth.kernels.utils, which imports bitsandbytes unconditionally, so collection errors instead of producing a skip. Put the importorskip before importing Unsloth.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch. Moved pytest.importorskip("bitsandbytes") above import unsloth so a runner without bitsandbytes skips at collection instead of erroring, since unsloth.kernels.utils imports bitsandbytes unconditionally.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 3e51bf6a0e

ℹ️ 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 cd9d251 into unslothai:main Jul 9, 2026
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.

1 participant