-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathovms_py_tensor_test.cpp
197 lines (173 loc) · 9.4 KB
/
ovms_py_tensor_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//*****************************************************************************
// Copyright 2023 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 <utility>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <pybind11/pybind11.h>
#include "../../ovms_py_tensor.hpp"
using namespace ovms;
namespace py = pybind11;
TEST(OvmsPyTensor, BuildKnownFormatMultiDimShape) {
py::ssize_t INPUT_BUFFER_SIZE = 1 * 3 * 300 * 300 * sizeof(float);
std::string data(INPUT_BUFFER_SIZE, '1');
void* ptr = data.data();
std::vector<py::ssize_t> shape{1, 3, 300, 300};
std::string datatype = "FP32";
OvmsPyTensor ovmsPyTensor("input", ptr, shape, datatype, INPUT_BUFFER_SIZE);
std::vector<py::ssize_t> expectedStrides{1080000, 360000, 1200, 4};
std::string expectedFormat = datatypeToBufferFormat.at(datatype);
size_t expectedItemsize = bufferFormatToItemsize.at(expectedFormat);
EXPECT_EQ(ovmsPyTensor.name, "input");
EXPECT_EQ(ovmsPyTensor.ptr, ptr);
EXPECT_EQ(ovmsPyTensor.userShape, shape);
EXPECT_EQ(ovmsPyTensor.bufferShape, shape);
EXPECT_EQ(ovmsPyTensor.strides, expectedStrides);
EXPECT_EQ(ovmsPyTensor.format, expectedFormat);
EXPECT_EQ(ovmsPyTensor.datatype, datatype);
EXPECT_EQ(ovmsPyTensor.itemsize, expectedItemsize);
OvmsPyTensor recreatedTensor("input", py::buffer_info{
ovmsPyTensor.ptr,
ovmsPyTensor.itemsize,
ovmsPyTensor.format,
ovmsPyTensor.ndim,
ovmsPyTensor.bufferShape,
ovmsPyTensor.strides});
EXPECT_EQ(recreatedTensor.name, "input");
EXPECT_EQ(recreatedTensor.ptr, ovmsPyTensor.ptr);
EXPECT_EQ(recreatedTensor.userShape, ovmsPyTensor.userShape);
EXPECT_EQ(recreatedTensor.bufferShape, ovmsPyTensor.bufferShape);
EXPECT_EQ(recreatedTensor.strides, ovmsPyTensor.strides);
EXPECT_EQ(recreatedTensor.format, ovmsPyTensor.format);
EXPECT_EQ(recreatedTensor.datatype, ovmsPyTensor.datatype);
EXPECT_EQ(recreatedTensor.itemsize, ovmsPyTensor.itemsize);
EXPECT_EQ(recreatedTensor.size, ovmsPyTensor.size);
}
TEST(OvmsPyTensor, BuildKnownFormatSingleDimShape) {
py::ssize_t INPUT_BUFFER_SIZE = 1 * 3 * 300 * 300 * sizeof(float);
std::string data(INPUT_BUFFER_SIZE, '1');
void* ptr = data.data();
std::vector<py::ssize_t> shape{1 * 3 * 300 * 300};
std::string datatype = "FP32";
OvmsPyTensor ovmsPyTensor("input", ptr, shape, datatype, INPUT_BUFFER_SIZE);
std::vector<py::ssize_t> expectedStrides{sizeof(float)};
std::string expectedFormat = datatypeToBufferFormat.at(datatype);
size_t expectedItemsize = bufferFormatToItemsize.at(expectedFormat);
EXPECT_EQ(ovmsPyTensor.name, "input");
EXPECT_EQ(ovmsPyTensor.ptr, ptr);
EXPECT_EQ(ovmsPyTensor.userShape, shape);
EXPECT_EQ(ovmsPyTensor.bufferShape, shape);
EXPECT_EQ(ovmsPyTensor.strides, expectedStrides);
EXPECT_EQ(ovmsPyTensor.format, expectedFormat);
EXPECT_EQ(ovmsPyTensor.datatype, datatype);
EXPECT_EQ(ovmsPyTensor.itemsize, expectedItemsize);
EXPECT_EQ(ovmsPyTensor.size, INPUT_BUFFER_SIZE);
OvmsPyTensor recreatedTensor("input", py::buffer_info{
ovmsPyTensor.ptr,
ovmsPyTensor.itemsize,
ovmsPyTensor.format,
ovmsPyTensor.ndim,
ovmsPyTensor.bufferShape,
ovmsPyTensor.strides});
EXPECT_EQ(recreatedTensor.name, "input");
EXPECT_EQ(recreatedTensor.ptr, ovmsPyTensor.ptr);
EXPECT_EQ(recreatedTensor.userShape, ovmsPyTensor.userShape);
EXPECT_EQ(recreatedTensor.bufferShape, ovmsPyTensor.bufferShape);
EXPECT_EQ(recreatedTensor.strides, ovmsPyTensor.strides);
EXPECT_EQ(recreatedTensor.format, ovmsPyTensor.format);
EXPECT_EQ(recreatedTensor.datatype, ovmsPyTensor.datatype);
EXPECT_EQ(recreatedTensor.itemsize, ovmsPyTensor.itemsize);
EXPECT_EQ(recreatedTensor.size, ovmsPyTensor.size);
}
TEST(OvmsPyTensor, BuildUnknownFormatSingleDimShape) {
py::ssize_t INPUT_BUFFER_SIZE = 3 * 1024;
std::string data(INPUT_BUFFER_SIZE, '1');
void* ptr = data.data();
std::vector<py::ssize_t> shape{3};
std::string datatype = "my_string_type";
OvmsPyTensor ovmsPyTensor("input", ptr, shape, datatype, INPUT_BUFFER_SIZE);
std::string expectedFormat = datatypeToBufferFormat.at("UINT8");
size_t expectedItemsize = bufferFormatToItemsize.at(expectedFormat);
// For unknown format the underlying buffer is UINT8 1-D with shape (num_bytes,) and strides (1,)
std::vector<py::ssize_t> expectedBufferShape{INPUT_BUFFER_SIZE};
std::vector<py::ssize_t> expectedStrides{1};
EXPECT_EQ(ovmsPyTensor.name, "input");
EXPECT_EQ(ovmsPyTensor.ptr, ptr);
EXPECT_EQ(ovmsPyTensor.userShape, shape);
EXPECT_EQ(ovmsPyTensor.bufferShape, expectedBufferShape);
EXPECT_EQ(ovmsPyTensor.strides, expectedStrides);
EXPECT_EQ(ovmsPyTensor.format, expectedFormat);
EXPECT_EQ(ovmsPyTensor.datatype, datatype);
EXPECT_EQ(ovmsPyTensor.itemsize, expectedItemsize);
EXPECT_EQ(ovmsPyTensor.size, INPUT_BUFFER_SIZE);
OvmsPyTensor recreatedTensor("input", py::buffer_info{
ovmsPyTensor.ptr,
ovmsPyTensor.itemsize,
ovmsPyTensor.format,
ovmsPyTensor.ndim,
ovmsPyTensor.bufferShape,
ovmsPyTensor.strides});
EXPECT_EQ(recreatedTensor.name, "input");
EXPECT_EQ(recreatedTensor.ptr, ovmsPyTensor.ptr);
// When creating from another buffer we assign bufferShape to userShape
EXPECT_EQ(recreatedTensor.userShape, ovmsPyTensor.bufferShape);
EXPECT_EQ(recreatedTensor.bufferShape, ovmsPyTensor.bufferShape);
EXPECT_EQ(recreatedTensor.strides, ovmsPyTensor.strides);
EXPECT_EQ(recreatedTensor.format, ovmsPyTensor.format);
// We cannot recreate known datatype only from buffer info. For unknown types we assume to UINT8
EXPECT_EQ(recreatedTensor.datatype, "UINT8");
EXPECT_EQ(recreatedTensor.itemsize, ovmsPyTensor.itemsize);
EXPECT_EQ(recreatedTensor.size, ovmsPyTensor.size);
}
TEST(OvmsPyTensor, BuildUnknownFormatMultiDimShape) {
py::ssize_t INPUT_BUFFER_SIZE = 10 * 3 * 1024;
std::string data(INPUT_BUFFER_SIZE, '1');
void* ptr = data.data();
std::vector<py::ssize_t> shape{10, 3};
std::string datatype = "my_string_type";
OvmsPyTensor ovmsPyTensor("input", ptr, shape, datatype, INPUT_BUFFER_SIZE);
std::string expectedFormat = datatypeToBufferFormat.at("UINT8");
size_t expectedItemsize = bufferFormatToItemsize.at(expectedFormat);
// For unknown format the underlying buffer is UINT8 1-D with shape (num_bytes,) and strides (1,)
std::vector<py::ssize_t> expectedBufferShape{INPUT_BUFFER_SIZE};
std::vector<py::ssize_t> expectedStrides{1};
EXPECT_EQ(ovmsPyTensor.name, "input");
EXPECT_EQ(ovmsPyTensor.ptr, ptr);
EXPECT_EQ(ovmsPyTensor.userShape, shape);
EXPECT_EQ(ovmsPyTensor.bufferShape, expectedBufferShape);
EXPECT_EQ(ovmsPyTensor.strides, expectedStrides);
EXPECT_EQ(ovmsPyTensor.format, expectedFormat);
EXPECT_EQ(ovmsPyTensor.datatype, datatype);
EXPECT_EQ(ovmsPyTensor.itemsize, expectedItemsize);
EXPECT_EQ(ovmsPyTensor.size, INPUT_BUFFER_SIZE);
OvmsPyTensor recreatedTensor("input", py::buffer_info{
ovmsPyTensor.ptr,
ovmsPyTensor.itemsize,
ovmsPyTensor.format,
ovmsPyTensor.ndim,
ovmsPyTensor.bufferShape,
ovmsPyTensor.strides});
EXPECT_EQ(recreatedTensor.name, "input");
EXPECT_EQ(recreatedTensor.ptr, ovmsPyTensor.ptr);
// When creating from another buffer we assign bufferShape to userShape
EXPECT_EQ(recreatedTensor.userShape, ovmsPyTensor.bufferShape);
EXPECT_EQ(recreatedTensor.bufferShape, ovmsPyTensor.bufferShape);
EXPECT_EQ(recreatedTensor.strides, ovmsPyTensor.strides);
EXPECT_EQ(recreatedTensor.format, ovmsPyTensor.format);
// We cannot recreate known datatype only from buffer info. For unknown types we assume to UINT8
EXPECT_EQ(recreatedTensor.datatype, "UINT8");
EXPECT_EQ(recreatedTensor.itemsize, ovmsPyTensor.itemsize);
EXPECT_EQ(recreatedTensor.size, ovmsPyTensor.size);
}