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

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

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