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.

generator_api.cc 12 kB

5 years ago
5 years ago
5 years ago
4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "generator/generator_api.h"
  17. #include "common/ge/ge_util.h"
  18. #include "common/util.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "generator/ge_generator.h"
  21. #include "graph/ge_attr_value.h"
  22. #include "graph/ge_tensor.h"
  23. #include "graph/op_desc.h"
  24. #include "graph/utils/tensor_utils.h"
  25. #define CHECK_PARAM_NOT_NULL(param) \
  26. do { \
  27. if (param == nullptr) { \
  28. GELOGE(ge::PARAM_INVALID, "Param: %s is null.", #param); \
  29. return ge::PARAM_INVALID; \
  30. } \
  31. } while (0)
  32. #define CHECK_PARAM_OBJECT(object, param) \
  33. ({ \
  34. object *obj_value = reinterpret_cast<object *>(param); \
  35. if (obj_value == nullptr) { \
  36. GELOGE(ge::PARAM_INVALID, "Param: %s is null.", #param); \
  37. return ge::PARAM_INVALID; \
  38. } \
  39. obj_value; \
  40. })
  41. class OpAttr {
  42. public:
  43. OpAttr() = default;
  44. ~OpAttr() = default;
  45. const std::map<string, ge::GeAttrValue> &Attrs() const { return attrs_; }
  46. template <typename T>
  47. Status_t SetAttr(const char *name, T value) {
  48. CHECK_PARAM_NOT_NULL(name);
  49. auto attr_value = ge::GeAttrValue::CreateFrom<T>(value);
  50. attrs_[std::string(name)] = attr_value;
  51. return ge::SUCCESS;
  52. }
  53. template <typename T>
  54. Status_t SetAttr(const char *name, const T *value, int num) {
  55. CHECK_PARAM_NOT_NULL(name);
  56. CHECK_PARAM_NOT_NULL(value);
  57. std::vector<T> values;
  58. for (int i = 0; i < num; ++i) {
  59. values.push_back(value[i]);
  60. }
  61. auto attr_value = ge::GeAttrValue::CreateFrom<std::vector<T>>(values);
  62. attrs_[std::string(name)] = attr_value;
  63. return ge::SUCCESS;
  64. }
  65. Status_t SetAttr(const char *name, const char *value) {
  66. CHECK_PARAM_NOT_NULL(name);
  67. CHECK_PARAM_NOT_NULL(value);
  68. auto attr_value = ge::GeAttrValue::CreateFrom<string>(string(value));
  69. attrs_[std::string(name)] = attr_value;
  70. return ge::SUCCESS;
  71. }
  72. Status_t SetAttr(const char *name, const char **value, int num) {
  73. CHECK_PARAM_NOT_NULL(name);
  74. CHECK_PARAM_NOT_NULL(value);
  75. std::vector<string> values;
  76. for (int i = 0; i < num; ++i) {
  77. values.push_back(string(value[i]));
  78. }
  79. auto attr_value = ge::GeAttrValue::CreateFrom<std::vector<string>>(values);
  80. attrs_[std::string(name)] = attr_value;
  81. return ge::SUCCESS;
  82. }
  83. private:
  84. std::map<string, ge::GeAttrValue> attrs_;
  85. };
  86. /**
  87. * @ingroup ge
  88. * @brief Generate offline model for the op.
  89. * @param [in] op_type: type name of the op.
  90. * @param [in] in_tensor: input description array (created by OpTensorCreate).
  91. * @param [in] in_num: number of in_tensor.
  92. * @param [in] out_tensor: output description array (created by OpTensorCreate).
  93. * @param [in] out_num: number of out_tensor.
  94. * @param [in] attr: the attributes of the op (created by OpAttrCreate).
  95. * @param [in] om_file: file name for the om to save.
  96. * @return 0 for success / others for fail
  97. */
  98. Status_t OpTaskGernerator(const char *op_type, const OpTensor_t *in_tensor, int in_num, const OpTensor_t *out_tensor,
  99. int out_num, const OpAttr_t attr, const char *om_file) {
  100. CHECK_PARAM_NOT_NULL(op_type);
  101. CHECK_PARAM_NOT_NULL(om_file);
  102. const std::string om_file_name(om_file);
  103. std::string op_name = std::string(op_type) + "_" + std::to_string(ge::GetCurrentTimestamp());
  104. ge::OpDescPtr op_desc = ge::MakeShared<ge::OpDesc>(op_name, op_type);
  105. if (op_desc == nullptr) {
  106. return ge::FAILED;
  107. }
  108. std::vector<ge::GeTensor> inputs;
  109. for (int i = 0; i < in_num && in_tensor != nullptr; ++i) {
  110. const ge::TensorDesc *in_desc = CHECK_PARAM_OBJECT(ge::TensorDesc, in_tensor[i]);
  111. ge::GeTensorDesc tensor_desc(ge::GeShape(in_desc->GetShape().GetDims()), in_desc->GetFormat(),
  112. in_desc->GetDataType());
  113. tensor_desc.SetOriginFormat(in_desc->GetFormat());
  114. ge::TensorUtils::SetRealDimCnt(tensor_desc, static_cast<uint32_t>(in_desc->GetShape().GetDims().size()));
  115. ge::TensorUtils::SetInputTensor(tensor_desc, true);
  116. ge::TensorUtils::SetOutputTensor(tensor_desc, false);
  117. if (op_desc->AddInputDesc(tensor_desc) != ge::GRAPH_SUCCESS) {
  118. GELOGE(ge::FAILED, "AddInputDesc fail.");
  119. return ge::FAILED;
  120. }
  121. inputs.emplace_back(tensor_desc);
  122. }
  123. std::vector<ge::GeTensor> outputs;
  124. for (int i = 0; i < out_num && out_tensor != nullptr; ++i) {
  125. const ge::TensorDesc *out_desc = CHECK_PARAM_OBJECT(ge::TensorDesc, out_tensor[i]);
  126. ge::GeTensorDesc tensor_desc(ge::GeShape(out_desc->GetShape().GetDims()), out_desc->GetFormat(),
  127. out_desc->GetDataType());
  128. tensor_desc.SetOriginFormat(out_desc->GetFormat());
  129. ge::TensorUtils::SetRealDimCnt(tensor_desc, static_cast<uint32_t>(out_desc->GetShape().GetDims().size()));
  130. ge::TensorUtils::SetInputTensor(tensor_desc, false);
  131. ge::TensorUtils::SetOutputTensor(tensor_desc, true);
  132. (void)op_desc->AddOutputDesc(tensor_desc);
  133. outputs.emplace_back(tensor_desc);
  134. }
  135. if (attr != nullptr) {
  136. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  137. for (const auto &it : op_attr->Attrs()) {
  138. GE_IF_BOOL_EXEC(op_desc->SetAttr(it.first, it.second) != ge::SUCCESS, GELOGE(ge::FAILED, "SetAttr failed.");
  139. return ge::FAILED);
  140. }
  141. }
  142. ge::GeGenerator generator;
  143. return generator.BuildSingleOpModel(op_desc, inputs, outputs, om_file_name);
  144. }
  145. /**
  146. * @ingroup ge
  147. * @brief Create Tensor Description.
  148. * @param [in] format: tensor format of the data.
  149. * @param [in] datatype: tensor type of the data.
  150. * @param [in] shape: tensor shape array.
  151. * @param [in] num: number of shape.
  152. * @return OpTensor_t for success / nullptr for fail
  153. */
  154. OpTensor_t OpTensorCreate(int format, int datatype, const int64_t *shape, int num) {
  155. std::vector<int64_t> dims;
  156. if (shape != nullptr) {
  157. for (int i = 0; i < num; ++i) {
  158. dims.push_back(shape[i]);
  159. }
  160. }
  161. ge::Format fmt = static_cast<ge::Format>(format);
  162. ge::DataType dt = static_cast<ge::DataType>(datatype);
  163. return new (std::nothrow) ge::TensorDesc(ge::Shape(dims), fmt, dt);
  164. }
  165. /**
  166. * @ingroup ge
  167. * @brief Destroy Tensor Description.
  168. * @param [in] OpTensor_t tensor: created by OpTensorCreate.
  169. * @param [out] none
  170. * @return 0 for success / others for fail.
  171. */
  172. Status_t OpTensorDestroy(OpTensor_t tensor) {
  173. ge::TensorDesc *op_tensor = CHECK_PARAM_OBJECT(ge::TensorDesc, tensor);
  174. delete op_tensor;
  175. op_tensor = nullptr;
  176. return ge::SUCCESS;
  177. }
  178. /**
  179. * @ingroup ge
  180. * @brief Create an attribute holder.
  181. * @param [in] none
  182. * @param [out] none
  183. * @return OpAttr_t for success / nullptr for fail.
  184. */
  185. OpAttr_t OpAttrCreate() { return new (std::nothrow) OpAttr; }
  186. /**
  187. * @ingroup ge
  188. * @brief Destroy Attribute holder.
  189. * @param [in] OpAttr_t attr: created by OpAttrCreate.
  190. * @param [out] none
  191. * @return 0 for success / others for fail.
  192. */
  193. Status_t OpAttrDestroy(OpAttr_t attr) {
  194. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  195. delete op_attr;
  196. return ge::SUCCESS;
  197. }
  198. /**
  199. * @ingroup ge
  200. * @brief Set a boolean attribute to the attribute holder.
  201. * @param [in] attr: attribute holder (created by OpAttrCreate).
  202. * @param [in] name: attribute name (can`t be nullptr, end with '\0').
  203. * @param [in] value: attribute value.
  204. * @return 0 for success / others for fail.
  205. */
  206. Status_t SetAttrBool(OpAttr_t attr, const char *name, bool value) {
  207. CHECK_PARAM_NOT_NULL(name);
  208. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  209. return op_attr->SetAttr(name, value);
  210. }
  211. /**
  212. * @ingroup ge
  213. * @brief Set an integer attribute to the attribute holder.
  214. * @param [in] attr: attribute holder (created by OpAttrCreate).
  215. * @param [in] name: attribute name (can`t be nullptr, end with '\0').
  216. * @param [in] value: attribute value.
  217. * @return 0 for success / others for fail.
  218. */
  219. Status_t SetAttrInt(OpAttr_t attr, const char *name, int64_t value) {
  220. CHECK_PARAM_NOT_NULL(name);
  221. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  222. return op_attr->SetAttr(name, value);
  223. }
  224. /**
  225. * @ingroup ge
  226. * @brief Set a float attribute to the attribute holder.
  227. * @param [in] attr: attribute holder (created by OpAttrCreate).
  228. * @param [in] name: attribute name (can`t be nullptr, end with '\0').
  229. * @param [in] value: attribute value.
  230. * @return 0 for success / others for fail.
  231. */
  232. Status_t SetAttrFloat(OpAttr_t attr, const char *name, float value) {
  233. CHECK_PARAM_NOT_NULL(name);
  234. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  235. return op_attr->SetAttr(name, value);
  236. }
  237. /**
  238. * @ingroup ge
  239. * @brief Set a string attribute to the attribute holder.
  240. * @param [in] attr: attribute holder (created by OpAttrCreate).
  241. * @param [in] name: attribute name (can`t be nullptr, end with '\0').
  242. * @param [in] value: attribute value (can`t be nullptr, end with '\0').
  243. * @return 0 for success / others for fail.
  244. */
  245. Status_t SetAttrString(OpAttr_t attr, const char *name, const char *value) {
  246. CHECK_PARAM_NOT_NULL(name);
  247. CHECK_PARAM_NOT_NULL(value);
  248. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  249. return op_attr->SetAttr(name, string(value));
  250. }
  251. /**
  252. * @ingroup ge
  253. * @brief Set a boolean array attribute to the attribute holder.
  254. * @param [in] attr: attribute holder (created by OpAttrCreate).
  255. * @param [in] name: attribute name (can`t be nullptr, end with '\0').
  256. * @param [in] value: attribute value array.
  257. * @param [in] num: number of value array.
  258. * @return 0 for success / others for fail.
  259. */
  260. Status_t SetAttrBoolList(OpAttr_t attr, const char *name, const bool *value, int num) {
  261. CHECK_PARAM_NOT_NULL(name);
  262. CHECK_PARAM_NOT_NULL(value);
  263. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  264. return op_attr->SetAttr(name, value, num);
  265. }
  266. /**
  267. * @ingroup ge
  268. * @brief Set an integer array attribute to the attribute holder.
  269. * @param [in] attr: attribute holder (created by OpAttrCreate).
  270. * @param [in] name: attribute name (can`t be nullptr, end with '\0').
  271. * @param [in] value: attribute value array.
  272. * @param [in] num: number of value array.
  273. * @return 0 for success / others for fail.
  274. */
  275. Status_t SetAttrIntList(OpAttr_t attr, const char *name, const int64_t *value, int num) {
  276. CHECK_PARAM_NOT_NULL(name);
  277. CHECK_PARAM_NOT_NULL(value);
  278. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  279. return op_attr->SetAttr(name, value, num);
  280. }
  281. /**
  282. * @ingroup ge
  283. * @brief Set a float array attribute to the attribute holder.
  284. * @param [in] attr: attribute holder (created by OpAttrCreate).
  285. * @param [in] name: attribute name (can`t be nullptr, end with '\0').
  286. * @param [in] value: attribute value array.
  287. * @param [in] num: number of value array.
  288. * @return 0 for success / others for fail.
  289. */
  290. Status_t SetAttrFloatList(OpAttr_t attr, const char *name, const float *value, int num) {
  291. CHECK_PARAM_NOT_NULL(name);
  292. CHECK_PARAM_NOT_NULL(value);
  293. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  294. return op_attr->SetAttr(name, value, num);
  295. }
  296. /**
  297. * @ingroup ge
  298. * @brief Set a string array attribute to the attribute holder.
  299. * @param [in] attr: attribute holder (created by OpAttrCreate).
  300. * @param [in] name: attribute name (can`t be nullptr, end with '\0').
  301. * @param [in] value: attribute value array (each value can`t be nullptr, end with '\0').
  302. * @param [in] num: number of value array.
  303. * @return 0 for success / others for fail.
  304. */
  305. Status_t SetAttrStringList(OpAttr_t attr, const char *name, const char **value, int num) {
  306. CHECK_PARAM_NOT_NULL(name);
  307. CHECK_PARAM_NOT_NULL(value);
  308. OpAttr *op_attr = CHECK_PARAM_OBJECT(OpAttr, attr);
  309. return op_attr->SetAttr(name, value, num);
  310. }

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