Repository navigation
NVFP4 QAT: keep the fake-quantized linear out of compiled graphs - #14812
Conversation
Inductor-compiled MLPs (torch 2.13, torchao 0.18) shrink the gate / up weight gradients of NVFP4FakeQuantizedLinear about 19x in full finetuning. Run the NVFP4 QAT forward eagerly.
|
@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 |
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
Conflicts in the NVFP4 QAT forward and its tests: keep main's autocast and contiguity handling (#14780) under this branch's decorator, and both test sets.
…sabling compile torch.compiler.disable on the patched forward fixed the compiled weight grads but made fullgraph=True fail (Skip calling torch.compiler.disable()d function), for training and for one-token decode. The bad grads come from Inductor compiling torchao's traced fake quantization: aot_eager is exact, Inductor returns the backward's dequantized activation about 2**-126 too small, so dW collapses (~100% relative error) while dX stays right. Wrap torchao's NVFP4 QAT forward and backward as two torch.library custom ops with fake impls and an autograd formula. Compile keeps them whole, so fullgraph and dynamic shapes work and the numerics are torchao's eager ones (bitwise equal to torchao's autograd Function; compiled == eager bitwise with emulate_precision_casts). Packed activations are saved as before. CPU and non-default activation configs keep torchao's own forward. [skip ci]
|
The I looked for the actual cause of the bad grads. A single NVFP4 QAT linear (torchao 0.18, torch 2.13, B200) already shows it:
The numbers are relative errors against eager. Dynamo and AOTAutograd are fine, so the problem is in Inductor. The compiled backward dequantizes the saved activation about 2**-126 too small for every normal fp4 code (the 0.5 denormal code is exact), so dW collapses and dX stays correct. The latest commit on this branch wraps torchao's NVFP4 QAT forward and backward in two
Tests on B200, torch 2.13.0, torchao 0.18.0 (
The bitwise checks set Not covered: torch 2.14 with this change (the bug itself reproduces there), torchao versions other than 0.18, GPUs other than B200 (no sm120 run), and long training runs. Fixed in 37cb689. @codex review |
|
Codex Review: Didn't find any major issues. Hooray! 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". |
…tizing it again, return the input dtype [skip ci]
|
Follow-up in 8909a7d:
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
Problem
With
full_finetuning=True, qat_scheme="nvfp4", the default compiled MLP (unsloth_compiled_module_*, Inductor,fullgraph=False, dynamic=True) runs torchao'sNVFP4FakeQuantizedLinearinside Inductor. The compiled graph returns gate_proj / up_proj weight gradients about 19x too small. down_proj and attention are close, so the loss looks normal and nothing warns.Tiny Llama (hidden 256, intermediate 512), one forward + backward, layer-0 grad norms on a B200 (torch 2.13, torchao 0.18, transformers 5.17), main 9a822f4:
UNSLOTH_COMPILE_DISABLE=1(eager), 2 runsIt reproduces with plain torchao and no Unsloth:
quantize_(mlp, QATConfig(NVFP4DynamicActivationNVFP4WeightConfig(use_triton_kernel=False), step="prepare")), thentorch.compile(mlp.forward)with Inductor gives about 99.8% relative error on the gate / up weight grads, whileaot_eageris exact. LoRA QAT (frozen base) andqat_scheme="mxfp8"were not affected in the same runs.Change
_nvfp4_any_rank_forward(the forward Unsloth installs on every NVFP4 QAT linear) is wrapped intorch.compiler.disable, so the NVFP4 fake-quantized matmul runs eagerly and the rest of the block stays compiled. The cost is one graph break per NVFP4 QAT linear, only whenqat_scheme="nvfp4"is used.Tests
tests/test_qat_microscaling_schemes.py:test_nvfp4_qat_forward_stays_out_of_compiled_graphs(CPU): the NVFP4 QAT linear forward is a graph break under Dynamo. Fails on main, passes here.test_nvfp4_qat_weight_grads_match_eager_under_compile(GPU with FP4 tensor cores): a gate / up / down MLP prepared withqat_scheme="nvfp4", compiled with Inductor, weight grads within 5% of eager. Fails on main, passes here.Full file: main 1 failed (CPU), 2 failed (B200); this branch all passed.
Not covered: torch 2.10 to 2.12 and 2.14, other torchao releases, RTX 50 series (sm_120). The fp16 autocast assertion in the same forward is handled separately in #14780.