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.

attr_value_util.cc 18 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/common/op/attr_value_util.h"
  17. #include "framework/common/debug/log.h"
  18. #include "framework/common/util.h"
  19. #include "external/register/register_types.h"
  20. namespace ge {
  21. #define DEFINE_SET_ATTR_VALUE_ONE(ARG_TYPE, FIELD) \
  22. FMK_FUNC_DEV_VISIBILITY void SetAttrDef(ARG_TYPE value, AttrDef *out) { \
  23. GE_CHECK_NOTNULL_JUST_RETURN(out); \
  24. out->set_##FIELD(value); \
  25. }
  26. #define DEFINE_SET_ATTR_VALUE_LIST(ARG_TYPE, FIELD) \
  27. FMK_FUNC_DEV_VISIBILITY void SetAttrList(ARG_TYPE value, AttrDef *out) { \
  28. GE_CHECK_NOTNULL_JUST_RETURN(out); \
  29. GE_CHECK_NOTNULL_JUST_RETURN(out->mutable_list()); \
  30. out->mutable_list()->add_##FIELD(value); \
  31. }
  32. DEFINE_SET_ATTR_VALUE_ONE(const std::string &, s);
  33. DEFINE_SET_ATTR_VALUE_ONE(const char *, s);
  34. DEFINE_SET_ATTR_VALUE_ONE(const uint32_t, u);
  35. DEFINE_SET_ATTR_VALUE_ONE(const int32_t, i);
  36. DEFINE_SET_ATTR_VALUE_ONE(const int64_t, i);
  37. DEFINE_SET_ATTR_VALUE_ONE(const float, f);
  38. DEFINE_SET_ATTR_VALUE_ONE(const double, f);
  39. DEFINE_SET_ATTR_VALUE_ONE(const bool, b);
  40. DEFINE_SET_ATTR_VALUE_LIST(float, f);
  41. DEFINE_SET_ATTR_VALUE_LIST(double, f);
  42. DEFINE_SET_ATTR_VALUE_LIST(uint32_t, u);
  43. DEFINE_SET_ATTR_VALUE_LIST(int32_t, i);
  44. DEFINE_SET_ATTR_VALUE_LIST(bool, b);
  45. DEFINE_SET_ATTR_VALUE_LIST(int64_t, i);
  46. DEFINE_SET_ATTR_VALUE_LIST(const std::string &, s);
  47. #define ADD_TO_ATTR_MAP(KEY, VALUE, ATTR_MAP) \
  48. do { \
  49. GE_CHECK_NOTNULL_JUST_RETURN(ATTR_MAP); \
  50. AttrDef out; \
  51. auto it = ATTR_MAP->find(KEY); \
  52. if (it != ATTR_MAP->end()) { \
  53. auto &attr_value = it->second; \
  54. SetAttrDef(VALUE, &attr_value); \
  55. } else { \
  56. SetAttrDef(VALUE, &out); \
  57. ATTR_MAP->insert(AttrDefPair(KEY, out)); \
  58. } \
  59. } while (0);
  60. #define ADD_TO_ATTR_MAP_LIST(KEY, VALUE, ATTR_MAP) \
  61. do { \
  62. GE_CHECK_NOTNULL_JUST_RETURN(ATTR_MAP); \
  63. AttrDef out; \
  64. auto it = ATTR_MAP->find(KEY); \
  65. if (it != ATTR_MAP->end()) { \
  66. auto &attr_value = it->second; \
  67. SetAttrList(VALUE, &attr_value); \
  68. } else { \
  69. SetAttrList(VALUE, &out); \
  70. ATTR_MAP->insert(AttrDefPair(KEY, out)); \
  71. } \
  72. } while (0);
  73. #define DEFINE_ADD_ATTR_VALUE(KEY_TYPE, VALUE_TYPE) \
  74. void AddOpAttr(KEY_TYPE map_key, VALUE_TYPE value, OpDef *op_def) { \
  75. GE_CHECK_NOTNULL_JUST_RETURN(op_def); \
  76. auto attr = op_def->mutable_attr(); \
  77. ADD_TO_ATTR_MAP(map_key, value, attr) \
  78. } \
  79. void AddOpAttr(KEY_TYPE map_key, VALUE_TYPE value, AttrDefMap *attr_map) { \
  80. ADD_TO_ATTR_MAP(map_key, value, attr_map) \
  81. } \
  82. void AddModelAttr(KEY_TYPE map_key, VALUE_TYPE value, ModelDef *model_def) { \
  83. GE_CHECK_NOTNULL_JUST_RETURN(model_def); \
  84. auto attr = model_def->mutable_attr(); \
  85. ADD_TO_ATTR_MAP(map_key, value, attr) \
  86. }
  87. #define DEFINE_ADD_ATTR_VALUE_LIST(KEY_TYPE, VALUE_TYPE) \
  88. void AddOpAttrList(KEY_TYPE map_key, VALUE_TYPE value, OpDef *op_def) { \
  89. GE_CHECK_NOTNULL_JUST_RETURN(op_def); \
  90. auto attr = op_def->mutable_attr(); \
  91. ADD_TO_ATTR_MAP_LIST(map_key, value, attr) \
  92. } \
  93. void AddOpAttrList(KEY_TYPE map_key, VALUE_TYPE value, AttrDefMap *attr_map) { \
  94. ADD_TO_ATTR_MAP_LIST(map_key, value, attr_map)} FMK_FUNC_DEV_VISIBILITY void \
  95. AddModelAttrList(KEY_TYPE map_key, VALUE_TYPE value, ModelDef *model_def) { \
  96. GE_CHECK_NOTNULL_JUST_RETURN(model_def); \
  97. auto attr = model_def->mutable_attr(); \
  98. ADD_TO_ATTR_MAP_LIST(map_key, value, attr) \
  99. }
  100. DEFINE_ADD_ATTR_VALUE(const std::string &, const std::string &);
  101. DEFINE_ADD_ATTR_VALUE(const char *, const char *);
  102. DEFINE_ADD_ATTR_VALUE(const std::string &, const char *);
  103. DEFINE_ADD_ATTR_VALUE(const std::string &, const uint32_t);
  104. DEFINE_ADD_ATTR_VALUE(const std::string &, const int32_t);
  105. DEFINE_ADD_ATTR_VALUE(const std::string &, const int64_t);
  106. DEFINE_ADD_ATTR_VALUE(const std::string &, const float);
  107. DEFINE_ADD_ATTR_VALUE(const std::string &, const double);
  108. DEFINE_ADD_ATTR_VALUE(const std::string &, const bool);
  109. DEFINE_ADD_ATTR_VALUE_LIST(const std::string &, const uint32_t);
  110. DEFINE_ADD_ATTR_VALUE_LIST(const std::string &, const float);
  111. DEFINE_ADD_ATTR_VALUE_LIST(const std::string &, const double);
  112. DEFINE_ADD_ATTR_VALUE_LIST(const std::string &, const int32_t);
  113. DEFINE_ADD_ATTR_VALUE_LIST(const std::string &, const bool);
  114. DEFINE_ADD_ATTR_VALUE_LIST(const std::string &, const int64_t);
  115. DEFINE_ADD_ATTR_VALUE_LIST(const std::string &, const std::string &);
  116. void AddOpAttr(const std::string &map_key, AttrDef &attr, OpDef *op_def) {
  117. GE_CHECK_NOTNULL_JUST_RETURN(op_def);
  118. GE_CHECK_NOTNULL_JUST_RETURN(op_def->mutable_attr());
  119. (void)op_def->mutable_attr()->insert(AttrDefPair(map_key, attr));
  120. }
  121. #define DEFINE_GET_ATTR_VALUE(ARG_TYPE_KEY, ARG_TYPE_VALUE, FIELD) \
  122. bool GetAttrDefValue(ARG_TYPE_KEY map_key, ARG_TYPE_VALUE value, const AttrDefMap &attr) { \
  123. auto it = attr.find(map_key); \
  124. if (it != attr.end()) { \
  125. *value = it->second.FIELD(); \
  126. return true; \
  127. } \
  128. return false; \
  129. }
  130. #define DEFINE_GET_ATTR_POINT_REF(ARG_TYPE_KEY, ARG_TYPE_VALUE, FIELD) \
  131. bool GetAttrDefValue(ARG_TYPE_KEY map_key, ARG_TYPE_VALUE *&value, AttrDefMap *attr) { \
  132. GE_RT_FALSE_CHECK_NOTNULL(attr); \
  133. auto it = attr->find(map_key); \
  134. if (it != attr->end()) { \
  135. value = it->second.mutable_##FIELD(); \
  136. return true; \
  137. } \
  138. return false; \
  139. }
  140. #define DEFINE_GET_ATTR_CONST_POINT_REF(ARG_TYPE_KEY, ARG_TYPE_VALUE, FIELD) \
  141. bool GetAttrDefValue(ARG_TYPE_KEY map_key, const ARG_TYPE_VALUE *&value, const AttrDefMap &attr) { \
  142. auto it = attr.find(map_key); \
  143. if (it == attr.end()) { \
  144. return false; \
  145. } \
  146. \
  147. value = &(it->second.FIELD()); \
  148. return true; \
  149. }
  150. #define DEFINE_GET_BYTES_ATTR_VALUE(ARG_TYPE_KEY, ARG_TYPE_VALUE) \
  151. bool GetBytesValue(ARG_TYPE_KEY key, ARG_TYPE_VALUE value, const AttrDefMap &attr) { \
  152. GE_RT_FALSE_CHECK_NOTNULL(value); \
  153. auto it = attr.find(key); \
  154. if (it != attr.end()) { \
  155. *value = it->second.bt(); \
  156. return true; \
  157. } \
  158. return false; \
  159. }
  160. #define DEFINE_GET_ATTR_LIST_VALUE(ARG_TYPE_KEY, ARG_TYPE_VALUE, FIELD) \
  161. FMK_FUNC_DEV_VISIBILITY bool GetAttrDefListValue(ARG_TYPE_KEY map_key, int idx, ARG_TYPE_VALUE value, \
  162. const AttrDefMap &attr) { \
  163. auto it = attr.find(map_key); \
  164. if (it == attr.end()) { \
  165. return false; \
  166. } \
  167. \
  168. const auto &list = it->second.list(); \
  169. if (idx < 0 || idx > list.FIELD##_size() - 1) { \
  170. return false; \
  171. } \
  172. \
  173. *value = list.FIELD(idx); \
  174. return true; \
  175. }
  176. DEFINE_GET_ATTR_VALUE(const std::string &, std::string *, s);
  177. DEFINE_GET_ATTR_VALUE(const std::string &, int32_t *, i);
  178. DEFINE_GET_ATTR_VALUE(const std::string &, int64_t *, i);
  179. DEFINE_GET_ATTR_VALUE(const std::string &, uint32_t *, u);
  180. DEFINE_GET_ATTR_VALUE(const std::string &, float *, f);
  181. DEFINE_GET_ATTR_VALUE(const std::string &, double *, f);
  182. DEFINE_GET_ATTR_VALUE(const std::string &, bool *, b);
  183. DEFINE_GET_ATTR_VALUE(const std::string &, AttrDef_ListValue *, list);
  184. DEFINE_GET_ATTR_LIST_VALUE(const std::string &, int32_t *, i);
  185. DEFINE_GET_ATTR_LIST_VALUE(const std::string &, uint32_t *, u);
  186. DEFINE_GET_ATTR_LIST_VALUE(const std::string &, float *, f);
  187. DEFINE_GET_ATTR_LIST_VALUE(const std::string &, double *, f);
  188. DEFINE_GET_ATTR_POINT_REF(const std::string &, NamedAttrs, func);
  189. DEFINE_GET_ATTR_CONST_POINT_REF(const std::string &, NamedAttrs, func);
  190. DEFINE_GET_BYTES_ATTR_VALUE(const std::string &, std::string *);
  191. #define DEFINE_GET_OP_ATTR(ARG_TYPE_KEY, ARG_TYPE_VALUE) \
  192. bool GetOpAttr(ARG_TYPE_KEY map_key, ARG_TYPE_VALUE value, const OpDef *op_def) { \
  193. GE_RT_FALSE_CHECK_NOTNULL(op_def); \
  194. return GetAttrDefValue(map_key, value, op_def->attr()); \
  195. } \
  196. bool GetModelAttr(ARG_TYPE_KEY map_key, ARG_TYPE_VALUE value, const ModelDef *model_def) { \
  197. GE_RT_FALSE_CHECK_NOTNULL(model_def); \
  198. return GetAttrDefValue(map_key, value, model_def->attr()); \
  199. }
  200. DEFINE_GET_OP_ATTR(const std::string &, std::string *);
  201. DEFINE_GET_OP_ATTR(const std::string &, int32_t *);
  202. DEFINE_GET_OP_ATTR(const std::string &, int64_t *);
  203. DEFINE_GET_OP_ATTR(const std::string &, uint32_t *);
  204. DEFINE_GET_OP_ATTR(const std::string &, float *);
  205. DEFINE_GET_OP_ATTR(const std::string &, double *);
  206. DEFINE_GET_OP_ATTR(const std::string &, bool *);
  207. DEFINE_GET_OP_ATTR(const std::string &, AttrDef_ListValue *);
  208. #define DEFINE_GET_BT_ATTR(ARG_TYPE_KEY, ARG_TYPE_VALUE) \
  209. bool GetBytesAttr(ARG_TYPE_KEY key, ARG_TYPE_VALUE value, const OpDef *op_def) { \
  210. GE_RT_FALSE_CHECK_NOTNULL(op_def); \
  211. return GetBytesValue(key, value, op_def->attr()); \
  212. } \
  213. FMK_FUNC_DEV_VISIBILITY bool GetBytesAttr(ARG_TYPE_KEY key, ARG_TYPE_VALUE value, const ModelDef *model_def) { \
  214. GE_RT_FALSE_CHECK_NOTNULL(model_def); \
  215. return GetBytesValue(key, value, model_def->attr()); \
  216. }
  217. DEFINE_GET_BT_ATTR(const std::string &, std::string *);
  218. bool HasOpAttr(const OpDef *op_def, const std::string &attr_name) {
  219. if (op_def == nullptr) {
  220. return false;
  221. }
  222. const AttrDefMap &attr = op_def->attr();
  223. const AttrDefMap::const_iterator it = attr.find(attr_name);
  224. if (it != attr.end()) {
  225. return true;
  226. }
  227. return false;
  228. }
  229. void AddModelAttr(const std::string &map_key, const void *value, size_t size, ModelDef *model_def) {
  230. if (model_def == nullptr) {
  231. return;
  232. }
  233. AttrDef out;
  234. auto attr = model_def->mutable_attr();
  235. auto it = attr->find(map_key);
  236. if (it != attr->end()) {
  237. auto &attr_value = it->second;
  238. attr_value.set_bt(value, size);
  239. } else {
  240. out.set_bt(value, size);
  241. attr->insert(AttrDefPair(map_key, out));
  242. }
  243. }
  244. void AddOpBytesAttr(const std::string &key, const void *value, size_t size, OpDef *op_def) {
  245. if (op_def == nullptr) {
  246. return;
  247. }
  248. AttrDef out;
  249. auto attr = op_def->mutable_attr();
  250. auto it = attr->find(key);
  251. if (it != attr->end()) {
  252. auto &attr_value = it->second;
  253. attr_value.set_bt(value, size);
  254. } else {
  255. out.set_bt(value, size);
  256. attr->insert(AttrDefPair(key, out));
  257. }
  258. }
  259. #define DEFINE_GET_ATTR_LIST_SIZE(ARG_TYPE_KEY, ARG_TYPE_VALUE, FIELD) \
  260. FMK_FUNC_DEV_VISIBILITY uint32_t GetOpAttrListSize(ARG_TYPE_KEY key, ARG_TYPE_VALUE value, const OpDef *op_def) { \
  261. GE_CHK_BOOL_RET_STATUS_NOLOG(op_def != nullptr, 0); \
  262. const AttrDefMap &attr_map = op_def->attr(); \
  263. auto it = attr_map.find(key); \
  264. if (it == attr_map.end()) { \
  265. return 0; \
  266. } \
  267. const auto &list = it->second.list(); \
  268. return list.FIELD##_size(); \
  269. }
  270. DEFINE_GET_ATTR_LIST_SIZE(const std::string &, const std::string &, s);
  271. DEFINE_GET_ATTR_LIST_SIZE(const std::string &, int32_t, i);
  272. DEFINE_GET_ATTR_LIST_SIZE(const std::string &, int64_t, i);
  273. DEFINE_GET_ATTR_LIST_SIZE(const std::string &, uint32_t, u);
  274. DEFINE_GET_ATTR_LIST_SIZE(const std::string &, float, f);
  275. DEFINE_GET_ATTR_LIST_SIZE(const std::string &, double, f);
  276. DEFINE_GET_ATTR_LIST_SIZE(const std::string &, bool, b);
  277. } // namespace ge

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