Repository navigation
NVFP4: keep fast decode callers in the activation dtype under autocast - #14750
Merged
Merged
Conversation
nvfp4_linear returns the autocast dtype since #14625, but fast_linear_forward updates the result in place with the LoRA product and the Llama decode path writes it into X.dtype buffers. In-place and out= ops do not autocast, so fp16 activations under bf16 autocast (and the reverse) raised dtype errors. Cast the NVFP4 result back to X.dtype inside fast_linear_forward only. With a bias, keep the result in the compute dtype instead of casting back to X.dtype, so a fp16 input under bf16 autocast no longer overflows.
[skip ci]
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Since #14625,
nvfp4_linearreturns the CUDA autocast dtype, matching the dense matmul it replaces. That is right for the autograd path, but two inference callers work in the activation dtype and use in-place orout=ops, which PyTorch does not autocast. With fp16 activations under bf16 autocast (or bf16 under fp16), on the fused path:fast_linear_forwardwith LoRA (decode, batch 1 and 2) updates the NVFP4 output in place with LoRA operands cached inX.dtype:addmv input tensors must have the same dtype, but got BFloat16, Half, and Half(batch 1)self and mat2 must have the same dtype, but got BFloat16 and Half(batch 2)LlamaAttention_fast_forward_inference, batch 1) allocates its KV and score buffers in the hidden-state dtype and runs_gqa_decode_scoreswithout=:expected scalar type BFloat16 but found Half(llama.py:607)Before #14625 the fused kernel returned
X.dtype, so both callers worked.A second, older issue: with a bias,
NVFP4Linear_matmul.forwardcast the result back toX.dtypeafter the bias add. A directnvfp4_linear(X_fp16, ..., bias)under bf16 autocast still returned fp16 and overflowed toinfwhereF.linearreturns finite bf16.Change
fast_linear_forward: cast the NVFP4 result back toX.dtypewhen it differs. This is the decode contract (buffers, LoRA scratch and cached LoRA weights are allX.dtype), and the cast only runs under mixed autocast. Without autocast nothing changes.NVFP4Linear_matmul.forwardwith a bias: keep the compute dtype (the autocast dtype under autocast, elseX.dtype). A float32 bias still promotes and is cast back to the compute dtype, and the cast is still skipped when the dtype already matches. Without autocast the output dtype is unchanged. The compressed-tensors forward adds its bias outside the op and already followsout.dtype.nvfp4_linearwithout a bias still returns the autocast dtype,grad_Xis stillX.dtype.Other callers checked:
matmul_loraand the fused LoRA MLP/QKV/O kernels dequantize NVFP4 weights and never callnvfp4_linear; all Llama decode projections go throughfast_linear_forward; the Studio diffusion NVFP4 module is a separate implementation.Validation
B200, fused path forced with
UNSLOTH_NVFP4_FUSED=1as the existing tests do. Newtests/test_nvfp4_autocast_callers.py, base = current main:fast_linear_forwardLoRA decode, fp16 / bf16 autocast, batch 1 and 2fast_linear_forwardLoRA decode, bf16 / fp16 autocast, batch 1 and 2nvfp4_linearwith fp16 / fp32 bias, fp16Xunder bf16 autocast, vsF.linearX.dtypeEach caller test checks that the fused kernel ran for every projection, the output dtype and the result against an fp64 reference.
NVFP4 and decode test files (
tests/test_nvfp4*.py,tests/test_decode_gqa_in_place.py,tests/test_decode_fused_rope.py, GPU tests included):The 2 remaining torch 2.10 failures,
test_nvfp4_loader.py::test_lora_trains_on_the_packed_base_and_reloads[qwen3_5-*-False], fail the same way on main.torch.compile (
CompileCounterplustorch._dynamo.explain, torch 2.14):nvfp4_linearforward and backward over rows 16 / 32 / 48 / 64 / 64 withdynamic=True, with and without a bias, with and without bf16 autocast, andfast_linear_forwarddecode with and without autocast: 0 graph breaks everywhere, and the same frame counts as main (2 fornvfp4_linear, 1 forfast_linear_forward).Not covered
fast_linear_forwardwith LoRA and notemp_lorascratch under mixed autocast fails on every weight type (dense included), before and after NVFP4 fused GEMM: honor autocast dtype and check K alignment for dX #14625:torch.mv/torch.mmautocast the LoRA product. That is an older limitation and is not changed here.