Skip to content

[PyTorch] Enable FP8 block scaling for fusible ops - #3242

Merged
vthumbe1503 merged 2 commits into
NVIDIA:mainfrom
denera:pytorch/fp8bs-in-fusible-ops
Jul 23, 2026
Merged

vthumbe1503 merged 2 commits into
NVIDIA:mainfrom
denera:pytorch/fp8bs-in-fusible-ops

Conversation

@denera

@denera denera commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Description

Enables FP8 block scaling (FP8BS) for TE/PyTorch fusible-ops with coverage for both non-grouped and grouped linears, following the MXFP8 paths.

IMPORTANT: FP8BS support for comm+GEMM overlap is deferred to a future PR.

Changes

  • Drop the NotImplementedError for FP8BS, use the generic recipe-state path like MXFP8.
  • Add Float8BlockwiseQTensorStorage.view() so quantized norm outputs reshape without dequantizing (mirrors MXFP8TensorStorage.view()).
  • Route grouped FP8BS through the Hopper graph-safe path (cuBLAS 13.4+). Fuse dbias in the rowwise pass when dgrad is required.
  • Exercise FP8BS across the fusible-ops test suite with 128-divisible sizes.

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

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

@denera
denera requested a review from vthumbe1503 July 23, 2026 10:16
@denera denera self-assigned this Jul 23, 2026
@denera denera added the FP8 label Jul 23, 2026
Enables FP8 block scaling (FP8BS) in composable fusible-ops for both
non-grouped and grouped linears.

- Drop the blanket FP8BS NotImplementedError in ops/op.py; FP8BS now uses the
  generic recipe-state path like MXFP8.
- Add `Float8BlockwiseQTensorStorage.view()` so quantized norm outputs reshape
  without dequantizing (mirrors `MXFP8TensorStorage.view()`).
- Route grouped FP8BS through the Hopper graph-safe path (cuBLAS 13.4+). Fuse
  dbias in the rowwise pass when dgrad is required.
- Exercise FP8BS across the fusible-ops test suite with 128-divisible sizes.

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

greptile-apps Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR enables FP8 block scaling (FP8BS) for TE/PyTorch fusible ops by removing the NotImplementedError guard and routing the recipe through the existing generic recipe-state path (mirroring the MXFP8 approach). It adds Float8BlockwiseQTensorStorage.view() so quantized norm outputs can be reshaped without dequantizing, routes grouped FP8BS through the Hopper + cuBLAS 13.4+ graph-safe path, and fuses dbias into the rowwise quantization pass only when a dgrad is required.

  • op.py: Drops the NotImplementedError for float8_block_scaling() recipes so they use the generic RecipeState construction path.
  • float8_blockwise_tensor_storage.py: Adds view() that reshapes leading token dims while keeping the inner (quantization-aligned) dim fixed; correctly handles both rowwise and transposed columnwise storage layouts.
  • grouped_linear.py: Adds a new Float8BlockQuantizer branch in _is_graph_safe_path_supported, gated on Hopper (CC < 10.0) and cuBLAS ≥ 13.4; refactors fuse_bgrad to only fuse the bias gradient computation into bgrad_group_quantize when a rowwise (dgrad) pass actually runs.

Confidence Score: 5/5

The changes are well-scoped: the NotImplementedError removal simply enables an already-tested code path; the new view() mirrors the MXFP8 pattern with the correct transposed-columnwise adjustment; the grouped-linear routing adds a tightly gated branch with hardware and cuBLAS version guards.

All three logic changes are conservative additions that fall back to pre-existing paths when the new conditions are not met. The fuse_bgrad refactor correctly keeps bias-gradient computation on the unquantized dy_2d when the fused kernel cannot be used, matching the pre-existing BF16/FP16 fallback. The view() implementation handles both the rowwise (logical-shape) and transposed-columnwise storage correctly, with runtime guards preventing inner-dimension reshapes. No pre-existing behavior changes unless the hardware + cuBLAS version guards are satisfied.

No files require special attention; the new Float8BlockQuantizer branch in grouped_linear.py and the view() method in float8_blockwise_tensor_storage.py are the most logic-dense additions but both follow established patterns.

Important Files Changed

Filename Overview
transformer_engine/pytorch/ops/op.py Removes NotImplementedError for FP8BS; recipe falls through to the shared RecipeState construction path that already serves MXFP8.
transformer_engine/pytorch/tensor/storage/float8_blockwise_tensor_storage.py New view() correctly reshapes leading token dims for both rowwise (logical shape) and transposed columnwise storage, with appropriate runtime guards for 1D vs 2D block-scaling inner-dim immutability.
transformer_engine/pytorch/ops/basic/grouped_linear.py Adds Float8BlockQuantizer branch to _is_graph_safe_path_supported (Hopper + cuBLAS >= 13.4) and correctly gates bias-gradient fusion on whether a rowwise (dgrad) pass is required.
tests/pytorch/test_fusible_ops.py Adds fp8_block_scaling to the quantization list, 128-divisible parametrize entries, and sequence_length bump to 64 (256 total tokens, divisible by 128) to keep the sequential-modules test unsuppressed.
tests/pytorch/utils.py Adds fp8_block_scaling to the tolerance table so it uses the same (relaxed) tolerances as other FP8 recipes.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[FusibleOp forward/backward] --> B{recipe.float8_block_scaling?}
    B -- Yes --> C[RecipeState.create - generic path]
    B -- No --> C
    C --> D[GroupedLinear backward]

    D --> E{with_quantized_compute?}
    E -- No --> F[BF16/FP16: wrap dy_2d as GroupedTensor]
    E -- Yes --> G{grad_output_quantizer type?}

    G -- MXFP8Quantizer --> H[fuse_bgrad = True always]
    G -- Float8BlockQuantizer --> I{ctx.input_requires_grad?}
    I -- True --> J[fuse_bgrad = True]
    I -- False --> K[fuse_bgrad = False]

    H --> L[bgrad_group_quantize - fused dbias + quantize]
    J --> L
    K --> M[group_quantize only]
    F --> N[compute_grouped_dbias from dy_2d]
    M --> N

    L --> O[dgrad GEMM via grouped cuBLAS]
    M --> O
    N --> P[assemble final bias grads]
    O --> P

    Q[_is_graph_safe_path_supported] --> R{CC range 9.0-11.0?}
    R -- No --> S[return False - legacy path]
    R -- Yes --> T{Float8BlockQuantizer?}
    T -- Yes --> U{CC >= 10.0 Blackwell?}
    U -- Yes --> S
    U -- No --> V{cuBLAS >= 13.4?}
    V -- Yes --> W[return True - Hopper graph-safe path]
    V -- No --> S
Loading

Reviews (3): Last reviewed commit: "Merge branch 'main' into pytorch/fp8bs-i..." | Re-trigger Greptile

Comment on lines +343 to +348
if -1 in shape:
d_inferred = -math.prod(cur_shape) // math.prod(shape)
for i, d in enumerate(shape):
if d == -1:
shape[i] = d_inferred
break

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 -1 inference silently truncates for non-divisible shapes

d_inferred = -math.prod(cur_shape) // math.prod(shape) uses integer floor division. If the total element count isn't evenly divisible by the product of the explicit dims (e.g. cur_shape=(3, 128), shape=[2, -1, 128]), the inferred dim is silently truncated (d_inferred = 1 instead of fractional 1.5). The subsequent self._rowwise_data.view(*shape) will then raise a PyTorch error because 2 * 1 * 128 ≠ 3 * 128, but the inferred shape[i] value (1) is already wrong. This mirrors the MXFP8 implementation exactly — it is a pre-existing pattern — but worth documenting that -1 semantics only produce a meaningful result when the remaining dimensions divide evenly (same contract as torch.Tensor.view).

@denera
denera force-pushed the pytorch/fp8bs-in-fusible-ops branch from f517d02 to 639233e Compare July 23, 2026 10:23
@denera

denera commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@timmoon10 timmoon10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vthumbe1503
vthumbe1503 merged commit c0f0bdb into NVIDIA:main Jul 23, 2026
8 of 13 checks passed
KshitijLakhani pushed a commit that referenced this pull request Jul 23, 2026
[PyTorch] Enable FP8 block scaling across fusible ops

Enables FP8 block scaling (FP8BS) in composable fusible-ops for both
non-grouped and grouped linears.

- Drop the blanket FP8BS NotImplementedError in ops/op.py; FP8BS now uses the
  generic recipe-state path like MXFP8.
- Add `Float8BlockwiseQTensorStorage.view()` so quantized norm outputs reshape
  without dequantizing (mirrors `MXFP8TensorStorage.view()`).
- Route grouped FP8BS through the Hopper graph-safe path (cuBLAS 13.4+). Fuse
  dbias in the rowwise pass when dgrad is required.
- Exercise FP8BS across the fusible-ops test suite with 128-divisible sizes.

Signed-off-by: Alp Dener <adener@nvidia.com>
Co-authored-by: vthumbe1503 <vthumbe@nvidia.com>
YangFei1990 pushed a commit to YangFei1990/TransformerEngine that referenced this pull request Aug 7, 2026
[PyTorch] Enable FP8 block scaling across fusible ops

Enables FP8 block scaling (FP8BS) in composable fusible-ops for both
non-grouped and grouped linears.

- Drop the blanket FP8BS NotImplementedError in ops/op.py; FP8BS now uses the
  generic recipe-state path like MXFP8.
- Add `Float8BlockwiseQTensorStorage.view()` so quantized norm outputs reshape
  without dequantizing (mirrors `MXFP8TensorStorage.view()`).
- Route grouped FP8BS through the Hopper graph-safe path (cuBLAS 13.4+). Fuse
  dbias in the rowwise pass when dgrad is required.
- Exercise FP8BS across the fusible-ops test suite with 128-divisible sizes.

Signed-off-by: Alp Dener <adener@nvidia.com>
Co-authored-by: vthumbe1503 <vthumbe@nvidia.com>
(cherry picked from commit c0f0bdb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants