Repository navigation
[PyTorch] Honor optimize_for_gemm in quantized outputs - #3817
Conversation
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
for more information, see https://pre-commit.ci
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…to quantizer_gemm_ready_outputs Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com> # Conflicts: # transformer_engine/pytorch/csrc/common.h # transformer_engine/pytorch/csrc/extensions/cast.cpp # transformer_engine/pytorch/csrc/extensions/pybind.cpp # transformer_engine/pytorch/csrc/quantizer.cpp
timmoon10
left a comment
There was a problem hiding this comment.
Good catch, it definitely is hacky that we sometimes ignore optimize_for_gemm. I'm skeptical about the changes to the C++ infrastructure since QuantizationWorkspace's design is rather ad hoc.
| GRAD_INPUT3 = 5 | ||
| }; | ||
|
|
||
| struct QuantizationWorkspace { |
There was a problem hiding this comment.
This class seems overengineered and ad hoc. The purpose is to handle a few kernels that require compact scales (cuDNN RMSNorm/LayerNorm and some NVFP4 quant configs) and it seems simpler to put this workspace logic there rather than expose it in all of the quantizer interfaces.
| QuantizationWorkspace scale_workspace; | ||
| if (cudnn_mxfp8_output && quantizer_cpp->optimize_for_gemm) { | ||
| scale_workspace.prepare(out_nvte); | ||
| } |
There was a problem hiding this comment.
This is redundant with the existing logic to instantiate an intermediate tensor when we can't output directly into the output tensor:
Better to fix the existing logic so it's not specific to high-precision intermediates, rather than creating a side path.
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Add FUSED_NORM_QUANT_UNSWIZZLED to layernorm_fwd and handle compact scale views and swizzling in the existing Impl switches of both normalization functions instead of a separate optional path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
for more information, see https://pre-commit.ci
Convert a reused GEMM output with the quantizer where the output tensor is constructed instead of after the GEMM. Limit compact scale views and batched swizzling to the grouped NVFP4 split-quantize kernel; other multi-tensor paths swizzle per tensor in quantize(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Quantize NVFP4 outputs whose kernel cannot write GEMM-swizzled scales into compact scratch views and swizzle them with one kernel in multi_tensor_quantize and split_quantize, while outputs with in-kernel swizzling are written directly. Declare the internal swizzle helpers in util.h next to the existing ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
for more information, see https://pre-commit.ci
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
|
My agent says:
|
[Medium impact] The PR appears safe to merge; no new actionable issue was found in the latest attention-test change. SummaryNative quantized outputs now honor
The earlier no-op coverage finding is addressed by the expanded replay test. The fused FP8 transpose finding is addressed by rejecting unsupported columnwise output. The earlier rejection-test coverage thread was manually resolved by Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Quantizer requests output layout] --> B{Kernel emits that layout?}
B -->|Yes| C[Write directly to output]
B -->|No| D[Write compact scratch scales]
D --> E[Swizzle into existing output scale buffers]
C --> F[Return requested layout]
E --> F
Reviews (14) · Last reviewed commit: "Request rowwise QKV output in custom FP8..." · Reviewed by Greptile |
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
|
/te-ci pytorch L1 |
Replace RedirectedScales and paired redirect/restore helpers with local intermediate tensor descriptors and explicit swizzle calls in MXFP8 normalization and NVFP4 quantization. Keep the final output descriptors and buffers unchanged. Prepare NVFP4 compact scales inside the existing multi-tensor dispatchers and retain one batched swizzle per populated orientation. Preserve cached scales when a GPU noop skips quantization, and extend the existing graph replay test to cover GEMM layouts and stable buffer addresses. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Replace enum-based descriptor copies with explicit MXFP8 and NVFP4 data, scale and amax setters. Use named rowwise and columnwise scratch buffers, and prepare mixed-path intermediates only for NVFP4 outputs that need them. Pass the original tensor directly as the swizzle destination and as the no-op preservation unswizzle source. Keep directional compact scale views, batched conversion, format metadata and stable output buffers. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Add a stateless helper for one orientation of data, scale and amax fields. Reuse it for compact kernel outputs and swizzle source views in MXFP8 normalization and NVFP4 single and batched quantization. Keep scratch ownership, layout policy and GPU operations at the call sites. The helper neither allocates storage nor redirects the original tensor. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Prepare batched output descriptors, scale storage and swizzle pointer lists in one private helper used by multi-tensor and split quantization. Return plain owning records and leave quantization and swizzle launches explicit at the call sites. Preserve complete descriptors for unselected and empty outputs, including the global scale used by per-tensor FP8. Keep original wrappers unchanged. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Keep scratch storage in caller-owned vectors and share ordinary preparation functions. Leave batched scale swizzles explicit at both quantization call sites, without introducing wrapper classes or aggregate state types. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Use a plain helper to prepare MXFP8 scratch scales and directional views for LayerNorm and RMSNorm. Keep storage in caller-owned vectors and leave normalization and scale swizzle calls explicit. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Use convert_and_update_tensor for every supplied output, including fused FP8 GEMM and unquantized outputs, as suggested in review. Keep the choice of fused or unfused quantization independent from output preparation. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Check the delayed-scaling output quantizer before creating or updating D. Reject requests requiring a separate transpose when fused GEMM only writes rowwise data. Allow columnwise usage when the hardware can consume that same representation, and leave unfused quantization unchanged. Extend the existing GEMM numerical test to cover both output usages and the expected error on architectures requiring a separate transpose. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Drop the added columnwise parameterization and error assertions at the author's request. Keep the original numerical case with a rowwise-only output quantizer, matching the supported fused GEMM output representation. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Request rowwise-only dgrad output with a private quantizer copy, leaving consumer transpose requirements intact. Align fake backward output usage and enable delayed-scaling coverage. Preserve supplied quantized output storage when no quantizer is provided by reusing the existing tensor conversion. Add an exact FP8 output reuse regression. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
| for (size_t i = 0; i < num_tensors; ++i) { | ||
| quantizer_cpp_list[i]->quantize(input_list[i], output_list[i]); | ||
| quantizer_cpp_list[i]->quantize(input_list[i], kernel_outputs[i]); | ||
| } |
There was a problem hiding this comment.
Nit: Instead of adding NVFP4-specific logic to the general case, it may be cleaner to have a dedicated NVFP4 code path.
switch (impl) {
case Impl::FP8_FUSED_QUANT:
nvte_multi_cast_transpose(...);
break;
case Impl::NVFP4_QUANT_MULTI_SWIZZLE:
prepare_compact_quantization_outputs(...);
for (size_t i = 0; i < num_tensors; ++i) {
quantizer_cpp_list[i]->quantize(input_list[i], output_list[i]);
}
nvte_multi_tensor_swizzle_scaling_factors(...);
break;
default:
for (size_t i = 0; i < num_tensors; ++i) {
quantizer_cpp_list[i]->quantize(input_list[i], output_list[i]);
}
}| if (noop_flag) { | ||
| // Preserve cached scales when the quantization kernel is a GPU no-op. | ||
| for (auto& scale_view : scale_inputs) { | ||
| NVTE_SCOPED_GIL_RELEASE({ | ||
| nvte_unswizzle_scaling_factors(out.data(), scale_view.data(), | ||
| at::cuda::getCurrentCUDAStream()); | ||
| }); | ||
| } | ||
| } |
There was a problem hiding this comment.
This extra kernel is unfortunate. That said, handling this properly may be outside the scope of this PR:
- Supporting the noop flag in the swizzle kernels is not hard, but would require an API change.
- Quantized weights are preferable to using cached quantized weights, which is the case where the noop flag is relevant.
- This extra swizzling is mostly relevant when the tensor sizes are too small to be well aligned, in which case NVFP4 may not be the best choice.
The custom MHA path passes QKV directly to fused attention and never uses its transpose. Match its GEMM output usage to the rowwise representation that fused GEMM produces, retaining the guard against unwritten columnwise output. Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Description
Some native operations return compact MXFP8 or NVFP4 scales even when their quantizer has
optimize_for_gemm=True. Callers and fake implementations must then predict the selected kernel's output layout.Honor the requested GEMM scale layout at the native operation boundary. When a kernel can only emit compact scales, a local tensor descriptor shares the output's data and amax buffers but uses scratch scales. The operation then explicitly swizzles those scales into the output's own buffers. Output buffers are never reallocated, so their addresses stay stable. Kernels that already emit GEMM-swizzled scales skip the conversion.
Type of change
Changes
noop_flag, the previous scales are unswizzled into the scratch buffers first, so a GPU no-op leaves the output unchanged.Impl::FUSED_NORM_QUANT_UNSWIZZLEDfor cuDNN MXFP8 LayerNorm/RMSNorm withoptimize_for_gemm, preparing compact scales and explicitly swizzling them within the existing intermediate-output switches. Keep 2D MXFP8 normalization on the compatible unfused path.inplace_swizzle_scale_for_gemmcalls, which replaced the scale buffers of the output.fake_bgradto derive the layout directly from the quantizer and remove the unused layout-query binding.Checklist: