|
| 1 | +// Copyright (C) 2018-2025 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "openvino/op/sparse_fill_empty_rows.hpp" |
| 6 | + |
| 7 | +#include <gtest/gtest.h> |
| 8 | + |
| 9 | +#include "base_reference_test.hpp" |
| 10 | +#include "openvino/core/type/element_type_traits.hpp" |
| 11 | +#include "openvino/op/constant.hpp" |
| 12 | + |
| 13 | +namespace { |
| 14 | +struct SparseFillEmptyRowsParams { |
| 15 | + SparseFillEmptyRowsParams(const reference_tests::Tensor& valuesTensor, |
| 16 | + const reference_tests::Tensor& denseShapeTensor, |
| 17 | + const reference_tests::Tensor& indicesTensor, |
| 18 | + const reference_tests::Tensor& defaultValueTensor, |
| 19 | + const reference_tests::Tensor& expectedIndicesTensor, |
| 20 | + const reference_tests::Tensor& expectedValuesTensor, |
| 21 | + const reference_tests::Tensor& expectedEmptyRowIndicatorTensor) |
| 22 | + : valuesTensor(valuesTensor), |
| 23 | + denseShapeTensor(denseShapeTensor), |
| 24 | + indicesTensor(indicesTensor), |
| 25 | + defaultValueTensor(defaultValueTensor), |
| 26 | + expectedIndicesTensor(expectedIndicesTensor), |
| 27 | + expectedValuesTensor(expectedValuesTensor), |
| 28 | + expectedEmptyRowIndicatorTensor(expectedEmptyRowIndicatorTensor) {} |
| 29 | + |
| 30 | + reference_tests::Tensor valuesTensor; |
| 31 | + reference_tests::Tensor denseShapeTensor; |
| 32 | + reference_tests::Tensor indicesTensor; |
| 33 | + reference_tests::Tensor defaultValueTensor; |
| 34 | + reference_tests::Tensor expectedIndicesTensor; |
| 35 | + reference_tests::Tensor expectedValuesTensor; |
| 36 | + reference_tests::Tensor expectedEmptyRowIndicatorTensor; |
| 37 | +}; |
| 38 | + |
| 39 | +class ReferenceSparseFillEmptyRowsV16LayerTest : public testing::TestWithParam<SparseFillEmptyRowsParams>, |
| 40 | + public reference_tests::CommonReferenceTest { |
| 41 | +protected: |
| 42 | + void SetUp() override { |
| 43 | + const auto& params = GetParam(); |
| 44 | + function = CreateFunction(params); |
| 45 | + inputData = {params.valuesTensor.data, |
| 46 | + params.denseShapeTensor.data, |
| 47 | + params.indicesTensor.data, |
| 48 | + params.defaultValueTensor.data}; |
| 49 | + refOutData = {params.expectedIndicesTensor.data, |
| 50 | + params.expectedValuesTensor.data, |
| 51 | + params.expectedEmptyRowIndicatorTensor.data}; |
| 52 | + } |
| 53 | + |
| 54 | +public: |
| 55 | + static std::string getTestCaseName(const testing::TestParamInfo<SparseFillEmptyRowsParams>& obj) { |
| 56 | + auto param = obj.param; |
| 57 | + std::ostringstream result; |
| 58 | + result << "valuesType=" << param.valuesTensor.type; |
| 59 | + result << "_valuesShape=" << param.valuesTensor.shape; |
| 60 | + result << "_denseShapeType=" << param.denseShapeTensor.type; |
| 61 | + result << "_denseShapeValues=" << testing::PrintToString(param.denseShapeTensor.data); |
| 62 | + result << "_indicesType=" << param.indicesTensor.type; |
| 63 | + result << "_indicesShape=" << param.indicesTensor.shape; |
| 64 | + result << "_defaultValue=" << testing::PrintToString(param.defaultValueTensor.data); |
| 65 | + return result.str(); |
| 66 | + } |
| 67 | + |
| 68 | +private: |
| 69 | + static std::shared_ptr<ov::Model> CreateFunction(const SparseFillEmptyRowsParams& params) { |
| 70 | + using ov::op::v0::Constant; |
| 71 | + using ov::op::v0::Parameter; |
| 72 | + |
| 73 | + const auto values = std::make_shared<Parameter>(params.valuesTensor.type, params.valuesTensor.shape); |
| 74 | + const auto dense_shape = |
| 75 | + std::make_shared<Parameter>(params.denseShapeTensor.type, params.denseShapeTensor.shape); |
| 76 | + const auto indices = std::make_shared<Parameter>(params.indicesTensor.type, params.indicesTensor.shape); |
| 77 | + const auto default_value = |
| 78 | + std::make_shared<Parameter>(params.defaultValueTensor.type, params.defaultValueTensor.shape); |
| 79 | + |
| 80 | + const auto sparseFillEmptyRows = |
| 81 | + std::make_shared<ov::op::v16::SparseFillEmptyRows>(values, dense_shape, indices, default_value); |
| 82 | + |
| 83 | + return std::make_shared<ov::Model>(ov::OutputVector{sparseFillEmptyRows->output(0), |
| 84 | + sparseFillEmptyRows->output(1), |
| 85 | + sparseFillEmptyRows->output(2)}, |
| 86 | + ov::ParameterVector{values, dense_shape, indices, default_value}); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +TEST_P(ReferenceSparseFillEmptyRowsV16LayerTest, CompareWithRefs) { |
| 91 | + Exec(); |
| 92 | +} |
| 93 | + |
| 94 | +template <ov::element::Type_t T, ov::element::Type_t T_idx> |
| 95 | +std::vector<SparseFillEmptyRowsParams> generateSparseFillEmptyRowsParams() { |
| 96 | + using T_D = typename ov::element_type_traits<T>::value_type; |
| 97 | + using T_I = typename ov::element_type_traits<T_idx>::value_type; |
| 98 | + using reference_tests::Tensor; |
| 99 | + |
| 100 | + std::vector<SparseFillEmptyRowsParams> params{ |
| 101 | + // No empty rows |
| 102 | + SparseFillEmptyRowsParams( |
| 103 | + Tensor({3}, T, std::vector<T_D>{1, 2, 3}), // values |
| 104 | + Tensor({2}, T_idx, std::vector<T_I>{3, 4}), // dense_shape |
| 105 | + Tensor({3, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0}), // indices |
| 106 | + Tensor({}, T, std::vector<T_D>{-1}), // default_value |
| 107 | + Tensor({3, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0}), // expected_indices |
| 108 | + Tensor({3}, T, std::vector<T_D>{1, 2, 3}), // expected_values |
| 109 | + Tensor({3}, ov::element::boolean, std::vector<uint8_t>{0, 0, 0}) // expected_empty_row_indicator |
| 110 | + ), |
| 111 | + |
| 112 | + // One empty row in the middle |
| 113 | + SparseFillEmptyRowsParams( |
| 114 | + Tensor({3}, T, std::vector<T_D>{1, 2, 3}), // values |
| 115 | + Tensor({2}, T_idx, std::vector<T_I>{4, 4}), // dense_shape |
| 116 | + Tensor({3, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 3, 0}), // indices |
| 117 | + Tensor({}, T, std::vector<T_D>{-1}), // default_value |
| 118 | + Tensor({4, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0, 3, 0}), // expected_indices |
| 119 | + Tensor({4}, T, std::vector<T_D>{1, 2, -1, 3}), // expected_values |
| 120 | + Tensor({4}, ov::element::boolean, std::vector<uint8_t>{0, 0, 1, 0}) // expected_empty_row_indicator |
| 121 | + ), |
| 122 | + |
| 123 | + // Multiple empty rows |
| 124 | + SparseFillEmptyRowsParams( |
| 125 | + Tensor({2}, T, std::vector<T_D>{1, 2}), // values |
| 126 | + Tensor({2}, T_idx, std::vector<T_I>{5, 3}), // dense_shape |
| 127 | + Tensor({2, 2}, T_idx, std::vector<T_I>{0, 0, 4, 0}), // indices |
| 128 | + Tensor({}, T, std::vector<T_D>{-1}), // default_value |
| 129 | + Tensor({5, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0, 3, 0, 4, 0}), // expected_indices |
| 130 | + Tensor({5}, T, std::vector<T_D>{1, -1, -1, -1, 2}), // expected_values |
| 131 | + Tensor({5}, ov::element::boolean, std::vector<uint8_t>{0, 1, 1, 1, 0}) // expected_empty_row_indicator |
| 132 | + ), |
| 133 | + |
| 134 | + // All rows empty |
| 135 | + SparseFillEmptyRowsParams( |
| 136 | + Tensor({0}, T, std::vector<T_D>{}), // values |
| 137 | + Tensor({2}, T_idx, std::vector<T_I>{3, 4}), // dense_shape |
| 138 | + Tensor({0, 2}, T_idx, std::vector<T_I>{}), // indices |
| 139 | + Tensor({}, T, std::vector<T_D>{-1}), // default_value |
| 140 | + Tensor({3, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0}), // expected_indices |
| 141 | + Tensor({3}, T, std::vector<T_D>{-1, -1, -1}), // expected_values |
| 142 | + Tensor({3}, ov::element::boolean, std::vector<uint8_t>{1, 1, 1}) // expected_empty_row_indicator |
| 143 | + ), |
| 144 | + |
| 145 | + // Non-zero column indices for empty rows |
| 146 | + SparseFillEmptyRowsParams( |
| 147 | + Tensor({4}, T, std::vector<T_D>{1, 2, 3, 4}), // values |
| 148 | + Tensor({2}, T_idx, std::vector<T_I>{5, 6}), // dense_shape |
| 149 | + Tensor({4, 2}, T_idx, std::vector<T_I>{0, 1, 1, 2, 3, 3, 4, 4}), // indices |
| 150 | + Tensor({}, T, std::vector<T_D>{99}), // default_value |
| 151 | + Tensor({5, 2}, T_idx, std::vector<T_I>{0, 1, 1, 2, 2, 0, 3, 3, 4, 4}), // expected_indices |
| 152 | + Tensor({5}, T, std::vector<T_D>{1, 2, 99, 3, 4}), // expected_values |
| 153 | + Tensor({5}, ov::element::boolean, std::vector<uint8_t>{0, 0, 1, 0, 0}) // expected_empty_row_indicator |
| 154 | + )}; |
| 155 | + |
| 156 | + return params; |
| 157 | +} |
| 158 | + |
| 159 | +std::vector<SparseFillEmptyRowsParams> generateSparseFillEmptyRowsV16CombinedParams() { |
| 160 | + using ov::element::Type_t; |
| 161 | + const std::vector<std::vector<SparseFillEmptyRowsParams>> SparseFillEmptyRowsTypeParams{ |
| 162 | + generateSparseFillEmptyRowsParams<Type_t::i32, Type_t::i32>(), |
| 163 | + generateSparseFillEmptyRowsParams<Type_t::i64, Type_t::i32>(), |
| 164 | + generateSparseFillEmptyRowsParams<Type_t::f32, Type_t::i32>(), |
| 165 | + generateSparseFillEmptyRowsParams<Type_t::i32, Type_t::i64>(), |
| 166 | + generateSparseFillEmptyRowsParams<Type_t::i64, Type_t::i64>(), |
| 167 | + generateSparseFillEmptyRowsParams<Type_t::f32, Type_t::i64>()}; |
| 168 | + |
| 169 | + std::vector<SparseFillEmptyRowsParams> combinedParams; |
| 170 | + for (const auto& params : SparseFillEmptyRowsTypeParams) { |
| 171 | + combinedParams.insert(combinedParams.end(), params.begin(), params.end()); |
| 172 | + } |
| 173 | + return combinedParams; |
| 174 | +} |
| 175 | + |
| 176 | +INSTANTIATE_TEST_SUITE_P(smoke_SparseFillEmptyRows_With_Hardcoded_Refs, |
| 177 | + ReferenceSparseFillEmptyRowsV16LayerTest, |
| 178 | + testing::ValuesIn(generateSparseFillEmptyRowsV16CombinedParams()), |
| 179 | + ReferenceSparseFillEmptyRowsV16LayerTest::getTestCaseName); |
| 180 | +} // namespace |
0 commit comments