Skip to content

Fix access violation for Broadcast operator when 0-size shape with scalar input#34874

Merged
sgbihu merged 8 commits intoopenvinotoolkit:masterfrom
sgbihu:fix_0size_constant_scalar
Mar 25, 2026
Merged

Fix access violation for Broadcast operator when 0-size shape with scalar input#34874
sgbihu merged 8 commits intoopenvinotoolkit:masterfrom
sgbihu:fix_0size_constant_scalar

Conversation

@sgbihu
Copy link
Copy Markdown
Contributor

@sgbihu sgbihu commented Mar 24, 2026

Details:

  • This case will make element_count == 0, m_shape.size() == 1, but shape_size(m_shape) == 0.
  • The repeats in src\plugins\intel_cpu\src\nodes\broadcast.cpp is 0. But the srcDims.size() = 1. That make the program access repeats[0] and cause the access violation.

Tickets:

AI Assistance:

  • AI assistance used: no / yes
  • If yes, summarize how AI was used and what human validation was performed (build/tests/manual checks).

@sgbihu sgbihu requested a review from a team as a code owner March 24, 2026 06:12
@github-actions github-actions bot added the category: ONNX FE OpenVINO ONNX FrontEnd label Mar 24, 2026
@MayureshV1
Copy link
Copy Markdown

@mvafin .. Can you please review this PR

Copy link
Copy Markdown
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 addresses an ONNX frontend crash/access violation scenario for Expand/Broadcast when the target shape tensor has zero elements (e.g., shape [0]), particularly impacting cases like scalar/1-element inputs, by converting such initializers into “failsafe” constants so the Expand importer can safely ignore the target shape.

Changes:

  • Add an ONNX FE test covering Expand with a scalar/1-element input and an empty target-shape constant.
  • Update ONNX Tensor::get_ov_constant() to produce a failsafe constant when the tensor has zero elements and an empty/zero-sized shape (including cases where shape_size(m_shape) == 0).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/frontends/onnx/tests/onnx_import.in.cpp Adds a regression test intended to reproduce the empty-shape Expand issue for scalar/1-element input.
src/frontends/onnx/frontend/src/core/tensor.cpp Adjusts constant creation to generate a failsafe constant for zero-element tensors with empty/zero-sized shapes, enabling downstream op handlers (e.g., Expand) to avoid unsafe behavior.

Copy link
Copy Markdown
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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

}
}
if (has_external_data()) {
if (element_count == 0 && (m_shape.size() == 0 || shape_size(m_shape) == 0)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In OV representation the shape[0] is not scalar is 1_D with zero elements. but scalar m_shape.size() == 0 still has single value.

The shape_size(m_shape) == 0) maybe should be error condition as this mean tensor has no data at all.
@mvafin , @rkazants
Could check this?

Copy link
Copy Markdown
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.
This code is used in both legacy and delegate paths and the test is added for both cases.
Good to merge if all tests pass.

Copy link
Copy Markdown
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment on lines +27 to +32
if (common::is_failsafe_node(pattern.get_node_shared_ptr())) {
// in case the "shape" input is connected to a failsafe node created in place of an invalid initializer
// the target shape should be ignored and this Expand operation should not modify its input tensor
// the Broadcast created below should be eliminated later on by an appropriate optimization pass
pattern = v0::Constant::create(ov::element::i64, {0}, {});
}
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

[BLOCKER] When the Reshape "shape" input is a failsafe node, replacing it with an empty 1D constant ({0}) will reshape any non-scalar input into a scalar and can lead to runtime validation failures due to element-count mismatch. If the intent is to ignore an invalid initializer and preserve the input tensor, this should instead behave as an identity (e.g., return the input unchanged / use ShapeOf(data) as the pattern) and update the comment (it currently refers to Expand/Broadcast, which is unrelated to Reshape).

Copilot uses AI. Check for mistakes.
Comment on lines 552 to 562
if (names.size() > 0) {
constant->set_friendly_name(names[0]);
constant->get_default_output().set_names({names.begin(), names.end()});
}
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

[HIGH] create_constant_from_place uses m_tensor_place (member) when setting names, and the unsupported-type path references m_tensor_proto->data_type(). Since Tensor can be constructed with only a TensorONNXPlace (and m_tensor_proto == nullptr), this can become a null dereference if an unexpected element type hits the default branch. Prefer using the tensor_place argument consistently (including for names), and avoid dereferencing m_tensor_proto in this code path.

Copilot uses AI. Check for mistakes.
@mvafin
Copy link
Copy Markdown
Contributor

mvafin commented Mar 24, 2026

You created lambda functions for place and for proto, this makes it harder to review, but doesn't really help to solve any issue. Please revert that change

Copy link
Copy Markdown
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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@sgbihu
Copy link
Copy Markdown
Contributor Author

sgbihu commented Mar 24, 2026

build_jenkins

@sgbihu sgbihu added this pull request to the merge queue Mar 25, 2026
Merged via the queue into openvinotoolkit:master with commit 02f54d4 Mar 25, 2026
202 checks passed
@sgbihu sgbihu deleted the fix_0size_constant_scalar branch March 25, 2026 07:14
@praasz praasz added this to the 2026.2 milestone Mar 25, 2026
ababushk pushed a commit that referenced this pull request Mar 25, 2026
#34895)

### Details:
- This case will make `element_count == 0`, `m_shape.size() == 1`, but
`shape_size(m_shape) == 0`.
- The `repeats` in `src\plugins\intel_cpu\src\nodes\broadcast.cpp` is
`0`. But the `srcDims.size() = 1`. That make the program access
`repeats[0]` and cause the access violation.

Port the #34874 to
2026.1

### Tickets:
 - [CVS-183409](https://jira.devtools.intel.com/browse/CVS-183409)

### AI Assistance:
 - *AI assistance used: no / yes*
- *If yes, summarize how AI was used and what human validation was
performed (build/tests/manual checks).*

---------

Co-authored-by: Michal Lukaszewski <michal.lukaszewski@intel.com>
github-merge-queue bot pushed a commit that referenced this pull request Apr 4, 2026
#34973)

### Details:
- A following PR for
#34874
 - It fixed the conflict with external 0 size weight. 


### Tickets:
 - [CVS-183409](https://jira.devtools.intel.com/browse/CVS-183409)

### AI Assistance:
 - *AI assistance used: no
- *If yes, summarize how AI was used and what human validation was
performed (build/tests/manual checks).*
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.

5 participants