Skip to content

[Common] Check that grouped GEMM matrix pointers are 16-byte aligned - #3845

Merged
vthumbe1503 merged 6 commits into
NVIDIA:mainfrom
siddhant-rajhans:fix/grouped-gemm-unaligned-main-grad
Nov 19, 2026
Merged

vthumbe1503 merged 6 commits into
NVIDIA:mainfrom
siddhant-rajhans:fix/grouped-gemm-unaligned-main-grad

Conversation

@siddhant-rajhans

@siddhant-rajhans siddhant-rajhans commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

cuBLASLt grouped GEMM needs 16-byte aligned matrix pointers, but a view into a larger buffer need not be aligned. te.ops.GroupedLinear (with accumulate_into_main_grad) and te.pytorch.GroupedLinear (with fuse_wgrad_accumulation) write the grouped wgrad GEMM straight into each weight's main_grad, and Megatron-LM's gradient buffer packs parameters without padding when the distributed optimizer is off, so a 3-element MoE router bias leaves the expert main_grad views 12 bytes past a 16-byte boundary. The GEMM then faults inside the kernel with misaligned address, and the process aborts.

This PR checks pointer alignment on the host, so a misaligned operand raises a clear error before any work is launched. As discussed in review, aligning the buffer is the caller's job, so there is no staging copy: NVIDIA/Megatron-LM#9564 pads each parameter start in Megatron-LM's default gradient-buffer layout, as its distributed optimizer already does.

Related to #3838 (misaligned main_grad); the Megatron-LM PR fixes the issue's command. The issue's second part, input widths below 16 bytes, is not changed here: on main it already fails with the 16-byte leading-dimension check from #3785.

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

  • cublaslt_grouped_gemm.cu: host-side 16-byte pointer checks for grouped A, B, C and D and discrete A, C and D operands (empty buffers are skipped), next to the 16-byte row checks from [PyTorch] Fix grouped split validation for GroupedLinear/GroupedMLP ops #3785. Example: Grouped GEMM: D_list tensor 0 data pointer must be 16-byte aligned, but it is 12 bytes past a 16-byte boundary.
  • Tests:
    • test_grouped_linear.py: test_grouped_gemm_grouped_tensor_unaligned_out (list and grouped outputs, FP32 and BF16, accumulate on and off) and test_grouped_gemm_grouped_tensor_unaligned_input: the GEMM raises the error, leaves the output untouched and leaves no CUDA error behind.
    • test_grouped_mlp.py: TestGroupedLinearOp::test_unaligned_main_grad, a Megatron-like FP32 buffer with a 3-element prefix, for discrete and single grouped weights, accumulate and overwrite, immediate and delayed wgrad: backward raises the error and leaves main_grad untouched.

Validation

NGC PyTorch 26.10 container (CUDA 13.4, cuBLASLt 13.8.1), TE built from source with NVTE_CUDA_ARCHS=90, on c4031d0 with this PR's changes:

  • H200 NVL: pytest tests/pytorch/test_grouped_linear.py tests/pytorch/test_grouped_mlp.py -k unaligned: 18 passed (the 17 cases above and the existing test_forward_rejects_unaligned_token_count); pytest tests/pytorch/test_grouped_linear.py -k "test_grouped_gemm_grouped_tensor and not unaligned": 767 passed, 417 skipped.
  • The C++ check is unchanged since the first commit, so these earlier runs of it still apply (H100 80GB HBM3 and H200): pytest tests/pytorch/test_grouped_linear.py: 2253 passed, 958 skipped; tests/pytorch/test_grouped_mlp.py: 693 passed, 1969 skipped; tests/pytorch/test_fusible_ops.py -k "grouped or Grouped": 197 passed, 226 skipped; test_operator --gtest_filter='*GroupedGemm*': 150 passed, 138 skipped (MXFP8 and NVFP4 cases need Blackwell); the qa/L0_pytorch_unittest suites that touch linear layers, grouped GEMM, MoE, CUDA graphs and torch.compile: 20936 passed, 0 failed; JAX tests/jax/test_custom_call_compute.py -k grouped: 180 passed. Those runs also had the staging copy, which only ran for misaligned outputs.
  • Megatron-LM: the issue's command (1) with 3 experts and the container's released TE 2.20.2, on one H200 NVL: on Megatron-LM main it aborts with CUDA error: misaligned address; with Align parameter starts in the gradient buffer without the distributed optimizer Megatron-LM#9564 it trains (lm loss 6.999570, 6.959368, 6.939404).
  • Blackwell, from the reporter of te.ops GroupedLinear in BF16: grouped wgrad GEMM into a main_grad that is not 16-byte aligned fails with misaligned address; in_features 2 gives NaN gradients #3838 (comment): on a B300 (sm_103, NGC PyTorch 26.08, cuBLASLt 13.7.0) with TE built from this PR (2081360), the 3-expert command on Megatron-LM main stops with Grouped GEMM: D_list tensor 0 data pointer must be 16-byte aligned, but it is 12 bytes past a 16-byte boundary. instead of the misaligned-address crash it hits with TE main, and trains with Align parameter starts in the gradient buffer without the distributed optimizer Megatron-LM#9564.
  • pre-commit run on the changed files is clean.

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

…igned

cuBLASLt grouped GEMM needs 16-byte aligned matrix pointers. Megatron-LM
packs parameters into its gradient buffer without padding when the
distributed optimizer is off, so expert main_grad views can start off a
16-byte boundary, and the grouped wgrad GEMM then faults with a
misaligned address and aborts the process.

- general_grouped_gemm_for_grouped_tensor computes unquantized outputs
  that are not 16-byte aligned, discrete or grouped, through aligned
  buffers and copies or adds the result. te.ops.GroupedLinear and
  te.pytorch.GroupedLinear therefore accept such main_grad views, with
  discrete or single grouped weights and with delayed wgrad.
- The grouped GEMM checks matrix pointer alignment on the host and raises
  an error that names the operand instead of faulting on the device.

Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Nov 10, 2026
@siddhant-rajhans
siddhant-rajhans marked this pull request as ready for review November 10, 2026 06:05
@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

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

Summary

The PR adds host-side checks that reject grouped GEMM matrix pointers that are not 16-byte aligned before launching the GEMM.

  • Grouped GEMM reports misaligned matrix pointers before launching work.

No new actionable issues found. Both earlier findings concerned staging code that is no longer present.

Reviews (6) · Last reviewed commit: "GroupedMLP test not needed. Already test..." · Reviewed by Greptile

Run the same GEMM, with the same accumulate, alpha, beta and bias, on an
aligned copy of the output and copy the result back. Accumulation and bias
then follow the direct path exactly, instead of rounding the product to the
output dtype and adding bias before the old output; an explicit beta is
supported too. Tests compare misaligned outputs with aligned ones bit for bit,
including a grouped output with bias and accumulation.

Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
…is given

The GEMM reads the output as C when it accumulates or when an explicit beta
is passed, so the staging buffer must start as a copy of the output in both
cases. Adds a test that compares an explicit beta on a misaligned output
with an aligned one bit for bit.

Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
@siddhant-rajhans

Copy link
Copy Markdown
Contributor Author

@vthumbe1503, could you take a look when you have time, or start /te-ci pytorch? This fixes #3838: grouped GEMMs abort when an output is not 16-byte aligned, as Megatron's main_grad can be. The reporter confirmed the fix on their B300s (above), and it still merges cleanly with main.

@vthumbe1503 vthumbe1503 self-assigned this Nov 17, 2026
@vthumbe1503

vthumbe1503 commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the PR @siddhant-rajhans! I have added a comment on that issue. I think the right solution is to handle alignment of main_grad buffers in Megatron rather than doing an additional D2D copy here.

That being said the part of the code to check for output's 16 byte alignment is definitely useful. Thanks for adding that

@vthumbe1503 vthumbe1503 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.

We shouldnt be doing D2D copies introducing performance penalty when output addr is not 16 byte aligned. Megatron internally handling 16 byte aligned main_grad is cleaner and more performant solution.

…g it

Remove the aligned staging copy: it adds a hidden device-to-device copy for
every misaligned output, and the caller can align its buffers instead (in
Megatron-LM, by padding the gradient buffer that main_grad views point into).
A misaligned output now stops at the host-side pointer check with a clear
error. The tests check that error, that the output is left untouched and
that no CUDA error is left behind.

Signed-off-by: Siddhant Rajhans <51508945+siddhant-rajhans@users.noreply.github.com>
@siddhant-rajhans siddhant-rajhans changed the title [PyTorch][Common] Handle grouped GEMM outputs that are not 16-byte aligned [Common] Check that grouped GEMM matrix pointers are 16-byte aligned Nov 18, 2026
@siddhant-rajhans

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed 2081360: the staging copy is gone, and the PR now only adds the host-side pointer checks, so a misaligned operand raises an error such as Grouped GEMM: D_list tensor 0 data pointer must be 16-byte aligned, but it is 12 bytes past a 16-byte boundary. before anything is launched. The tests now check that error, that nothing is written and that no CUDA error is left behind (18 passed on H200).

The Megatron-LM side is NVIDIA/Megatron-LM#9564: it starts each parameter on a 64-element boundary in the default gradient-buffer layout, as the distributed optimizer already does. With it and the released TE 2.20.2, the 3-expert command from #3838 trains on an H200; on Megatron-LM main it still aborts with misaligned address.

I updated the title and description to match.

@vthumbe1503

Copy link
Copy Markdown
Collaborator

/te-ci pytorch

…r ggemm

Removed the test for unaligned main gradient handling in grouped GEMM.

Signed-off-by: vthumbe1503 <vthumbe@nvidia.com>
@vthumbe1503
vthumbe1503 merged commit db1aff6 into NVIDIA:main Nov 19, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants