Skip to content

Handle empty CPU LpNormalization inputs - #32020

Merged
Akshay Sonawane (apsonawane) merged 1 commit into
mainfrom
fix/cpu-lpnorm-zero-extent
Aug 13, 2026
Merged

Handle empty CPU LpNormalization inputs#32020
Akshay Sonawane (apsonawane) merged 1 commit into
mainfrom
fix/cpu-lpnorm-zero-extent

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request adds handling and tests for cases where the LpNormalization operator receives input tensors with zero elements along the normalization axis. The main changes include an early return in the implementation to avoid unnecessary computation and a new test to verify correct behavior for zero-extent axes.

LpNormalization operator improvements:

  • Added an early return in LpNorm<T>::Compute to immediately return success when the input tensor has zero elements, preventing unnecessary computation for empty inputs.

Testing enhancements:

  • Introduced the LpNormalizationZeroExtentAxis test, which checks that the operator correctly handles input tensors with a zero-extent axis for both p=1 and p=2, and for both float and double types.

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 pull request updates the CPU implementation of the LpNormalization operator to correctly handle empty inputs (zero total elements due to a zero-extent dimension), avoiding invalid normalization math (e.g., division by zero) and adding a regression test to ensure the behavior remains correct.

Changes:

  • Added an early return in LpNorm<T>::Compute when input_shape.Size() == 0 to safely bypass normalization work for empty tensors.
  • Added a CPU-only unit test covering a zero-extent normalization axis for p = 1 and p = 2, for both float and double.

Reviewed changes

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

File Description
onnxruntime/core/providers/cpu/nn/lp_norm.cc Early-return for empty tensors to prevent invalid normalization computations and unnecessary work.
onnxruntime/test/providers/cpu/nn/lp_norm_op_test.cc Adds a regression test ensuring LpNormalization succeeds with a zero-extent axis (empty tensor) on CPU.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@apsonawane
Akshay Sonawane (apsonawane) merged commit 4a08422 into main Aug 13, 2026
88 checks passed
@apsonawane
Akshay Sonawane (apsonawane) deleted the fix/cpu-lpnorm-zero-extent branch August 13, 2026 17:52
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