-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add SparseFillEmptyRows-16
to Core
#30191
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f6063c5
Initial commit
p-wysocki d7996b2
Merge branch 'master' into empty_rows_core
p-wysocki 6cd533b
Fix CI after changes on master
p-wysocki a4e2ce9
Fix Constant include
p-wysocki 8fd7f69
Merge branch 'master' of https://github.com/openvinotoolkit/openvino …
p-wysocki 5d6838b
Apply CR
p-wysocki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (C) 2018-2025 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include "openvino/op/op.hpp" | ||
|
||
namespace ov::op::v16 { | ||
/// \brief An operation which fills empty rows of a sparse tensor with a default value. | ||
/// \ingroup ov_ops_cpp_api | ||
class OPENVINO_API SparseFillEmptyRows : public ov::op::Op { | ||
public: | ||
OPENVINO_OP("SparseFillEmptyRows", "opset16"); | ||
|
||
SparseFillEmptyRows() = default; | ||
|
||
/// \brief Constructs a SparseFillEmptyRows operation. | ||
/// | ||
/// \param indices 2D tensor indicating the positions of values in the sparse tensor. | ||
/// \param values 1D tensor containing the values to be inserted at the specified indices. | ||
/// \param dense_shape 1D tensor indicating the shape of the 2D dense tensor. | ||
/// \param default_value Scalar value to be inserted into the empty rows. | ||
SparseFillEmptyRows(const Output<Node>& indices, | ||
const Output<Node>& values, | ||
const Output<Node>& dense_shape, | ||
const Output<Node>& default_value); | ||
|
||
void validate_and_infer_types() override; | ||
std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override; | ||
}; | ||
|
||
} // namespace ov::op::v16 |
114 changes: 114 additions & 0 deletions
114
src/core/shape_inference/include/sparse_fill_empty_rows_shape_inference.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,114 @@ | ||||||
// Copyright (C) 2018-2025 Intel Corporation | ||||||
// SPDX-License-Identifier: Apache-2.0 | ||||||
// | ||||||
|
||||||
#pragma once | ||||||
|
||||||
#include <sstream> | ||||||
#include <unordered_set> | ||||||
|
||||||
#include "openvino/op/sparse_fill_empty_rows.hpp" | ||||||
#include "utils.hpp" | ||||||
|
||||||
namespace ov::op::v16 { | ||||||
template <class TShape, class TRShape = result_shape_t<TShape>> | ||||||
std::vector<TRShape> shape_infer(const SparseFillEmptyRows* op, | ||||||
const std::vector<TShape>& input_shapes, | ||||||
const ITensorAccessor& tensor_accessor = make_tensor_accessor()) { | ||||||
NODE_VALIDATION_CHECK(op, input_shapes.size() == 4); | ||||||
|
||||||
const auto& values_shape = input_shapes[0]; | ||||||
NODE_SHAPE_INFER_CHECK(op, | ||||||
input_shapes, | ||||||
values_shape.rank().compatible(1), | ||||||
"The values input must be a 1D tensor.", | ||||||
values_shape); | ||||||
|
||||||
const auto& dense_shape = input_shapes[1]; | ||||||
const bool is_dense_shape_rank_dynamic = dense_shape.rank().is_dynamic(); | ||||||
const bool is_dense_shape_valid = | ||||||
is_dense_shape_rank_dynamic || (dense_shape.size() == 1 && dense_shape[0].compatible(2)); | ||||||
NODE_SHAPE_INFER_CHECK( | ||||||
op, | ||||||
input_shapes, | ||||||
is_dense_shape_valid, | ||||||
"The dense_shape input must be 1D and have exactly 2 elements, meaning only 2D sparse tensors are supported."); | ||||||
|
||||||
const auto& indices_shape = input_shapes[2]; | ||||||
const bool is_indices_shape_valid = indices_shape.rank().is_dynamic() || | ||||||
(indices_shape.size() == 2 && indices_shape[1].compatible(2) && | ||||||
(is_dense_shape_rank_dynamic || indices_shape[0].compatible(values_shape[0]))); | ||||||
NODE_SHAPE_INFER_CHECK(op, | ||||||
input_shapes, | ||||||
is_indices_shape_valid, | ||||||
"The indices input must be a 2D tensor with the first dimension matching the size of values " | ||||||
"and the second dimension having 2 elements.", | ||||||
indices_shape); | ||||||
|
||||||
const auto& default_value_shape = input_shapes[3]; | ||||||
NODE_SHAPE_INFER_CHECK(op, | ||||||
input_shapes, | ||||||
default_value_shape.rank().compatible(0), | ||||||
"The default_value input must be a scalar.", | ||||||
default_value_shape); | ||||||
|
||||||
auto output_shapes = std::vector<TRShape>(3); | ||||||
auto& output_indices_shape = output_shapes[0]; | ||||||
auto& output_values_shape = output_shapes[1]; | ||||||
auto& empty_row_indicator_shape = output_shapes[2]; | ||||||
output_indices_shape.resize(2); | ||||||
output_values_shape.resize(1); | ||||||
empty_row_indicator_shape.resize(1); | ||||||
output_indices_shape[1] = 2; // Only 2D cases are supported | ||||||
|
||||||
if (auto dense_shape_value = get_input_const_data_as_shape<TRShape>(op, 1, tensor_accessor)) { | ||||||
const auto& number_of_rows = (*dense_shape_value)[0].get_length(); | ||||||
empty_row_indicator_shape[0] = number_of_rows; | ||||||
|
||||||
if (auto indices_value = get_input_const_data_as<TRShape, int64_t>(op, 2, tensor_accessor)) { | ||||||
auto is_valid_index = [](int64_t index, int64_t max_value) -> bool { | ||||||
return index >= 0 && index < max_value; | ||||||
}; | ||||||
auto create_index_error_message = | ||||||
[](const std::string& dim_name, int64_t index, int64_t max_value) -> std::string { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional detail:
Suggested change
For local lambda auto deduce could pass as literal char array (pointer) instead convert it to temporary string. |
||||||
std::stringstream ss; | ||||||
ss << "Sparse tensor index out of bounds: " << dim_name << " " << index | ||||||
<< " is outside the valid range [0, " << (max_value - 1) << "]"; | ||||||
return ss.str(); | ||||||
}; | ||||||
|
||||||
// Rows can be referenced multiple times in sparse representation | ||||||
std::unordered_set<int64_t> existing_rows; | ||||||
const auto& indices_data = *indices_value; | ||||||
const auto& number_of_cols = (*dense_shape_value)[1].get_length(); | ||||||
for (size_t i = 0, i_next = 1; i_next < indices_data.size(); i += 2, i_next += 2) { | ||||||
auto row = indices_data[i]; | ||||||
NODE_SHAPE_INFER_CHECK(op, | ||||||
input_shapes, | ||||||
is_valid_index(row, number_of_rows), | ||||||
create_index_error_message("row", row, number_of_rows)); | ||||||
|
||||||
auto col = indices_data[i_next]; | ||||||
NODE_SHAPE_INFER_CHECK(op, | ||||||
input_shapes, | ||||||
is_valid_index(col, number_of_cols), | ||||||
create_index_error_message("column", col, number_of_cols)); | ||||||
|
||||||
existing_rows.insert(row); | ||||||
} | ||||||
|
||||||
using TDim = typename TRShape::value_type; | ||||||
TDim empty_rows_count = number_of_rows - existing_rows.size(); | ||||||
output_indices_shape[0] = indices_shape[0] + empty_rows_count; | ||||||
output_values_shape[0] = values_shape[0] + empty_rows_count; | ||||||
} else { | ||||||
output_indices_shape[0] = Dimension::dynamic(); | ||||||
output_values_shape[0] = Dimension::dynamic(); | ||||||
} | ||||||
} else { | ||||||
empty_row_indicator_shape[0] = Dimension::dynamic(); | ||||||
} | ||||||
|
||||||
return output_shapes; | ||||||
} | ||||||
} // namespace ov::op::v16 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (C) 2018-2025 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "openvino/op/sparse_fill_empty_rows.hpp" | ||
|
||
#include "itt.hpp" | ||
#include "openvino/core/validation_util.hpp" | ||
#include "openvino/op/op.hpp" | ||
#include "sparse_fill_empty_rows_shape_inference.hpp" | ||
|
||
namespace ov::op::v16 { | ||
|
||
SparseFillEmptyRows::SparseFillEmptyRows(const Output<Node>& values, | ||
const Output<Node>& dense_shape, | ||
const Output<Node>& indices, | ||
const Output<Node>& default_value) | ||
: Op({values, dense_shape, indices, default_value}) { | ||
constructor_validate_and_infer_types(); | ||
} | ||
|
||
void SparseFillEmptyRows::validate_and_infer_types() { | ||
OV_OP_SCOPE(v16_SparseFillEmptyRows_validate_and_infer_types); | ||
|
||
const auto& indices_element_type = get_input_element_type(2); | ||
NODE_VALIDATION_CHECK(this, | ||
indices_element_type == element::i32 || indices_element_type == element::i64, | ||
"The element type of the indices input must be i32 or i64. Got: ", | ||
indices_element_type); | ||
|
||
const auto& dense_shape_element_type = get_input_element_type(1); | ||
NODE_VALIDATION_CHECK(this, | ||
dense_shape_element_type == element::i32 || dense_shape_element_type == element::i64, | ||
"The element type of the dense_shape input must be i32 or i64. Got: ", | ||
dense_shape_element_type); | ||
|
||
const auto output_shapes = shape_infer(this, ov::util::get_node_input_partial_shapes(*this)); | ||
|
||
set_output_type(0, indices_element_type, output_shapes[0]); | ||
set_output_type(1, get_input_element_type(0), output_shapes[1]); | ||
set_output_type(2, element::boolean, output_shapes[2]); | ||
} | ||
|
||
std::shared_ptr<Node> SparseFillEmptyRows::clone_with_new_inputs(const ov::OutputVector& new_args) const { | ||
OV_OP_SCOPE(v16_SparseFillEmptyRows_clone_with_new_inputs); | ||
check_new_args_count(this, new_args); | ||
return std::make_shared<SparseFillEmptyRows>(new_args.at(0), new_args.at(1), new_args.at(2), new_args.at(3)); | ||
} | ||
|
||
} // namespace ov::op::v16 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It will throw for negative indices, probably something worth to mention in the op specification.
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.
I added it, but it's also worth to remember in the future in case we have more sparse operators to add some sort of unified spec for a sparse tensor representation in OpenVINO.