forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ov_tensor_test.cpp
142 lines (125 loc) · 5.01 KB
/
ov_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
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "ov_test.hpp"
inline void setup_4d_shape(ov_shape_t* shape, int64_t d0, int64_t d1, int64_t d2, int64_t d3) {
int64_t dims[4] = {d0, d1, d2, d3};
ov_shape_create(4, dims, shape);
}
TEST(ov_tensor, ov_tensor_create) {
ov_element_type_e type = ov_element_type_e::U8;
ov_shape_t shape;
setup_4d_shape(&shape, 10, 20, 30, 40);
ov_tensor_t* tensor = nullptr;
OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor));
EXPECT_NE(nullptr, tensor);
ov_tensor_free(tensor);
ov_shape_free(&shape);
}
TEST(ov_tensor, ov_tensor_create_from_host_ptr) {
ov_element_type_e type = ov_element_type_e::U8;
ov_shape_t shape;
setup_4d_shape(&shape, 1, 3, 4, 4);
uint8_t host_ptr[1][3][4][4] = {{{{0}}}};
ov_tensor_t* tensor = nullptr;
OV_EXPECT_OK(ov_tensor_create_from_host_ptr(type, shape, &host_ptr, &tensor));
EXPECT_NE(nullptr, tensor);
ov_tensor_free(tensor);
ov_shape_free(&shape);
}
static size_t product(const std::vector<size_t>& dims) {
if (dims.empty())
return 0;
return std::accumulate(std::begin(dims), std::end(dims), (size_t)1, std::multiplies<size_t>());
}
inline size_t calculate_size(ov_shape_t shape) {
std::vector<size_t> tmp_shape;
std::copy_n(shape.dims, shape.rank, std::back_inserter(tmp_shape));
return product(tmp_shape);
}
inline size_t calculate_byteSize(ov_shape_t shape, ov_element_type_e type) {
return (calculate_size(shape) * GET_ELEMENT_TYPE_SIZE(type) + 7) >> 3;
}
class ov_tensor_create_test : public ::testing::TestWithParam<ov_element_type_e> {
protected:
void SetUp() override {
ov_element_type_e type = GetParam();
setup_4d_shape(&shape, 10, 20, 30, 40);
tensor = nullptr;
OV_EXPECT_OK(ov_tensor_create(type, shape, &tensor));
EXPECT_NE(nullptr, tensor);
}
void TearDown() override {
ov_shape_free(&shape);
ov_tensor_free(tensor);
}
public:
ov_shape_t shape;
ov_tensor_t* tensor;
};
INSTANTIATE_TEST_SUITE_P(ov_tensor,
ov_tensor_create_test,
::testing::Values(ov_element_type_e::BOOLEAN,
ov_element_type_e::BF16,
ov_element_type_e::F16,
ov_element_type_e::F32,
ov_element_type_e::F64,
ov_element_type_e::I4,
ov_element_type_e::I8,
ov_element_type_e::I16,
ov_element_type_e::I32,
ov_element_type_e::I64,
ov_element_type_e::U1,
ov_element_type_e::U4,
ov_element_type_e::U8,
ov_element_type_e::U16,
ov_element_type_e::U32,
ov_element_type_e::U64));
TEST_P(ov_tensor_create_test, get_tensor_element_type) {
ov_element_type_e type = GetParam();
ov_element_type_e type_res;
OV_EXPECT_OK(ov_tensor_get_element_type(tensor, &type_res));
EXPECT_EQ(type, type_res);
}
TEST_P(ov_tensor_create_test, get_tensor_size) {
size_t size = calculate_size(shape);
size_t size_res;
OV_EXPECT_OK(ov_tensor_get_size(tensor, &size_res));
EXPECT_EQ(size_res, size);
}
TEST_P(ov_tensor_create_test, get_tensor_byte_size) {
void* data = nullptr;
OV_EXPECT_OK(ov_tensor_data(tensor, &data));
EXPECT_NE(nullptr, data);
}
TEST_P(ov_tensor_create_test, get_tensor_data) {
ov_element_type_e type = GetParam();
size_t size = calculate_byteSize(shape, type);
size_t size_res;
OV_EXPECT_OK(ov_tensor_get_byte_size(tensor, &size_res));
EXPECT_EQ(size_res, size);
}
TEST_P(ov_tensor_create_test, get_tensor_shape) {
ov_shape_t shape_res;
OV_EXPECT_OK(ov_tensor_get_shape(tensor, &shape_res));
EXPECT_EQ(shape.rank, shape_res.rank);
EXPECT_EQ(shape.dims[0], shape_res.dims[0]);
EXPECT_EQ(shape.dims[1], shape_res.dims[1]);
EXPECT_EQ(shape.dims[2], shape_res.dims[2]);
EXPECT_EQ(shape.dims[3], shape_res.dims[3]);
ov_shape_free(&shape_res);
}
TEST_P(ov_tensor_create_test, set_tensor_shape) {
ov_shape_t shape_update;
setup_4d_shape(&shape_update, 16, 16, 16, 16);
OV_EXPECT_OK(ov_tensor_set_shape(tensor, shape_update));
ov_shape_t shape_res;
OV_EXPECT_OK(ov_tensor_get_shape(tensor, &shape_res));
EXPECT_EQ(shape_update.rank, shape_res.rank);
EXPECT_EQ(shape_update.dims[0], shape_res.dims[0]);
EXPECT_EQ(shape_update.dims[1], shape_res.dims[1]);
EXPECT_EQ(shape_update.dims[2], shape_res.dims[2]);
EXPECT_EQ(shape_update.dims[3], shape_res.dims[3]);
ov_shape_free(&shape_update);
ov_shape_free(&shape_res);
}