Repository navigation
[Common] Check that grouped GEMM matrix pointers are 16-byte aligned - #3845
vthumbe1503 merged 6 commits into
Conversation
…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>
[Medium impact] The PR appears safe to merge; no new blocking issues were found. SummaryThe PR adds host-side checks that reject grouped GEMM matrix pointers that are not 16-byte aligned before launching the GEMM.
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>
|
@vthumbe1503, could you take a look when you have time, or start |
|
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
left a comment
There was a problem hiding this comment.
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>
|
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 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 I updated the title and description to match. |
|
/te-ci pytorch |
…r ggemm Removed the test for unaligned main gradient handling in grouped GEMM. Signed-off-by: vthumbe1503 <vthumbe@nvidia.com>
Description
cuBLASLt grouped GEMM needs 16-byte aligned matrix pointers, but a view into a larger buffer need not be aligned.
te.ops.GroupedLinear(withaccumulate_into_main_grad) andte.pytorch.GroupedLinear(withfuse_wgrad_accumulation) write the grouped wgrad GEMM straight into each weight'smain_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 expertmain_gradviews 12 bytes past a 16-byte boundary. The GEMM then faults inside the kernel withmisaligned 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
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.test_grouped_linear.py:test_grouped_gemm_grouped_tensor_unaligned_out(list and grouped outputs, FP32 and BF16, accumulate on and off) andtest_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 leavesmain_graduntouched.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:pytest tests/pytorch/test_grouped_linear.py tests/pytorch/test_grouped_mlp.py -k unaligned: 18 passed (the 17 cases above and the existingtest_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.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); theqa/L0_pytorch_unittestsuites that touch linear layers, grouped GEMM, MoE, CUDA graphs andtorch.compile: 20936 passed, 0 failed; JAXtests/jax/test_custom_call_compute.py -k grouped: 180 passed. Those runs also had the staging copy, which only ran for misaligned outputs.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).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 runon the changed files is clean.Checklist: