Skip to content

[Common][PyTorch] Performance Improvements for FP8BS emulation on Blackwell - #3866

Open
denera wants to merge 13 commits into
NVIDIA:mainfrom
denera:common/fp8bs-blackwell-tn-layout-fix
Open

denera wants to merge 13 commits into
NVIDIA:mainfrom
denera:common/fp8bs-blackwell-tn-layout-fix

Conversation

@denera

@denera denera commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Description

BEFORE: FP8 block scaling (FP8BS) stores columnwise data as a physical transpose on every GPU, because
Hopper's FP8 GEMM only runs TN. On Blackwell, FP8BS GEMMs already run as MXFP8 GEMMs (the FP32 block
scales are converted to E8M0), and MXFP8 GEMM supports TN, NN and NT, so the transposed copy only
costs time: 1D quantization stages a transpose through shared memory, 2D quantization writes a second
full FP8 copy of every weight, distributed paths re-transpose after all-gathers, and the GEMM binding
forces every GEMM to TN to match.

AFTER: FP8BS columnwise data is laid out the same way as MXFP8 wherever FP8 GEMM supports non-TN
layouts (nvte_is_non_tn_fp8_gemm_supported(): SM 10.x, 11.x, 13.x and newer):

  • 1D (1x128 rowwise / 128x1 columnwise): columnwise data no longer in the transposed logical
    shape when non-TN FP8 GEMM is supported; the columnwise scales keep their shape and bytes.
  • 2D (128x128): no separate columnwise copy. A 128x128 block quantizes to the same bytes either
    way it is read, so rowwise data and scales serve columnwise usage.
  • GEMM: the binding keeps the caller's transa/transb and passes MXFP8 columnwise operands to
    cuBLASLt, as the MXFP8 recipe does.

Hopper and SM 12.x keep the transposed layout and TN GEMMs. Output values remain unchanged, bitwise
identical against main.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Common (C++/CUDA)

  • New Tensor parameters kNVTEColumnwiseTransposed and kNVTEGroupedColumnwiseTransposed record
    whether a tensor's columnwise quantization is stored in transposed layout (currently only useful
    for FP8BS). Quantize, grouped quantize, Tensor::shape() and the FP8BS→MXFP8 conversion read the
    tensor's flag to avoid frequent mutex hits on te::cuda::current_device() when retrieving SM arch
    info cached per device ID (measured non-trivial E2E impact).
  • 1D quantize kernel: compile-time split between the transposed pass (unchanged source) and a new
    non-transposed columnwise pass with coalesced stores.
  • 2D quantize kernel: unchanged. Without the transposed layout, rowwise data and scales serve
    columnwise usage, so the kernel writes a single copy and columnwise output buffers are rejected.
  • Grouped 1D/2D quantize: non-transposed columnwise output, using the same row mapping for the
    columnwise pass; grouped dequantize follows the input's layout.
  • FP8BS→MXFP8 scale conversion APIs support columnwise output: 1D columnwise data that is not
    transposed, and 2D rowwise scales read tile-transposed. No new APIs.

PyTorch

  • The quantizer queries non-TN FP8 GEMM support once per process and records the layout in every
    tensor it creates (columnwise_transposed); storage, grouped tensors, bulk allocation and the C++
    bindings follow each tensor's flag. Modules work through set_usage/update_usage unchanged. 2D
    tensors allocate no columnwise buffers on non-TN GPUs.
  • GEMM binding chooses the layout per operand: untransposed operands run NN/NT natively, and operands
    stored transposed are relabeled to TN as before.
  • Distributed: the sequence-parallel all-gather no longer de-interleaves FP8BS 1D columnwise data
    when non-TN FP8 GEMM is supported; FSDP2 and distributed-optimizer paths skip the post-all-gather
    transpose for 2D weights.
  • GEMM wrappers: where non-TN FP8 GEMM is supported, FP8BS GEMMs use MXFP8's accumulation settings
    (fast accumulation in forward, split accumulation in backward), selected by the grad flag. Every
    backward GEMM that can take FP8BS operands now passes grad=True; the grouped-tensor GEMM accepts
    grad=True and rejects only the unsupported bias-gradient epilogue. Hopper and
    SM 12.x keep split accumulation to avoid numerical changes in existing FP8BS user code.
  • Pickling: FP8BS tensors are always pickled in the legacy transposed columnwise layout (2D tensors
    also carry the transposed columnwise buffers), so older Transformer Engine versions read new
    pickles correctly; unpickling converts to the local layout based on runtime support for non-TN FP8
    GEMM. CPU-only processes without CUDA keep the transposed layout. In-place copies between tensors
    of different layouts requantize the destination.
  • Comm+GEMM overlap (Userbuffers and cuBLASMp) with FP8BS operands raises NotImplementedError.
    Extending support deferred to future work.
  • Float8BlockScaling recipe docstring describes the per-arch accumulation behaviour (recipe fields
    unchanged); FP8BS docs describe the per-arch layout.

Bug Fixes

  • te.LayerNormMLP FC1 WGRAD with custom_gemm now passes the correct grad=True option and works
    correctly. Previously passing grad=False and thus running WGRAD as FPROP and failing.
  • Grouped GEMM tests (C++ GroupedGemmTest, PyTorch test_grouped_gemm_grouped_tensor{,_mxfp8})
    now skip on GPUs without grouped GEMM support (SM 12.x) instead of failing; the PyTorch tests use
    is_module_grouped_tensor_path_supported().

Performance (GB200, SM 10.0)

Quantize kernels (µs, BF16 → E4M3):

Shape Usage 1D main → PR 2D main → PR
16384² rowwise 181.8 → 127.7 (−29.7%) 129.5 → 129.5 (0.0%)
16384² columnwise 210.5 → 129.9 (−38.3%) 139.7 → 129.9 (−7.0%)
16384² both 307.7 → 191.4 (−37.8%) 182.0 → 130.0 (−28.6%)
40960×4096 rowwise 113.7 → 81.3 (−28.5%) 82.3 → 82.3 (0.0%)
40960×4096 columnwise 132.2 → 81.9 (−38.0%) 86.6 → 82.3 (−5.0%)
40960×4096 both 193.7 → 121.2 (−37.4%) 113.4 → 82.3 (−27.4%)
Grouped activations 16×2560, K=4096 rowwise 102.1 → 102.1 (0.0%) —
Grouped activations 16×2560, K=4096 columnwise 123.6 → 81.0 (−34.5%) —
Grouped activations 16×2560, K=4096 both 210.8 → 140.2 (−33.5%) —
Grouped weights 16 × 2048×4096 rowwise — 67.6 → 67.6 (0.0%)
Grouped weights 16 × 2048×4096 both — 93.5 → 67.6 (−27.8%)

NOTE: Per the FP8BS recipe, grouped activations are quantized with 1D blocks and grouped weights are
quantized with 2D blocks. Columnwise usage for grouped weights never set in E2E execution and
omitted from benchmarks.

Modules, fwd+bwd (µs per microbatch, BF16 params):

Module main → PR FP8BS/MXFP8
Linear 8192→8192, 8192 tokens 1459 → 1410 (−3.4%) 1.03x
LayerNormLinear 8192→28672 5048 → 4888 (−3.0%) 1.01x
LayerNormMLP 8192/28672 10561 → 10265 (−2.5%) 1.01x
GroupedLinear (grouped tensor), 16×2560 FC1 1380 → 1268 (−7.4%) 1.00x
GroupedLinear (grouped tensor), 64×512 FC1 3225 → 2763 (−14.5%) 0.91x
te.ops.GroupedLinear, 16×2560 FC1 1542 → 1421 (−8.1%) 0.97x
te.ops.GroupedLinear, 64×512 FC1 3786 → 3257 (−13.0%) 0.90x
GroupedLinear (split tensors), 16×2560 FC1 2460 → 2337 (−5.4%) 0.98x
GroupedLinear (split tensors), 64×512 FC1 7298 → 6844 (−5.8%) 0.96x

Known Issues

  • [Bug] FP8BS recipe options for fast accumulation are not propagated consistently or correctly.
    Different GEMM wrappers hardcode different choices for the use_split_accumulator option,
    ignoring the recipe entirely but also NOT staying consistent with each other. As a temporary
    work-around, this PR modifies all the forced choices to disable split accumulation in the FWD
    pass and enable it in DGRAD/WGRAD whenever non-TN FP8 GEMM is supported. This makes the
    MXFP8-emulated FP8BS GEMM execution on Blackwell numerically consistent with native-MXFP8 GEMM,
    but the recipe is still being ignored and the E2E logic needs to be reworked. TN-layout FP8 GEMM
    on Hopper and SM12x remain untouched to avoid altering numerical results for existing FP8BS users.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Grouped GEMM requires Hopper (SM90) or Blackwell (SM10x and SM110). The C++ grouped GEMM test and the PyTorch grouped-tensor GEMM tests checked only the lower bound, so they failed on SM12x instead of skipping. The PyTorch tests now use is_module_grouped_tensor_path_supported(), and the C++ test also checks the upper bound.

Signed-off-by: Alp Dener <adener@nvidia.com>
Add the kNVTEColumnwiseTransposed tensor parameter and the kNVTEGroupedColumnwiseTransposed grouped tensor parameter, with TensorWrapper and GroupedTensorWrapper accessors. They say whether FP8 block-scaling columnwise data is stored transposed, and default to true, the existing layout. Other formats ignore them.

Tensor::shape() rotates the shape of columnwise-only FP8 block-scaling tensors only when their columnwise data is stored transposed. Comm+GEMM overlap tensor chunks copy the flag like the other tensor parameters.

test::Tensor::set_columnwise_transposed() sets the flag and relabels the shape of the columnwise buffer.

Signed-off-by: Alp Dener <adener@nvidia.com>
FP8 block-scaling quantization takes the columnwise layout from the output tensor's kNVTEColumnwiseTransposed flag:

- 1D: columnwise data that is not stored transposed has the input shape and uses the existing COLUMNWISE_COMPACT option. The vector kernel selects its columnwise pass at compile time. The non-transposed pass maps lanes to consecutive columns and warps to tile rows, reduces the per-warp amaxes through shared memory, and writes data and scales with coalesced stores. Columnwise scales are the same in both layouts.
- 2D: without the transposed layout, rowwise data and scales serve columnwise usage, so columnwise output buffers are rejected.

The cast tests run both layouts and add an unaligned multi-tile shape.

Signed-off-by: Alp Dener <adener@nvidia.com>
Grouped FP8 block-scaling quantize and dequantize follow the grouped tensor's kNVTEGroupedColumnwiseTransposed flag:

- 1D quantize: the TMA kernel takes the layout as a compile-time parameter. Without the transposed layout, expert t's columnwise data is row-major (M_t, K) at the same element offset as its rowwise data, and the columnwise pass quantizes whole rows of the input tile with coalesced stores. Columnwise scales are unchanged.
- 2D quantize: without the transposed layout, columnwise buffers are rejected.
- Dequantize reads columnwise data in the input's layout.

The grouped quantize and dequantize tests run both layouts. The quantize test adds partial 128-column blocks and empty experts.

Signed-off-by: Alp Dener <adener@nvidia.com>
The FP8 block-scaling to MXFP8 scale conversions (single, multi-tensor and grouped) write MXFP8 columnwise scales when the output has columnwise data, for columnwise data that is not stored transposed:

- 1D: the columnwise scales are the 1D scales of the transposed data, so the existing 1D kernels convert them on swapped dims.
- 2D: the rowwise scales serve columnwise output through a tile-transposed read.

An output holds one direction. Multi-tensor launches batch by scaling mode and output direction. Rowwise output is unchanged.

The conversion tests add columnwise cases with an independent reference.

Signed-off-by: Alp Dener <adener@nvidia.com>
Where FP8 block-scaling GEMMs are emulated with MXFP8, an operand used columnwise (A with transa=N, B with transb=T) is converted per its columnwise layout. Transposed columnwise data is relabeled as rowwise data of the transposed tensor, which flips that operand's transpose flag. Other columnwise data converts to MXFP8 columnwise data with the logical shape and keeps the caller's layout. This applies at all five emulation sites. Tensor lists must share one layout.

Every tensor still has the transposed layout, so GEMMs still run TN.

Signed-off-by: Alp Dener <adener@nvidia.com>
general_gemm raises NotImplementedError for FP8 block-scaling operands with a comm+GEMM overlap (Userbuffers or cuBLASMp).

Signed-off-by: Alp Dener <adener@nvidia.com>
Where FP8 GEMM supports non-TN layouts, Float8BlockQuantizer stores 1D columnwise data with the logical shape, and 2D tensors have no columnwise buffers because rowwise data and scales serve columnwise usage. Hopper and SM12x keep the transposed layout.

- The C++ Float8BlockQuantizer queries non-TN support once per process and sets the layout flag on every tensor wrapper it creates or updates. Python FP8 block-scaling tensors, storage and grouped tensors take a columnwise_transposed constructor argument, and their C++ wrappers take the flag from it.
- Allocation (create_tensor, create_grouped_tensor, bulk allocation, inner_tensor_specs, make_grouped_tensor) follows the quantizer's layout. In-place quantization into a tensor of the other layout drops its columnwise buffers and adopts the quantizer's layout. Setting .data from another FP8 block-scaling tensor takes its layout.
- size(), view(), reshape, the shape property, dequantization, update_usage(), _create_columnwise() and get_data_tensors() follow the tensor's layout.
- mark_grouped_tensor marks the rowwise data and scales of 2D grouped tensors whose rowwise data serves columnwise usage. The debug scale-inverse stats read those rowwise scales.
- Return from the columnwise-only aten.view dispatch instead of falling through.

Tests cover columnwise-only dequantization for 1D and 2D, and the grouped and exact quantization tests follow the arch layout.

Signed-off-by: Alp Dener <adener@nvidia.com>
GroupedLinear with a single grouped BF16 weight drops its cached quantized workspace when columnwise usage is needed and the workspace has no columnwise data. 2D FP8 block-scaled workspaces whose rowwise data serves columnwise usage are now kept, so the weight is no longer re-quantized every microbatch.

Tests:
- test_single_grouped_weight_matches_discrete_grouped_tensor_path also covers non-square weights, 1D FP8 block-scaled weights and quantized primary weights.
- test_single_grouped_weight_workspace_cache covers 1D and 2D FP8 block scaling, checks that the cached workspace and its outputs are reused, and checks that a workspace cached in a no-grad forward serves a training step.

Signed-off-by: Alp Dener <adener@nvidia.com>
The 1D FP8 block-scaling all-gather gathers columnwise data that is not stored transposed along dim 0, without the de-interleave of transposed data.

The distributed numerics test adds FP8 block scaling to the quantized all-gather test.

Signed-off-by: Alp Dener <adener@nvidia.com>
Pickles hold columnwise data in the transposed layout, which older TE versions read. Tensors that store it untransposed write transposed columnwise data and scales (from the rowwise ones for 2D). Loading converts the columnwise data to the local layout: 1D data is transposed back, and 2D tensors with rowwise data drop it. 2D columnwise data without rowwise data, and pickles loaded without CUDA, keep the transposed layout.

aten.copy_ between FP8 block-scaling tensors of different layouts requantizes the destination from the dequantized source.

test_pickle_compatibility checks the pickled layout against an independent dequantization and loads payloads through every reconstructor.

Signed-off-by: Alp Dener <adener@nvidia.com>
Where FP8 GEMM supports non-TN layouts, FP8 block-scaling GEMMs run as MXFP8 GEMMs. general_gemm, general_grouped_gemm and general_grouped_gemm_for_grouped_tensor now use MXFP8's split-accumulator settings there: off for forward GEMMs and on for backward GEMMs. Hopper and SM12x keep split accumulation for all GEMMs.

The rule relies on grad, so backward GEMM calls that can take FP8 block-scaling operands pass grad=True: GroupedLinear (module and op) dgrad and wgrad, and LayerNormMLP FC1 and FC2 wgrad. LayerNormMLP drops its FP8 block-scaling special case that passed grad=False for FC2 wgrad. general_grouped_gemm_for_grouped_tensor accepts grad=True and raises only when a bias is also given.

custom_gemm picks the GEMM role from grad, so custom recipes ran LayerNormMLP FC1 wgrad as fprop and failed. The custom recipe test covers this with reference quantizers.

The FP8 block-scaling grouped GEMM test also compares every case against a dequantized reference.

Signed-off-by: Alp Dener <adener@nvidia.com>
Signed-off-by: Alp Dener <adener@nvidia.com>
@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes affect FP8 block-scaling quantization, GEMMs, tensor storage, and distributed use on supported GPUs.

Fix the device-specific layout choice before merging.

Findings

  1. P1 First GPU controls later layouts ▶

Summary

This PR removes unnecessary FP8 block-scaling transposes on GPUs that support non-TN GEMMs, while keeping the legacy layout on Hopper and SM 12.x.

  • FP8 block-scaled quantization skips extra transposes on supported GPUs.
  • FP8 block-scaled tensors carry their columnwise layout through storage and loading.
  • FP8 block-scaled GEMMs use their stored layout and direction-specific accumulation.
  • Distributed FP8 block-scaled gathers keep the columnwise data layout intact.

denera explicitly notes that recipe accumulation options remain ignored; fixed per-pass settings are an intentional temporary workaround. FP8 block-scaling communication overlap is also intentionally deferred.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Q[FP8 block quantizer] --> L{Columnwise layout}
  L -->|Transposed| T[Separate columnwise data]
  T --> TN[TN GEMM]
  L -->|Not transposed, 1D| C[Columnwise data in logical shape]
  L -->|Not transposed, 2D| R[Reuse rowwise data and scales]
  C --> M[Convert scales to MXFP8]
  R --> M
  M --> G[Keep requested GEMM layout]
Loading

Reviews (1) · Last reviewed commit: "[Docs] Describe per-architecture FP8 blo..." · Reviewed by Greptile

@ptrendx ptrendx self-assigned this Nov 13, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants