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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 34 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,34 @@
// 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);

bool visit_attributes(AttributeVisitor& visitor) override;
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,125 @@
// Copyright (C) 2018-2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <unordered_set>

#include "openvino/op/sparse_fill_empty_rows.hpp"
#include "utils.hpp"

namespace ov {
namespace op {
namespace 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. Got: ",
values_shape);

const auto& dense_shape = input_shapes[1];
NODE_SHAPE_INFER_CHECK(op,
input_shapes,
dense_shape.rank().compatible(1),
"The dense_shape input must be a 1D tensor. Got: ",
dense_shape);
if (dense_shape.rank().is_static()) {
NODE_SHAPE_INFER_CHECK(
op,
input_shapes,
dense_shape[0].compatible(2),
"The dense_shape input must have exactly 2 elements. Only 2D sparse tensors are supported.");
}

const auto& indices_shape = input_shapes[2];
NODE_SHAPE_INFER_CHECK(op,
input_shapes,
indices_shape.rank().compatible(2),
"The indices input must be a 2D tensor. Got: ",
indices_shape);

if (indices_shape.rank().is_static()) {
NODE_SHAPE_INFER_CHECK(
op,
input_shapes,
indices_shape[1].compatible(2),
"The indices_shape's second dimension must have 2 elements. Only 2D sparse tensors are supported.");
if (values_shape.rank().is_static()) {
NODE_SHAPE_INFER_CHECK(op,
input_shapes,
indices_shape[0].compatible(values_shape[0]),
"The first dimension of indices must match the size of values.");
}
}

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. Got: ",
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)) {
// Rows can be referenced multiple times in sparse representation
std::unordered_set<int64_t> existing_rows;
const auto& indices_data = *indices_value;
size_t indices_count = indices_data.size() / 2;
const auto& number_of_cols = (*dense_shape_value)[1].get_length();
for (size_t i = 0; i < indices_count; ++i) {
int64_t row = indices_data[i * 2];
NODE_SHAPE_INFER_CHECK(op,
input_shapes,
row >= 0 && row < static_cast<int64_t>(number_of_rows),
"Sparse tensor index out of bounds: row ",
row,
" is outside the valid range [0, ",
number_of_rows - 1,
"]");
int64_t col = indices_data[i * 2 + 1];
NODE_SHAPE_INFER_CHECK(op,
input_shapes,
col >= 0 && col < static_cast<int64_t>(number_of_cols),
"Sparse tensor index out of bounds: column ",
col,
" is outside the valid range [0, ",
number_of_cols - 1,
"]");
existing_rows.insert(row);
}
int64_t 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 v16
} // namespace op
} // namespace ov
59 changes: 59 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,59 @@
// 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 {
namespace op {
namespace 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();
}

bool SparseFillEmptyRows::visit_attributes(ov::AttributeVisitor& visitor) {
OV_OP_SCOPE(v16_SparseFillEmptyRows_visit_attributes);
return true;
}

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 v16
} // namespace op
} // namespace ov
Loading
Loading