Skip to content

Conversation

iksnagreb
Copy link
Contributor

Adds constant folding of Split with all constant inputs as well as replacing single-output Splits by Identity via the registry. Also adds 5 simple test cases covering both options: Second input specifying the sizes per split as well as nearly even splits into num_outputs via the attribute.

Copy link
Contributor

@Copilot 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 adds constant folding optimization for the Split operator when all inputs are constants, replacing them with individual Constant nodes. It also optimizes single-output Split operations by replacing them with Identity nodes.

  • Implements constant folding for Split operations with all constant inputs
  • Adds optimization to replace single-output Splits with Identity operations
  • Includes comprehensive test coverage for both num_outputs and splits parameter variants

Reviewed Changes

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

File Description
onnxscript/optimizer/_constant_folding.py Adds the split function to handle constant folding and Identity replacement for Split operations
onnxscript/optimizer/_constant_folding_test.py Adds 5 test cases covering Identity replacement and constant folding scenarios

@justinchuby
Copy link
Collaborator

Thank you!

iksnagreb added a commit to iksnagreb/onnx-passes that referenced this pull request Aug 23, 2025
Note: Only add this temporarily until this is properly supported by
onnxscript, see microsoft/onnxscript#2506
Copy link
Collaborator

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

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

LGTM. @gramalingam what do you think?

Copy link

codecov bot commented Aug 28, 2025

Codecov Report

❌ Patch coverage is 94.44444% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.13%. Comparing base (e8005e9) to head (89d57a0).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
onnxscript/optimizer/_constant_folding.py 84.00% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2506      +/-   ##
==========================================
+ Coverage   70.08%   70.13%   +0.05%     
==========================================
  Files         215      215              
  Lines       25810    25938     +128     
  Branches     2587     2612      +25     
==========================================
+ Hits        18089    18192     +103     
- Misses       6823     6841      +18     
- Partials      898      905       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

return None


@register("Split")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think a better, more general, solution would be to fix the restriction in the general constant-folding (this else branch) ... so the underlying evaluator evaluates the Split op ... and we just need to bind each output value to the corresponding constant value.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you share what needs to be done to enable that part?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@iksnagreb as suggested here, we will instead fix handling for multi-output nodes. Please feel free to contribute or provide your inputs, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good question. What needs to be done is create an appropriate Replacement object which wraps the list of new nodes (a list of Constant nodes like in current PR), and a list of values (which should be more or less what is returned by the reference implementation).

I realize that this is lacking even in the current PR down below ... if we don't specify the list of new values (to replace the list of old values produced by the constant-folded node), we are not going to connect these values to the consumers of the old values.

So, if we added a test-case where the outputs of Split are used subsequently (or if we test the graph outputs in the tests below actually refer to the correct split const values), we will likely find that this PR doesn't work. It stems from a limitation in the core constant-folding logic that needs to be fixed.

Copy link
Collaborator

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

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

Blocking for now according to reviews, thanks!

@github-project-automation github-project-automation bot moved this from Done to In Progress in ONNX Script Review Board Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants