Skip to content

[Common] Pipeline the dbias reduction of the MXFP8 and FP8 quantize - #3852

Merged
tdophung merged 5 commits into
NVIDIA:mainfrom
tdophung:teddy/mxfp8-reduce-dbias
Nov 23, 2026
Merged

tdophung merged 5 commits into
NVIDIA:mainfrom
tdophung:teddy/mxfp8-reduce-dbias

Conversation

@tdophung

@tdophung tdophung commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Description

common::reduce_dbias adds up the FP32 dbias workspace (one partial row per CTA of the quantize kernel) into the
dbias vector. It serves:

  • the MXFP8 quantize with fused dbias, on both the CUDA and the CuTeDSL backends (quantize_mxfp8.cuh,
    quantize_mxfp8_cutedsl.cuh);
  • the FP8 tensor-scaling quantize with fused dbias (quantize_fp8.cuh).

The grouped reduction (grouped_reduce_dbias) and the cast-transpose dbias reductions are separate kernels and do not
change.

The old kernel ran 256 threads per CTA with four columns each (two for FP32 output). A 7168-column BF16 workspace
therefore launched 7 CTAs, and each thread waited on one load per row. This PR uses one thread per column, 64
threads per CTA:

  • On sm_80 and newer, each thread copies its column into shared memory with cp.async, 32 rows per stage and 4
    stages deep, and adds each stage once it has landed.
  • Below sm_80, each thread loads its column directly.

Each column is still added in row order from 0 and rounded once, so dbias is bit-identical to before. The
cols % nvec check is dropped: any column count works.

The change is on by default for every caller above; there is no option. It was split out of #3761.

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

  • transformer_engine/common/cast/core/grouped_layout.cuh: the new reduce_dbias_kernel and its launch.
  • tests/cpp/operator/test_cast_dbias.cu: CastDBiasOrderTestSuite checks dbias bit for bit against the row-order sum
    of the 128-row tile sums.
    • One nonzero row per tile, so each tile sum is exact. FP32, BF16 and FP16 input.
    • Magnitudes span 2^-30..2^31 with mixed signs, so for FP32 any other order or grouping of the adds changes some
      sums; BF16 and FP16 mostly check the rounding. FP16 input spans 2^-24..2^13, so most of its sums stay finite and
      some overflow.
    • 1-1000 tiles (up to 128K rows), around the reduction's 32-row stages and 4-stage pipeline; 16-1040 columns, around
      its 64-thread CTA.

Performance

One GB300 GPU, CuTeDSL backend on, this PR against its base main b9e8e57, on the same node:

Op (BF16 in, MXFP8 E4M3 out) Shape main (us) this PR (us) Speedup (round 1 / round 2) reduce_dbias grid, main -> PR Occupancy, theoretical / achieved, main -> PR Kernels, both arms
cast + dbias (bgrad_quantize) 4096x7168 28.83 25.15 1.15x (1.157 / 1.136) 7 x 256 -> 112 x 64 100% / 12.7% -> 18.75% / 3.3% CuTeDSL general kernel + reduce_dbias
cast + dbias (bgrad_quantize) 4096x36864 100.58 95.25 1.06x (1.054 / 1.058) 36 x 256 -> 576 x 64 100% / 12.3% -> 18.75% / 11.4% same
  • Occupancy (ncu, one reduce_dbias launch): the reducer sees a 32 x N workspace at both shapes. The 32 KB of
    shared memory per CTA caps the PR at 6 CTAs per SM, but even 576 CTAs on 152 SMs is 0.63 waves, so the cap only
    binds past about 58k columns. Main's 7 / 36 CTAs leave most SMs idle, which is where the speedup comes from.
  • Bits: dbias, the FP8 data and the scales are bit-identical to main's (SHA-256). In both arms they are bitwise
    equal to the CUDA backend's.
  • Not timed here: the CUDA backend and the FP8 tensor-scaling quantize call the same reduce_dbias.

Measurement details:

  • Hardware: one NVIDIA GB300, exclusive node, driver 580.173.10.
  • Software:
    • Container nvcr.io/nvidia/pytorch:26.10-py3, build 477939455, index digest
      sha256:6d347d649d5dd108b4652f1b9cea6a3c859692ac408617a9108616da40e3da1f.
    • PyTorch 2.15.0a0+875d815, CUDA 13.4.3, cuDNN 9.27.0.42, NCCL 2.32.3.
    • nvidia-cutlass-dsl 4.8.0, apache-tvm-ffi 0.1.14.post1, cuda-core 1.2.2.
    • TE 2.22.0.dev0 built from source at each arm's commit, with
      NVTE_FRAMEWORK=pytorch NVTE_WITH_CUTEDSL=1 NVTE_CUDA_ARCHS=100 (nvcc 13.4).
  • Backend and environment: NVTE_ENABLE_CUTEDSL_BACKEND=1 NVTE_ACTIVATION_TABLE=1 in both arms; the tables do
    not apply to a plain cast, and main ignores the variable. The kernels each arm launched were checked with
    torch.profiler.
  • Call: one tex.bgrad_quantize call with an MXFP8Quantizer (MXFP8 block scaling): BF16 input from randn,
    E4M3 output, rowwise and columnwise, GEMM-swizzled scales.
  • Occupancy: Nsight Compute 2026.3.1, LaunchStats and Occupancy sections, --clock-control none, on the third
    call (after the CuTeDSL compile).
  • Timing:
    • The window holds the quantize kernel and reduce_dbias, between two CUDA events.
    • Before each call, the L2 is flushed by reading a buffer twice its size, and a GPU sleep longer than the host
      launch is enqueued. Launch overhead therefore stays outside the window.
    • 20 warm-up calls run first; the first one compiles the CuTeDSL kernel.
    • Each run reports the median of 500 calls.
  • Order: main, PR, PR, main on one GPU. The table gives the mean of each arm's two medians, and the speedup per
    round.

Testing

The table ran on one GB300 (SM 10.3), with the C++ tests built for sm_103a.

Suite CuTeDSL backend off CuTeDSL backend on, NVTE_REPORT_IF_CUTEDSL_BACKEND_NOT_CHOSEN=ERROR
test_operator FP8 dbias, *CastDBias*:*CastDBiasDGelu* 168 passed 168 passed
test_operator MXFP8 cast and dbias, *FusedCastMXFP8*:*SwizzledScalesFusedCastMXFP8* 688 passed, 488 skipped 688 passed, 488 skipped

The new CastDBiasOrderTestSuite ran on one GB200 (SM 10.0), with the C++ tests built for sm_100a:

  • test_operator --gtest_filter='*CastDBias*': 233 passed (CastDBiasOrder 65, CastDBias 84, CastDBiasDGelu 84).

  • compute-sanitizer racecheck and synccheck on CastDBiasOrder 65X80 and 512X1040: 0 hazards, 0 errors.

  • With the reduction's adds reversed, 55 of the 65 cases fail, all in FP32: every case with 31 or more tiles. With 1
    or 2 tiles there is no other order.

  • Lint: cpplint 1.6.0 and clang-format 18.1.6 are clean.

  • Not run here: the minimum-version builds (GCC 11, sm_70, where the plain-loop path compiles), the MXFP8 suites
    on SM 10.0, and JAX.

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

common::reduce_dbias sums the FP32 dbias workspace column by column for the
MXFP8 quantize (CUDA and CuTeDSL backends) and the FP8 tensor-scaling quantize.
It ran 256 threads per CTA with four columns each, so a 7168-column workspace
got 7 CTAs, and each thread waited on one load per row.

Use one thread per column, 64 per CTA. On sm_80 and newer, copy each column
into shared memory with cp.async, 32 rows per stage and 4 stages deep, and add
each stage once it has landed; below sm_80, load the column directly. Each
column is still added in row order from 0 and rounded once, so dbias does not
change. Any column count is now accepted.

Add test_reduce_dbias, which checks the reduction bit for bit against that sum
on the host for FP32, BF16 and FP16 output.

Signed-off-by: Teddy Do <tdophung@nvidia.com>
@lxcxjxhx

Copy link
Copy Markdown
Contributor

I went through the pipeline maths and the test matrix; the ordering argument holds up. What I could not find in the PR is the resource side, and one coverage gap.

The pipeline is correct as written. With STAGES = 4 the prologue issues stages 0–2, and at iteration k the issue(k + 3) goes into slot (k+3) % 4 == (k-1) % 4, i.e. the slot of the stage consumed in the previous iteration, so nothing is overwritten while still in flight; cp.async.wait_group 3 then guarantees stage k landed before it is read. The empty-group commit for k >= num_stages keeps the group count aligned past the end, which is the part that usually goes wrong in this pattern.

Resource cost — worth a number in the description. stages[4][32][64] is 4 × 32 × 64 × 4 B = 32 KB of static shared memory per CTA, and the CTA is 64 threads. On sm_80 with the 48 KB default carveout that is one resident CTA per SM, i.e. two warps. The kernel it replaces used 256 threads and no shared memory, so the comparison is "one CTA/SM with 3 stages in flight" against "four CTAs/SM with a straight ld.global loop". I believe the pipelining wins (that is what the PR title claims), but the description doesn't say for which shapes and by how much — the benchmark you used (rows up to the 128K-row workspace in the test comment? the 7168/7170-column cases?) would make the claim checkable instead of plausible. If occupancy was measured and is not a concern, one sentence saying so is enough.

Coverage gaps, both small:

  1. rows = 0 is not in the matrix (Values(1, 2, 31, …)). The code path is: num_stages = 0, prologue issues three empty groups, the main loop doesn't run, acc = 0, one store — which looks right, and cols = 0 already launches a zero-block grid on main (DIVUP(0, …) == 0), so it is pre-existing either way. A rows = 0 case would pin the "empty workspace still writes zeros" behaviour, since cudaMemset(0xA5) in the fixture makes a missed store visible.
  2. The #else branch (pre-sm_80, no cp.async) is only compiled for __CUDA_ARCH__ < 800. If CI is sm_80+, that path is never executed anywhere — it is four lines and obviously right, but nothing would notice if it stopped matching the pipelined order. A comment saying "untested below sm_80" (or a CI job) would keep the next person honest.

One note on the test itself: comparing with memcmp after copying both sides through the same OType vector is exactly right for "bit for bit in row order", and the 2^-30..2^30 magnitude spread is what makes reassociation detectable — without it the test would pass for any order. Nothing to change there.

@tdophung

tdophung commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

Resource cost — worth a number in the description. stages[4][32][64] is 4 × 32 × 64 × 4 B = 32 KB of static shared memory per CTA, and the CTA is 64 threads. On sm_80 with the 48 KB default carveout that is one resident CTA per SM, i.e. two warps. The kernel it replaces used 256 threads and no shared memory, so the comparison is "one CTA/SM with 3 stages in flight" against "four CTAs/SM with a straight ld.global loop". I believe the pipelining wins (that is what the PR title claims), but the description doesn't say for which shapes and by how much — the benchmark you used (rows up to the 128K-row workspace in the test comment? the 7168/7170-column cases?) would make the claim checkable instead of plausible. If occupancy was measured and is not a concern, one sentence saying so is enough.

Coverage gaps, both small:

  1. rows = 0 is not in the matrix (Values(1, 2, 31, …)). The code path is: num_stages = 0, prologue issues three empty groups, the main loop doesn't run, acc = 0, one store — which looks right, and cols = 0 already launches a zero-block grid on main (DIVUP(0, …) == 0), so it is pre-existing either way. A rows = 0 case would pin the "empty workspace still writes zeros" behaviour, since cudaMemset(0xA5) in the fixture makes a missed store visible.
  2. The #else branch (pre-sm_80, no cp.async) is only compiled for __CUDA_ARCH__ < 800. If CI is sm_80+, that path is never executed anywhere — it is four lines and obviously right, but nothing would notice if it stopped matching the pipelined order. A comment saying "untested below sm_80" (or a CI job) would keep the next person honest.

One note on the test itself: comparing with memcmp after copying both sides through the same OType vector is exactly right for "bit for bit in row order", and the 2^-30..2^30 magnitude spread is what makes reassociation detectable — without it the test would pass for any order. Nothing to change there.

@lxcxjxhx
On the share mem, 32KB per CTA is correct, but 48KB is the per-block limit for static shared mem not what the SM has in total. So even with sm_80 (Ampere), A1-- should have up to 164KB per SM, and GH100/GB100 should have up to 228KB/sm, so there's plenty of these CTAs will fit on 1 SM. Occupancy shouldnt be the limiter here as the grid is one 64-thread CTA per 64 columns, so a 7168-column workspace launches 112 CTAs < num SM on GPU.

I can post the occupancy on the description if needed. But the above reasonging should explain why we should see where the speed up comes from.

Also, about the shapes, the shapes and numbers are in the Perforrmance section in the desc, comparing between main (before merge to main that just happened) with this PR.

About the row=0 case, I dont think this is a realistic case to test, as nothing calls. reduce_dibaisd with an empty workspace in practice. So it is quite unrealistic of a test. Same with sm_80, yes, we do not test it in CI because it is again an unreallistic case of users and we have limmited resource on our CI

@lxcxjxhx

Copy link
Copy Markdown
Contributor

You are right on both counts and I was wrong on the shared-memory one.

The 48 KB I quoted is the per-block static shared memory limit, not what an SM has. With 164 KB on Ampere and 228 KB on Hopper, 32 KB per CTA is six CTAs — and as you say, the grid is one 64-thread CTA per 64 columns, so a 7168-column workspace is 112 CTAs against 152 SMs, below one wave anyway. The occupancy number in your table (18.75% theoretical, 3.3% achieved) already said this; I read the "100% / 12.7%" column as an SM total rather than an occupancy figure and drew the wrong conclusion from it.

Same for the shapes: the Performance table has both shapes with per-round medians, the round-1/round-2 split, and the SHA-256 check that dbias and the FP8 data are bit-identical. My sentence "the description doesn't say for which shapes and by how much" was wrong — it does, and more completely than most PRs.

On the two coverage points, your reasoning holds and I withdraw both: an empty workspace is not a call site that exists, and pre-sm_80 is a CI resource decision, not a test gap this PR should carry. A code comment saying the #else arm is not exercised in CI would be cheap, but it is your call and not a blocker from me.

Nothing left from my side.

@tdophung
tdophung marked this pull request as ready for review November 19, 2026 01:18
@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

The PR pipelines reduce_dbias loads while keeping each column’s additions in row order.

  • Each dbias column gets its own thread and staged loads.

Reviews (3) · Last reviewed commit: "Move the dbias reduction test into the c..." · Reviewed by Greptile

Comment thread tests/cpp/util/test_reduce_dbias.cu Resolved
Signed-off-by: Teddy Do <tdophung@nvidia.com>
@tdophung

Copy link
Copy Markdown
Collaborator Author

/te-ci

@tdophung
tdophung requested a review from kainzhong November 19, 2026 05:35
@kainzhong kainzhong self-assigned this Nov 19, 2026
Signed-off-by: Teddy Do <tdophung@nvidia.com>
}
stg_vec.store_to(thread_out_base);
#else
for (size_t r = 0; r < rows; ++r) acc += in[r * cols];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Emmmm so do we just lose the vectorized memory load and store with < sm80? (I guess that is OK since they are old anyway?)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yeah basically, the case for < sm_80 is just so that sm_70/75 can still compile.

@kainzhong kainzhong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I feel the new test is a bit over engineering (we have existing numeric tests and this new test feels like a specific case LLM constructs to make it bit-exact) but it probably doesn't hurt anyway.

@tdophung
tdophung merged commit 123eafd into NVIDIA:main Nov 23, 2026
13 checks passed
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.

3 participants