Skip to content

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 6 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/dev_api/openvino/op/ops_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,5 @@ namespace ov::op::v16 {
class ISTFT;
class Identity;
class SegmentMax;
class SparseFillEmptyRows;
} // namespace ov::op::v16
1 change: 1 addition & 0 deletions src/core/include/openvino/op/ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
#include "openvino/op/softsign.hpp"
#include "openvino/op/space_to_batch.hpp"
#include "openvino/op/space_to_depth.hpp"
#include "openvino/op/sparse_fill_empty_rows.hpp"
#include "openvino/op/split.hpp"
#include "openvino/op/sqrt.hpp"
#include "openvino/op/squared_difference.hpp"
Expand Down
33 changes: 33 additions & 0 deletions src/core/include/openvino/op/sparse_fill_empty_rows.hpp
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
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;
};
Comment on lines +69 to +71
Copy link
Contributor

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.

Copy link
Contributor Author

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.

auto create_index_error_message =
[](const std::string& dim_name, int64_t index, int64_t max_value) -> std::string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional detail:

Suggested change
[](const std::string& dim_name, int64_t index, int64_t max_value) -> std::string {
[](auto&& dim_name, int64_t index, int64_t max_value) -> std::string {

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
50 changes: 50 additions & 0 deletions src/core/src/op/sparse_fill_empty_rows.cpp
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
Loading
Loading