[ONNX] Fix Split operator for uneven splits with num_outputs#18774
[ONNX] Fix Split operator for uneven splits with num_outputs#18774Mr-Neutr0n wants to merge 1 commit intoapache:mainfrom
Conversation
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>
Summary of ChangesHello @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 Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
| 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)) |
There was a problem hiding this comment.
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
splitsizes are provided, both_impl_v1and_impl_v13pass the rawnum_outputsinteger torelax.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_indiceshelper that computes explicit cumulative split indices using ceil-based block sizes when the input shape is statically known: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