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.

tensor.cc 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 "external/graph/tensor.h"
  17. #include "debug/ge_util.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "graph/ge_tensor.h"
  20. #include "securec.h"
  21. #include "utils/attr_utils.h"
  22. #include "utils/tensor_adapter.h"
  23. #include "utils/tensor_utils.h"
  24. #include "utils/type_utils.h"
  25. namespace {
  26. /// Extra 8 bytes store pointer of string
  27. /// Extra 1 byte store '\0'
  28. const int EXTRA_STORE_POINTER_FOR_STRING = 8;
  29. const int EXTRA_STORE_POINTER_FOR_STRING_AND_END_SYMBOL = 9;
  30. const int64_t UNKNOWN_DIM_SIZE = -1;
  31. } // namespace
  32. namespace ge {
  33. // If not overflow return true
  34. static bool Int64MulNotOverflow(int64_t a, int64_t b) {
  35. if (a > 0) {
  36. if (b > 0) {
  37. if (a > (INT64_MAX / b)) {
  38. return false;
  39. }
  40. } else {
  41. if (b < (INT64_MIN / a)) {
  42. return false;
  43. }
  44. }
  45. } else {
  46. if (b > 0) {
  47. if (a < (INT64_MIN / b)) {
  48. return false;
  49. }
  50. } else {
  51. if ((a != 0) && (b < (INT64_MAX / a))) {
  52. return false;
  53. }
  54. }
  55. }
  56. return true;
  57. }
  58. class TensorDescImpl {
  59. public:
  60. TensorDescImpl() = default;
  61. ~TensorDescImpl() = default;
  62. TensorDescImpl(const Shape &shape, Format format, DataType dt) : shape_(shape), format_(format), data_type_(dt) {}
  63. Shape shape_;
  64. std::vector<std::pair<int64_t, int64_t>> range_;
  65. Format format_ = FORMAT_ND;
  66. Format origin_format_ = FORMAT_ND;
  67. DataType data_type_ = DT_FLOAT;
  68. Shape origin_shape_;
  69. int64_t size_ = 0;
  70. int64_t real_dim_cnt_ = 0;
  71. std::string name_;
  72. };
  73. class TensorImpl {
  74. public:
  75. TensorImpl() = default;
  76. ~TensorImpl() = default;
  77. explicit TensorImpl(const TensorDesc &tensor_desc) : ge_tensor(TensorAdapter::TensorDesc2GeTensorDesc(tensor_desc)) {}
  78. TensorImpl(const TensorDesc &tensor_desc, const std::vector<uint8_t> &data)
  79. : ge_tensor(TensorAdapter::TensorDesc2GeTensorDesc(tensor_desc), data) {}
  80. TensorImpl(const TensorDesc &tensor_desc, const uint8_t *data, size_t size)
  81. : ge_tensor(TensorAdapter::TensorDesc2GeTensorDesc(tensor_desc), data, size) {}
  82. TensorImpl(TensorDesc &&tensor_desc, std::vector<uint8_t> &&data)
  83. : ge_tensor(TensorAdapter::TensorDesc2GeTensorDesc(tensor_desc), std::move(data)) {}
  84. graphStatus SetData(const std::string &data) {
  85. if (!data.empty()) {
  86. /// Extra 8 bytes store pointer of string
  87. /// Extra 1 byte store '\0'
  88. size_t total_size = data.size() + EXTRA_STORE_POINTER_FOR_STRING_AND_END_SYMBOL;
  89. std::unique_ptr<char[]> buff(new (std::nothrow) char[total_size]());
  90. if (buff == nullptr) {
  91. GELOGE(GRAPH_FAILED, "allocate string raw data buff failed");
  92. return GRAPH_FAILED;
  93. }
  94. uint64_t *p = reinterpret_cast<uint64_t *>(buff.get());
  95. // Front 8 bytes store pointer of string
  96. char *raw_data = buff.get() + EXTRA_STORE_POINTER_FOR_STRING;
  97. p[0] = reinterpret_cast<uintptr_t>(raw_data);
  98. int32_t memcpy_ret = memcpy_s(raw_data,
  99. total_size - EXTRA_STORE_POINTER_FOR_STRING,
  100. data.c_str(),
  101. data.size() + 1);
  102. GE_CHK_BOOL_RET_STATUS(memcpy_ret == EOK, GRAPH_FAILED, "copy data failed");
  103. (void)ge_tensor.SetData(reinterpret_cast<const uint8_t *>(buff.get()), total_size);
  104. return GRAPH_SUCCESS;
  105. }
  106. return GRAPH_FAILED;
  107. }
  108. graphStatus SetData(const std::vector<std::string> &data) {
  109. if (data.empty()) {
  110. GELOGE(GRAPH_FAILED, "there is no data, please check the input variable");
  111. return GRAPH_FAILED;
  112. }
  113. size_t total_size = 0;
  114. for (auto str : data) {
  115. /// Extra 8 bytes store pointer of each string
  116. /// Extra 1 byte store '\0'
  117. total_size += (str.size() + EXTRA_STORE_POINTER_FOR_STRING_AND_END_SYMBOL);
  118. }
  119. std::unique_ptr<char[]> buff(new (std::nothrow) char[total_size]);
  120. if (buff == nullptr) {
  121. GELOGE(GRAPH_FAILED, "allocate string raw data buff failed");
  122. return GRAPH_FAILED;
  123. }
  124. uint64_t *p = reinterpret_cast<uint64_t *>(buff.get());
  125. // Front some bytes store pointer of each string
  126. char *raw_data = buff.get() + data.size() * sizeof(uint64_t);
  127. uint64_t ptr_size = data.size() * sizeof(uint64_t);
  128. for (size_t i = 0; i < data.size(); ++i) {
  129. p[i] = reinterpret_cast<uintptr_t>(raw_data);
  130. if (total_size < ptr_size) {
  131. GELOGE(GRAPH_FAILED, "Subtraction invalid, total_size: %zu, ptr_size: %lu", total_size, ptr_size);
  132. return GRAPH_FAILED;
  133. }
  134. int32_t memcpy_ret = memcpy_s(raw_data, total_size - ptr_size, data[i].c_str(), data[i].size() + 1);
  135. GE_CHK_BOOL_RET_STATUS(memcpy_ret == EOK, GRAPH_FAILED, "copy data failed");
  136. raw_data += (data[i].size() + 1);
  137. ptr_size += (data[i].size() + 1);
  138. }
  139. (void)ge_tensor.SetData(reinterpret_cast<const uint8_t *>(buff.get()), total_size);
  140. return GRAPH_SUCCESS;
  141. }
  142. GeTensor ge_tensor;
  143. };
  144. class ShapeImpl {
  145. public:
  146. ShapeImpl() = default;
  147. ~ShapeImpl() = default;
  148. explicit ShapeImpl(const std::vector<int64_t> &dims) {
  149. bool is_unknown_dim_num = false;
  150. for (const auto &dim : dims) {
  151. if (dim == UNKNOWN_DIM_NUM) {
  152. is_unknown_dim_num = true;
  153. break;
  154. }
  155. }
  156. dims_ = is_unknown_dim_num ? std::vector<int64_t>({UNKNOWN_DIM_NUM}) : dims;
  157. }
  158. std::vector<int64_t> dims_;
  159. };
  160. Shape::Shape() { impl_ = ComGraphMakeShared<ShapeImpl>(); }
  161. Shape::Shape(const std::vector<int64_t> &dims) { impl_ = ComGraphMakeShared<ShapeImpl>(dims); }
  162. size_t Shape::GetDimNum() const {
  163. if (impl_ != nullptr) {
  164. for (auto i : impl_->dims_) {
  165. if (i == UNKNOWN_DIM_NUM) {
  166. return 0;
  167. }
  168. }
  169. return impl_->dims_.size();
  170. }
  171. return 0;
  172. }
  173. int64_t Shape::GetDim(size_t idx) const {
  174. if (impl_ != nullptr) {
  175. if (idx >= impl_->dims_.size()) {
  176. return 0;
  177. }
  178. return impl_->dims_[idx];
  179. }
  180. return 0;
  181. }
  182. graphStatus Shape::SetDim(size_t idx, int64_t value) {
  183. if (impl_ != nullptr) {
  184. if (idx >= impl_->dims_.size()) {
  185. return GRAPH_FAILED;
  186. }
  187. impl_->dims_[idx] = value;
  188. return GRAPH_SUCCESS;
  189. }
  190. return GRAPH_FAILED;
  191. }
  192. std::vector<int64_t> Shape::GetDims() const {
  193. vector<int64_t> dims;
  194. if (impl_ != nullptr) {
  195. return impl_->dims_;
  196. }
  197. return dims;
  198. }
  199. int64_t Shape::GetShapeSize() const {
  200. if (impl_ != nullptr) {
  201. if (impl_->dims_.empty()) {
  202. return 0;
  203. }
  204. int64_t size = 1;
  205. for (auto i : impl_->dims_) {
  206. if (i == UNKNOWN_DIM_NUM || i == UNKNOWN_DIM) {
  207. return UNKNOWN_DIM_SIZE;
  208. }
  209. if (!Int64MulNotOverflow(size, i)) {
  210. GELOGE(GRAPH_FAILED, "mul overflow: %ld, %ld", size, i);
  211. size = 0;
  212. return size;
  213. }
  214. size *= i;
  215. }
  216. return size;
  217. }
  218. return 0;
  219. }
  220. TensorDesc::TensorDesc() {
  221. impl = ComGraphMakeShared<TensorDescImpl>(); // lint !e665
  222. }
  223. TensorDesc::TensorDesc(Shape shape, Format format, DataType dt) {
  224. impl = ComGraphMakeShared<TensorDescImpl>(shape, format, dt); // lint !e665
  225. SetRealDimCnt(shape.GetDimNum());
  226. }
  227. TensorDesc::TensorDesc(const TensorDesc &desc) {
  228. // Copy
  229. impl = ComGraphMakeShared<TensorDescImpl>(); // lint !e665
  230. if (desc.impl != nullptr && impl != nullptr) {
  231. *impl = *desc.impl;
  232. }
  233. }
  234. TensorDesc::TensorDesc(TensorDesc &&desc) {
  235. // Move
  236. impl = std::move(desc.impl);
  237. }
  238. TensorDesc &TensorDesc::operator=(const TensorDesc &desc) {
  239. // Copy
  240. if (&desc != this) {
  241. impl = ComGraphMakeShared<TensorDescImpl>();
  242. if (desc.impl != nullptr && impl != nullptr) {
  243. *impl = *desc.impl;
  244. }
  245. }
  246. return *this;
  247. }
  248. TensorDesc &TensorDesc::operator=(TensorDesc &&desc) {
  249. if (&desc != this) {
  250. impl = std::move(desc.impl);
  251. }
  252. return *this;
  253. }
  254. void TensorDesc::Update(const Shape &shape, Format format, DataType dt) {
  255. if (impl != nullptr) {
  256. impl->shape_ = shape;
  257. impl->format_ = format;
  258. impl->data_type_ = dt;
  259. }
  260. }
  261. Shape TensorDesc::GetShape() const {
  262. if (impl != nullptr) {
  263. return impl->shape_;
  264. }
  265. return Shape();
  266. }
  267. void TensorDesc::SetShape(const Shape &shape) {
  268. if (impl != nullptr) {
  269. impl->shape_ = shape;
  270. }
  271. }
  272. // set shape with -2, it stand for unknown shape
  273. graphStatus TensorDesc::SetUnknownDimNumShape() {
  274. if (impl != nullptr) {
  275. impl->shape_ = Shape({UNKNOWN_DIM_NUM});
  276. return GRAPH_SUCCESS;
  277. }
  278. GELOGE(GRAPH_FAILED, "Set unknown shape failed,because no impl class!");
  279. return GRAPH_FAILED;
  280. }
  281. // for unknown shape
  282. graphStatus TensorDesc::SetShapeRange(const std::vector<std::pair<int64_t, int64_t>> &range) {
  283. if (impl != nullptr) {
  284. impl->range_ = range;
  285. return GRAPH_SUCCESS;
  286. }
  287. GELOGE(GRAPH_FAILED, "SetShapeRange failed!impl is nullptr!");
  288. return GRAPH_FAILED;
  289. }
  290. graphStatus TensorDesc::GetShapeRange(std::vector<std::pair<int64_t, int64_t>> &range) const {
  291. if (impl != nullptr) {
  292. range = impl->range_;
  293. return GRAPH_SUCCESS;
  294. }
  295. GELOGE(GRAPH_FAILED, "impl is nullptr!");
  296. return GRAPH_FAILED;
  297. }
  298. Shape TensorDesc::GetOriginShape() const {
  299. if (impl != nullptr) {
  300. return impl->origin_shape_;
  301. }
  302. return Shape();
  303. }
  304. void TensorDesc::SetOriginShape(const Shape &origin_shape) {
  305. if (impl != nullptr) {
  306. impl->origin_shape_ = origin_shape;
  307. }
  308. }
  309. Format TensorDesc::GetFormat() const {
  310. if (impl != nullptr) {
  311. return impl->format_;
  312. }
  313. return FORMAT_RESERVED;
  314. }
  315. void TensorDesc::SetFormat(Format format) {
  316. if (impl != nullptr) {
  317. impl->format_ = format;
  318. }
  319. }
  320. Format TensorDesc::GetOriginFormat() const {
  321. if (impl != nullptr) {
  322. return impl->origin_format_;
  323. }
  324. return FORMAT_RESERVED;
  325. }
  326. void TensorDesc::SetOriginFormat(Format origin_format) {
  327. if (impl != nullptr) {
  328. impl->origin_format_ = origin_format;
  329. }
  330. }
  331. DataType TensorDesc::GetDataType() const {
  332. if (impl != nullptr) {
  333. return impl->data_type_;
  334. }
  335. return DT_UNDEFINED;
  336. }
  337. void TensorDesc::SetDataType(DataType dt) {
  338. if (impl != nullptr) {
  339. impl->data_type_ = dt;
  340. }
  341. }
  342. void TensorDesc::SetSize(int64_t size) {
  343. if (impl != nullptr) {
  344. impl->size_ = size;
  345. }
  346. }
  347. int64_t TensorDesc::GetSize() const {
  348. if (impl != nullptr) {
  349. return impl->size_;
  350. }
  351. return 0;
  352. }
  353. void TensorDesc::SetRealDimCnt(const int64_t real_dim_cnt) {
  354. if (impl != nullptr) {
  355. impl->real_dim_cnt_ = real_dim_cnt;
  356. }
  357. }
  358. int64_t TensorDesc::GetRealDimCnt() const {
  359. if (impl != nullptr) {
  360. return impl->real_dim_cnt_;
  361. }
  362. return 0;
  363. }
  364. std::string TensorDesc::GetName() const {
  365. if (impl != nullptr) {
  366. return impl->name_;
  367. }
  368. return "";
  369. }
  370. void TensorDesc::SetName(const std::string &name) {
  371. if (impl != nullptr) {
  372. impl->name_ = name;
  373. }
  374. }
  375. graphStatus TensorDesc::GetName(AscendString &name) {
  376. if (impl != nullptr) {
  377. name = AscendString(impl->name_.c_str());
  378. return GRAPH_SUCCESS;
  379. }
  380. return GRAPH_FAILED;
  381. }
  382. void TensorDesc::SetName(const char *name) {
  383. if (impl != nullptr && name != nullptr) {
  384. impl->name_ = name;
  385. }
  386. }
  387. Tensor::Tensor() { impl = ComGraphMakeShared<TensorImpl>(); }
  388. Tensor::Tensor(const TensorDesc &tensor_desc) {
  389. impl = ComGraphMakeShared<TensorImpl>(tensor_desc); // lint !e665
  390. }
  391. Tensor::Tensor(const TensorDesc &tensor_desc, const std::vector<uint8_t> &data) {
  392. uint64_t shape_size = tensor_desc.GetShape().GetShapeSize();
  393. DataType data_type = tensor_desc.GetDataType();
  394. uint32_t type_length;
  395. bool ret = TypeUtils::GetDataTypeLength(data_type, type_length);
  396. if (!ret) {
  397. GELOGW("datatype %d is not found.", data_type);
  398. }
  399. auto data_size = data.size();
  400. if (ret && (shape_size || (data_size != type_length))) {
  401. if (type_length != 0 && UINT64_MAX / type_length < shape_size) {
  402. GELOGW("mul overflow: %lu, %u", shape_size, type_length);
  403. } else {
  404. if (shape_size * type_length != data_size) {
  405. GELOGW("tensor length not equal: shape_byte_size=%lu, data_size=%zu, dt_type=%s.", shape_size * type_length,
  406. data_size, TypeUtils::DataTypeToSerialString(data_type).c_str());
  407. }
  408. }
  409. }
  410. impl = ComGraphMakeShared<TensorImpl>(tensor_desc, data); // lint !e665
  411. }
  412. Tensor::Tensor(const TensorDesc &tensor_desc, const uint8_t *data, size_t size) {
  413. uint64_t shape_size = tensor_desc.GetShape().GetShapeSize();
  414. DataType data_type = tensor_desc.GetDataType();
  415. uint32_t type_length;
  416. bool ret = TypeUtils::GetDataTypeLength(data_type, type_length);
  417. if (!ret) {
  418. GELOGW("datatype %d is not found.", data_type);
  419. }
  420. if (ret && (shape_size || (size != type_length))) {
  421. if (type_length != 0 && UINT64_MAX / type_length < shape_size) {
  422. GELOGW("mul overflow: %lu, %u", shape_size, type_length);
  423. } else {
  424. if (shape_size * type_length != size) {
  425. GELOGW("tensor length not equal: shape_byte_size=%lu, data_size=%zu, dt_type=%s.", shape_size * type_length,
  426. size, TypeUtils::DataTypeToSerialString(data_type).c_str());
  427. }
  428. }
  429. }
  430. impl = ComGraphMakeShared<TensorImpl>(tensor_desc, data, size); // lint !e665
  431. }
  432. Tensor::Tensor(TensorDesc &&tensor_desc, std::vector<uint8_t> &&data) {
  433. uint64_t shape_size = tensor_desc.GetShape().GetShapeSize();
  434. DataType data_type = tensor_desc.GetDataType();
  435. uint32_t type_length;
  436. bool ret = TypeUtils::GetDataTypeLength(data_type, type_length);
  437. if (!ret) {
  438. GELOGW("datatype %d is not found.", data_type);
  439. }
  440. auto data_size = data.size();
  441. if (ret && (shape_size || (data_size != type_length))) {
  442. if (type_length != 0 && UINT64_MAX / type_length < shape_size) {
  443. GELOGW("mul overflow: %lu, %u", shape_size, type_length);
  444. } else {
  445. if (shape_size * type_length != data_size) {
  446. GELOGW("tensor length not equal: shape_byte_size=%lu, data_size=%zu, dt_type=%s.", shape_size * type_length,
  447. data_size, TypeUtils::DataTypeToSerialString(data_type).c_str());
  448. }
  449. }
  450. }
  451. impl = ComGraphMakeShared<TensorImpl>(std::move(tensor_desc), std::move(data)); // lint !e665
  452. }
  453. TensorDesc Tensor::GetTensorDesc() const {
  454. if (impl != nullptr) {
  455. return TensorAdapter::GeTensorDesc2TensorDesc(impl->ge_tensor.MutableTensorDesc());
  456. }
  457. return TensorDesc();
  458. }
  459. graphStatus Tensor::SetTensorDesc(const TensorDesc &tensor_desc) {
  460. if (impl != nullptr) {
  461. impl->ge_tensor.SetTensorDesc(TensorAdapter::TensorDesc2GeTensorDesc(tensor_desc));
  462. return GRAPH_SUCCESS;
  463. }
  464. return GRAPH_FAILED;
  465. }
  466. const uint8_t *Tensor::GetData() const {
  467. if (impl != nullptr) {
  468. return impl->ge_tensor.GetData().data();
  469. }
  470. return nullptr;
  471. }
  472. uint8_t *Tensor::GetData() {
  473. if (impl != nullptr) {
  474. return impl->ge_tensor.MutableData().data();
  475. }
  476. return nullptr;
  477. }
  478. size_t Tensor::GetSize() const {
  479. if (impl != nullptr) {
  480. return impl->ge_tensor.GetData().size();
  481. }
  482. return 0;
  483. }
  484. graphStatus Tensor::SetData(std::vector<uint8_t> &&data) {
  485. if (impl != nullptr) {
  486. (void)impl->ge_tensor.SetData(data);
  487. return GRAPH_SUCCESS;
  488. }
  489. return GRAPH_FAILED;
  490. }
  491. graphStatus Tensor::SetData(const std::vector<uint8_t> &data) {
  492. if (impl != nullptr) {
  493. (void)impl->ge_tensor.SetData(data);
  494. return GRAPH_SUCCESS;
  495. }
  496. return GRAPH_FAILED;
  497. }
  498. graphStatus Tensor::SetData(const uint8_t *data, size_t size) {
  499. if (impl != nullptr) {
  500. (void)impl->ge_tensor.SetData(data, size);
  501. return GRAPH_SUCCESS;
  502. }
  503. return GRAPH_FAILED;
  504. }
  505. graphStatus Tensor::SetData(const std::string &data) {
  506. if (impl != nullptr) {
  507. if (impl->SetData(data) != GRAPH_SUCCESS) {
  508. GELOGE(GRAPH_FAILED, "Tensor set data failed.");
  509. return GRAPH_FAILED;
  510. }
  511. return GRAPH_SUCCESS;
  512. }
  513. return GRAPH_FAILED;
  514. }
  515. graphStatus Tensor::SetData(const std::vector<std::string> &data) {
  516. if (impl != nullptr) {
  517. if (impl->SetData(data) != GRAPH_SUCCESS) {
  518. GELOGE(GRAPH_FAILED, "Tensor set vector data failed.");
  519. return GRAPH_FAILED;
  520. }
  521. return GRAPH_SUCCESS;
  522. }
  523. return GRAPH_FAILED;
  524. }
  525. graphStatus Tensor::SetData(const char *data) {
  526. if (impl != nullptr && data != nullptr) {
  527. std::string tensor_data = data;
  528. if (impl->SetData(tensor_data) != GRAPH_SUCCESS) {
  529. GELOGE(GRAPH_FAILED, "Tensor set data failed.");
  530. return GRAPH_FAILED;
  531. }
  532. return GRAPH_SUCCESS;
  533. }
  534. return GRAPH_FAILED;
  535. }
  536. graphStatus Tensor::SetData(const std::vector<AscendString> &datas) {
  537. if (impl != nullptr) {
  538. std::vector<std::string> tensor_data;
  539. for (auto &data : datas) {
  540. if (data.GetString() == nullptr) {
  541. GELOGE(GRAPH_FAILED, "Data is nullptr.");
  542. return GRAPH_FAILED;
  543. }
  544. tensor_data.emplace_back(data.GetString());
  545. }
  546. if (impl->SetData(tensor_data) != GRAPH_SUCCESS) {
  547. GELOGE(GRAPH_FAILED, "Tensor set vector data failed.");
  548. return GRAPH_FAILED;
  549. }
  550. return GRAPH_SUCCESS;
  551. }
  552. return GRAPH_FAILED;
  553. }
  554. graphStatus Tensor::IsValid() {
  555. uint64_t shape_size = GetTensorDesc().GetShape().GetShapeSize();
  556. DataType data_type = GetTensorDesc().GetDataType();
  557. uint32_t type_length;
  558. bool ret = TypeUtils::GetDataTypeLength(data_type, type_length);
  559. if (!ret) {
  560. GELOGW("datatype %d is not found.", data_type);
  561. return GRAPH_SUCCESS;
  562. }
  563. size_t data_size = GetSize();
  564. if (data_type != DT_STRING) {
  565. if (shape_size || (data_size != type_length)) {
  566. if (type_length != 0 && UINT64_MAX / type_length < shape_size) {
  567. GELOGW("mul overflow: %lu, %u", shape_size, type_length);
  568. } else {
  569. if (shape_size * type_length != data_size) {
  570. GELOGW("tensor length not equal: shape_byte_size=%lu, data_size=%zu, dt_type=%s.", shape_size * type_length,
  571. data_size, TypeUtils::DataTypeToSerialString(data_type).c_str());
  572. return GRAPH_FAILED;
  573. }
  574. }
  575. }
  576. }
  577. return GRAPH_SUCCESS;
  578. }
  579. Tensor Tensor::Clone() const {
  580. Tensor tensor;
  581. if (impl != nullptr && tensor.impl != nullptr) {
  582. tensor.impl->ge_tensor = impl->ge_tensor.Clone();
  583. }
  584. return tensor;
  585. }
  586. GeTensorDesc TensorAdapter::TensorDesc2GeTensorDesc(const TensorDesc &tensor_desc) {
  587. GeTensorDesc ge_tensor_desc(GeShape(tensor_desc.GetShape().GetDims()), tensor_desc.GetFormat(),
  588. tensor_desc.GetDataType());
  589. ge_tensor_desc.SetOriginShape(GeShape(tensor_desc.GetOriginShape().GetDims()));
  590. ge_tensor_desc.SetOriginFormat(tensor_desc.GetOriginFormat());
  591. ge_tensor_desc.SetName(tensor_desc.GetName());
  592. std::vector<std::pair<int64_t, int64_t>> shape_range;
  593. auto status = tensor_desc.GetShapeRange(shape_range);
  594. if (status != GRAPH_SUCCESS) {
  595. GELOGE(GRAPH_FAILED, "Get shape range failed!");
  596. return ge_tensor_desc;
  597. }
  598. status = ge_tensor_desc.SetShapeRange(shape_range);
  599. if (status != GRAPH_SUCCESS) {
  600. GELOGE(GRAPH_FAILED, "Set shape range failed!");
  601. return ge_tensor_desc;
  602. }
  603. auto size = tensor_desc.GetSize();
  604. TensorUtils::SetSize(ge_tensor_desc, size);
  605. auto real_dim_cnt = static_cast<uint32_t>(tensor_desc.GetRealDimCnt());
  606. TensorUtils::SetRealDimCnt(ge_tensor_desc, real_dim_cnt);
  607. return ge_tensor_desc;
  608. }
  609. TensorDesc TensorAdapter::GeTensorDesc2TensorDesc(const GeTensorDesc &ge_tensor_desc) {
  610. TensorDesc tensor_desc(Shape(ge_tensor_desc.GetShape().GetDims()), ge_tensor_desc.GetFormat(),
  611. ge_tensor_desc.GetDataType());
  612. tensor_desc.SetOriginShape(Shape(ge_tensor_desc.GetOriginShape().GetDims()));
  613. tensor_desc.SetOriginFormat(ge_tensor_desc.GetOriginFormat());
  614. tensor_desc.SetName(ge_tensor_desc.GetName());
  615. std::vector<std::pair<int64_t, int64_t>> shape_range;
  616. auto status = ge_tensor_desc.GetShapeRange(shape_range);
  617. if (status != GRAPH_SUCCESS) {
  618. GELOGE(GRAPH_FAILED, "Get shape range failed!");
  619. return tensor_desc;
  620. }
  621. status = tensor_desc.SetShapeRange(shape_range);
  622. if (status != GRAPH_SUCCESS) {
  623. GELOGE(GRAPH_FAILED, "Set shape range failed!");
  624. return tensor_desc;
  625. }
  626. int64_t size = 0;
  627. (void)TensorUtils::GetSize(ge_tensor_desc, size);
  628. tensor_desc.SetSize(size);
  629. uint32_t real_dim_cnt = 0;
  630. (void)TensorUtils::GetRealDimCnt(ge_tensor_desc, real_dim_cnt);
  631. tensor_desc.SetRealDimCnt(real_dim_cnt);
  632. return tensor_desc;
  633. }
  634. GeTensorPtr TensorAdapter::Tensor2GeTensor(const Tensor &tensor) {
  635. GeTensorPtr ge_tensor;
  636. if (tensor.impl != nullptr) {
  637. ge_tensor = ComGraphMakeShared<GeTensor>(tensor.impl->ge_tensor.Clone()); // lint !e665
  638. }
  639. return ge_tensor;
  640. }
  641. Tensor TensorAdapter::GeTensor2Tensor(const ConstGeTensorPtr &ge_tensor) {
  642. Tensor tensor;
  643. if (ge_tensor != nullptr && tensor.impl != nullptr) {
  644. tensor.impl->ge_tensor = ge_tensor->Clone();
  645. }
  646. return tensor;
  647. }
  648. ConstGeTensorPtr TensorAdapter::AsGeTensorPtr(const Tensor &tensor) {
  649. GeTensorPtr ge_tensor;
  650. if (tensor.impl != nullptr) {
  651. ge_tensor = ComGraphMakeShared<GeTensor>(tensor.impl->ge_tensor); // lint !e665
  652. }
  653. return ge_tensor;
  654. }
  655. GeTensorPtr TensorAdapter::AsGeTensorPtr(Tensor &tensor) {
  656. GeTensorPtr ge_tensor;
  657. if (tensor.impl != nullptr) {
  658. ge_tensor = ComGraphMakeShared<GeTensor>(tensor.impl->ge_tensor); // lint !e665
  659. }
  660. return ge_tensor;
  661. }
  662. const GeTensor TensorAdapter::AsGeTensor(const Tensor &tensor) {
  663. if (tensor.impl != nullptr) {
  664. return tensor.impl->ge_tensor;
  665. }
  666. return GeTensor();
  667. }
  668. GeTensor TensorAdapter::AsGeTensor(Tensor &tensor) {
  669. if (tensor.impl != nullptr) {
  670. return tensor.impl->ge_tensor;
  671. }
  672. return GeTensor();
  673. }
  674. const Tensor TensorAdapter::AsTensor(const GeTensor &ge_tensor) {
  675. Tensor tensor;
  676. if (tensor.impl != nullptr) {
  677. tensor.impl->ge_tensor = ge_tensor;
  678. }
  679. return tensor;
  680. }
  681. Tensor TensorAdapter::AsTensor(GeTensor &ge_tensor) {
  682. Tensor tensor;
  683. if (tensor.impl != nullptr) {
  684. tensor.impl->ge_tensor = ge_tensor;
  685. }
  686. return tensor;
  687. }
  688. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示