Skip to content

Handle zero-element input/bias in BiasGelu and FastGelu as a no-op - #31698

Open
Ti-Tai Wang (titaiwangms) wants to merge 1 commit into
microsoft:mainfrom
titaiwangms:fix/bias-gelu-zero-length-bias
Open

Handle zero-element input/bias in BiasGelu and FastGelu as a no-op#31698
Ti-Tai Wang (titaiwangms) wants to merge 1 commit into
microsoft:mainfrom
titaiwangms:fix/bias-gelu-zero-length-bias

Conversation

@titaiwangms

Copy link
Copy Markdown
Contributor

Description

An empty input (with a correspondingly empty bias, per the existing shape-matching validation in bias_gelu_helper::CheckInputs) is a legal degenerate case under the ONNX shape model. The CPU BiasGelu/FastGelu kernel and the CUDA BiasGelu kernel previously computed a divisor from the bias length before checking for this case, causing an integer division by zero when both input and bias have zero elements.

This adds an early return for the zero-element case in both kernels, matching the existing no-op behavior already used by:

  • the standard ONNX Gelu CPU kernel (core/providers/cpu/tensor/gelu.cc)
  • CUDA FastGelu (contrib_ops/cuda/bert/fast_gelu.cc)
  • WebGPU BiasGelu/FastGelu

This also closes a second, independent division-by-zero that only affects the CUDA BiasGelu launch-grid computation, which wasn't guarded by the existing shape-equality check.

Motivation and Context

A model with a BiasGelu/FastGelu node whose input's last dimension (and bias length) is 0 could previously trigger a crash at inference on CPU or CUDA. Rather than rejecting this input outright (which would make CPU/CUDA diverge from WebGPU's and the standard Gelu kernel's existing behavior of treating it as a no-op), this fix makes all paths consistent.

Testing

Added BiasGeluTest.ZeroLengthBiasIsNoOp and FastGeluTest.ZeroLengthBiasIsNoOp regression tests verifying the zero-element case completes successfully with an empty output, rather than asserting on an EP-specific error message string, so the tests remain valid across execution providers.

An empty input (with a correspondingly empty bias, per the existing
shape-matching validation) is a legal degenerate case under the ONNX
shape model. The CPU BiasGelu/FastGelu kernel and the CUDA BiasGelu
kernel previously computed a divisor from the bias length before
checking for this case, causing an integer division by zero when both
were zero.

Add an early return for the zero-element case in both kernels, matching
the existing no-op behavior already used by the standard ONNX Gelu CPU
kernel and by CUDA FastGelu. This also closes a second, separate
division that only affects the CUDA BiasGelu launch-grid computation.

Add regression tests for both ops verifying the zero-element case
completes successfully rather than exercising an EP-specific error
message, so the tests remain valid across execution providers whose
existing implementations already treat this input as a no-op.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 77dcaf1b-748a-4379-94a7-478f7a924d73

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.

3 participants