-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[ONNX Frontend] Reshape Scale & Bias inputs of Normalization layer if shapes are mismatched but compatible #32630
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: master
Are you sure you want to change the base?
[ONNX Frontend] Reshape Scale & Bias inputs of Normalization layer if shapes are mismatched but compatible #32630
Conversation
| auto scale = inputs.at(1); | ||
| if (scale.get_partial_shape() != normalized.get_partial_shape()) | ||
| scale = std::make_shared<v1::Reshape>(scale, normalized_shape, false); |
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.
Do you think this is a good way of comparing shapes?
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.
No. What would like to do? Please describe.
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.
In the original model there's a Normalization operation that accepts an input with shape [1, 3, 32, 32], however the scale and bias parameters of the operation are of shape [768] (3 * 32 * 32 = 768).

It looks like onnxruntime is able to perform reshaping or flattening here, or handle it in some smart manner, whilst in openvino we get an exception of shapes mismatch. So what I do is performing reshaping of the scale and bias to be compatible with the input shape. What would you suggest checking with? Checking for broadcasting?
|
Please add description to PR, what the issue is and how you fix it. |
|
According to Roman, the Reshapes are going to be removed down the pipeline if not needed. |
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 PR fixes a shape mismatch issue in the ONNX frontend's LayerNormalization operator. When the scale and bias inputs have compatible but different shapes compared to the normalized input (e.g., [768] vs [1, 3, 32, 32] where 33232=768), the code now reshapes these inputs to match the expected dimensions. This aligns OpenVINO's behavior with ONNX Runtime's handling of such cases.
Key Changes:
- Added automatic reshaping of scale and bias inputs in LayerNormalization when shapes are compatible but mismatched
- Added comprehensive test case with real-world scenario involving shape transformations through multiple operations
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| layer_normalization.cpp | Implements reshape logic for scale/bias inputs using Slice and Reshape operations to extract and match normalized tensor dimensions |
| onnx_import_com_microsoft.in.cpp | Adds test case validating LayerNormalization with mismatched but compatible scale/bias shapes |
| normalization_with_different_scale_and_bias_shape.prototxt | Defines ONNX test model with operations producing the shape mismatch scenario |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| auto scale = std::make_shared<v1::Reshape>(inputs.at(1), sub_shape, false); | ||
| auto scaled = std::make_shared<Multiply>(normalized, scale); | ||
|
|
||
| auto bias = std::make_shared<v1::Reshape>(inputs.at(2), sub_shape, false); |
Copilot
AI
Nov 3, 2025
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.
The bias is always reshaped on line 90, but it's only used when num_inputs == 3. This will cause a runtime error when there are only 2 inputs (scale without bias), as inputs.at(2) will be out of bounds. The bias reshape should be conditional and only performed when num_inputs == 3.
| auto bias = std::make_shared<v1::Reshape>(inputs.at(2), sub_shape, false); | |
| std::shared_ptr<ov::Node> bias; | |
| if (num_inputs == 3) { | |
| bias = std::make_shared<v1::Reshape>(inputs.at(2), sub_shape, false); | |
| } |
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.
Looks good, but can we not always reshape?
Details:
In the original onnx model despite the LayerNormalization node having [1, 3, 32, 32] as input shape, the bias and scale inputs are of shape [768] which is compatible (3 * 32 * 32 = 768), but is a shape mismatch. It looks like onnxruntime is able to handle such cases, whilst OpenVINO doesn't.
Fix it by Reshaping the scale and bias inputs in case of shape mismatch.
Tickets:
Signed-off-by: Andrii Staikov andrii.staikov@intel.com