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

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