Skip to content

[ONNX] Fix Split operator for uneven splits with num_outputs#18774

Open
Mr-Neutr0n wants to merge 1 commit intoapache:mainfrom
Mr-Neutr0n:fix/onnx-split-uneven-num-outputs
Open

[ONNX] Fix Split operator for uneven splits with num_outputs#18774
Mr-Neutr0n wants to merge 1 commit intoapache:mainfrom
Mr-Neutr0n:fix/onnx-split-uneven-num-outputs

Conversation

@Mr-Neutr0n
Copy link

Summary

Fixes #18751

The ONNX frontend's Split converter crashes when the tensor dimension isn't evenly divisible by num_outputs. For example, splitting a length-10 tensor into 3 parts should produce [4, 4, 2] per the ONNX Opset 18 spec, but the converter only handled perfectly divisible cases.

Root cause: When no explicit split sizes are provided, both _impl_v1 and _impl_v13 pass the raw num_outputs integer to relax.op.split. While the underlying op supports uneven splits in theory, the conversion fails during model normalization for non-divisible dimensions.

Fix: Added a _compute_split_indices helper that computes explicit cumulative split indices using ceil-based block sizes when the input shape is statically known:

block_size = ceil(dim / num_outputs)
indices = [block_size, 2*block_size, ..., (N-1)*block_size]

For a length-10 tensor with num_outputs=3: block_size = ceil(10/3) = 4, indices = [4, 8], producing splits of [4, 4, 2]. Falls back to passing the integer for dynamic shapes.

Test plan

  • Added test cases for 1D uneven split (10 / 3 -> [4, 4, 2])
  • Added test cases for 2D uneven split along axis 1 (7 / 3 -> [3, 3, 1])
  • Existing even-split tests remain unchanged and should continue to pass

The ONNX frontend's Split converter failed when the tensor dimension
wasn't evenly divisible by num_outputs (e.g., splitting a length-10
tensor into 3 parts). This implements the ceil-based split calculation
from the ONNX Opset 18 spec: block_size = ceil(dim / num_outputs),
yielding [block_size] * (N-1) + [remainder] for the output sizes.

When the input shape is statically known, explicit cumulative split
indices are computed and passed to relax.op.split. For dynamic shapes,
the integer num_outputs is passed through as before.

Fixes apache#18751

Signed-off-by: Mr-Neutr0n <64578610+Mr-Neutr0n@users.noreply.github.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Mr-Neutr0n, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the ONNX frontend's Split operator within TVM Relax, which previously failed when encountering uneven tensor dimensions combined with the num_outputs attribute. The core of the solution involves dynamically computing precise split indices based on the ONNX Opset 18 specification, allowing for correct handling of non-divisible tensor splits. This enhancement significantly improves the robustness and compliance of the ONNX frontend, preventing crashes and ensuring accurate tensor partitioning in more complex scenarios.

Highlights

  • ONNX Split Operator Fix: Resolved an issue where the ONNX frontend's Split operator crashed when attempting to split a tensor into an uneven number of parts using num_outputs, which previously only supported perfectly divisible cases.
  • Uneven Split Handling: Implemented a new helper method, _compute_split_indices, to calculate explicit cumulative split indices for static input shapes, adhering to the ONNX Opset 18 specification for ceil-based block sizes (e.g., splitting a length-10 tensor into 3 parts yields [4, 4, 2]).
  • Test Coverage: Added new test cases to validate the correct behavior of the Split operator for both 1D and 2D uneven splits, ensuring compliance with the ONNX Opset 18 specification.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • python/tvm/relax/frontend/onnx/onnx_frontend.py
    • Added _compute_split_indices method to correctly calculate split indices for uneven splits based on ONNX Opset 18.
    • Updated _impl_v1 and _impl_v13 to utilize the new _compute_split_indices method when split attribute is not provided.
  • tests/python/relax/test_frontend_onnx.py
    • Added new test cases for 1D and 2D uneven splits to verify the fix.
Activity
  • No activity found for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly fixes a bug in the ONNX Split operator converter for uneven splits when num_outputs is provided. The introduction of the _compute_split_indices helper function to handle static shapes aligns with the ONNX specification and is a clean solution. The new test cases for 1D and 2D uneven splits are also a great addition, ensuring the fix is robust. I have a couple of minor suggestions to improve code readability by avoiding redundant dictionary lookups.

axis = attr.get("axis", 0)
num_outputs = attr["tvm_custom"]["num_outputs"]
indices = cls._compute_split_indices(inputs[0], axis, num_outputs)
return relax.op.split(inputs[0], indices, attr.get("axis", 0))
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To improve readability and avoid a redundant dictionary lookup, you can reuse the axis variable that was already retrieved on line 1824.

Suggested change
return relax.op.split(inputs[0], indices, attr.get("axis", 0))
return relax.op.split(inputs[0], indices, axis)

axis = attr.get("axis", 0)
num_outputs = attr.get("num_outputs", attr["tvm_custom"]["num_outputs"])
indices = cls._compute_split_indices(inputs[0], axis, num_outputs)
return relax.op.split(inputs[0], indices, attr.get("axis", 0))
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to the change in _impl_v1, you can reuse the axis variable that was retrieved on line 1847 to improve readability and avoid a redundant dictionary lookup.

Suggested change
return relax.op.split(inputs[0], indices, attr.get("axis", 0))
return relax.op.split(inputs[0], indices, axis)

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.

[Bug][Frontend][ONNX] Split fails to handle uneven splitting with 'num_outputs' in Opset 18

1 participant