Skip to content

[UP][release/2.13] Guard native BMM outer-product launches against the HIP work-item limit - #3596

Open
naromero77amd wants to merge 1 commit into
release/2.13from
release/2.13-native-triton-fix
Open

[UP][release/2.13] Guard native BMM outer-product launches against the HIP work-item limit#3596
naromero77amd wants to merge 1 commit into
release/2.13from
release/2.13-native-triton-fix

Conversation

@naromero77amd

@naromero77amd naromero77amd commented Aug 24, 2026

Copy link
Copy Markdown

Resolves: https://amd-hub.atlassian.net/browse/ROCM-29320. Request is from a high-priority customer.

Backport of landed pytorch/pytorch#194131 to release/2.13 as a single cherry-pick -x of upstream commit 58a938a.

Summary

  • decline the eager native outer-product BMM specialization when its launch would
    exceed HIP's work-item limit, and fall back to ATen
  • share the launch configuration between the guard and the launch, and pin
    num_warps, so the checked and launched grids cannot drift
  • add a ROCm regression that pins the boundary from both sides

HIP rejects a launch once gridDim.x * blockDim.x reaches 2^32. The eager
outer-product kernel launches a 1D grid with one program per (batch, M tile, N
tile) using a fixed configuration and, unlike Inductor, cannot grow its tiles to
shrink the grid, so it declines the specialization and falls back to ATen.

The limit is on total work items, so the boundary moves with the wave size and
the guard reads it from the device. For M = N = 1 that is B = 16,777,216 on
wave64 (CDNA) and B = 33,554,432 on wave32 (RDNA).

At these sizes ATen can also fail on some architectures, but it raises a clean,
catchable AcceleratorError; the rejected Triton launch can leave the HIP
context unusable.

Why this needs a guard

On gfx90a the rejected launch does not fail cleanly. Triton reports
RuntimeError: Triton Error [HIP]: Code: 1, Messsage: invalid argument, which
its knobs machinery then mangles into a SystemError, and the HIP context is
left unusable—the next allocation fails with
AcceleratorError: CUDA error: invalid argument.

Boundary verification (gfx90a, wave64)

Measured by calling the launcher directly, bypassing the guard:

B (M = N = 1) work items guard unguarded launch
16,777,215 4,294,967,040 accept succeeds, results correct
16,777,216 4,294,967,296 (2^32) decline fails, HIP context left unusable

The guard declines exactly the launches that fail and accepts every launch that
works.

Test Plan

The upstream PR passed its full BMM outer-product suite and its ROCm regression
ran successfully on MI350 (gfx950, wave64).

The release backport's adapted files pass targeted lint:

lintrunner --take FLAKE8,PYFMT,RUFF,CODESPELL,NEWLINE,SPACES,TABS,TESTOWNERS,TEST_HAS_MAIN \
  test/test_bmm_outer_product.py \
  torch/_native/ops/bmm_outer_product/triton_impl.py \
  torch/_native/ops/bmm_outer_product/triton_kernels.py

release/2.13 predates the upstream branch's device-generic test harness and
COW/instrumentation refactors. Conflict resolution preserves the release
branch's existing behavior while applying the landed launch guard, shared
configuration, explicit warp count, and regression test.

Made with Cursor

cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @pragupta @jerrymannil @xinyazhang @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben

@naromero77amd naromero77amd changed the title [release/2.13] Port upstream #194131: Fix triton-rocm detection and guard BMM thread limit [UP][release/2.13] Port upstream #194131: Fix triton-rocm detection and guard BMM thread limit Aug 24, 2026
@naromero77amd
naromero77amd requested a review from jataylo August 24, 2026 16:04
@naromero77amd naromero77amd changed the title [UP][release/2.13] Port upstream #194131: Fix triton-rocm detection and guard BMM thread limit [UP][release/2.13] Fix triton-rocm detection and guard BMM thread limit Aug 24, 2026
@naromero77amd naromero77amd changed the title [UP][release/2.13] Fix triton-rocm detection and guard BMM thread limit [release/2.13] Fix triton-rocm detection and guard BMM thread limit Aug 24, 2026
@naromero77amd
naromero77amd marked this pull request as draft August 25, 2026 21:23
…tem limit (pytorch#194131)

- decline the eager native outer-product BMM specialization when its launch would
  exceed HIP's work-item limit, and let ATen handle the operation instead
- share the launch configuration between the guard and the launch, and pin
  `num_warps`, so the checked and launched grids cannot drift
- add a ROCm regression that pins the boundary from both sides

HIP rejects a launch once `gridDim.x * blockDim.x` reaches `2^32`. The eager
outer-product kernel launches a 1D grid with one program per (batch, M tile, N
tile) using a fixed configuration and, unlike Inductor, cannot grow its tiles to
shrink the grid, so it declines the specialization and falls back to ATen.

The limit is on total work items, so the boundary moves with the wave size and
the guard reads it from the device. For `M = N = 1` that is `B = 16,777,216` on
wave64 (CDNA) and `B = 33,554,432` on wave32 (RDNA).

The guard is also restricted to CUDA/HIP tensors, since the override is
registered for the XPU dispatch key as well, where querying CUDA device
properties raises.

On gfx90a the rejected launch does not fail cleanly. Triton reports
`RuntimeError: Triton Error [HIP]: Code: 1, Messsage: invalid argument`, which
its knobs machinery then mangles into a `SystemError`, and the HIP context is
left unusable — the next allocation fails with
`AcceleratorError: CUDA error: invalid argument`.

Measured by calling the launcher directly, bypassing the guard:

| B (M = N = 1) | work items | guard | unguarded launch |
|---|---|---|---|
| 16,777,215 | 4,294,967,040 | accept | succeeds, results correct |
| 16,777,216 | 4,294,967,296 (2^32) | decline | fails, HIP context left unusable |

The guard declines exactly the launches that fail and accepts every launch that
works.

```bash
PYTHONPATH="$PWD" LD_LIBRARY_PATH="$PWD/build/lib:${LD_LIBRARY_PATH:-}" python test/test_bmm_outer_product.py
PYTHONPATH="$PWD" LD_LIBRARY_PATH="$PWD/build/lib:${LD_LIBRARY_PATH:-}" python test/test_bmm_outer_product.py -k hip_grid_limit_fallback
lintrunner test/test_bmm_outer_product.py torch/_native/ops/bmm_outer_product/triton_impl.py torch/_native/ops/bmm_outer_product/triton_kernels.py
```

26 tests pass on gfx90a (12 skipped: single-GPU and non-ROCm paths). Verified on
wave64 hardware only; the wave32 boundary is arithmetic.

The Inductor changes previously in this PR have been dropped per @slayton58's
review, so `torch/_inductor/runtime/triton_heuristics.py` and
`torch/utils/_triton.py` are no longer touched.

Made with [Cursor](https://cursor.com)

Pull Request resolved: pytorch#194131
Approved by: https://github.com/jansel

Co-authored-by: Cursor <cursoragent@cursor.com>
(cherry picked from commit 58a938a)
@naromero77amd
naromero77amd force-pushed the release/2.13-native-triton-fix branch from e3a498a to f270754 Compare August 31, 2026 16:01
@naromero77amd naromero77amd changed the title [release/2.13] Fix triton-rocm detection and guard BMM thread limit [release/2.13] Guard native BMM outer-product launches against the HIP work-item limit Aug 31, 2026
@naromero77amd naromero77amd changed the title [release/2.13] Guard native BMM outer-product launches against the HIP work-item limit [UP][release/2.13] Guard native BMM outer-product launches against the HIP work-item limit Aug 31, 2026
@naromero77amd
naromero77amd marked this pull request as ready for review August 31, 2026 17:37
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.

1 participant