Skip to content

[Common] Compute decode scale first in FP8 blockwise scaling - #4034

Open
xiuhu17 wants to merge 1 commit into
NVIDIA:mainfrom
xiuhu17:fix/fp8-block-scaling-order
Open

xiuhu17 wants to merge 1 commit into
NVIDIA:mainfrom
xiuhu17:fix/fp8-block-scaling-order

Conversation

@xiuhu17

@xiuhu17 xiuhu17 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Description

FP8 blockwise scaling (Float8BlockScaling with 1x128 and 128x128 tiles) stores only the decoding scale (scale_inv) and passes it to block-scaled GEMMs, unlike per-tensor FP8 recipes (DelayedScaling, Float8CurrentScaling) which store an encoding scale and derive scale_inv = 1 / scale from it.

Previously, FP8 blockwise quantization kernels and distributed master-weight casting reused compute_scale_from_amax, computing scale = max_fp8 / amax first and scale_inv = 1 / scale second. This caused two classes of numerical discrepancies relative to standard blockwise FP8 producers/consumers (e.g., DeepGEMM, MXFP8 E8M0 conventions):

  1. Non-power-of-2 FP32 scales (force_pow_2_scales=False):
    • Double-reciprocal rounding drift (1.0f / (max_fp8 / amax) vs. amax / max_fp8) changes scale_inv by 1 FP32 ULP on ~33.2% of normal BF16 amax values (e.g., amax = 3.03125 for E4M3).
    • Quantizing with x * (max_fp8 / amax) instead of x * (1.0f / scale_inv) can round near-midpoint elements to a different FP8 bin than the stored scale_inv represents (e.g., x = amax = 0.51171875 for E4M3).
  2. Power-of-2 / UE8M0-compatible scales (force_pow_2_scales=True):
    • For tiny positive amax <= 2^-120 (with epsilon = 0), max_fp8 / amax overflows FP32 to +inf, triggering per-tensor value_for_inf fallback or yielding subnormal scale_inv = 2^-127 when max_fp8 / amax is clamped to FLT_MAX, whereas UE8M0's minimum normal unbiased exponent is 2^-126 = FLT_MIN.
    • At boundary values where max_fp8 / amax is non-exact before power-of-2 truncation, 1 / floor_pow2(max_fp8 / amax) can disagree with ceil_pow2(amax / max_fp8).

This PR switches FP8 blockwise scaling to compute the decoding scale (scale_inv) directly from amax first, clamp tiny positive scales to FLT_MIN = 2^-126, optionally round scale_inv up to a power of two via FTZ-safe IEEE-754 bit manipulation, and derive the quantization multiplier as scale = 1 / scale_inv. Per-tensor scaling paths and public C APIs remain unchanged.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes

  • Add compute_decoding_scaling_factor_blockwise and compute_blockwise_scale_and_scale_inv in transformer_engine/common/recipe/recipe_common.cuh (scale_inv = max(amax / max_fp8, FLT_MIN) with FTZ-safe power-of-2 round-up when force_pow_2_scales=true, returning 1.0f for zero/non-positive/non-finite blocks, and scale = __frcp_rn(scale_inv)).
  • Update 2D (128x128) and 1D (1x128) single-tensor and grouped FP8 blockwise quantization kernels (quantize_transpose_square_blockwise.cu, quantize_transpose_vector_blockwise.cu, group_quantize_fp8_blockwise.cuh) to use compute_blockwise_scale_and_scale_inv (also removing accidental FP64 1.0 / scale divisions in the 1D vector kernel).
  • Add nvte_multi_tensor_compute_scale_and_scale_inv_blockwise_cuda and PyTorch binding multi_tensor_compute_scale_and_scale_inv_blockwise for _cast_master_weights_to_fp8_blockwise_scaling while preserving nvte_multi_tensor_compute_scale_and_scale_inv_cuda for per-tensor recipes.
  • Update FP8 blockwise scaling documentation (docs/features/low_precision_training/fp8_blockwise_scaling/fp8_blockwise_scaling.rst), C++/Python reference helpers, and add targeted unit tests in tests/pytorch/test_float8_blockwise_scaling_exact.py and tests/pytorch/test_multi_tensor.py.

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

Created with Perplexity

FP8 blockwise scaling (`1x128` and `128x128`) stores only the decoding
scale (`scale_inv`) and feeds it to block-scaled GEMMs, whereas per-tensor
FP8 recipes derive `scale_inv` from a stored encoding `scale`. Computing
`scale = max_fp8 / amax` first and taking `scale_inv = 1 / scale` second
caused:

- Double-reciprocal FP32 rounding drift (`1 / (max_fp8 / amax)` vs
  `amax / max_fp8`) on ~33.2% of normal BF16 block amaxes when
  `force_pow_2_scales=false`, as well as quantized element mismatches
  when `x * (1 / scale_inv)` differs from `x * scale`.
- Overflow of `max_fp8 / amax` to `+inf` for tiny positive `amax` values
  (`amax <= 2^-120`), corrupting `scale_inv` via per-tensor `value_for_inf`
  fallback or producing subnormal `2^-127` scales incompatible with UE8M0
  (`E8M0` minimum normal exponent is `2^-126 = FLT_MIN`) when
  `force_pow_2_scales=true`, plus boundary direction mismatches where
  `round_down_pow2(max_fp8 / amax)` differs from `ceil_pow2(amax / max_fp8)`.

Introduce `compute_decoding_scaling_factor_blockwise` and
`compute_blockwise_scale_and_scale_inv` in `recipe_common.cuh` to compute
`scale_inv = max(amax / max_fp8, FLT_MIN)` first (rounding up to a power
of two via FTZ-safe IEEE-754 bit operations when `force_pow_2_scales` is
enabled, and returning `1.0f` for zero/non-positive/non-finite blocks) and
derive the encoding multiplier as `scale = 1 / scale_inv`. Wire all single
and grouped `1x128`/`128x128` blockwise quantization kernels and distributed
master-weight casting (`multi_tensor_compute_scale_and_scale_inv_blockwise`)
to this path while keeping existing per-tensor APIs unchanged.

Signed-off-by: Zhihao Wang <[email protected]>
Co-authored-by: Perplexity <[email protected]>
@xiuhu17
xiuhu17 marked this pull request as ready for review December 5, 2026 02:58
@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The changes affect blockwise FP8 quantization and distributed master-weight casting.

The PR appears safe to merge, with a non-blocking improvement needed in the targeted regression test.

Findings

  1. P2 FP8 values go unchecked ▶

Summary

This PR makes FP8 blockwise quantization compute scale_inv first, then derive the multiplier used to produce FP8 values.

  • Blockwise FP8 tiles derive their decode scale before encoding values.
  • Distributed weight casting uses the blockwise decode-scale calculation.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Block amax] --> B[Apply epsilon]
  B --> C[Compute scale_inv]
  C --> D[Floor at minimum normal FP32]
  D --> E[Optionally round up to a power of two]
  E --> F[Store scale_inv]
  E --> G[Compute reciprocal]
  G --> H[Multiply input and cast to FP8]
Loading

Reviews (1) · Last reviewed commit: "[Common] Compute decode scale first in F..." · Reviewed by Greptile

Comment on lines +551 to +553
assert expected_scale_inv.item() != encode_first_scale_inv.item()
assert q._rowwise_scale_inv.flatten()[0].item() == expected_scale_inv.item()
assert q._columnwise_scale_inv.flatten()[0].item() == expected_scale_inv.item()

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 FP8 values go unchecked

The new regression test checks only scale_inv, not the FP8 values produced with it. It would still pass if a kernel stored the new decoding scale but kept the old quantization multiplier—the second discrepancy this PR aims to fix. Add an interior value near an FP8 rounding boundary and compare both _rowwise_data and _columnwise_data against the decode-first reference.

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.

1 participant