|
| 1 | +// Copyright (C) 2018-2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include <regex> |
| 6 | + |
| 7 | +#include "common_test_utils/node_builders/constant.hpp" |
| 8 | +#include "common_test_utils/node_builders/fake_quantize.hpp" |
| 9 | +#include "common_test_utils/node_builders/reshape.hpp" |
| 10 | +#include "openvino/openvino.hpp" |
| 11 | +#include "shared_test_classes/base/ov_subgraph.hpp" |
| 12 | + |
| 13 | +namespace ov { |
| 14 | +namespace test { |
| 15 | + |
| 16 | +enum class FQInterval { U8, I8 }; |
| 17 | +inline std::ostream& operator<<(std::ostream& os, FQInterval interval) { |
| 18 | + switch (interval) { |
| 19 | + case FQInterval::U8: |
| 20 | + os << "U8"; |
| 21 | + break; |
| 22 | + case FQInterval::I8: |
| 23 | + os << "I8"; |
| 24 | + break; |
| 25 | + default: |
| 26 | + OPENVINO_THROW("Unknown FQInterval"); |
| 27 | + } |
| 28 | + return os; |
| 29 | +} |
| 30 | + |
| 31 | +typedef std::tuple<InputShape, InputShape, FQInterval, FQInterval> QuantizedMatMulsWithSharedWeightsParans; |
| 32 | + |
| 33 | +/* This test verifies the correctness of the hash function computation for the shared weights. |
| 34 | + Specifically, it checks that when one op requires compensations computation and second one does not, |
| 35 | + the resulting hashes are not identical, and the weights are repacked for each op separately |
| 36 | +*/ |
| 37 | +class QuantizedMatMulsWithSharedWeightsTest |
| 38 | + : public testing::WithParamInterface<QuantizedMatMulsWithSharedWeightsParans>, |
| 39 | + virtual public SubgraphBaseTest { |
| 40 | +public: |
| 41 | + static std::string getTestCaseName(const testing::TestParamInfo<QuantizedMatMulsWithSharedWeightsParans>& obj) { |
| 42 | + InputShape shape1; |
| 43 | + InputShape shape2; |
| 44 | + FQInterval interval1; |
| 45 | + FQInterval interval2; |
| 46 | + std::tie(shape1, shape2, interval1, interval2) = obj.param; |
| 47 | + std::ostringstream result; |
| 48 | + result << "IS1=" << shape1 << "IS2=" << shape2 << "FQInterval1=" << interval1 << "FQInterval2=" << interval2; |
| 49 | + return result.str(); |
| 50 | + } |
| 51 | + |
| 52 | + void SetUp() override { |
| 53 | + targetDevice = ov::test::utils::DEVICE_CPU; |
| 54 | + abs_threshold = 1e-4; |
| 55 | + |
| 56 | + InputShape shape1; |
| 57 | + InputShape shape2; |
| 58 | + FQInterval interval1; |
| 59 | + FQInterval interval2; |
| 60 | + std::tie(shape1, shape2, interval1, interval2) = this->GetParam(); |
| 61 | + init_input_shapes({shape1, shape2}); |
| 62 | + |
| 63 | + const auto weights = ov::test::utils::make_constant(ov::element::i8, {16, 16}); |
| 64 | + const auto convert = std::make_shared<ov::op::v0::Convert>(weights, ov::element::f32); |
| 65 | + const auto scale = ov::test::utils::make_constant(ov::element::f32, {16, 1}, ov::test::utils::InputGenerateData(0, 1, 5)); |
| 66 | + const auto mul = std::make_shared<ov::op::v1::Multiply>(convert, scale); |
| 67 | + |
| 68 | + auto build_fq = [](const ov::Output<ov::Node>& parent, FQInterval interval_type) { |
| 69 | + const auto low = interval_type == FQInterval::I8 ? std::vector<float>{-12.8f} : std::vector<float>{0.f}; |
| 70 | + const auto high = interval_type == FQInterval::I8 ? std::vector<float>{12.7f} : std::vector<float>{25.5f}; |
| 71 | + return ov::test::utils::make_fake_quantize(parent, ov::element::f32, 256, {1, 1, 1, 1}, low, high, low, high); |
| 72 | + }; |
| 73 | + |
| 74 | + const auto param1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, inputDynamicShapes[0]); |
| 75 | + const auto fq1 = build_fq(param1, interval1); |
| 76 | + const auto mm1 = std::make_shared<ov::op::v0::MatMul>(fq1, mul, false, true); |
| 77 | + |
| 78 | + const auto param2 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, inputDynamicShapes[1]); |
| 79 | + const auto fq2 = build_fq(param2, interval2); |
| 80 | + const auto mm2 = std::make_shared<ov::op::v0::MatMul>(fq2, mul, false, true); |
| 81 | + |
| 82 | + function = std::make_shared<ov::Model>(ov::OutputVector{mm1, mm2}, ov::ParameterVector{param1, param2}); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +TEST_P(QuantizedMatMulsWithSharedWeightsTest, CompareWithRefs) { |
| 87 | + run(); |
| 88 | +} |
| 89 | + |
| 90 | +namespace { |
| 91 | + |
| 92 | +std::vector<InputShape> shapes1{{{-1, -1, -1, 16}, {{1, 1, 15, 16}, {1, 1, 12, 16}, {1, 1, 15, 16}}}}; |
| 93 | +std::vector<InputShape> shapes2{{{-1, -1, -1, 16}, {{1, 1, 12, 16}, {1, 1, 15, 16}, {1, 1, 12, 16}}}}; |
| 94 | +INSTANTIATE_TEST_SUITE_P(smoke_CustomTest, QuantizedMatMulsWithSharedWeightsTest, |
| 95 | + ::testing::Combine( |
| 96 | + ::testing::ValuesIn(shapes1), |
| 97 | + ::testing::ValuesIn(shapes2), |
| 98 | + ::testing::Values(FQInterval::U8, FQInterval::I8), |
| 99 | + ::testing::Values(FQInterval::U8, FQInterval::I8)), |
| 100 | + QuantizedMatMulsWithSharedWeightsTest::getTestCaseName); |
| 101 | +} // namespace |
| 102 | +} // namespace test |
| 103 | +} // namespace ov |
0 commit comments