forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiplugin_test.cpp
152 lines (127 loc) · 6.84 KB
/
iplugin_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "openvino/runtime/iplugin.hpp"
#include <gmock/gmock-spec-builders.h>
#include <gtest/gtest.h>
#include "openvino/core/node_output.hpp"
#include "openvino/core/node_vector.hpp"
#include "openvino/core/partial_shape.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/op/relu.hpp"
#include "openvino/op/result.hpp"
#include "openvino/runtime/compiled_model.hpp"
#include "openvino/runtime/iinfer_request.hpp"
#include "openvino/runtime/make_tensor.hpp"
#include "unit_test_utils/mocks/openvino/runtime/mock_icompiled_model.hpp"
#include "unit_test_utils/mocks/openvino/runtime/mock_iplugin.hpp"
#include "unit_test_utils/mocks/openvino/runtime/mock_isync_infer_request.hpp"
using namespace ::testing;
using namespace std;
class IPluginTest : public ::testing::Test {
private:
std::shared_ptr<ov::Model> create_model() {
auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 3, 2, 2});
param->set_friendly_name("Param");
param->output(0).set_names({"param", "name1", "name2", "name3"});
auto relu = std::make_shared<ov::op::v0::Relu>(param);
relu->set_friendly_name("ReLU");
relu->output(0).set_names({"relu"});
return std::make_shared<ov::Model>(ov::OutputVector{relu->output(0)}, ov::ParameterVector{param});
}
protected:
shared_ptr<ov::IPlugin> plugin;
shared_ptr<ov::MockIPlugin> mock_plugin_impl;
shared_ptr<ov::MockICompiledModel> mock_compiled_model;
shared_ptr<ov::MockISyncInferRequest> mock_infer_request;
std::shared_ptr<const ov::Model> model = create_model();
std::string pluginId;
void TearDown() override {
EXPECT_TRUE(Mock::VerifyAndClearExpectations(mock_plugin_impl.get()));
EXPECT_TRUE(Mock::VerifyAndClearExpectations(mock_compiled_model.get()));
EXPECT_TRUE(Mock::VerifyAndClearExpectations(mock_infer_request.get()));
}
void SetUp() override {
pluginId = "TEST";
mock_plugin_impl.reset(new ov::MockIPlugin());
mock_plugin_impl->set_device_name(pluginId);
plugin = std::static_pointer_cast<ov::IPlugin>(mock_plugin_impl);
mock_compiled_model = make_shared<ov::MockICompiledModel>(model, plugin);
ON_CALL(*mock_compiled_model.get(), inputs()).WillByDefault(ReturnRefOfCopy(model->inputs()));
ON_CALL(*mock_compiled_model.get(), outputs()).WillByDefault(ReturnRefOfCopy(model->outputs()));
mock_infer_request = make_shared<ov::MockISyncInferRequest>(mock_compiled_model);
}
void getInferRequestWithMockImplInside(std::shared_ptr<ov::IAsyncInferRequest>& request) {
std::shared_ptr<ov::ICompiledModel> compiled_model;
EXPECT_CALL(*mock_plugin_impl.get(), compile_model(A<const std::shared_ptr<const ov::Model>&>(), _))
.WillOnce(Return(mock_compiled_model));
EXPECT_CALL(*mock_compiled_model.get(), create_sync_infer_request()).WillOnce(Return(mock_infer_request));
ON_CALL(*mock_compiled_model.get(), create_infer_request()).WillByDefault([&]() {
return mock_compiled_model->create_infer_request_default();
});
compiled_model = plugin->compile_model(model, {});
ASSERT_NE(nullptr, compiled_model);
request = compiled_model->create_infer_request();
ASSERT_NE(nullptr, request);
}
};
MATCHER_P(blob_in_map_pointer_is_same, ref_blob, "") {
return reinterpret_cast<float*>(arg.begin()->second->buffer()) == reinterpret_cast<float*>(ref_blob->buffer());
}
TEST_F(IPluginTest, SetTensorWithIncorrectPortNames) {
ov::SoPtr<ov::ITensor> tensor = ov::make_tensor(ov::element::f32, {1, 3, 2, 2});
auto updated_param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 3, 2, 2});
updated_param->set_friendly_name("Param");
updated_param->output(0).set_names({"new_name"});
EXPECT_THROW(mock_infer_request->set_tensor(updated_param->output(0), tensor), ov::Exception);
updated_param->output(0).set_names({"param", "new_name"});
EXPECT_THROW(mock_infer_request->set_tensor(updated_param->output(0), tensor), ov::Exception);
updated_param->output(0).set_names({"new_name", "name2"});
EXPECT_THROW(mock_infer_request->set_tensor(updated_param->output(0), tensor), ov::Exception);
}
TEST_F(IPluginTest, SetTensorWithCorrectPortNames) {
ov::SoPtr<ov::ITensor> tensor = ov::make_tensor(ov::element::f32, {1, 3, 2, 2});
auto updated_param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 3, 2, 2});
updated_param->set_friendly_name("Param");
updated_param->output(0).set_names({"param"});
EXPECT_NO_THROW(mock_infer_request->set_tensor(updated_param->output(0), tensor));
updated_param->output(0).set_names({"name1", "param"});
EXPECT_NO_THROW(mock_infer_request->set_tensor(updated_param->output(0), tensor));
updated_param->output(0).set_names({"name1", "name2"});
EXPECT_NO_THROW(mock_infer_request->set_tensor(updated_param->output(0), tensor));
updated_param->output(0).set_names({"param", "name1", "name2"});
EXPECT_NO_THROW(mock_infer_request->set_tensor(updated_param->output(0), tensor));
updated_param->output(0).set_names({"param", "name1", "name2", "name3"});
EXPECT_NO_THROW(mock_infer_request->set_tensor(updated_param->output(0), tensor));
}
TEST_F(IPluginTest, failToSetTensorWithIncorrectPort) {
auto incorrect_param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 2});
ov::SoPtr<ov::ITensor> tensor = ov::make_tensor(ov::element::f32, {1, 1, 1, 1});
std::string refError = "Cannot find tensor for port";
std::shared_ptr<ov::IAsyncInferRequest> infer_request;
getInferRequestWithMockImplInside(infer_request);
try {
infer_request->set_tensor(incorrect_param->output(0), tensor);
} catch (ov::Exception& ex) {
ASSERT_TRUE(std::string{ex.what()}.find(refError) != std::string::npos)
<< "\tExpected: " << refError << "\n\tActual: " << ex.what();
}
}
TEST_F(IPluginTest, failToSetEmptyITensor) {
ov::SoPtr<ov::ITensor> tensor;
std::string refError = "Failed to set tensor. ";
std::shared_ptr<ov::IAsyncInferRequest> infer_request;
getInferRequestWithMockImplInside(infer_request);
try {
infer_request->set_tensor(model->input(0), tensor);
} catch (ov::Exception& ex) {
ASSERT_TRUE(std::string{ex.what()}.find(refError) != std::string::npos)
<< "\tExpected: " << refError << "\n\tActual: " << ex.what();
}
}
TEST_F(IPluginTest, SetTensorWithCorrectPort) {
ov::SoPtr<ov::ITensor> tensor = ov::make_tensor(ov::element::f32, {1, 3, 2, 2});
std::shared_ptr<ov::IAsyncInferRequest> infer_request;
getInferRequestWithMockImplInside(infer_request);
EXPECT_NO_THROW(infer_request->set_tensor(model->input(0), tensor));
}