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 20 kB

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

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