Refactor EinsumLayerTest, EyeLayerTest (#20288)

* Refactor EinsumLayerTest

* Refator EyeLayerTest

* Apply comments

* Fix

---------

Co-authored-by: Vitaliy Urusovskij <vitaliy.urusovskij@intel.com>
This commit is contained in:
Oleg Pipikin
2023-10-17 17:35:24 +02:00
committed by GitHub
parent 1e7977332b
commit a5fb3823f6
8 changed files with 255 additions and 35 deletions

View File

@@ -4,35 +4,34 @@
#include <vector>
#include "single_layer_tests/einsum.hpp"
using namespace ngraph::helpers;
using namespace LayerTestsDefinitions;
#include "single_op_tests/einsum.hpp"
namespace {
const std::vector<InferenceEngine::Precision> precisions = {
InferenceEngine::Precision::FP32,
InferenceEngine::Precision::FP16
using ov::test::EinsumLayerTest;
const std::vector<ov::element::Type> model_types = {
ov::element::f32,
ov::element::f16
};
const std::vector<EinsumEquationWithInput> equationsWithInput = {
{ "ij->ji", {{{1, 2}}} }, // transpose 2d
{ "ijk->kij", { {1, 2, 3} } }, // transpose 3d
{ "ij->i", { {2, 3} } }, // reduce
{ "ab,cd->abcd", { { 1, 2}, {3, 4} } }, // no reduction
{ "ab,bc->ac", { {2, 3}, {3, 2} } }, // matrix multiplication
{ "ab,bcd,bc->ca", { {2, 4}, {4, 3, 1}, {4, 3} } }, // multiple multiplications
{ "kii->ki", { {1, 3, 3} } }, // diagonal
{ "abbac,bad->ad", { {2, 3, 3, 2, 4}, {3, 2, 1} } }, // diagonal and multiplication with repeated labels
{ "a...->...a", { {2, 2, 3} } }, // transpose with ellipsis
{ "a...->...", { {2, 2, 3} } }, // reduce with ellipsis
{ "ab...,...->ab...", { {2, 2, 3}, {1} } }, // multiply by scalar
{ "a...j,j...->a...", { {1, 1, 4, 3}, {3, 4, 2, 1} } } // complex multiplication
const std::vector<ov::test::EinsumEquationWithInput> equationsWithInput = {
{ "ij->ji", ov::test::static_shapes_to_test_representation({ {1, 2} }) }, // transpose 2d
{ "ijk->kij", ov::test::static_shapes_to_test_representation({ {1, 2, 3} }) }, // transpose 3d
{ "ij->i", ov::test::static_shapes_to_test_representation({ {2, 3} }) }, // reduce
{ "ab,cd->abcd", ov::test::static_shapes_to_test_representation({ { 1, 2}, {3, 4} }) }, // no reduction
{ "ab,bc->ac", ov::test::static_shapes_to_test_representation({ {2, 3}, {3, 2} }) }, // matrix multiplication
{ "ab,bcd,bc->ca", ov::test::static_shapes_to_test_representation({ {2, 4}, {4, 3, 1}, {4, 3} }) }, // multiple multiplications
{ "kii->ki", ov::test::static_shapes_to_test_representation({ {1, 3, 3} }) }, // diagonal
{ "abbac,bad->ad", ov::test::static_shapes_to_test_representation({ {2, 3, 3, 2, 4}, {3, 2, 1} }) }, // diagonal and multiplication with repeated labels
{ "a...->...a", ov::test::static_shapes_to_test_representation({ {2, 2, 3} }) }, // transpose with ellipsis
{ "a...->...", ov::test::static_shapes_to_test_representation({ {2, 2, 3} }) }, // reduce with ellipsis
{ "ab...,...->ab...", ov::test::static_shapes_to_test_representation({ {2, 2, 3}, {1} }) }, // multiply by scalar
{ "a...j,j...->a...", ov::test::static_shapes_to_test_representation({ {1, 1, 4, 3}, {3, 4, 2, 1} }) } // complex multiplication
};
INSTANTIATE_TEST_SUITE_P(smoke_Einsum,
EinsumLayerTest,
::testing::Combine(::testing::ValuesIn(precisions),
::testing::Combine(::testing::ValuesIn(model_types),
::testing::ValuesIn(equationsWithInput),
::testing::Values(ov::test::utils::DEVICE_GPU)),
EinsumLayerTest::getTestCaseName);

View File

@@ -2,19 +2,18 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "shared_test_classes/single_layer/eye.hpp"
using namespace LayerTestsDefinitions;
#include "single_op_tests/eye.hpp"
namespace {
using ov::test::EyeLayerTest;
TEST_P(EyeLayerTest, CompareWithRefs) {
SKIP_IF_CURRENT_TEST_IS_DISABLED()
Run();
}
const std::vector<ov::element::Type_t> netPrecisions =
{ElementType::f32, ElementType::f16, ElementType::i32, ElementType::i8, ElementType::u8, ElementType::i64};
const std::vector<ov::element::Type> model_types = {
ov::element::f32,
ov::element::f16,
ov::element::i32,
ov::element::i8,
ov::element::u8,
ov::element::i64};
const std::vector<std::vector<int>> eyePars = {
// rows, cols, diag_shift
@@ -40,7 +39,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_Eye2D_WithNonScalar_Test,
::testing::Combine(::testing::ValuesIn(std::vector<std::vector<ov::Shape>>{{{1}, {1}, {1}}}),
::testing::ValuesIn(emptyBatchShape),
::testing::ValuesIn(eyePars),
::testing::ValuesIn(netPrecisions),
::testing::ValuesIn(model_types),
::testing::Values(ov::test::utils::DEVICE_GPU)),
EyeLayerTest::getTestCaseName);
@@ -50,7 +49,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_Eye_1DBatch_Test,
{{1}, {1}, {1}, {1}}}),
::testing::ValuesIn(batchShapes1D),
::testing::ValuesIn(eyePars),
::testing::ValuesIn(netPrecisions),
::testing::ValuesIn(model_types),
::testing::Values(ov::test::utils::DEVICE_GPU)),
EyeLayerTest::getTestCaseName);
@@ -60,7 +59,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_Eye_2DBatch_Test,
{{1}, {1}, {1}, {2}}}),
::testing::ValuesIn(batchShapes2D),
::testing::ValuesIn(eyePars),
::testing::ValuesIn(netPrecisions),
::testing::ValuesIn(model_types),
::testing::Values(ov::test::utils::DEVICE_GPU)),
EyeLayerTest::getTestCaseName);
@@ -70,7 +69,7 @@ INSTANTIATE_TEST_SUITE_P(smoke_Eye_3DBatch_Test,
{{1}, {1}, {1}, {3}}}),
::testing::ValuesIn(batchShapes3D),
::testing::ValuesIn(eyePars),
::testing::ValuesIn(netPrecisions),
::testing::ValuesIn(model_types),
::testing::Values(ov::test::utils::DEVICE_GPU)),
EyeLayerTest::getTestCaseName);

View File

@@ -0,0 +1,15 @@
// Copyright (C) 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "shared_test_classes/single_op/einsum.hpp"
namespace ov {
namespace test {
TEST_P(EinsumLayerTest, Inference) {
run();
}
} // namespace test
} // namespace ov

View File

@@ -0,0 +1,14 @@
// Copyright (C) 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "shared_test_classes/single_op/eye.hpp"
namespace ov {
namespace test {
TEST_P(EyeLayerTest, Inference) {
run();
}
} // namespace test
} // namespace ov

View File

@@ -0,0 +1,34 @@
// Copyright (C) 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <string>
#include <tuple>
#include <vector>
#include "shared_test_classes/base/ov_subgraph.hpp"
namespace ov {
namespace test {
typedef std::tuple<
std::string, // Equation
std::vector<InputShape> // Input shapes
> EinsumEquationWithInput;
typedef std::tuple<
ov::element::Type, // Model type
EinsumEquationWithInput, // Equation with corresponding input shapes
std::string // Device name
> EinsumLayerTestParamsSet;
class EinsumLayerTest : public testing::WithParamInterface<EinsumLayerTestParamsSet>,
virtual public ov::test::SubgraphBaseTest {
public:
static std::string getTestCaseName(const testing::TestParamInfo<EinsumLayerTestParamsSet>& obj);
protected:
void SetUp() override;
};
} // namespace test
} // namespace ov

View File

@@ -0,0 +1,24 @@
// Copyright (C) 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "shared_test_classes/base/ov_subgraph.hpp"
namespace ov {
namespace test {
using EyeLayerTestParams = std::tuple<
std::vector<ov::Shape>, // eye shape
std::vector<int>, // output batch shape
std::vector<int>, // eye params (rows, cols, diag_shift)
ov::element::Type, // Model type
std::string>; // Device name
class EyeLayerTest : public testing::WithParamInterface<EyeLayerTestParams>,
virtual public ov::test::SubgraphBaseTest {
public:
static std::string getTestCaseName(testing::TestParamInfo<EyeLayerTestParams> obj);
void SetUp() override;
};
} // namespace test
} // namespace ov

View File

@@ -0,0 +1,61 @@
// Copyright (C) 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "shared_test_classes/single_op/einsum.hpp"
namespace ov {
namespace test {
std::string EinsumLayerTest::getTestCaseName(const testing::TestParamInfo<EinsumLayerTestParamsSet>& obj) {
EinsumEquationWithInput equation_with_input;
ov::element::Type model_type;
std::string targetDevice;
std::tie(model_type, equation_with_input, targetDevice) = obj.param;
std::string equation;
std::vector<InputShape> shapes;
std::tie(equation, shapes) = equation_with_input;
std::ostringstream result;
result << "IS=(";
for (size_t i = 0lu; i < shapes.size(); i++) {
result << ov::test::utils::partialShape2str({shapes[i].first}) << (i < shapes.size() - 1lu ? "_" : "");
}
result << ")_TS=";
for (size_t i = 0lu; i < shapes.front().second.size(); i++) {
result << "{";
for (size_t j = 0lu; j < shapes.size(); j++) {
result << ov::test::utils::vec2str(shapes[j].second[i]) << (j < shapes.size() - 1lu ? "_" : "");
}
result << "}_";
}
result << "PRC=" << model_type.get_type_name() << "_";
result << "Eq=" << equation << "_";
result << "trgDev=" << targetDevice;
return result.str();
}
void EinsumLayerTest::SetUp() {
EinsumEquationWithInput equation_with_input;
ov::element::Type model_type;
std::tie(model_type, equation_with_input, targetDevice) = this->GetParam();
std::string equation;
std::vector<InputShape> shapes;
std::tie(equation, shapes) = equation_with_input;
init_input_shapes(shapes);
ov::ParameterVector params;
ov::OutputVector param_outs;
for (const auto& shape : inputDynamicShapes) {
auto param = std::make_shared<ov::op::v0::Parameter>(model_type, shape);
params.push_back(param);
param_outs.push_back(param);
}
auto einsum = std::make_shared<ov::op::v7::Einsum>(param_outs, equation);
auto result = std::make_shared<ov::op::v0::Result>(einsum);
function = std::make_shared<ov::Model>(result, params, "einsum");
}
} // namespace test
} // namespace ov

View File

@@ -0,0 +1,74 @@
// Copyright (C) 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "shared_test_classes/single_op/eye.hpp"
#include "common_test_utils/ov_tensor_utils.hpp"
#include "openvino/pass/constant_folding.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/result.hpp"
#include "openvino/op/eye.hpp"
namespace ov {
namespace test {
std::string EyeLayerTest::getTestCaseName(testing::TestParamInfo<EyeLayerTestParams> obj) {
std::string td;
std::vector<ov::Shape> input_shapes;
ov::element::Type model_type;
std::vector<int> out_batch_shape;
std::vector<int> eye_par;
std::tie(input_shapes, out_batch_shape, eye_par, model_type, td) = obj.param;
std::ostringstream result;
result << "EyeTest_";
result << "IS=(";
for (const auto& shape : input_shapes) {
result << ov::test::utils::partialShape2str({shape}) << "_";
}
result << ")";
result << "rowNum=" << eye_par[0] << "_";
result << "colNum=" << eye_par[1] << "_";
result << "diagShift=" << eye_par[2] << "_";
result << "batchShape=" << ov::test::utils::vec2str(out_batch_shape) << "_";
result << model_type << "_";
result << std::to_string(obj.index);
return result.str();
}
void EyeLayerTest::SetUp() {
std::vector<ov::Shape> input_shapes;
int row_num, col_num;
int shift;
std::vector<int> out_batch_shape;
ov::element::Type model_type;
std::vector<int> eye_par;
std::tie(input_shapes, out_batch_shape, eye_par, model_type, targetDevice) = this->GetParam();
row_num = eye_par[0];
col_num = eye_par[1];
shift = eye_par[2];
std::shared_ptr<ov::op::v9::Eye> eye_operation;
auto rows_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, input_shapes[0], &row_num);
rows_const->set_friendly_name("rows");
auto cols_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, input_shapes[1], &col_num);
cols_const->set_friendly_name("cols");
auto diag_const = std::make_shared<ov::op::v0::Constant>(ov::element::i32, input_shapes[2], &shift);
diag_const->set_friendly_name("diagInd");
if (!out_batch_shape.empty() && out_batch_shape[0] != 0) {
auto batch_shape_par = std::make_shared<ov::op::v0::Constant>(ov::element::i32,
ov::Shape{out_batch_shape.size()},
out_batch_shape.data());
batch_shape_par->set_friendly_name("batchShape");
eye_operation = std::make_shared<ov::op::v9::Eye>(rows_const, cols_const, diag_const, batch_shape_par, model_type);
} else {
eye_operation = std::make_shared<ov::op::v9::Eye>(rows_const, cols_const, diag_const, model_type);
}
// Without this call the eye operation will be calculated by CPU and substituted by Constant operator
ov::pass::disable_constant_folding(eye_operation);
auto result = std::make_shared<ov::op::v0::Result>(eye_operation);
function = std::make_shared<ov::Model>(result, ov::ParameterVector(), "eye");
}
} // namespace test
} // namespace ov