Skip to content

Fix GatherND division by zero when batch dimensions mismatch - #27090

Merged
justinchuby merged 16 commits into
microsoft:mainfrom
chaya2350:fix-gathernd-division-by-zero
Feb 18, 2026
Merged

Fix GatherND division by zero when batch dimensions mismatch#27090
justinchuby merged 16 commits into
microsoft:mainfrom
chaya2350:fix-gathernd-division-by-zero

Conversation

@chaya2350

@chaya2350 chaya2350 commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #23828

Added validation to check:

  • num_batches is not zero
  • num_slices is divisible by num_batches

Before this fix, mismatched batch dimensions caused a crash due to division by zero.

Description

This PR fixes a division by zero crash in the GatherND operator when batch dimensions mismatch between input and indices tensors.

Changes made:

Added validation in gather_nd.cc to check that num_batches is not zero before division
Added validation that num_slices is divisible by num_batches
Added a unit test to verify the fix

Motivation and Context

Description
Fixes #23828

When batch_dims is set but the actual batch dimensions of the input tensor and indices tensor don't align correctly, the code performs a division that can result in division by zero, causing a crash.

For example, with:

Input shape: [2, 2, 2]
Indices shape: [2, 1]
batch_dims=1
The calculation num_slices / num_batches would crash if num_batches is 0, or produce unexpected results if they don't divide evenly.

This fix returns a clear error message instead of crashing.

Fixes microsoft#23828

Added validation to check:
- num_batches is not zero
- num_slices is divisible by num_batches

Before this fix, mismatched batch dimensions caused a crash due to
division by zero.
@chaya2350

Copy link
Copy Markdown
Contributor Author

@fdwr @justinchuby Could you please review this PR? It fixes the issue you reported. Thanks! 🙂

@justinchuby

Copy link
Copy Markdown
Contributor

@tianleiwu

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 fixes a division by zero crash in the GatherND operator when batch dimensions between input and indices tensors are mismatched. The fix adds validation to ensure num_batches is non-zero and that num_slices is evenly divisible by num_batches before performing the division.

Changes:

  • Added validation checks in gather_nd.cc to prevent division by zero
  • Added a unit test to verify the fix handles batch dimension mismatches correctly

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/core/providers/cpu/tensor/gather_nd.cc Added validation to check num_batches is not zero and num_slices is divisible by num_batches before division
onnxruntime/test/providers/cpu/tensor/gather_nd_op_test.cc Added test case to verify batch dimension mismatch returns an error instead of crashing

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/core/providers/cpu/tensor/gather_nd.cc Outdated
justinchuby
justinchuby previously approved these changes Jan 21, 2026
@justinchuby
justinchuby requested review from fdwr and tianleiwu January 21, 2026 17:08
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@chaya2350

Copy link
Copy Markdown
Contributor Author

I've addressed the Copilot comment. Ready for re-review! 🙂

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

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/test/providers/cpu/tensor/gather_nd_op_test.cc Outdated
Comment thread onnxruntime/test/providers/cpu/tensor/gather_nd_op_test.cc Outdated
chaya2350 and others added 2 commits January 22, 2026 09:50
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/test/providers/cpu/tensor/gather_nd_op_test.cc
Address Copilot review: added test case to cover the num_batches == 0 validation path.

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

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

justinchuby
justinchuby previously approved these changes Jan 22, 2026
@justinchuby
justinchuby enabled auto-merge (squash) January 22, 2026 16:09
fdwr
fdwr previously approved these changes Jan 23, 2026

@fdwr fdwr 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.

👍 Thanks :).

auto-merge was automatically disabled January 26, 2026 11:18

Head branch was pushed to by a user without write access

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.

[CPU EP] GatherND crashes with division by zero when batch dimensions mismatch between input and indices

5 participants