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.cc 21 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /**
  2. * Copyright 2019 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 "ir/meta_tensor.h"
  17. #include <functional>
  18. #include <numeric>
  19. #include <vector>
  20. #include <sstream>
  21. #include <string>
  22. #include "device/device_address.h"
  23. #include "pybind_api/api_register.h"
  24. #include "pybind_api/export_flags.h"
  25. #include "pipeline/static_analysis/abstract_value.h"
  26. namespace mindspore {
  27. namespace tensor {
  28. void DataBuf2Contiguous(const py::array &src, py::array *const dest) {
  29. if (dest == nullptr) {
  30. MS_LOG(EXCEPTION) << "Failed to copy data to a contiguous buffer as dest is nullptr!";
  31. }
  32. Py_buffer pybuf_src;
  33. if (PyObject_GetBuffer(src.ptr(), &pybuf_src, PyBUF_ANY_CONTIGUOUS)) {
  34. MS_LOG(EXCEPTION) << "Failed to get buffer info from the src!";
  35. }
  36. if (!PyBuffer_IsContiguous(&pybuf_src, 'C')) {
  37. if (PyBuffer_ToContiguous(dest->request(true).ptr, &pybuf_src, pybuf_src.len, 'C')) {
  38. MS_LOG(EXCEPTION) << "Can't copy numpy.ndarray to a contiguous buffer.";
  39. }
  40. } else {
  41. *dest = src;
  42. }
  43. PyBuffer_Release(&pybuf_src);
  44. }
  45. // MetaTensor has default type_id_ which is TypeId::kTypeUnknown.
  46. MetaTensor::MetaTensor() : data_type_(TypeId::kTypeUnknown) {}
  47. MetaTensor::MetaTensor(const TypeId data_type, const std::vector<int> &shape) : data_type_(data_type), shape_(shape) {}
  48. MetaTensor::MetaTensor(const TypePtr &type_ptr, const py::tuple &shape) {
  49. TypeId data_type = TypeId::kTypeUnknown;
  50. if (type_ptr != nullptr) {
  51. data_type = type_ptr->type_id();
  52. }
  53. data_type_ = data_type;
  54. shape_.resize(shape.size());
  55. for (size_t i = 0; i < shape.size(); ++i) {
  56. shape_[i] = py::int_(shape[i]);
  57. }
  58. }
  59. MetaTensor::MetaTensor(const MetaTensor &meta_tensor)
  60. : Value(meta_tensor), data_type_(meta_tensor.data_type()), shape_(meta_tensor.shape()) {}
  61. MetaTensor &MetaTensor::operator=(const MetaTensor &meta_tensor) {
  62. if (&meta_tensor == this) {
  63. return *this;
  64. }
  65. data_type_ = meta_tensor.data_type();
  66. shape_ = meta_tensor.shape();
  67. device_info_ = meta_tensor.device_info();
  68. return *this;
  69. }
  70. bool MetaTensor::operator==(const MetaTensor &meta_tensor) const {
  71. return data_type_ == meta_tensor.data_type() && shape_ == meta_tensor.shape();
  72. }
  73. // Get the size of a given dimension by its index number.
  74. // The given index number should be in [0, shape_.size()).
  75. // param index Dimension index number.
  76. // return The size of the dimension if succeed, or -1 if failed.
  77. int MetaTensor::DimensionSize(const size_t index) const {
  78. int dim_size = -1;
  79. if (index < shape_.size()) {
  80. dim_size = shape_[index];
  81. } else {
  82. MS_LOG(ERROR) << "Dimension index is wrong: " << index;
  83. }
  84. return dim_size;
  85. }
  86. int MetaTensor::ElementsNum() const {
  87. return std::accumulate(shape_.begin(), shape_.end(), 1LL, std::multiplies<int>());
  88. }
  89. TypePtr MetaTensor::Dtype() const { return TypeIdToType(data_type_); }
  90. TypePtr MetaTensor::SetDtype(const TypePtr type_ptr) {
  91. if (type_ptr == nullptr) {
  92. MS_LOG(ERROR) << "Dtype to be set is nullptr.";
  93. return nullptr;
  94. }
  95. (void)set_data_type(type_ptr->type_id());
  96. return type_ptr;
  97. }
  98. void MetaTensor::SetDeviceInfo(const std::string &format, const TypePtr &data_type) {
  99. DeviceInfo info(format, data_type);
  100. set_device_info(info);
  101. }
  102. std::string MetaTensor::ToString() const {
  103. std::ostringstream buf;
  104. buf << "MetaTensor shape:[" << shape() << "]";
  105. return buf.str();
  106. }
  107. std::string MetaTensor::DumpText() const {
  108. std::ostringstream oss;
  109. oss << type_name() << "(" << SizeToInt(data_type_) << ")[";
  110. for (size_t i = 0; i < shape_.size(); ++i) {
  111. oss << (i > 0 ? ", " : "") << shape_[i];
  112. }
  113. oss << "]";
  114. return oss.str();
  115. }
  116. Tensor::Tensor(const TypePtr &type_ptr, const py::tuple &shape) {
  117. TypeId data_type = TypeId::kTypeUnknown;
  118. if (type_ptr != nullptr) {
  119. data_type = type_ptr->type_id();
  120. }
  121. data_type_ = data_type;
  122. shape_.resize(shape.size());
  123. for (size_t i = 0; i < shape.size(); ++i) {
  124. shape_[i] = py::int_(shape[i]);
  125. }
  126. init(data_type_, shape_, &data_);
  127. }
  128. Tensor::Tensor(TypeId data_type, const std::vector<int> &shape) { init(data_type, shape, &data_); }
  129. Tensor::Tensor(const py::array &input, const TypePtr &data_type) { init(input, data_type); }
  130. Tensor::Tensor(const py::list &input, const TypePtr &data_type) { init(py::array(input), data_type); }
  131. Tensor::Tensor(const py::tuple &input, const TypePtr &data_type) { init(py::array(input), data_type); }
  132. Tensor::Tensor(const py::float_ &input, const TypePtr &data_type) { init(py::array(input), data_type); }
  133. Tensor::Tensor(const py::int_ &input, const TypePtr &data_type) { init(py::array(input), data_type); }
  134. Tensor::Tensor(const Tensor &tensor, const TypePtr &data_type)
  135. : MetaTensor(tensor), dirty_(tensor.dirty_), device_address_(tensor.device_address_) {
  136. init(tensor.data_, data_type);
  137. }
  138. Tensor &Tensor::operator=(const Tensor &tensor) {
  139. if (this != &tensor) {
  140. MetaTensor::operator=(tensor);
  141. dirty_ = tensor.is_dirty();
  142. device_address_ = tensor.device_address();
  143. data_ = tensor.data_;
  144. }
  145. return *this;
  146. }
  147. bool Tensor::operator==(const Tensor &tensor) const {
  148. return (MetaTensor::operator==(tensor) && data_ == tensor.data_);
  149. }
  150. bool Tensor::ValueEqual(const Tensor &other) const {
  151. auto equal = [&other, this]() -> bool {
  152. auto np = py::module::import("numpy");
  153. auto equal = np.attr("equal")(data_, other.data_);
  154. auto all_equal = np.attr("all")(equal);
  155. return all_equal.cast<bool>();
  156. };
  157. return (MetaTensor::operator==(other) && (data_.is(other.data_) || equal()));
  158. }
  159. int Tensor::DataDim() const { return static_cast<int>(data_.ndim()); }
  160. int Tensor::DataSize() const { return static_cast<int>(data_.size()); }
  161. py::tuple Tensor::GetPyTupleShape() const {
  162. py::tuple dims(shape_.size());
  163. for (size_t i = 0; i < dims.size(); ++i) {
  164. dims[i] = py::int_(shape_[i]);
  165. }
  166. return dims;
  167. }
  168. py::array Tensor::data() const { return data_; }
  169. int Tensor::data_type_c() const { return static_cast<int>(data_type_); }
  170. std::vector<int> Tensor::shape_c(void) const { return shape(); }
  171. void *Tensor::data_c(bool writable) {
  172. // operand of bit operation should be unsigned int.
  173. unsigned int flags = ((unsigned int)data_.flags()) & pybind11::detail::npy_api::NPY_ARRAY_C_CONTIGUOUS_;
  174. bool is_c_contiguous = (flags != 0) ? true : false;
  175. if (!is_c_contiguous) {
  176. py::array data_c;
  177. init(data_type_, shape_, &data_c);
  178. DataBuf2Contiguous(data_, &data_c);
  179. data_ = data_c;
  180. }
  181. return data_.request(writable).ptr;
  182. }
  183. TypeId Tensor::GetDataType(const py::buffer_info &buf) const {
  184. TypeId data_type = TypeId::kTypeUnknown;
  185. if (buf.format.compare("e") == 0) {
  186. data_type = TypeId::kNumberTypeFloat16;
  187. } else if (buf.format.compare("f") == 0) {
  188. data_type = TypeId::kNumberTypeFloat32;
  189. } else if (buf.format.compare("d") == 0) {
  190. data_type = TypeId::kNumberTypeFloat64;
  191. } else if (buf.format.compare("B") == 0) {
  192. data_type = TypeId::kNumberTypeUInt8;
  193. } else if (buf.format.compare("H") == 0) {
  194. data_type = TypeId::kNumberTypeUInt16;
  195. } else if (buf.format.compare("I") == 0) {
  196. data_type = TypeId::kNumberTypeUInt32;
  197. } else if (buf.format.compare("L") == 0 || buf.format.compare("Q") == 0) {
  198. data_type = TypeId::kNumberTypeUInt64;
  199. } else if (buf.format.compare("b") == 0) {
  200. data_type = TypeId::kNumberTypeInt8;
  201. } else if (buf.format.compare("h") == 0) {
  202. data_type = TypeId::kNumberTypeInt16;
  203. } else if (buf.format.compare("i") == 0) {
  204. data_type = TypeId::kNumberTypeInt32;
  205. } else if (buf.format.compare("l") == 0 || buf.format.compare("q") == 0) {
  206. data_type = TypeId::kNumberTypeInt64;
  207. } else if (buf.format.compare("?") == 0) {
  208. data_type = TypeId::kNumberTypeBool;
  209. } else {
  210. MS_LOG(WARNING) << "Get unsupported DataType " << buf.format << ".";
  211. }
  212. return data_type;
  213. }
  214. void Tensor::init(const py::array &input, const TypePtr &type_ptr) {
  215. TypeId data_type = TypeId::kTypeUnknown;
  216. if (type_ptr != nullptr) {
  217. data_type = type_ptr->type_id();
  218. }
  219. init(input, data_type);
  220. }
  221. void Tensor::init(const py::array &input, const TypeId &data_type) {
  222. py::buffer_info buf = input.request();
  223. data_type_ = GetDataType(buf);
  224. if (TypeId::kTypeUnknown == data_type && TypeId::kTypeUnknown == data_type_) {
  225. MS_LOG(EXCEPTION) << "Unsupported tensor type!";
  226. }
  227. std::vector<ssize_t> tm = buf.shape;
  228. size_t len = tm.size();
  229. std::vector<int> dims(len);
  230. for (size_t i = 0; i < len; ++i) {
  231. dims[i] = static_cast<int>(tm[i]);
  232. }
  233. (void)set_shape(dims);
  234. if (TypeId::kTypeUnknown != data_type && TypeId::kTypeUnknown != data_type_ && data_type_ != data_type) {
  235. // If user defined data type is not same as GetDataType from the data
  236. bool success = convert_data(input, data_type_, &data_, data_type);
  237. if (success) {
  238. data_type_ = data_type;
  239. } else {
  240. data_type_ = TypeId::kTypeUnknown;
  241. MS_LOG(EXCEPTION) << "Convert data from " << data_type_ << " to " << data_type << " failed!";
  242. }
  243. } else {
  244. data_ = input;
  245. }
  246. }
  247. void Tensor::init(TypeId data_type, const std::vector<int> &shape, py::array *const data) {
  248. data_type_ = data_type;
  249. shape_ = shape;
  250. switch (data_type) {
  251. case kNumberTypeBool:
  252. *data = py::array_t<bool, py::array::c_style>(shape);
  253. break;
  254. case kNumberTypeInt8:
  255. *data = py::array_t<int8_t, py::array::c_style>(shape);
  256. break;
  257. case kNumberTypeInt16:
  258. *data = py::array_t<int16_t, py::array::c_style>(shape);
  259. break;
  260. case kNumberTypeInt32:
  261. *data = py::array_t<int32_t, py::array::c_style>(shape);
  262. break;
  263. case kNumberTypeInt64:
  264. *data = py::array_t<int64_t, py::array::c_style>(shape);
  265. break;
  266. case kNumberTypeUInt8:
  267. *data = py::array_t<uint8_t, py::array::c_style>(shape);
  268. break;
  269. case kNumberTypeUInt16:
  270. *data = py::array_t<uint16_t, py::array::c_style>(shape);
  271. break;
  272. case kNumberTypeUInt32:
  273. *data = py::array_t<uint32_t, py::array::c_style>(shape);
  274. break;
  275. case kNumberTypeUInt64:
  276. *data = py::array_t<uint64_t, py::array::c_style>(shape);
  277. break;
  278. case kNumberTypeFloat16:
  279. *data = py::array_t<float16, py::array::c_style>(shape);
  280. break;
  281. case kNumberTypeFloat32:
  282. *data = py::array_t<float, py::array::c_style>(shape);
  283. break;
  284. case kNumberTypeFloat64:
  285. *data = py::array_t<double, py::array::c_style>(shape);
  286. break;
  287. default:
  288. MS_LOG(EXCEPTION) << "Cannot construct Tensor because of unsupported data type: " << data_type << ".";
  289. break;
  290. }
  291. }
  292. TypePtr Tensor::SetDtype(const TypePtr type_ptr) {
  293. MS_EXCEPTION_IF_NULL(type_ptr);
  294. (void)set_data_type(type_ptr->type_id());
  295. return type_ptr;
  296. }
  297. TypeId Tensor::set_data_type(const TypeId data_type) {
  298. if (data_.size() > 0 && data_type_ != data_type) {
  299. bool success = convert_data(data_, data_type_, &data_, data_type);
  300. if (success) {
  301. data_type_ = data_type;
  302. } else {
  303. MS_LOG(EXCEPTION) << "Convert data from " << data_type_ << " to " << data_type << " failed!";
  304. }
  305. } else if (data_.size() == 0) {
  306. data_type_ = data_type;
  307. }
  308. return data_type_;
  309. }
  310. bool Tensor::convert_data(const py::array &in, const TypeId in_data_type, py::array *const out,
  311. const TypeId out_data_type) {
  312. if (out == nullptr) {
  313. return false;
  314. }
  315. bool result = true;
  316. if (TypeId::kTypeUnknown == in_data_type || TypeId::kTypeUnknown == out_data_type) {
  317. result = false;
  318. } else if (in_data_type == out_data_type) {
  319. *out = in;
  320. } else if (TypeId::kNumberTypeFloat64 == out_data_type) {
  321. *out = in.attr("astype").cast<py::function>()("float64").cast<py::array>();
  322. } else if (TypeId::kNumberTypeFloat32 == out_data_type) {
  323. *out = in.attr("astype").cast<py::function>()("float32").cast<py::array>();
  324. } else if (TypeId::kNumberTypeFloat16 == out_data_type) {
  325. *out = in.attr("astype").cast<py::function>()("float16").cast<py::array>();
  326. } else if (TypeId::kNumberTypeInt64 == out_data_type) {
  327. *out = in.attr("astype").cast<py::function>()("int64").cast<py::array>();
  328. } else if (TypeId::kNumberTypeInt32 == out_data_type) {
  329. *out = in.attr("astype").cast<py::function>()("int32").cast<py::array>();
  330. } else if (TypeId::kNumberTypeInt16 == out_data_type) {
  331. *out = in.attr("astype").cast<py::function>()("int16").cast<py::array>();
  332. } else if (TypeId::kNumberTypeInt8 == out_data_type) {
  333. *out = in.attr("astype").cast<py::function>()("int8").cast<py::array>();
  334. } else if (TypeId::kNumberTypeUInt8 == out_data_type) {
  335. *out = in.attr("astype").cast<py::function>()("uint8").cast<py::array>();
  336. } else if (TypeId::kNumberTypeUInt16 == out_data_type) {
  337. *out = in.attr("astype").cast<py::function>()("uint16").cast<py::array>();
  338. } else if (TypeId::kNumberTypeUInt32 == out_data_type) {
  339. *out = in.attr("astype").cast<py::function>()("uint32").cast<py::array>();
  340. } else if (TypeId::kNumberTypeUInt64 == out_data_type) {
  341. *out = in.attr("astype").cast<py::function>()("uint64").cast<py::array>();
  342. } else {
  343. data_type_ = TypeId::kTypeUnknown;
  344. MS_LOG(EXCEPTION) << "Cannot convert from " << TypeIdLabel(in_data_type) << " to " << TypeIdLabel(out_data_type)
  345. << ".";
  346. }
  347. return result;
  348. }
  349. abstract::AbstractBasePtr Tensor::ToAbstract() {
  350. auto tens = shared_from_base<Tensor>();
  351. auto dtype = tens->Dtype();
  352. if (!IsSubType(dtype, kNumber)) {
  353. MS_LOG(EXCEPTION) << "Expect tensor type kNumber but got: " << dtype->ToString() << ".";
  354. }
  355. auto tensor_shape = tens->shape();
  356. auto abs_tensor = std::make_shared<abstract::AbstractTensor>(dtype, tensor_shape);
  357. abs_tensor->set_value(shared_from_base<Tensor>());
  358. return abs_tensor;
  359. }
  360. std::string Tensor::GetShapeAndDataTypeInfo() const {
  361. std::ostringstream buf;
  362. buf << "Tensor \nshape:[" << shape() << "]" << this->Dtype()->ToString();
  363. return buf.str();
  364. }
  365. std::string Tensor::ToString() const {
  366. const int small_tensor_size = 30;
  367. std::ostringstream buf;
  368. buf << "Tensor \nshape:[" << shape() << "]" << this->Dtype()->ToString();
  369. // only print small tensor
  370. if (DataSize() < small_tensor_size) {
  371. buf << "val:" << std::string(py::str(data()));
  372. }
  373. return buf.str();
  374. }
  375. std::string Tensor::ToStringRepr() const {
  376. std::ostringstream buf;
  377. auto type_ptr = this->Dtype();
  378. MS_EXCEPTION_IF_NULL(type_ptr);
  379. buf << "Tensor shape:[" << shape() << "]" << type_ptr->ToString();
  380. buf << "\nval:" << std::string(py::str(data()));
  381. return buf.str();
  382. }
  383. py::array Tensor::data_sync() {
  384. if (device_address_ != nullptr) {
  385. if (!device_address_->SyncDeviceToHost(this->shape(), static_cast<size_t>(this->data().nbytes()), this->data_type(),
  386. this->data_c(true))) {
  387. MS_LOG(EXCEPTION) << "SyncDeviceToHost when asnumpy.";
  388. }
  389. }
  390. return data_;
  391. }
  392. REGISTER_PYBIND_DEFINE(Tensor, ([](const py::module *m) {
  393. // dtype should define before Tensor, because Tensor init depend dtype
  394. (void)py::class_<Tensor, std::shared_ptr<Tensor>>(*m, "Tensor")
  395. .def(py::init<TypePtr, py::tuple>(), py::arg("dtype"), py::arg("shape"))
  396. .def(py::init<py::array, TypePtr>(), py::arg("input"), py::arg("dtype") = nullptr)
  397. .def(py::init<py::float_, TypePtr>(), py::arg("input"), py::arg("dtype") = nullptr)
  398. .def(py::init<py::int_, TypePtr>(), py::arg("input"), py::arg("dtype") = nullptr)
  399. .def(py::init<py::list, TypePtr>(), py::arg("input"), py::arg("dtype") = nullptr)
  400. .def(py::init<py::tuple, TypePtr>(), py::arg("input"), py::arg("dtype") = nullptr)
  401. .def(py::init<Tensor, TypePtr>(), py::arg("input"), py::arg("dtype") = nullptr)
  402. .def_readonly(PYTHON_TENSOR_FLAG, &Tensor::parse_info_)
  403. .def("asnumpy", &Tensor::data_sync, R"mydelimiter(
  404. Convert tensor to numpy.ndarray.
  405. Returns:
  406. numpy.ndarray.
  407. Examples:
  408. >>> data = mindspore.Tensor(np.ones((2, 3)))
  409. >>> array = data.asnumpy()
  410. >>> array
  411. array([[1., 1., 1.],
  412. [1., 1., 1.]])
  413. )mydelimiter")
  414. .def("size", &Tensor::DataSize, R"mydelimiter(
  415. Get tensor's data size.
  416. Returns:
  417. int, the size of tensor.
  418. Examples:
  419. >>> data = mindspore.Tensor(np.ones((2, 3)))
  420. >>> data.size()
  421. 6
  422. )mydelimiter")
  423. .def("dim", &Tensor::DataDim, R"mydelimiter(
  424. Get tensor's data dimension.
  425. Returns:
  426. int, the dimension of tensor.
  427. Examples:
  428. >>> data = mindspore.Tensor(np.ones((2, 3)))
  429. >>> data.dim()
  430. 2
  431. )mydelimiter")
  432. .def("dtype", &Tensor::Dtype, R"mydelimiter(
  433. Get the tensor's data type.
  434. Returns:
  435. type, the data type of tensor.
  436. Examples:
  437. >>> data = mindspore.Tensor(np.ones((2, 1), np.int32))
  438. >>> data.dtype()
  439. Int32
  440. )mydelimiter")
  441. .def("set_dtype", &Tensor::SetDtype, R"mydelimiter(
  442. Set the tensor's data type.
  443. Arg:
  444. dtype (:class:`mindspore.dtype`): The type of output tensor.
  445. Examples:
  446. >>> data = mindspore.Tensor(np.ones((1, 2), np.float32))
  447. >>> data.set_dtype(mindspore.int32)
  448. mindspore.int32
  449. )mydelimiter")
  450. .def("shape", &Tensor::GetPyTupleShape, R"mydelimiter(
  451. Get the tensor's shape.
  452. Returns:
  453. tuple[int], the shape of tensor.
  454. Examples:
  455. >>> data = mindspore.Tensor(np.ones((3, 3)))
  456. >>> data.shape()
  457. (3, 3)
  458. )mydelimiter")
  459. .def("__str__", &Tensor::ToString)
  460. .def("__repr__", &Tensor::ToStringRepr)
  461. .def(py::pickle(
  462. [](const Tensor &t) { // __getstate__
  463. /* Return a tuple that fully encodes the state of the object */
  464. return py::make_tuple(t.data());
  465. },
  466. [](const py::tuple &t) { // __setstate__
  467. if (t.size() != 1) {
  468. throw std::runtime_error("Invalid state!");
  469. }
  470. /* Create a new C++ instance */
  471. Tensor tensor(t[0].cast<py::array>());
  472. return tensor;
  473. }));
  474. (void)py::class_<MetaTensor, std::shared_ptr<MetaTensor>>(*m, "MetaTensor")
  475. .def(py::init<TypePtr, py::tuple>(), py::arg("dtype"), py::arg("shape"));
  476. }));
  477. } // namespace tensor
  478. } // namespace mindspore