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
Open
Handle zero-element input/bias in BiasGelu and FastGelu as a no-op#31698Ti-Tai Wang (titaiwangms) wants to merge 1 commit into
Ti-Tai Wang (titaiwangms) wants to merge 1 commit into
Conversation
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
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.
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 CPUBiasGelu/FastGelukernel and the CUDABiasGelukernel 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:
GeluCPU kernel (core/providers/cpu/tensor/gelu.cc)FastGelu(contrib_ops/cuda/bert/fast_gelu.cc)BiasGelu/FastGeluThis also closes a second, independent division-by-zero that only affects the CUDA
BiasGelulaunch-grid computation, which wasn't guarded by the existing shape-equality check.Motivation and Context
A model with a
BiasGelu/FastGelunode 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 standardGelukernel's existing behavior of treating it as a no-op), this fix makes all paths consistent.Testing
Added
BiasGeluTest.ZeroLengthBiasIsNoOpandFastGeluTest.ZeroLengthBiasIsNoOpregression 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.