forked from PaddlePaddle/FastDeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fd_tensor.cc
103 lines (86 loc) · 3.12 KB
/
test_fd_tensor.cc
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
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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 "fastdeploy/core/fd_tensor.h"
#include "gtest_utils.h"
#include "gtest/gtest.h"
#include <array>
#include <cstring>
#include <vector>
namespace fastdeploy {
TEST(fastdeploy, fd_tensor_constructor) {
CheckShape check_shape;
CheckData check_data;
FDTensor tensor1;
check_shape(tensor1.shape, {0});
ASSERT_EQ(tensor1.name, "");
ASSERT_EQ(tensor1.dtype, FDDataType::INT8);
ASSERT_EQ(tensor1.device, Device::CPU);
std::vector<int> inputs = {2, 4, 3, 7, 1, 5};
tensor1.SetExternalData({2, 3}, FDDataType::INT32, inputs.data());
ASSERT_EQ(tensor1.dtype, FDDataType::INT32);
FDTensor tensor2(tensor1);
check_shape(tensor1.shape, {2, 3});
ASSERT_EQ(tensor2.name, "");
ASSERT_EQ(tensor2.dtype, FDDataType::INT32);
ASSERT_EQ(tensor2.device, Device::CPU);
FDTensor tensor3;
tensor3.Resize({2, 3}, FDDataType::INT32, "tensor3");
check_shape(tensor3.shape, {2, 3});
ASSERT_EQ(tensor3.Nbytes(), 24);
// Copy constructor
FDTensor tensor4(tensor3);
check_shape(tensor4.shape, {2, 3});
ASSERT_EQ(tensor3.Nbytes(), tensor4.Nbytes());
check_data(reinterpret_cast<int*>(tensor3.Data()),
reinterpret_cast<int*>(tensor4.Data()), tensor4.Numel());
// Move constructor
ASSERT_NE(tensor1.external_data_ptr, nullptr);
FDTensor tensor5(std::move(tensor1));
ASSERT_EQ(tensor1.external_data_ptr, nullptr);
ASSERT_EQ(tensor5.external_data_ptr, inputs.data());
check_shape(tensor5.shape, {2, 3});
}
TEST(fastdeploy, fd_tensor_assignment) {
CheckShape check_shape;
CheckData check_data;
FDTensor tensor1("T1");
std::vector<int> inputs = {2, 4, 3, 7, 1, 5};
tensor1.SetExternalData({2, 3}, FDDataType::INT32, inputs.data());
FDTensor tensor2;
tensor2 = tensor1;
ASSERT_EQ(tensor2.name, "T1");
ASSERT_EQ(tensor2.dtype, FDDataType::INT32);
ASSERT_EQ(tensor2.device, Device::CPU);
ASSERT_EQ(tensor2.Data(), inputs.data());
check_shape(tensor2.shape, {2, 3});
FDTensor tensor3;
tensor3 = std::move(tensor1);
ASSERT_EQ(tensor3.name, "T1");
ASSERT_EQ(tensor3.dtype, FDDataType::INT32);
ASSERT_EQ(tensor3.device, Device::CPU);
ASSERT_EQ(tensor3.Data(), inputs.data());
ASSERT_EQ(tensor1.Data(), nullptr);
}
TEST(fastdeploy, fd_tensor_reshape) {
CheckShape check_shape;
FDTensor x;
x.Allocate({2, 3, 4, 5}, FDDataType::FP32);
x.Reshape({-1, 3, 2, 2, 5});
check_shape(x.Shape(), {2, 3, 2, 2, 5});
x.Reshape({0, -1, 5, 2});
check_shape(x.Shape(), {2, 6, 5, 2});
x.Reshape({2, 3, 0, 0, 2});
check_shape(x.Shape(), {2, 3, 5, 2, 2});
}
} // namespace fastdeploy