[feat]: Add NVFP4QAT linear layer (Attn-QAT 3/12) - #1350
Conversation
Adds NVFP4QATConfig — the Attn-QAT flavor of FP4 quantization. Distinct from main's NVFP4Config (NVIDIA Blackwell hardware FP4 inference); this config is for the QAT training side of the Attn-QAT stack. Pure deadcode: no existing code path imports NVFP4QATConfig yet. The `nvfp4_qat` method literal in QuantizationMethods makes it selectable, but the caller lands in slice 3+. Renamed from PR-1225's original `Fp4Config` / `fp4_config.py` / `"fp4"` literal to avoid collision with PR #1334's NVFP4 plumbing already merged to main. Extracted from PR #1225 (#1225) by @RandNMR73. Source SHA: 3f818d0 Attn-QAT-Stack: 2/12 Co-Authored-By: jzhang38 <42993249+jzhang38@users.noreply.github.com> Co-Authored-By: RandNMR73 <99706358+RandNMR73@users.noreply.github.com>
Merge ProtectionsYour pull request matches the following merge protections and will not be merged until they are valid. 🔴 PR merge requirementsWaiting for
This rule is failing.
|
There was a problem hiding this comment.
Code Review
This pull request introduces an FP4 linear layer implementation leveraging the flashinfer library. It includes a custom autograd function, _LinearFWD4BWD16Fn, which handles FP4 quantization and matrix multiplication in the forward pass, while maintaining standard precision for the backward pass. The review feedback suggests optimizing the _global_sf helper function by moving it to the module level to avoid re-definition overhead during each forward pass and using the more idiomatic .clamp() method for numerical stability.
| return flashinfer | ||
|
|
||
|
|
||
| class _LinearFWD4BWD16Fn(torch.autograd.Function): |
There was a problem hiding this comment.
Move the _global_sf helper to the module level and decorate it with @torch.compile there. Defining and decorating a function inside the forward method of an autograd.Function is highly inefficient as it causes the function to be re-defined (and potentially re-compiled or cache-checked) on every forward pass, leading to significant overhead. Additionally, using .clamp(min=...) is more idiomatic and efficient than torch.maximum with a newly created tensor.
@torch.compile
def _global_sf(t: torch.Tensor) -> torch.Tensor:
maxabs = t.float().abs().nan_to_num().max()
maxabs = maxabs.clamp(min=1e-12)
return (448.0 * 6.0) / maxabs
class _LinearFWD4BWD16Fn(torch.autograd.Function):There was a problem hiding this comment.
Done in dbc9e7c. Hoisted _global_sf to module level with @torch.compile and switched to .clamp(min=1e-12). Thanks for the catch.
| @torch.compile | ||
| def _global_sf(t: torch.Tensor) -> torch.Tensor: | ||
| maxabs = t.float().abs().nan_to_num().max() | ||
| maxabs = torch.maximum(maxabs, torch.tensor(1e-12, device=t.device, dtype=maxabs.dtype)) | ||
| return (448.0 * 6.0) / maxabs | ||
|
|
There was a problem hiding this comment.
Done in dbc9e7c. Hoisted _global_sf to module level with @torch.compile and switched to .clamp(min=1e-12). Thanks for the catch.
Adds the FastVideo-native FP4 linear forward helper module extracted from PR-1225. The source module does not define a public Fp4Linear class, so this slice keeps the original fp4linear.py filename and helper names. Pure deadcode: no existing code path imports this helper yet. Activation lands in Slice-12 of the decomposition. Extracted from PR #1225 (#1225) by @RandNMR73. Source SHA: 3f818d0. Attn-QAT-Stack: 3/12 Co-Authored-By: jzhang38 <42993249+jzhang38@users.noreply.github.com> Co-Authored-By: RandNMR73 <99706358+RandNMR73@users.noreply.github.com>
061ec5c to
dbc9e7c
Compare
|
/merge |
Summary
Slice 3 of 12 in the decomposition of #1225 (
[feat] Upstream Attn-QAT Video Diffusion Code). Adds the FastVideo-native FP4 linear forward helper module extracted from PR-1225.Pure deadcode: no existing code path imports
fp4_linear_forwardyet. The QAT attention backends (Slice 4) and beyond will wire it up; activation lands in Slice 12.Stacking
pr1225_s2(Slice 2, [feat]: Add NVFP4QAT quantization config (Attn-QAT 2/12) #1348, still open).main.Naming family
NVFP4QATConfig(Slice 2, [feat]: Add NVFP4QAT quantization config (Attn-QAT 2/12) #1348)NVFP4Config/NVFP4quant method (Blackwell hardware FP4 inference — added by PR [infra] Dreamverse 11/14: Add NVFP4 quantization support #1334).Files
fastvideo/layers/fp4linear.py(NEW, 115 LOC) — source PR-1225 module. The extracted file does not define a publicFp4Linear*class or importFp4Config, so no class/file rename or config-import substitution was applied.Provenance
Extracted from PR #1225 by @RandNMR73 (source SHA:
3f818d0f). Git author is @SolitaryThinker; @jzhang38 and @RandNMR73 are co-authored via commit trailers.Test plan
pre-commit run --files fastvideo/layers/fp4linear.pypasses.