-
Notifications
You must be signed in to change notification settings - Fork 790
Fix panic for Expand onnx operation with scalar input #4230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
I created this pull request to get a bit of feedback. I think I should extract the logic to create the temporary Tensor for the scalar. |
|
CC'ing @antimora since you self-assigned the linked issue. |
I think for this one it's better if make changes on your PR directly because there are a few things to consider. This is related to tensor creation with dtype |
@antimora let me know if you have any directions that I should implement. |
|
@GregorySech sorry I thought I could handle it before my travels (be back on 20th). Recently I have discovered that tensor creation from native value was defaulting to backends default dtype. You need to make sure you pass scalars dtype when creating a tensor from native scalar. You can find some existing examples. @laggui is also aware of this issue. Please proceed with this PR and assume I am not working on it. I will have access to my computer after Jan 20th. Thank you for your contribution. |
There was a problem hiding this 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 pull request fixes a panic that occurred when the ONNX Expand operation receives a scalar input instead of a tensor. The fix properly handles scalar inputs by converting them to rank-1 tensors during code generation.
Changes:
- Modified type inference in
ExpandProcessor::infer_typesto accept scalar inputs and propagate their data types - Updated code generation in
ExpandNode::forwardto convert scalar inputs to rank-1 tensors with appropriate tensor kinds (Float, Int, Bool) - Added comprehensive test coverage including unit tests and an end-to-end ONNX model test
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
crates/onnx-ir/src/node/expand.rs |
Added scalar type handling in type inference, extracting dtype from scalar inputs |
crates/burn-onnx/src/burn/node/expand.rs |
Implemented scalar-to-tensor conversion logic for all supported data types (float, int, uint, bool) with comprehensive unit tests |
crates/burn-onnx/onnx-tests/tests/expand/mod.rs |
Added integration test for scalar expand operation verifying both shape and values |
crates/burn-onnx/onnx-tests/tests/expand/expand_scalar.py |
Python script to generate test ONNX model with scalar input |
crates/burn-onnx/onnx-tests/tests/expand/expand_scalar.onnx |
Generated ONNX model file |
crates/burn-onnx/onnx-tests/build.rs |
Added new test model to build configuration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Add scalar input handling in ExpandNode codegen by converting scalar to rank-1 tensor using `from_data_dtype` with explicit dtype preservation - Update type inference in onnx-ir to accept Scalar inputs - Add expand_scalar integration test with value verification Co-Authored-By: GregorySech <16958043+GregorySech@users.noreply.github.com>
antimora
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made the fixes directly, so personally approving. Can be merged after @laggui has a chance to review.
Though this fixes the scalar expand issue, there are more issues when converting decode onnx:
DEBUG onnx_ir::phases::node_conversion: Preserving outer-scope value_store for 'constant459_out1' (Constant)
DEBUG onnx_ir::pipeline: PHASE 3: Type Inference
ERROR burn_onnx::logger: PANIC => panicked at crates/burn-onnx/src/model_gen.rs:296:33:
Failed to parse ONNX file './out/edge_sam_3x_decoder_opset16.onnx': Type inference failed: Custom("Cannot determine output rank for Expand node expand4 with fully dynamic shape tensor")
thread 'main' (484957) panicked at crates/burn-onnx/src/model_gen.rs:296:33:
Failed to parse ONNX file './out/edge_sam_3x_decoder_opset16.onnx': Type inference failed: Custom("Cannot determine output rank for Expand node expand4 with fully dynamic shape tensor")
I would encourage to open a new issue or PR submission.
Codecov Report❌ Patch coverage is
❌ Your project check has failed because the head coverage (68.67%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #4230 +/- ##
==========================================
+ Coverage 68.65% 68.67% +0.02%
==========================================
Files 1411 1411
Lines 168676 168805 +129
==========================================
+ Hits 115804 115933 +129
Misses 52872 52872 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hi, wait for the merge, i need to push a fix to handle the corner case where onnx cannot infer the rank of the output but the input is scalar. Currently it’s still panicing but it can be handled |
laggui
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, wait for the merge, i need to push a fix to handle the corner case where onnx cannot infer the rank of the output but the input is scalar. Currently it’s still panicing but it can be handled
Sounds good! Will request changes just to block any accidental merge 😄
Otherwise the current changes LGTM
| let init = quote! { | ||
| let input = Tensor::<B, 1 #kind>::from_data_dtype( | ||
| burn::tensor::TensorData::from([#input]), | ||
| &*self.device, | ||
| #dtype_tokens | ||
| ); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use Tensor::<B, 1, #kind>::full instead? Just a small suggestion.
This pull request fixes a panic that occurred when the ONNX Expand operation receives a scalar input instead of a tensor. The fix properly handles scalar inputs by converting them to rank-1 tensors during code generation.
Checklist
cargo run-checkscommand has been executed.Related Issues/PRs
Fixes #4206
Changes
ExpandProcessor::infer_typesto accept scalar inputs and propagate their data typesExpandNode::forwardto convert scalar inputs to rank-1 tensors with appropriate tensor kinds (Float, Int, Bool)Testing