Handle zero-sized outputs in CUDA random generator kernels - #31997
Merged
Conversation
RandomKernelImpl computed grid_size from CeilDiv(N, block_size * UNROLL), which is zero when the output tensor has no elements. The counter_offset expression then divided by block_size * grid_size * UNROLL, a host-side division by zero that traps before the kernel is launched. Zero-sized tensors are legal in ONNX, so return early when there is nothing to fill. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Akshay Sonawane (apsonawane)
requested review from
Tianlei Wu (tianleiwu)
and
a lite review from Copilot
August 11, 2026 23:04
Copilot started reviewing on behalf of
Akshay Sonawane (apsonawane)
August 11, 2026 23:04
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a host-side divide-by-zero in the CUDA random generator implementation when producing zero-element outputs (legal in ONNX), by returning early before computing a zero grid_size and using it in counter_offset math.
Changes:
- Add an
N == 0early-return guard in the CUDARandomKernelImplto avoid dividing byblock_size * grid_size * UNROLLwhengrid_size == 0. - Add a CUDA-only unit test that exercises
RandomNormal/RandomUniformwith an empty output shape and expects successful execution.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| onnxruntime/core/providers/cuda/generator/random_impl.cu | Adds an early return for N == 0 to prevent host-side division by zero before kernel launch. |
| onnxruntime/test/providers/cpu/generator/random_test.cc | Adds a CUDA-gated test covering zero-sized output tensors for CUDA random ops. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Akshay Sonawane (apsonawane)
enabled auto-merge (squash)
August 11, 2026 23:14
Ti-Tai Wang (titaiwangms)
approved these changes
Aug 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RandomKernelImpl computed grid_size from CeilDiv(N, block_size * UNROLL), which is zero when the output tensor has no elements. The counter_offset expression then divided by block_size * grid_size * UNROLL, a host-side division by zero that traps before the kernel is launched. Zero-sized tensors are legal in ONNX, so return early when there is nothing to fill.