Skip to content

Handle empty tensors in CUDA InstanceNormalization - #31647

Open
apsonawane wants to merge 1 commit into
mainfrom
msrc/cuda-instancenorm-divzero-fix
Open

Handle empty tensors in CUDA InstanceNormalization#31647
apsonawane wants to merge 1 commit into
mainfrom
msrc/cuda-instancenorm-divzero-fix

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request improves the handling of empty input tensors in the CUDA implementation of the InstanceNormalization operator and adds corresponding unit tests to ensure correct behavior. The main focus is to ensure compliance with the ONNX specification, which allows empty inputs and expects empty outputs.

CUDA Implementation Improvements:

  • Updated InstanceNorm<T>::ComputeInternal and InstanceNorm<MLFloat16>::ComputeInternal in instance_norm.cc to check for empty input tensors and return early, ensuring that empty inputs produce empty outputs as required by ONNX. This prevents unnecessary computation and potential errors when the input size is zero. [1] [2]

Unit Test Additions:

  • Added two CUDA-specific tests in instance_norm_op_test.cc:
    • InstanceNormEmptyChannel_Cuda verifies correct handling when the channel dimension is zero.
    • InstanceNormEmptyBatch_Cuda verifies correct handling when the batch dimension is zero.
      These tests confirm that the operator returns empty outputs without error for these edge cases.

Early-return success for empty input tensors in both generic and MLFloat16 CUDA paths to avoid host-side divide-by-zero in N!=1 branch.

Add CUDA regression tests for zero-channel and zero-batch inputs.

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.

Pull request overview

This PR updates the CUDA InstanceNormalization kernel to correctly handle valid ONNX cases where the input tensor has zero elements (any dimension is 0), ensuring it returns an empty output without invoking cuDNN paths that would otherwise hit divide-by-zero/invalid descriptor scenarios. It also adds CUDA-targeted unit tests to cover zero-channel and zero-batch edge cases.

Changes:

  • Add early-return guards in CUDA InstanceNorm<T>::ComputeInternal (including MLFloat16 specialization) for x_shape.Size() == 0 after input validation.
  • Add CUDA-only tests to verify empty outputs are produced (and no error occurs) when N==0 or C==0.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
onnxruntime/core/providers/cuda/nn/instance_norm.cc Early-return on empty input tensors (after ValidateInputs) to avoid invalid cuDNN/statistics paths and produce empty outputs.
onnxruntime/test/providers/cpu/nn/instance_norm_op_test.cc Adds CUDA-scoped tests for empty channel and empty batch cases to validate correct empty-output behavior.

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.

2 participants