Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

12.5 KB
Raw Blame
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "openvino/op/embeddingbag_packed.hpp"

#include <gtest/gtest.h>

#include "base_reference_test.hpp"
#include "openvino/op/constant.hpp"

using namespace reference_tests;
using namespace ov;

struct EmbeddingBagPackedParams {
    template <class IT>
    EmbeddingBagPackedParams(const ov::Shape& iShape,
                             const ov::element::Type& iType,
                             const std::vector<IT>& iValues,
                             const ov::Shape& oShape,
                             const ov::element::Type& oType,
                             const std::vector<IT>& oValues,
                             const std::shared_ptr<ov::op::v0::Constant>& indices,
                             const ov::op::v15::EmbeddingBagPacked::Reduction reduction,
                             const std::shared_ptr<ov::op::v0::Constant>& per_sample_weights = nullptr)
        : _iShape(iShape),
          _iType(iType),
          _iData(CreateTensor(iShape, iType, iValues)),
          _refShape(oShape),
          _refType(oType),
          _refData(CreateTensor(oShape, oType, oValues)) {
        _indices = indices;
        _reduction = reduction;
        _perSampleWeights = per_sample_weights;
    }
    ov::Shape _iShape;
    ov::element::Type _iType;
    ov::Tensor _iData;

    ov::Shape _refShape;
    ov::element::Type _refType;
    ov::Tensor _refData;

    std::shared_ptr<ov::op::v0::Constant> _indices;
    ov::op::v15::EmbeddingBagPacked::Reduction _reduction;
    std::shared_ptr<ov::op::v0::Constant> _perSampleWeights;  // Optional, default is tensor of ones.
};

class ReferenceEmbeddingBagPackedLayerTest : public testing::TestWithParam<EmbeddingBagPackedParams>,
                                             public CommonReferenceTest {
public:
    void SetUp() override {
        auto params = GetParam();
        function =
            CreateFunction(params._iShape, params._iType, params._indices, params._reduction, params._perSampleWeights);
        inputData = {params._iData};
        refOutData = {params._refData};
    }
    static std::string getTestCaseName(const testing::TestParamInfo<EmbeddingBagPackedParams>& obj) {
        auto param = obj.param;
        std::ostringstream result;
        result << "_iShape=" << param._iShape << "_";
        result << "_iType=" << param._iType << "_";
        result << "_refShape=" << param._refShape << "_";
        result << "_refType=" << param._refType << "_";
        result << "_reduction=" << param._reduction;
        return result.str();
    }

private:
    static std::shared_ptr<Model> CreateFunction(const Shape& input_shape,
                                                 const element::Type& input_type,
                                                 const std::shared_ptr<ov::op::v0::Constant> indices,
                                                 const ov::op::v15::EmbeddingBagPacked::Reduction reduction,
                                                 const std::shared_ptr<ov::op::v0::Constant> per_sample_weights) {
        const auto in = std::make_shared<op::v0::Parameter>(input_type, input_shape);

        if (per_sample_weights) {
            const auto ess = std::make_shared<op::v15::EmbeddingBagPacked>(in, indices, per_sample_weights, reduction);
            return std::make_shared<Model>(OutputVector{ess}, ParameterVector{in});
        } else {
            const auto ess = std::make_shared<op::v15::EmbeddingBagPacked>(in, indices, reduction);
            return std::make_shared<Model>(OutputVector{ess}, ParameterVector{in});
        }
    }
};

TEST_P(ReferenceEmbeddingBagPackedLayerTest, CompareWithRefs) {
    Exec();
}

template <class T>
inline std::shared_ptr<ov::op::v0::Constant> CreateConstant(const std::vector<std::vector<T>>& val,
                                                            const ov::element::Type& element_type) {
    if (val.size() > 0) {
        ov::Shape i_shape({val.size(), val[0].size()});

        size_t i_size = ov::shape_size(i_shape);
        std::vector<T> i_values(i_size);

        for (size_t i = 0; i < i_shape[0]; i++) {
            for (size_t j = 0; j < i_shape[1]; j++) {
                i_values[i * i_shape[1] + j] = val[i][j];
            }
        }

        return std::make_shared<ov::op::v0::Constant>(element_type, i_shape, i_values);
    } else {
        return std::make_shared<ov::op::v0::Constant>(element_type, ov::Shape(), std::vector<T>());
    }
}

INSTANTIATE_TEST_SUITE_P(
    smoke_EmbeddingBagPacked_With_Hardcoded_Refs,
    ReferenceEmbeddingBagPackedLayerTest,
    ::testing::Values(
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::f32,
                                 std::vector<float>{-0.2, -0.6, -0.1, -0.4, -1.9, -1.8, -1., 1.5, 0.8, -0.7},
                                 ov::Shape{3, 2},
                                 ov::element::f32,
                                 std::vector<float>{-1.05f, -1.2f, -1.f, -1.1f, -0.1f, 0.4f},
                                 CreateConstant<int32_t>({{0, 2}, {1, 2}, {3, 4}}, element::i32),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::SUM,
                                 CreateConstant<float>({{0.5, 0.5}, {0.5, 0.5}, {0.5, 0.5}}, element::f32)),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::f64,
                                 std::vector<double>{-0.2, -0.6, -0.1, -0.4, -1.9, -1.8, -1., 1.5, 0.8, -0.7},
                                 ov::Shape{3, 2},
                                 ov::element::f64,
                                 std::vector<double>{-2.1, -2.4, -2.0, -2.2, -0.2, 0.8},
                                 CreateConstant<int32_t>({{0, 2}, {1, 2}, {3, 4}}, element::i32),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::SUM),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::i32,
                                 std::vector<int32_t>{-1, 2, 3, 4, -5, -6, -7, 8, 9, 10},
                                 ov::Shape{3, 2},
                                 ov::element::i32,
                                 std::vector<int32_t>{-6, -4, -2, -2, 2, 18},
                                 CreateConstant<int32_t>({{0, 2}, {1, 2}, {3, 4}}, element::i32),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::SUM),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::u32,
                                 std::vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                                 ov::Shape{3, 2},
                                 ov::element::u32,
                                 std::vector<uint32_t>{6, 8, 8, 10, 16, 18},
                                 CreateConstant<int32_t>({{0, 2}, {1, 2}, {3, 4}}, element::i32),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::SUM),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::f16,
                                 std::vector<float16>{-0.2, -0.6, -0.1, -0.4, -1.9, -1.8, -1., 1.5, 0.8, -0.7},
                                 ov::Shape{3, 2},
                                 ov::element::f16,
                                 std::vector<float16>{-2.1, -2.4, -2.0, -2.2, -0.2, 0.8},
                                 CreateConstant<int64_t>({{0, 2}, {1, 2}, {3, 4}}, element::i64),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::SUM),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::i64,
                                 std::vector<int64_t>{-1, 2, 3, 4, -5, -6, -7, 8, 9, 10},
                                 ov::Shape{3, 2},
                                 ov::element::i64,
                                 std::vector<int64_t>{-6, -4, -2, -2, 2, 18},
                                 CreateConstant<int64_t>({{0, 2}, {1, 2}, {3, 4}}, element::i64),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::SUM),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::i8,
                                 std::vector<int8_t>{-1, 2, 3, 4, -5, -6, -7, 8, 9, 10},
                                 ov::Shape{3, 2},
                                 ov::element::i8,
                                 std::vector<int8_t>{-12, -8, -4, -4, 4, 36},
                                 CreateConstant<int64_t>({{0, 2}, {1, 2}, {3, 4}}, element::i64),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::SUM,
                                 CreateConstant<int8_t>({{2, 2}, {2, 2}, {2, 2}}, element::i8)),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::u8,
                                 std::vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                                 ov::Shape{3, 2},
                                 ov::element::u8,
                                 std::vector<uint8_t>{6, 8, 8, 10, 16, 18},
                                 CreateConstant<int64_t>({{0, 2}, {1, 2}, {3, 4}}, element::i64),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::SUM),
        // Reduction MEAN
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::f64,
                                 std::vector<double>{-0.2, -0.6, -0.1, -0.4, -1.9, -1.8, -1., 1.5, 0.8, -0.7},
                                 ov::Shape{3, 2},
                                 ov::element::f64,
                                 std::vector<double>{-1.05, -1.2, -1., -1.1, -0.1, 0.4},
                                 CreateConstant<int32_t>({{0, 2}, {1, 2}, {3, 4}}, element::i32),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::MEAN),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::i32,
                                 std::vector<int32_t>{-1, 2, 3, 4, -5, -6, -7, 8, 9, 10},
                                 ov::Shape{3, 2},
                                 ov::element::i32,
                                 std::vector<int32_t>{-3, -2, -1, -1, 1, 9},
                                 CreateConstant<int32_t>({{0, 2}, {1, 2}, {3, 4}}, element::i32),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::MEAN),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::u32,
                                 std::vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                                 ov::Shape{3, 2},
                                 ov::element::u32,
                                 std::vector<uint32_t>{3, 4, 4, 5, 8, 9},
                                 CreateConstant<int32_t>({{0, 2}, {1, 2}, {3, 4}}, element::i32),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::MEAN),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::f16,
                                 std::vector<float16>{-0.2, -0.6, -0.1, -0.4, -1.9, -1.8, -1., 1.5, 0.8, -0.7},
                                 ov::Shape{3, 2},
                                 ov::element::f16,
                                 std::vector<float16>{-1.05, -1.2, -1.0, -1.1, -0.1, 0.4},
                                 CreateConstant<int64_t>({{0, 2}, {1, 2}, {3, 4}}, element::i64),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::MEAN),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::i64,
                                 std::vector<int64_t>{-1, 2, 3, 4, -5, -6, -7, 8, 9, 10},
                                 ov::Shape{3, 2},
                                 ov::element::i64,
                                 std::vector<int64_t>{-3, -2, -1, -1, 1, 9},
                                 CreateConstant<int64_t>({{0, 2}, {1, 2}, {3, 4}}, element::i64),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::MEAN),
        EmbeddingBagPackedParams(ov::Shape{5, 2},
                                 ov::element::u8,
                                 std::vector<uint8_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                                 ov::Shape{3, 2},
                                 ov::element::u8,
                                 std::vector<uint8_t>{3, 4, 4, 5, 8, 9},
                                 CreateConstant<int64_t>({{0, 2}, {1, 2}, {3, 4}}, element::i64),
                                 ov::op::v15::EmbeddingBagPacked::Reduction::MEAN)),
    ReferenceEmbeddingBagPackedLayerTest::getTestCaseName);