[ROCm] Fix int32 overflow in the custom multi-tensor L2 norm #798
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.48) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 552 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.378
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #798"
general
weight: 0.35
Micky774
Description
The ROCm custom multi-tensor L2 norm (
multi_tensor_l2norm/multi_tensor_unscale_l2normwithoutper_tensor, used by Megatron's gradient-norm clipping) faults with a GPU memory access error when one tensor in the list has 2^31 or more elements.CustomL2NormCachestored each tensor's element count asint, andcustom_multi_tensor_l2norm_kernelcomputed the chunk offset inint. For a tensor of 2^31 elements, the truncated count is negative, so the cache'sblock_to_tensormap gets fewer entries thantotal_chunks(whichl2norm.cppcomputes from the 64-bitnumel()), and the chunk offsets of all later tensors shift. The kernel still launchestotal_chunksblocks: the extra blocks readblock_to_tensorentries copied from past the end of the host vector, and blocks of later tensors index past their tensor. Both read unmapped memory:This was hit in training: a Primus-Turbo grouped-GEMM expert layer keeps the local experts' fc1 weights in one stacked parameter, which at 64 local experts x 8192 x 4096 is exactly 2^31 elements. The first optimizer step faulted in this kernel during gradient clipping (on gfx1250, and reproduced on MI300X). The same truncation also let
shapes_valid()treat a tensor of 2^32 + k elements as matching a cached tensor of k elements.The custom Adam path (
CustomMultiTensorCache,custom_adam_kernel) already usesint64_tsizes and anint64_telement offset; this PR makes the L2 norm path do the same.Type of change
The
nvte_multi_tensor_l2norm_cuda_custom/nvte_multi_tensor_unscale_l2norm_cuda_customsignatures changeconst int sizestoconst int64_t sizes. Both are HIP-only, marked experimental, and only called frompytorch/csrc/extensions/multi_tensor/l2norm.cpp.Changes
CustomL2NormCache(multi_tensor_cache.h): store sizes asint64_ton host and device, compute chunk counts from the 64-bitnumel(), and check that the chunk map covers exactlytotal_chunksbefore uploading it.custom_multi_tensor_l2norm_kernel(l2norm.cu): takeint64_tsizes and compute the element offset and remaining count inint64_t, ascustom_adam_kerneldoes. The per-chunk vectorized/scalar path selection is unchanged.multi_tensor.h,l2norm.cpp: passint64_tsizes through the two*_cuda_customAPIs.tests/pytorch/test_multi_tensor.py: addtest_multi_tensor_l2norm_tensor_over_2_31_elements(a tensor of 2^31 + 3 bf16 elements between two small tensors, formulti_tensor_l2normandmulti_tensor_unscale_l2norm; skipped with less than ~5 GiB of free GPU memory).Testing
On MI300X (gfx942), TE
dbcfb92with this patch (it applies cleanly there and ondev), built forgfx942;gfx1250:| | before | after |
|---|---|---|
|
multi_tensor_l2normon tensors of 555, 2^31 + 3, 65537 elements (bf16) | memory access fault | correct (rel. err 2.0e-8), also with unscale || same total split into tensors < 2^31 | correct | correct |
|
pytest tests/pytorch/test_multi_tensor.py -k l2norm| | 578 passed (incl. the 2 new tests) |The only compiler warnings in
l2norm.hipare the existing "loop not unrolled" ones from#pragma unrollon runtime-bounded loops inreduce_block_into_lanes, reported at every kernel that inlines it.Checklist: