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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/tensor.h"
  23. #include "ir/tensor_py.h"
  24. using mindspore::tensor::TensorPy;
  25. namespace mindspore {
  26. namespace tensor {
  27. class TestMetaTensor : public UT::Common {
  28. public:
  29. TestMetaTensor() {}
  30. virtual void SetUp() {
  31. std::vector<int> dimensions({2, 3});
  32. meta_tensor_ = MetaTensor(TypeId::kNumberTypeFloat64, dimensions);
  33. }
  34. protected:
  35. MetaTensor meta_tensor_;
  36. };
  37. TEST_F(TestMetaTensor, InitTest) {
  38. std::vector<int> dimensions({2, 3});
  39. MetaTensor meta_tensor(TypeId::kNumberTypeFloat64, dimensions);
  40. // Test type
  41. ASSERT_EQ(TypeId::kNumberTypeFloat64, meta_tensor.data_type());
  42. // Test dimensions
  43. ASSERT_EQ(2, meta_tensor.DimensionSize(0));
  44. ASSERT_EQ(3, meta_tensor.DimensionSize(1));
  45. ASSERT_EQ(-1, meta_tensor.DimensionSize(2));
  46. // Test number of elements
  47. ASSERT_EQ(6, meta_tensor.ElementsNum());
  48. }
  49. // Test type
  50. TEST_F(TestMetaTensor, TypeTest) {
  51. meta_tensor_.set_data_type(TypeId::kNumberTypeInt32);
  52. ASSERT_EQ(TypeId::kNumberTypeInt32, meta_tensor_.data_type());
  53. }
  54. // Test shape
  55. TEST_F(TestMetaTensor, ShapeTest) {
  56. std::vector<int> dimensions({5, 6, 7});
  57. meta_tensor_.set_shape(dimensions);
  58. ASSERT_EQ(5, meta_tensor_.DimensionSize(0));
  59. ASSERT_EQ(6, meta_tensor_.DimensionSize(1));
  60. ASSERT_EQ(7, meta_tensor_.DimensionSize(2));
  61. // Test number of elements
  62. ASSERT_EQ(210, meta_tensor_.ElementsNum());
  63. }
  64. TEST_F(TestMetaTensor, EqualTest) {
  65. std::vector<int> dimensions({2, 3});
  66. MetaTensor meta_tensor_x(TypeId::kNumberTypeFloat64, dimensions);
  67. MetaTensor meta_tensor_y(meta_tensor_x);
  68. ASSERT_TRUE(meta_tensor_x == meta_tensor_y);
  69. MetaTensor meta_tensor_z(TypeId::kNumberTypeFloat32, dimensions);
  70. ASSERT_FALSE(meta_tensor_x == meta_tensor_z);
  71. meta_tensor_z = meta_tensor_x;
  72. ASSERT_TRUE(meta_tensor_x == meta_tensor_z);
  73. }
  74. class TestTensor : public UT::Common {
  75. public:
  76. TestTensor() {}
  77. virtual void SetUp() { UT::InitPythonPath(); }
  78. };
  79. py::array_t<float, py::array::c_style> BuildInputTensor() {
  80. // Init tensor data by py::array_t<float>
  81. py::array_t<float, py::array::c_style> input = py::array_t<float, py::array::c_style>({2, 3});
  82. auto array = input.mutable_unchecked();
  83. float start = 0;
  84. for (int i = 0; i < array.shape(0); i++) {
  85. for (int j = 0; j < array.shape(1); j++) {
  86. array(i, j) = start++;
  87. }
  88. }
  89. return input;
  90. }
  91. TEST_F(TestTensor, PyArrayScalarTest) {
  92. std::vector<int> dimensions;
  93. py::array data = py::array_t<int64_t, py::array::c_style>(dimensions);
  94. uint8_t *data_buf = reinterpret_cast<uint8_t *>(data.request(true).ptr);
  95. int64_t num = 1;
  96. errno_t ret = memcpy_s(data_buf, sizeof(int64_t), &num, sizeof(int64_t));
  97. ASSERT_EQ(0, ret);
  98. ASSERT_EQ(num, *data_buf);
  99. }
  100. TEST_F(TestTensor, InitScalarTest) {
  101. std::vector<int> dimensions;
  102. Tensor tensor(TypeId::kNumberTypeInt64, dimensions);
  103. uint8_t *data_buf = reinterpret_cast<uint8_t *>(tensor.data_c());
  104. int64_t num = 1;
  105. errno_t ret = memcpy_s(data_buf, sizeof(int64_t), &num, sizeof(int64_t));
  106. ASSERT_EQ(0, ret);
  107. ASSERT_EQ(num, *data_buf);
  108. // Test type
  109. ASSERT_EQ(TypeId::kNumberTypeInt64, tensor.data_type());
  110. // Test dimensions
  111. ASSERT_EQ(0, tensor.DataDim());
  112. // Test shape
  113. ASSERT_EQ(0, tensor.shape().size());
  114. std::vector<int> empty_shape;
  115. ASSERT_EQ(empty_shape, tensor.shape());
  116. // Test number of elements
  117. ASSERT_EQ(1, tensor.ElementsNum());
  118. ASSERT_EQ(1, tensor.DataSize());
  119. }
  120. TEST_F(TestTensor, InitTensorPtrTest) {
  121. std::vector<int> dimensions;
  122. Tensor tensor(TypeId::kNumberTypeInt64, dimensions);
  123. std::shared_ptr<Tensor> tensor_ptr = std::make_shared<Tensor>(tensor);
  124. // Test type
  125. ASSERT_EQ(TypeId::kNumberTypeInt64, tensor_ptr->data_type());
  126. // Test dimensions
  127. ASSERT_EQ(0, tensor_ptr->DataDim());
  128. // Test shape
  129. ASSERT_EQ(0, tensor_ptr->shape().size());
  130. std::vector<int> empty_shape;
  131. ASSERT_EQ(empty_shape, tensor_ptr->shape());
  132. // Test number of elements
  133. ASSERT_EQ(1, tensor_ptr->ElementsNum());
  134. ASSERT_EQ(1, tensor_ptr->DataSize());
  135. }
  136. TEST_F(TestTensor, InitByTupleTest) {
  137. const std::vector<int> shape = {2, 3, 4};
  138. TypePtr data_type = kFloat32;
  139. Tensor tuple_tensor(data_type->type_id(), shape);
  140. ASSERT_EQ(2, tuple_tensor.DimensionSize(0));
  141. ASSERT_EQ(3, tuple_tensor.DimensionSize(1));
  142. ASSERT_EQ(4, tuple_tensor.DimensionSize(2));
  143. // Test number of elements
  144. ASSERT_EQ(24, tuple_tensor.ElementsNum());
  145. ASSERT_EQ(TypeId::kNumberTypeFloat32, tuple_tensor.data_type());
  146. py::tuple tuple = py::make_tuple(1.0, 2.0, 3, 4, 5, 6);
  147. TensorPtr tensor = TensorPy::MakeTensor(py::array(tuple), kFloat64);
  148. py::array array = TensorPy::AsNumpy(*tensor);
  149. std::cout << "Dim: " << array.ndim() << std::endl;
  150. ASSERT_EQ(1, array.ndim());
  151. std::cout << "Num of Elements: " << array.size() << std::endl;
  152. ASSERT_EQ(6, array.size());
  153. std::cout << "Elements: " << std::endl;
  154. // Must be double, or the result is not right
  155. double *tensor_data = reinterpret_cast<double *>(tensor->data_c());
  156. for (int i = 0; i < array.size(); i++) {
  157. std::cout << tensor_data[i] << std::endl;
  158. }
  159. }
  160. TEST_F(TestTensor, EqualTest) {
  161. py::tuple tuple = py::make_tuple(1, 2, 3, 4, 5, 6);
  162. TensorPtr tensor_int8 = TensorPy::MakeTensor(py::array(tuple), kInt8);
  163. ASSERT_TRUE(*tensor_int8 == *tensor_int8);
  164. ASSERT_EQ(TypeId::kNumberTypeInt8, tensor_int8->data_type_c());
  165. TensorPtr tensor_int16 = TensorPy::MakeTensor(py::array(tuple), kInt16);
  166. ASSERT_EQ(TypeId::kNumberTypeInt16, tensor_int16->data_type_c());
  167. TensorPtr tensor_int32 = TensorPy::MakeTensor(py::array(tuple), kInt32);
  168. ASSERT_EQ(TypeId::kNumberTypeInt32, tensor_int32->data_type_c());
  169. TensorPtr tensor_float16 = TensorPy::MakeTensor(py::array(tuple), kFloat16);
  170. ASSERT_EQ(TypeId::kNumberTypeFloat16, tensor_float16->data_type_c());
  171. TensorPtr tensor_float32 = TensorPy::MakeTensor(py::array(tuple), kFloat32);
  172. ASSERT_EQ(TypeId::kNumberTypeFloat32, tensor_float32->data_type_c());
  173. TensorPtr tensor_float64 = TensorPy::MakeTensor(py::array(tuple), kFloat64);
  174. ASSERT_EQ(TypeId::kNumberTypeFloat64, tensor_float64->data_type_c());
  175. }
  176. TEST_F(TestTensor, PyArrayTest) {
  177. py::array_t<float, py::array::c_style> input({2, 3});
  178. auto array = input.mutable_unchecked();
  179. float sum = 0;
  180. std::cout << "sum"
  181. << " = " << std::endl;
  182. float start = 0;
  183. for (int i = 0; i < array.shape(0); i++) {
  184. for (int j = 0; j < array.shape(1); j++) {
  185. array(i, j) = start++;
  186. sum += array(i, j);
  187. std::cout << "sum + "
  188. << "array[" << i << ", " << j << "]"
  189. << " = " << sum << std::endl;
  190. }
  191. }
  192. ASSERT_EQ(15, sum);
  193. }
  194. TEST_F(TestTensor, InitByFloatArrayDataCTest) {
  195. // Init tensor data by py::array_t<float>
  196. auto tensor = TensorPy::MakeTensor(BuildInputTensor());
  197. // Print some information of the tensor
  198. std::cout << "Datatype: " << tensor->data_type() << std::endl;
  199. ASSERT_EQ(TypeId::kNumberTypeFloat32, tensor->data_type());
  200. std::cout << "Dim: " << tensor->DataDim() << std::endl;
  201. ASSERT_EQ(2, tensor->DataDim());
  202. std::cout << "Num of Elements: " << tensor->ElementsNum() << std::endl;
  203. ASSERT_EQ(6, tensor->ElementsNum());
  204. // Print each elements
  205. std::cout << "Elements: " << std::endl;
  206. float *tensor_data = reinterpret_cast<float *>(tensor->data_c());
  207. for (int i = 0; i < tensor->ElementsNum(); i++) {
  208. std::cout << tensor_data[i] << std::endl;
  209. }
  210. }
  211. TEST_F(TestTensor, InitByFloatArrayDataTest) {
  212. // Init tensor data by py::array_t<float>
  213. TensorPtr tensor = TensorPy::MakeTensor(BuildInputTensor());
  214. // Print some information of the tensor
  215. std::cout << "Datatype: " << tensor->data_type() << std::endl;
  216. ASSERT_EQ(TypeId::kNumberTypeFloat32, tensor->data_type());
  217. std::cout << "Dim: " << tensor->DataDim() << std::endl;
  218. ASSERT_EQ(2, tensor->DataDim());
  219. std::vector<int> dimensions = tensor->shape();
  220. ASSERT_GT(dimensions.size(), 1);
  221. std::cout << "Dim0: " << dimensions[0] << std::endl;
  222. ASSERT_EQ(2, dimensions[0]);
  223. std::cout << "Dim1: " << dimensions[1] << std::endl;
  224. ASSERT_EQ(3, dimensions[1]);
  225. std::cout << "Num of Elements: " << tensor->ElementsNum() << std::endl;
  226. ASSERT_EQ(6, tensor->ElementsNum());
  227. // Print each elements
  228. std::cout << "Elements: " << std::endl;
  229. py::array_t<float> data = py::cast<py::array_t<float>>(TensorPy::AsNumpy(*tensor));
  230. auto array = data.unchecked<2>();
  231. for (int i = 0; i < array.shape(0); i++) {
  232. for (int j = 0; j < array.shape(1); j++) {
  233. std::cout << array(i, j) << std::endl;
  234. }
  235. }
  236. }
  237. TEST_F(TestTensor, PyArrayDataTest) {
  238. py::array_t<float, py::array::c_style> input({2, 3});
  239. float *data = reinterpret_cast<float *>(input.request().ptr);
  240. float ge_tensor_data[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
  241. errno_t ret = memcpy_s(data, input.nbytes(), ge_tensor_data, sizeof(ge_tensor_data));
  242. ASSERT_EQ(0, ret);
  243. auto array = input.mutable_unchecked();
  244. for (int i = 0; i < array.shape(0); i++) {
  245. for (int j = 0; j < array.shape(1); j++) {
  246. ASSERT_EQ(array(i, j), ge_tensor_data[3 * i + j]);
  247. }
  248. }
  249. }
  250. TEST_F(TestTensor, TensorDataTest) {
  251. // Init a data buffer
  252. float ge_tensor_data[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
  253. // Create a Tensor with wanted data type and shape
  254. Tensor tensor(TypeId::kNumberTypeFloat32, std::vector<int>({2, 3}));
  255. // Get the writable data pointer from the tensor
  256. float *me_tensor_data = reinterpret_cast<float *>(tensor.data_c());
  257. // Copy data from buffer to tensor's data
  258. errno_t ret = memcpy_s(me_tensor_data, tensor.data().nbytes(), ge_tensor_data, sizeof(ge_tensor_data));
  259. ASSERT_EQ(0, ret);
  260. // Testify if the data has been copied to the tensor data
  261. py::array_t<float> data = py::cast<py::array_t<float>>(TensorPy::AsNumpy(tensor));
  262. auto array = data.mutable_unchecked();
  263. for (int i = 0; i < array.shape(0); i++) {
  264. for (int j = 0; j < array.shape(1); j++) {
  265. std::cout << "array[" << i << ", " << j << "]"
  266. << " = " << array(i, j) << std::endl;
  267. ASSERT_EQ(array(i, j), ge_tensor_data[3 * i + j]);
  268. }
  269. }
  270. }
  271. TEST_F(TestTensor, TensorPyCast) {
  272. std::vector<int> shape{2, 3, 4, 5};
  273. py::tuple py_tuple = py::make_tuple(std::make_shared<Tensor>(kNumberTypeFloat32, shape));
  274. auto shape1 = py::cast<Tensor &>(py_tuple[0]).shape();
  275. const py::tuple &t = py_tuple;
  276. auto shape2 = py::cast<const Tensor &>(t[0]).shape();
  277. auto shape3 = py::cast<Tensor &>(t[0]).shape();
  278. ASSERT_EQ(shape, shape1);
  279. ASSERT_EQ(shape, shape2);
  280. ASSERT_EQ(shape, shape3);
  281. }
  282. } // namespace tensor
  283. } // namespace mindspore