You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

meta_tensor_test.cc 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <iostream>
  17. #include <memory>
  18. #include <vector>
  19. #include "common/common_test.h"
  20. #include "common/py_func_graph_fetcher.h"
  21. #include "securec/include/securec.h"
  22. #include "ir/meta_tensor.h"
  23. namespace mindspore {
  24. namespace tensor {
  25. class TestMetaTensor : public UT::Common {
  26. public:
  27. TestMetaTensor() {}
  28. virtual void SetUp() {
  29. std::vector<int> dimensions({2, 3});
  30. meta_tensor_ = MetaTensor(TypeId::kNumberTypeFloat64, dimensions);
  31. }
  32. protected:
  33. MetaTensor meta_tensor_;
  34. };
  35. TEST_F(TestMetaTensor, InitTest) {
  36. std::vector<int> dimensions({2, 3});
  37. MetaTensor meta_tensor(TypeId::kNumberTypeFloat64, dimensions);
  38. // Test type
  39. ASSERT_EQ(TypeId::kNumberTypeFloat64, meta_tensor.data_type());
  40. // Test dimensions
  41. ASSERT_EQ(2, meta_tensor.DimensionSize(0));
  42. ASSERT_EQ(3, meta_tensor.DimensionSize(1));
  43. ASSERT_EQ(-1, meta_tensor.DimensionSize(2));
  44. // Test number of elements
  45. ASSERT_EQ(6, meta_tensor.ElementsNum());
  46. }
  47. // Test type
  48. TEST_F(TestMetaTensor, TypeTest) {
  49. meta_tensor_.set_data_type(TypeId::kNumberTypeInt32);
  50. ASSERT_EQ(TypeId::kNumberTypeInt32, meta_tensor_.data_type());
  51. }
  52. // Test shape
  53. TEST_F(TestMetaTensor, ShapeTest) {
  54. std::vector<int> dimensions({5, 6, 7});
  55. meta_tensor_.set_shape(dimensions);
  56. ASSERT_EQ(5, meta_tensor_.DimensionSize(0));
  57. ASSERT_EQ(6, meta_tensor_.DimensionSize(1));
  58. ASSERT_EQ(7, meta_tensor_.DimensionSize(2));
  59. // Test number of elements
  60. ASSERT_EQ(210, meta_tensor_.ElementsNum());
  61. }
  62. TEST_F(TestMetaTensor, EqualTest) {
  63. std::vector<int> dimensions({2, 3});
  64. MetaTensor meta_tensor_x(TypeId::kNumberTypeFloat64, dimensions);
  65. MetaTensor meta_tensor_y(meta_tensor_x);
  66. ASSERT_TRUE(meta_tensor_x == meta_tensor_y);
  67. MetaTensor meta_tensor_z(TypeId::kNumberTypeFloat32, dimensions);
  68. ASSERT_FALSE(meta_tensor_x == meta_tensor_z);
  69. meta_tensor_z = meta_tensor_x;
  70. ASSERT_TRUE(meta_tensor_x == meta_tensor_z);
  71. }
  72. class TestTensor : public UT::Common {
  73. public:
  74. TestTensor() {}
  75. virtual void SetUp() {
  76. UT::InitPythonPath();
  77. // Init tensor data by py::array_t<float>
  78. input_ = py::array_t<float, py::array::c_style>({2, 3});
  79. auto array = input_.mutable_unchecked();
  80. float start = 0;
  81. for (int i = 0; i < array.shape(0); i++) {
  82. for (int j = 0; j < array.shape(1); j++) {
  83. array(i, j) = start++;
  84. }
  85. }
  86. }
  87. protected:
  88. py::array_t<float, py::array::c_style> input_;
  89. };
  90. TEST_F(TestTensor, PyArrayScalarTest) {
  91. std::vector<int> dimensions;
  92. py::array data = py::array_t<int64_t, py::array::c_style>(dimensions);
  93. uint8_t *data_buf = reinterpret_cast<uint8_t *>(data.request(true).ptr);
  94. int64_t num = 1;
  95. errno_t ret = memcpy_s(data_buf, sizeof(int64_t), &num, sizeof(int64_t));
  96. ASSERT_EQ(0, ret);
  97. ASSERT_EQ(num, *data_buf);
  98. }
  99. TEST_F(TestTensor, InitScalarTest) {
  100. std::vector<int> dimensions;
  101. Tensor tensor(TypeId::kNumberTypeInt64, dimensions);
  102. uint8_t *data_buf = reinterpret_cast<uint8_t *>(tensor.data_c(true));
  103. int64_t num = 1;
  104. errno_t ret = memcpy_s(data_buf, sizeof(int64_t), &num, sizeof(int64_t));
  105. ASSERT_EQ(0, ret);
  106. ASSERT_EQ(num, *data_buf);
  107. // Test type
  108. ASSERT_EQ(TypeId::kNumberTypeInt64, tensor.data_type());
  109. // Test dimensions
  110. ASSERT_EQ(0, tensor.DataDim());
  111. // Test shape
  112. ASSERT_EQ(0, tensor.shape().size());
  113. std::vector<int> empty_shape;
  114. ASSERT_EQ(empty_shape, tensor.shape());
  115. // Test number of elements
  116. ASSERT_EQ(1, tensor.ElementsNum());
  117. ASSERT_EQ(1, tensor.DataSize());
  118. }
  119. TEST_F(TestTensor, InitTensorPtrTest) {
  120. std::vector<int> dimensions;
  121. Tensor tensor(TypeId::kNumberTypeInt64, dimensions);
  122. std::shared_ptr<Tensor> tensor_ptr = std::make_shared<Tensor>(tensor);
  123. // Test type
  124. ASSERT_EQ(TypeId::kNumberTypeInt64, tensor_ptr->data_type());
  125. // Test dimensions
  126. ASSERT_EQ(0, tensor_ptr->DataDim());
  127. // Test shape
  128. ASSERT_EQ(0, tensor_ptr->shape().size());
  129. std::vector<int> empty_shape;
  130. ASSERT_EQ(empty_shape, tensor_ptr->shape());
  131. // Test number of elements
  132. ASSERT_EQ(1, tensor_ptr->ElementsNum());
  133. ASSERT_EQ(1, tensor_ptr->DataSize());
  134. }
  135. TEST_F(TestTensor, InitByTupleTest) {
  136. py::tuple dimensions = py::make_tuple(2, 3, 4);
  137. TypePtr data_type = kFloat32;
  138. Tensor tuple_tensor = Tensor(data_type, dimensions);
  139. ASSERT_EQ(2, tuple_tensor.DimensionSize(0));
  140. ASSERT_EQ(3, tuple_tensor.DimensionSize(1));
  141. ASSERT_EQ(4, tuple_tensor.DimensionSize(2));
  142. // Test number of elements
  143. ASSERT_EQ(24, tuple_tensor.ElementsNum());
  144. ASSERT_EQ(TypeId::kNumberTypeFloat32, tuple_tensor.data_type());
  145. py::tuple tuple = py::make_tuple(1.0, 2.0, 3, 4, 5, 6);
  146. TensorPtr tensor = std::make_shared<Tensor>(tuple, kFloat64);
  147. py::array array = tensor->data();
  148. std::cout << "Dim: " << array.ndim() << std::endl;
  149. ASSERT_EQ(1, array.ndim());
  150. std::cout << "Num of Elements: " << array.size() << std::endl;
  151. ASSERT_EQ(6, array.size());
  152. std::cout << "Elements: " << std::endl;
  153. // Must be double, or the result is not right
  154. double *tensor_data = reinterpret_cast<double *>(tensor->data_c());
  155. for (int i = 0; i < array.size(); i++) {
  156. std::cout << tensor_data[i] << std::endl;
  157. }
  158. }
  159. TEST_F(TestTensor, EqualTest) {
  160. py::tuple tuple = py::make_tuple(1, 2, 3, 4, 5, 6);
  161. TensorPtr tensor_int8 = std::make_shared<Tensor>(tuple, kInt8);
  162. ASSERT_TRUE(*tensor_int8 == *tensor_int8);
  163. ASSERT_EQ(TypeId::kNumberTypeInt8, tensor_int8->data_type_c());
  164. TensorPtr tensor_int16 = std::make_shared<Tensor>(tuple, kInt16);
  165. ASSERT_EQ(TypeId::kNumberTypeInt16, tensor_int16->data_type_c());
  166. TensorPtr tensor_int32 = std::make_shared<Tensor>(tuple, kInt32);
  167. ASSERT_EQ(TypeId::kNumberTypeInt32, tensor_int32->data_type_c());
  168. TensorPtr tensor_float16 = std::make_shared<Tensor>(tuple, kFloat16);
  169. ASSERT_EQ(TypeId::kNumberTypeFloat16, tensor_float16->data_type_c());
  170. TensorPtr tensor_float32 = std::make_shared<Tensor>(tuple, kFloat32);
  171. ASSERT_EQ(TypeId::kNumberTypeFloat32, tensor_float32->data_type_c());
  172. TensorPtr tensor_float64 = std::make_shared<Tensor>(tuple, kFloat64);
  173. ASSERT_EQ(TypeId::kNumberTypeFloat64, tensor_float64->data_type_c());
  174. }
  175. TEST_F(TestTensor, PyArrayTest) {
  176. py::array_t<float, py::array::c_style> input({2, 3});
  177. auto array = input.mutable_unchecked();
  178. float sum = 0;
  179. std::cout << "sum"
  180. << " = " << std::endl;
  181. float start = 0;
  182. for (int i = 0; i < array.shape(0); i++) {
  183. for (int j = 0; j < array.shape(1); j++) {
  184. array(i, j) = start++;
  185. sum += array(i, j);
  186. std::cout << "sum + "
  187. << "array[" << i << ", " << j << "]"
  188. << " = " << sum << std::endl;
  189. }
  190. }
  191. ASSERT_EQ(15, sum);
  192. }
  193. TEST_F(TestTensor, InitByFloatArrayDataCTest) {
  194. // Init tensor data by py::array_t<float>
  195. TensorPtr tensor = std::make_shared<Tensor>(input_);
  196. // Print some information of the tensor
  197. std::cout << "Datatype: " << tensor->data_type() << std::endl;
  198. ASSERT_EQ(TypeId::kNumberTypeFloat32, tensor->data_type());
  199. std::cout << "Dim: " << tensor->DataDim() << std::endl;
  200. ASSERT_EQ(2, tensor->DataDim());
  201. std::cout << "Num of Elements: " << tensor->ElementsNum() << std::endl;
  202. ASSERT_EQ(6, tensor->ElementsNum());
  203. // Print each elements
  204. std::cout << "Elements: " << std::endl;
  205. float *tensor_data = reinterpret_cast<float *>(tensor->data_c());
  206. for (int i = 0; i < tensor->ElementsNum(); i++) {
  207. std::cout << tensor_data[i] << std::endl;
  208. }
  209. }
  210. TEST_F(TestTensor, InitByFloatArrayDataTest) {
  211. // Init tensor data by py::array_t<float>
  212. TensorPtr tensor = std::make_shared<Tensor>(input_);
  213. // Print some information of the tensor
  214. std::cout << "Datatype: " << tensor->data_type() << std::endl;
  215. ASSERT_EQ(TypeId::kNumberTypeFloat32, tensor->data_type());
  216. std::cout << "Dim: " << tensor->DataDim() << std::endl;
  217. ASSERT_EQ(2, tensor->DataDim());
  218. std::vector<int> dimensions = tensor->shape();
  219. ASSERT_GT(dimensions.size(), 1);
  220. std::cout << "Dim0: " << dimensions[0] << std::endl;
  221. ASSERT_EQ(2, dimensions[0]);
  222. std::cout << "Dim1: " << dimensions[1] << std::endl;
  223. ASSERT_EQ(3, dimensions[1]);
  224. std::cout << "Num of Elements: " << tensor->ElementsNum() << std::endl;
  225. ASSERT_EQ(6, tensor->ElementsNum());
  226. // Print each elements
  227. std::cout << "Elements: " << std::endl;
  228. py::array_t<float> data = (py::array_t<float>)tensor->data();
  229. auto array = data.unchecked<2>();
  230. for (int i = 0; i < array.shape(0); i++) {
  231. for (int j = 0; j < array.shape(1); j++) {
  232. std::cout << array(i, j) << std::endl;
  233. }
  234. }
  235. }
  236. TEST_F(TestTensor, PyArrayDataTest) {
  237. py::array_t<float, py::array::c_style> input({2, 3});
  238. float *data = reinterpret_cast<float *>(input.request().ptr);
  239. float ge_tensor_data[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
  240. errno_t ret = memcpy_s(data, input.nbytes(), ge_tensor_data, sizeof(ge_tensor_data));
  241. ASSERT_EQ(0, ret);
  242. auto array = input.mutable_unchecked();
  243. for (int i = 0; i < array.shape(0); i++) {
  244. for (int j = 0; j < array.shape(1); j++) {
  245. ASSERT_EQ(array(i, j), ge_tensor_data[3 * i + j]);
  246. }
  247. }
  248. }
  249. TEST_F(TestTensor, TensorDataTest) {
  250. // Init a data buffer
  251. float ge_tensor_data[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
  252. // Create a Tensor with wanted data type and shape
  253. Tensor tensor = Tensor(TypeId::kNumberTypeFloat32, std::vector<int>({2, 3}));
  254. // Get the writable data pointer from the tensor
  255. float *me_tensor_data = reinterpret_cast<float *>(tensor.data_c(true));
  256. // Copy data from buffer to tensor's data
  257. errno_t ret = memcpy_s(me_tensor_data, tensor.data().nbytes(), ge_tensor_data, sizeof(ge_tensor_data));
  258. ASSERT_EQ(0, ret);
  259. // Testify if the data has been copied to the tensor data
  260. py::array_t<float> data = (py::array_t<float>)tensor.data();
  261. auto array = data.mutable_unchecked();
  262. for (int i = 0; i < array.shape(0); i++) {
  263. for (int j = 0; j < array.shape(1); j++) {
  264. std::cout << "array[" << i << ", " << j << "]"
  265. << " = " << array(i, j) << std::endl;
  266. ASSERT_EQ(array(i, j), ge_tensor_data[3 * i + j]);
  267. }
  268. }
  269. }
  270. } // namespace tensor
  271. } // namespace mindspore