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_assign.h 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef TENSOR_ASSIGN_H_
  17. #define TENSOR_ASSIGN_H_
  18. #include "graph/ge_tensor.h"
  19. #include "proto/tensorflow/tensor.pb.h"
  20. namespace domi {
  21. using GeTensorPtr = std::shared_ptr<ge::GeTensor>;
  22. using Status = uint32_t;
  23. using domi::tensorflow::TensorProto;
  24. using google::protobuf::int32;
  25. using google::protobuf::int64;
  26. class TensorAssign {
  27. public:
  28. static Status SetGeTensor(const TensorProto &tensor, GeTensorPtr &weight);
  29. static Status SetGeTensorDataType(int64_t dataType, GeTensorPtr &weight);
  30. static ge::DataType ConvertTensorflowDataType(uint32_t tf_data_type);
  31. private:
  32. static bool CheckBoolVal(tensorflow::DataType data_type);
  33. static bool CheckHalfVal(tensorflow::DataType data_type);
  34. static bool CheckFloatVal(tensorflow::DataType data_type);
  35. static bool CheckDoubleVal(tensorflow::DataType data_type);
  36. static bool CheckComplex64Val(tensorflow::DataType data_type);
  37. static bool CheckComplex128Val(tensorflow::DataType data_type);
  38. static bool CheckStringVal(tensorflow::DataType data_type);
  39. static bool CheckByte(tensorflow::DataType data_type);
  40. static bool CheckDoubleByte(tensorflow::DataType data_type);
  41. static bool CheckSignedFourByte(tensorflow::DataType data_type);
  42. static bool CheckUnsignedFourByte(tensorflow::DataType data_type);
  43. static bool CheckSignedEightByte(tensorflow::DataType data_type);
  44. static bool CheckUnsignedEightByte(tensorflow::DataType data_type);
  45. static Status GetDoubleByteVal(int32_t val_size, const google::protobuf::RepeatedField<int32> &val_vector, int count,
  46. GeTensorPtr &weight);
  47. static Status GetByteVal(int32_t val_size, const google::protobuf::RepeatedField<int32> &val_vector, int count,
  48. GeTensorPtr &weight);
  49. static Status GetStringVal(int32_t val_size, const google::protobuf::RepeatedPtrField<std::string> &val_vector,
  50. int count, GeTensorPtr &weight);
  51. static void SetGeTensorWeightData(const TensorProto &tensor, int32_t val_size, int count, GeTensorPtr &weight);
  52. static void SetWeightData(tensorflow::DataType data_type, int count, const std::string &tensor_content,
  53. GeTensorPtr &weight);
  54. template <typename T>
  55. static Status GetVal(int32_t val_size, const google::protobuf::RepeatedField<T> &val_vector, int count,
  56. GeTensorPtr &weight) {
  57. bool zerosLike = (count != val_size && val_size == 1);
  58. T *addr = new (std::nothrow) T[count]();
  59. GE_CHECK_NOTNULL(addr);
  60. int minCount = (count > val_size) ? val_size : count;
  61. if (!zerosLike) {
  62. for (int32_t i = 0; i < minCount; i++) {
  63. *(addr + i) = val_vector.Get(i);
  64. }
  65. for (int32_t i = minCount; i < count; i++) {
  66. *(addr + i) = val_vector.Get(minCount - 1);
  67. }
  68. } else {
  69. for (int32_t i = 0; i < count; i++) {
  70. *(addr + i) = val_vector.Get(0);
  71. }
  72. }
  73. (void)weight->SetData(reinterpret_cast<uint8_t *>(addr), count * sizeof(T));
  74. GE_DELETE_NEW_ARRAY(addr);
  75. return SUCCESS;
  76. }
  77. };
  78. } // namespace domi
  79. #endif // TENSOR_ASSIGN_H_

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