Skip to content

[Fix][TOPI] Fuse GPU scan blocks to avoid CUDA gridDim.y overflow - #20108

Open
chenmiaoming wants to merge 1 commit into
apache:mainfrom
chenmiaoming:bugfix-20106
Open

[Fix][TOPI] Fuse GPU scan blocks to avoid CUDA gridDim.y overflow#20108
chenmiaoming wants to merge 1 commit into
apache:mainfrom
chenmiaoming:bugfix-20106

Conversation

@chenmiaoming

Copy link
Copy Markdown

Motivation

Fixes #20106.

The fallback TOPI GPU scan maps the per-row scan block to blockIdx.x and the
batch to blockIdx.y. CUDA limits gridDim.y to 65,535, so a valid Relax
cumprod with shape (65536, 1) fails at execution time with
CUDA_ERROR_INVALID_VALUE and grid=(1,65536,1).

Changes

Fuse the virtual scan-block and batch dimensions into a single blockIdx.x
launch dimension, then recover both indices with integer division and modulo.
Apply this mapping to the initial copy, up-sweep, and down-sweep stages. Fusing
the dimensions fixes large batches without moving the same 65,535 limit onto
long scan axes.

Add a dynamic-shape CUDA cumprod regression with an input of shape
(65536, 3). The three-element scan axis exercises the up-sweep and down-sweep
stages while retaining the batch size that previously caused the invalid
launch.

Testing

  • python -m pytest tests/python/relax/test_backend_dispatch_sort_scan.py -xvs
    • 10 passed, 1 skipped, 1 pre-existing xpassed
  • Manual CUDA checks for the issue shape (65536, 1) and a multi-block scan
    shape (8, 2000)
  • pre-commit run --files python/tvm/topi/gpu/scan.py tests/python/relax/test_backend_dispatch_sort_scan.py

Flatten the batch and scan-block axes into blockIdx.x for each scan stage so large batches do not exceed CUDA's grid-y limit.\n\nAdd a dynamic-shape CUDA cumprod regression covering 65,536 batches.\n\nFixes apache#20106
@chenmiaoming
chenmiaoming marked this pull request as ready for review August 8, 2026 13:30
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.

[Bug] TVM CUDA cumprod launch grid overflow (CUDA_ERROR_INVALID_VALUE)

1 participant