Repository navigation
Conversation
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]>
Confidence Score: 4/5
[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
SummaryThis PR makes FP8 blockwise quantization compute
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]
Reviews (1) · Last reviewed commit: "[Common] Compute decode scale first in F..." · Reviewed by Greptile |
| 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() |
There was a problem hiding this comment.
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.
Description
FP8 blockwise scaling (
Float8BlockScalingwith1x128and128x128tiles) stores only the decoding scale (scale_inv) and passes it to block-scaled GEMMs, unlike per-tensor FP8 recipes (DelayedScaling,Float8CurrentScaling) which store an encodingscaleand derivescale_inv = 1 / scalefrom it.Previously, FP8 blockwise quantization kernels and distributed master-weight casting reused
compute_scale_from_amax, computingscale = max_fp8 / amaxfirst andscale_inv = 1 / scalesecond. This caused two classes of numerical discrepancies relative to standard blockwise FP8 producers/consumers (e.g., DeepGEMM, MXFP8E8M0conventions):force_pow_2_scales=False):1.0f / (max_fp8 / amax)vs.amax / max_fp8) changesscale_invby 1 FP32 ULP on ~33.2% of normal BF16amaxvalues (e.g.,amax = 3.03125for E4M3).x * (max_fp8 / amax)instead ofx * (1.0f / scale_inv)can round near-midpoint elements to a different FP8 bin than the storedscale_invrepresents (e.g.,x = amax = 0.51171875for E4M3).force_pow_2_scales=True):amax <= 2^-120(withepsilon = 0),max_fp8 / amaxoverflows FP32 to+inf, triggering per-tensorvalue_for_inffallback or yielding subnormalscale_inv = 2^-127whenmax_fp8 / amaxis clamped toFLT_MAX, whereas UE8M0's minimum normal unbiased exponent is2^-126 = FLT_MIN.max_fp8 / amaxis non-exact before power-of-2 truncation,1 / floor_pow2(max_fp8 / amax)can disagree withceil_pow2(amax / max_fp8).This PR switches FP8 blockwise scaling to compute the decoding scale (
scale_inv) directly fromamaxfirst, clamp tiny positive scales toFLT_MIN = 2^-126, optionally roundscale_invup to a power of two via FTZ-safe IEEE-754 bit manipulation, and derive the quantization multiplier asscale = 1 / scale_inv. Per-tensor scaling paths and public C APIs remain unchanged.Type of change
Changes
compute_decoding_scaling_factor_blockwiseandcompute_blockwise_scale_and_scale_invintransformer_engine/common/recipe/recipe_common.cuh(scale_inv = max(amax / max_fp8, FLT_MIN)with FTZ-safe power-of-2 round-up whenforce_pow_2_scales=true, returning1.0ffor zero/non-positive/non-finite blocks, andscale = __frcp_rn(scale_inv)).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 usecompute_blockwise_scale_and_scale_inv(also removing accidental FP641.0 / scaledivisions in the 1D vector kernel).nvte_multi_tensor_compute_scale_and_scale_inv_blockwise_cudaand PyTorch bindingmulti_tensor_compute_scale_and_scale_inv_blockwisefor_cast_master_weights_to_fp8_blockwise_scalingwhile preservingnvte_multi_tensor_compute_scale_and_scale_inv_cudafor per-tensor recipes.docs/features/low_precision_training/fp8_blockwise_scaling/fp8_blockwise_scaling.rst), C++/Python reference helpers, and add targeted unit tests intests/pytorch/test_float8_blockwise_scaling_exact.pyandtests/pytorch/test_multi_tensor.py.Checklist:
Created with Perplexity