Repository navigation
Fix fast inference crash on compressed-tensors FP8 models - #7025
danielhanchen merged 2 commits into
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
@codex review |
There was a problem hiding this comment.
💡 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".
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| try: | ||
| from unsloth.kernels.utils import _is_bnb_quant_state | ||
| _IMPORT_OK = True | ||
| except Exception: | ||
| _IMPORT_OK = False |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@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.
8fe1961 to
b70f03f
Compare
|
@codex review |
There was a problem hiding this comment.
💡 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 | ||
|
|
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Summary
Loading a compressed-tensors FP8 checkpoint (for example
unsloth/Llama-3.2-1B-Instruct-FP8-Block) withfast_inference=Falseand running generation crashes:Root cause
A compressed-tensors
CompressedLinearexposes an already-dequantized bf16weightat forward time, while still holding aweight_scaleParameter.get_lora_parameters_biasfalls back to thatweight_scaleas the quant state. Thenfast_linear_forward(andmatmul_lora) see a non-None quant state on a non-fp8 weight and route into the bitsandbytesfast_gemv/fast_dequantizepath, which assumes the quant state is a bitsandbytesQuantStatewith.absmax. Aweight_scaleParameter has no.absmax, so it crashes.Fix
Only take the bitsandbytes path when the quant state is actually a bitsandbytes 4bit
QuantState(hasabsmax, or the legacy list form). Otherwise the weight is already a usable bf16 tensor, so matmul it directly. New helper_is_bnb_quant_stategates bothfast_linear_forwardandmatmul_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...").load_in_4bit=True) still usesfast_gemvand works.unsloth/Qwen3-8B->Qwen3-8B-FP8) uses the fp8 path, unchanged.tests/test_fast_gemv_dispatch.pycovers_is_bnb_quant_state(Parameter / tensor / None -> not bnb; QuantState-with-absmax / list / tuple -> bnb).Files
unsloth/kernels/utils.py-_is_bnb_quant_statehelper and the two dispatch guards.tests/test_fast_gemv_dispatch.py- new test.