Skip to content

Conversation

@CuriousPanCake
Copy link
Contributor

@CuriousPanCake CuriousPanCake commented Oct 30, 2025

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

@CuriousPanCake CuriousPanCake requested a review from a team as a code owner October 30, 2025 13:35
@github-actions github-actions bot added the category: ONNX FE OpenVINO ONNX FrontEnd label Oct 30, 2025
Comment on lines 82 to 84
auto scale = inputs.at(1);
if (scale.get_partial_shape() != normalized.get_partial_shape())
scale = std::make_shared<v1::Reshape>(scale, normalized_shape, false);
Copy link
Contributor Author

@CuriousPanCake CuriousPanCake Oct 30, 2025

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?

Copy link
Collaborator

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.

Copy link
Contributor Author

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).
image

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?

@mvafin mvafin self-assigned this Oct 30, 2025
@mvafin
Copy link
Contributor

mvafin commented Oct 30, 2025

Please add description to PR, what the issue is and how you fix it.

@CuriousPanCake CuriousPanCake changed the title Fix [ONNX Frontend] Reshape Scale & Bias inputs of Normalization layer if shapes are mismatched but compatible Oct 31, 2025
@CuriousPanCake
Copy link
Contributor Author

CuriousPanCake commented Nov 3, 2025

According to Roman, the Reshapes are going to be removed down the pipeline if not needed.

@mvafin mvafin requested a review from Copilot November 3, 2025 11:46
Copy link
Contributor

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 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);
Copy link

Copilot AI Nov 3, 2025

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.

Suggested change
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);
}

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@mvafin mvafin left a 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: ONNX FE OpenVINO ONNX FrontEnd

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants