Skip to content

[Kernel] Fix bug in Einsum implementation when a lone operand had a reduction operation#27225

Merged
hariharans29 merged 12 commits intomainfrom
hari/einsum_fix_1
Feb 6, 2026
Merged

[Kernel] Fix bug in Einsum implementation when a lone operand had a reduction operation#27225
hariharans29 merged 12 commits intomainfrom
hari/einsum_fix_1

Conversation

@hariharans29
Copy link
Member

@hariharans29 hariharans29 commented Feb 3, 2026

Description

When the lone operand had a reduction operation in Einsum, there was a mis-management in the shape which led to a mis-match in terms of the subscript indices in the output as deciphered from the Einsum equation and the rank of the candidate output being produced after the reduction operation which caused issues while finalizing the output of the Einsum op.

This change fixes the bug and adds a test

Motivation and Context

Resolve #18654

@hariharans29 hariharans29 changed the title Fix bug in Einsum impl when a lone operand had a reduction operation Fix bug in Einsum implementation when a lone operand had a reduction operation Feb 3, 2026
@hariharans29 hariharans29 requested a review from Copilot February 3, 2026 03:56
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can commit the suggested changes from lintrunner.

Copy link
Contributor

Copilot AI left a comment

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 bug in the Einsum implementation that occurred when a single input operand underwent both a reduction and transpose operation. The issue manifested as a shape mismatch error where the candidate output rank didn't match the number of subscript indices.

Changes:

  • Fixed the rank mismatch issue by ensuring all dimensions (including reduced ones with value 1) are tracked when calling FinalizeOutput for single-input cases
  • Added an ORT_ENFORCE check to catch this type of internal bug earlier in FinalizeOutput
  • Added a test case covering the specific bug scenario ("ijk->ki" equation)
  • Improved comment clarity by adding "relative" to describe order preservation in IsTransposeReshapeForEinsum

Reviewed changes

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

File Description
onnxruntime/core/providers/cpu/math/einsum_utils/einsum_typed_compute_processor.cc Fixed single-input reduction bug by changing from preserved_dims to all_dims, added validation check, improved comment
onnxruntime/test/providers/cpu/math/einsum_test.cc Added test case for the reduce-with-transpose scenario that was previously failing

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

hariharans29 and others added 4 commits February 2, 2026 20:08
…compute_processor.cc

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…compute_processor.cc

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…compute_processor.cc

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

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.

…compute_processor.cc

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@hariharans29 hariharans29 changed the title Fix bug in Einsum implementation when a lone operand had a reduction operation [Kernel] Fix bug in Einsum implementation when a lone operand had a reduction operation Feb 4, 2026
@hariharans29 hariharans29 requested a review from edgchen1 February 4, 2026 20:05
edgchen1
edgchen1 previously approved these changes Feb 5, 2026
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can commit the suggested changes from lintrunner.

Copy link
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated 1 comment.


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

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can commit the suggested changes from lintrunner.

hariharans29 and others added 3 commits February 5, 2026 10:29
…compute_processor.cc

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@hariharans29 hariharans29 requested a review from Copilot February 5, 2026 18:42
Copy link
Contributor

Copilot AI left a comment

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 4 out of 4 changed files in this pull request and generated 1 comment.


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

@hariharans29 hariharans29 enabled auto-merge (squash) February 5, 2026 19:16
@hariharans29 hariharans29 merged commit 040ae1f into main Feb 6, 2026
99 of 106 checks passed
@hariharans29 hariharans29 deleted the hari/einsum_fix_1 branch February 6, 2026 02:58
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.

einsum can't handle equation 'ijk->ki' using onnxruntime

2 participants