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

Commit
31 days ago
Handle zero-element input/bias in BiasGelu and FastGelu as a no-op (#31698) ### 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. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 77dcaf1b-748a-4379-94a7-478f7a924d73
Author
Parents
Loading