-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathcustom_node_output_allocator_test.cpp
167 lines (143 loc) · 7.11 KB
/
custom_node_output_allocator_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//*****************************************************************************
// Copyright 2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "../dags/custom_node_output_allocator.hpp"
#include "../precision.hpp"
#include "../shape.hpp"
using namespace ovms;
class NodeLibraryCheckingReleaseCalled {
public:
static bool releaseBufferCalled;
static int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount);
static int deinitialize(void* customNodeLibraryInternalManager);
static int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager);
static int getInputsInfo(struct CustomNodeTensorInfo** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager);
static int getOutputsInfo(struct CustomNodeTensorInfo** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager);
static int release(void* ptr, void* customNodeLibraryInternalManager);
};
int NodeLibraryCheckingReleaseCalled::initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
return 5;
}
int NodeLibraryCheckingReleaseCalled::deinitialize(void* customNodeLibraryInternalManager) {
return 6;
}
int NodeLibraryCheckingReleaseCalled::execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
return 1;
}
int NodeLibraryCheckingReleaseCalled::getInputsInfo(struct CustomNodeTensorInfo** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
return 2;
}
int NodeLibraryCheckingReleaseCalled::getOutputsInfo(struct CustomNodeTensorInfo** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
return 3;
}
int NodeLibraryCheckingReleaseCalled::release(void* ptr, void* customNodeLibraryInternalManager) {
releaseBufferCalled = true;
return 0;
}
bool NodeLibraryCheckingReleaseCalled::releaseBufferCalled = false;
class CustomNodeOutputAllocatorCheckingFreeCalled {
// Since test is copying/moving the allocator to ov::Tensor class
// we need to use counter to ensure how many times deallocate was called
std::shared_ptr<int> freeCallCount{std::make_shared<int>(0)};
std::shared_ptr<CustomNodeOutputAllocator> allocImpl;
public:
CustomNodeOutputAllocatorCheckingFreeCalled(struct CustomNodeTensor tensor, NodeLibrary nodeLibrary, void* customNodeLibraryInternalManager) :
allocImpl(std::make_shared<CustomNodeOutputAllocator>(tensor, nodeLibrary, customNodeLibraryInternalManager)) {
}
~CustomNodeOutputAllocatorCheckingFreeCalled() {
EXPECT_EQ(*freeCallCount, 1);
}
void* allocate(const size_t bytes, const size_t alignment) {
return allocImpl->allocate(bytes, alignment);
}
void deallocate(void* handle, const size_t bytes, size_t alignment) {
allocImpl->deallocate(handle, bytes, alignment);
(*freeCallCount)++;
}
bool is_equal(const CustomNodeOutputAllocatorCheckingFreeCalled& other) const {
return allocImpl->is_equal(*other.allocImpl);
}
};
TEST(CustomNodeOutputAllocator, TensorDeallocationCallsReleaseBuffer) {
unsigned int elementsCount = 10;
std::vector<float> data(elementsCount);
CustomNodeTensor tensor{
"name",
reinterpret_cast<uint8_t*>(data.data()),
sizeof(float) * elementsCount,
reinterpret_cast<uint64_t*>(&elementsCount),
1,
CustomNodeTensorPrecision::FP32};
NodeLibrary library{
NodeLibraryCheckingReleaseCalled::initialize,
NodeLibraryCheckingReleaseCalled::deinitialize,
NodeLibraryCheckingReleaseCalled::execute,
NodeLibraryCheckingReleaseCalled::getInputsInfo,
NodeLibraryCheckingReleaseCalled::getOutputsInfo,
NodeLibraryCheckingReleaseCalled::release};
void* customNodeLibraryInternalManager = nullptr;
CustomNodeOutputAllocatorCheckingFreeCalled alloc(tensor, library, customNodeLibraryInternalManager);
EXPECT_FALSE(NodeLibraryCheckingReleaseCalled::releaseBufferCalled);
{
auto elemType = ovmsPrecisionToIE2Precision(Precision::FP32);
shape_t shape{data.size()};
auto tensorIE2 = std::make_shared<ov::Tensor>(elemType, shape, alloc);
}
EXPECT_TRUE(NodeLibraryCheckingReleaseCalled::releaseBufferCalled);
}
int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
return 5;
}
int deinitialize(void* customNodeLibraryInternalManager) {
return 6;
}
int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
return 1;
}
int getInputsInfo(struct CustomNodeTensorInfo** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
return 2;
}
int getOutputsInfo(struct CustomNodeTensorInfo** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
return 3;
}
int release(void* ptr, void* customNodeLibraryInternalManager) {
return 0;
}
TEST(CustomNodeOutputAllocator, TensorReturnsCorrectPointer) {
unsigned int elementsCount = 10;
std::vector<float> data(elementsCount);
CustomNodeTensor tensor{
"name",
reinterpret_cast<uint8_t*>(data.data()),
sizeof(float) * elementsCount,
reinterpret_cast<uint64_t*>(&elementsCount),
1,
CustomNodeTensorPrecision::FP32};
NodeLibrary library{
initialize,
deinitialize,
execute,
getInputsInfo,
getOutputsInfo,
release};
void* customNodeLibraryInternalManager = nullptr;
CustomNodeOutputAllocator alloc(tensor, library, customNodeLibraryInternalManager);
auto elemType = ovmsPrecisionToIE2Precision(Precision::FP32);
shape_t shape{10};
auto tensorIE2 = std::make_shared<ov::Tensor>(elemType, shape, alloc);
EXPECT_EQ(tensorIE2->data(), tensor.data);
}