← Back to Pull requests

[ROCm] Fix int32 overflow in the custom multi-tensor L2 norm #798

Closed
unknown closed this pull request without merging on Nov 11, 2026 18:30 UTC
View on GitHub
unknown unknown commented on Nov 09, 2026 01:21 UTC
Author

Description

The ROCm custom multi-tensor L2 norm (multi_tensor_l2norm / multi_tensor_unscale_l2norm without per_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.

CustomL2NormCache stored each tensor's element count as int, and custom_multi_tensor_l2norm_kernel computed the chunk offset in int. For a tensor of 2^31 elements, the truncated count is negative, so the cache's block_to_tensor map gets fewer entries than total_chunks (which l2norm.cpp computes from the 64-bit numel()), and the chunk offsets of all later tensors shift. The kernel still launches total_chunks blocks: the extra blocks read block_to_tensor entries copied from past the end of the host vector, and blocks of later tensors index past their tensor. Both read unmapped memory:

Memory access fault by GPU node-9 ... Reason: Unknown.
kernel: void transformer_engine::multi_tensor_l2norm::custom_multi_tensor_l2norm_kernel<hip_bfloat16, false>(int, int volatile*, long const*, int const*, int const*, int const*, int, float const*, float*)

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 uses int64_t sizes and an int64_t element offset; this PR makes the L2 norm path do the same.

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
  • The nvte_multi_tensor_l2norm_cuda_custom / nvte_multi_tensor_unscale_l2norm_cuda_custom signatures change const int sizes to const int64_t sizes. Both are HIP-only, marked experimental, and only called from pytorch/csrc/extensions/multi_tensor/l2norm.cpp.

    Changes

  • CustomL2NormCache (multi_tensor_cache.h): store sizes as int64_t on host and device, compute chunk counts from the 64-bit numel(), and check that the chunk map covers exactly total_chunks before uploading it.

  • custom_multi_tensor_l2norm_kernel (l2norm.cu): take int64_t sizes and compute the element offset and remaining count in int64_t, as custom_adam_kernel does. The per-chunk vectorized/scalar path selection is unchanged.

  • multi_tensor.h, l2norm.cpp: pass int64_t sizes through the two *_cuda_custom APIs.

  • tests/pytorch/test_multi_tensor.py: add test_multi_tensor_l2norm_tensor_over_2_31_elements (a tensor of 2^31 + 3 bf16 elements between two small tensors, for multi_tensor_l2norm and multi_tensor_unscale_l2norm; skipped with less than ~5 GiB of free GPU memory).
  • Testing

    On MI300X (gfx942), TE dbcfb92 with this patch (it applies cleanly there and on dev), built for gfx942;gfx1250:

    | | before | after |
    |---|---|---|
    | multi_tensor_l2norm on 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.hip are the existing "loop not unrolled" ones from #pragma unroll on runtime-bounded loops in reduce_block_into_lanes, reported at every kernel that inlines it.

    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
  • Diff: 552 lines (+414 / -138) across 6 files Reviewer: Micky774 Review duration: 3909m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.1% (MEDIUM) · Higher = larger blast radius
    48.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.8% · Safe to merge
    20.8%