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.

resource.cc 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "pipeline/resource.h"
  19. #include "pipeline/pipeline.h"
  20. #include "pipeline/static_analysis/static_analysis.h"
  21. #include "debug/draw.h"
  22. #include "debug/trace.h"
  23. #include "ir/dtype.h"
  24. #include "pipeline/parse/data_converter.h"
  25. #include "operator/ops.h"
  26. #include "utils/graph_utils.h"
  27. #include "optimizer/ad/dfunctor.h"
  28. #include "vm/segment_runner.h"
  29. namespace mindspore {
  30. // namespace to support opmap definition
  31. namespace pipeline {
  32. MethodMap &GetMethodMap() {
  33. static MethodMap method_map = {{kObjectTypeString,
  34. {
  35. {"__bool__", std::string("str_bool")} // C.str_bool
  36. }},
  37. {kMetaTypeNone,
  38. {
  39. {"__bool__", std::string("none_bool")} // C.none_bool
  40. }},
  41. {kNumberTypeBool,
  42. {
  43. {"__and__", prim::kPrimBoolAnd}, // P.bool_and
  44. {"__or__", prim::kPrimBoolOr}, // P.bool_or
  45. {"__eq__", prim::kPrimBoolEq}, // P.bool_eq
  46. {"__ne__", std::string("bool_ne")}, // C.bool_ne
  47. {"__bool__", prim::kPrimIdentity} // P.identity
  48. }},
  49. {kNumberTypeInt,
  50. {
  51. {"__add__", prim::kPrimScalarAdd}, // P.scalar_add
  52. {"__sub__", prim::kPrimScalarSub}, // P.scalar_sub
  53. {"__mul__", prim::kPrimScalarMul}, // P.scalar_mul
  54. {"__floordiv__", std::string("int_floordiv")}, // C.int_floordiv
  55. {"__truediv__", std::string("int_truediv")}, // C.int_truediv
  56. {"__mod__", prim::kPrimScalarMod}, // P.scalar_mod
  57. {"__pow__", prim::kPrimScalarPow}, // P.scalar_pow
  58. {"__floor__", prim::kPrimIdentity}, // P.identity
  59. {"__trunc__", prim::kPrimIdentity}, // P.identity
  60. {"__pos__", prim::kPrimScalarUadd}, // P.scalar_uadd
  61. {"__neg__", prim::kPrimScalarUsub}, // P.scalar_usub
  62. {"__eq__", prim::kPrimScalarEq}, // P.scalar_eq
  63. {"__ne__", prim::kPrimScalarNe}, // P.scalar_ne
  64. {"__lt__", prim::kPrimScalarLt}, // P.scalar_lt
  65. {"__gt__", prim::kPrimScalarGt}, // P.scalar_gt
  66. {"__le__", prim::kPrimScalarLe}, // P.scalar_le
  67. {"__ge__", prim::kPrimScalarGe}, // P.scalar_ge
  68. {"__bool__", std::string("int_bool")}, // C.int_bool
  69. {"__ms_to_array__", prim::kPrimScalarToArray}, // P.scalar_to_array
  70. }},
  71. {kNumberTypeUInt,
  72. {
  73. {"__add__", prim::kPrimScalarAdd}, // P.scalar_add,
  74. {"__sub__", prim::kPrimScalarSub}, // P.scalar_sub,
  75. {"__mul__", prim::kPrimScalarMul}, // P.scalar_mul,
  76. {"__floordiv__", prim::kPrimScalarDiv}, // P.scalar_div,
  77. {"__truediv__", std::string("int_truediv")}, // C.int_truediv
  78. {"__mod__", prim::kPrimScalarMod}, // P.scalar_mod,
  79. {"__pow__", prim::kPrimScalarPow}, // P.scalar_pow,
  80. {"__floor__", prim::kPrimIdentity}, // P.identity,
  81. {"__trunc__", prim::kPrimIdentity}, // P.identity,
  82. {"__pos__", prim::kPrimScalarUadd}, // P.scalar_uadd,
  83. {"__neg__", prim::kPrimScalarUsub}, // P.scalar_usub,
  84. {"__eq__", prim::kPrimScalarEq}, // P.scalar_eq,
  85. {"__ne__", prim::kPrimScalarNe}, // P.scalar_ne,
  86. {"__lt__", prim::kPrimScalarLt}, // P.scalar_lt,
  87. {"__gt__", prim::kPrimScalarGt}, // P.scalar_gt,
  88. {"__le__", prim::kPrimScalarLe}, // P.scalar_le,
  89. {"__ge__", prim::kPrimScalarGe}, // P.scalar_ge,
  90. {"__bool__", std::string("int_bool")}, // C.int_bool
  91. {"__ms_to_array__", prim::kPrimScalarToArray}, // P.scalar_to_array,
  92. }},
  93. {kNumberTypeFloat,
  94. {
  95. {"__add__", prim::kPrimScalarAdd}, // P.scalar_add,
  96. {"__sub__", prim::kPrimScalarSub}, // P.scalar_sub,
  97. {"__mul__", prim::kPrimScalarMul}, // P.scalar_mul,
  98. {"__floordiv__", std::string("float_floordiv")}, // C.float_floordiv
  99. {"__truediv__", prim::kPrimScalarDiv}, // P.scalar_div,
  100. {"__mod__", prim::kPrimScalarMod}, // P.scalar_mod,
  101. {"__pow__", prim::kPrimScalarPow}, // P.scalar_pow,
  102. {"__floor__", prim::kPrimScalarFloor}, // P.scalar_floor,
  103. {"__trunc__", prim::kPrimScalarTrunc}, // P.scalar_trunc,
  104. {"__pos__", prim::kPrimScalarUadd}, // P.scalar_uadd,
  105. {"__neg__", prim::kPrimScalarUsub}, // P.scalar_usub,
  106. {"__eq__", prim::kPrimScalarEq}, // P.scalar_eq,
  107. {"__ne__", prim::kPrimScalarNe}, // P.scalar_ne,
  108. {"__lt__", prim::kPrimScalarLt}, // P.scalar_lt,
  109. {"__gt__", prim::kPrimScalarGt}, // P.scalar_gt,
  110. {"__le__", prim::kPrimScalarLe}, // P.scalar_le,
  111. {"__ge__", prim::kPrimScalarGe}, // P.scalar_ge,
  112. {"__bool__", std::string("float_bool")}, // C.float_bool
  113. {"__ms_to_array__", prim::kPrimScalarToArray}, // P.scalar_to_array,
  114. }},
  115. {kObjectTypeTuple,
  116. {
  117. {"__len__", prim::kPrimTupleLen}, // P.tuple_len,
  118. {"__getitem__", prim::kPrimTupleGetItem}, // P.tuple_getitem,
  119. {"__setitem__", prim::kPrimTupleSetItem}, // P.tuple_setitem,
  120. {"__ms_iter__", prim::kPrimIdentity}, // P.identity,
  121. {"__ms_next__", std::string("tuple_next")}, // C.tuple_next,
  122. {"__ms_hasnext__", std::string("tuple_hasnext")}, // C.tuple_hasnext
  123. {"__bool__", std::string("tuple_bool")} // C.tuple_bool
  124. }},
  125. {kObjectTypeList,
  126. {
  127. {"__len__", prim::kPrimListLen}, // P.list_len,
  128. {"__getitem__", prim::kPrimListGetItem}, // P.list_getitem,
  129. {"__setitem__", prim::kPrimListSetItem}, // P.list_setitem,
  130. {"__ms_iter__", prim::kPrimIdentity}, // P.identity
  131. {"__ms_next__", std::string("list_next")}, // C.list_next
  132. {"append", std::string("list_append")}, // C.list_next
  133. {"__bool__", std::string("list_bool")}, // C.list_bool
  134. {"__ms_hasnext__", std::string("list_hasnext")},
  135. }},
  136. {kObjectTypeDictionary,
  137. {
  138. {"__len__", prim::kPrimDictLen}, // P.dict_len
  139. {"__getitem__", prim::kPrimDictGetItem}, // P.dict_getitem
  140. {"__setitem__", prim::kPrimDictSetItem}, // P.dict_setitem,
  141. {"__bool__", std::string("dict_bool")} // C.dict_bool
  142. }},
  143. {kObjectTypeTensorType,
  144. {
  145. {"__add__", std::string("add")}, // C.add
  146. {"__sub__", std::string("sub")}, // C.sub
  147. {"__mul__", std::string("mul")}, // C.mul
  148. {"__truediv__", std::string("truediv")}, // C.truediv
  149. {"__floordiv__", std::string("floordiv")}, // C.floordiv
  150. {"__mod__", std::string("mod")}, // C.mod
  151. {"__pow__", std::string("pow_")}, // C.pow
  152. {"__floor__", std::string("array_floor")}, // C.array_floor
  153. {"__trunc__", std::string("array_trunc")}, // C.array_trunc
  154. {"__pos__", std::string("array_uadd")}, // C.array_uadd
  155. {"__neg__", std::string("array_usub")}, // C.array_usub
  156. {"__eq__", std::string("eq")}, // C.eq
  157. {"__ne__", std::string("ne")}, // C.ne
  158. {"__lt__", std::string("lt")}, // C.lt
  159. {"__gt__", std::string("gt")}, // C.gt
  160. {"__le__", std::string("le")}, // C.le
  161. {"__ge__", std::string("ge")}, // C.ge
  162. {"__matmul__", prim::kPrimDot}, // P.dot,
  163. {"__len__", prim::kPrimArrayLen}, // P.array_len,
  164. {"__getitem__", prim::kPrimArrayGetItem}, // P.array_getitem,
  165. {"__setitem__", prim::kPrimArraySetItem}, // P.array_setitem,
  166. {"__ms_iter__", std::string("array_iter")}, // C.array_iter
  167. {"__ms_to_array__", prim::kPrimIdentity}, // P.identity,
  168. {"item", prim::kPrimArrayToScalar}, // P.array_to_scalar,
  169. {"transpose", std::string("transpose")}, // P.transpose
  170. {"__bool__", std::string("tensor_bool")}, // C.tensor_bool
  171. }},
  172. {kObjectTypeJTagged, {}},
  173. {kObjectTypeSymbolicKeyType, {}},
  174. {kObjectTypeEnvType, {}}};
  175. return method_map;
  176. }
  177. Resource::Resource(const py::object &obj)
  178. : engine_(std::make_shared<abstract::AnalysisEngine>(abstract::GetPrimEvaluatorConstructors(), manager_)),
  179. input_(obj),
  180. is_cleaned_(false) {}
  181. Resource::~Resource() {
  182. MS_LOG(DEBUG) << "Resource clear";
  183. // If exit normally, these global variables will be cleaned
  184. // in Resource::Clean call by MsPipeline::Compile, but if exit with MS_LOGEXCEPTION,
  185. // these global variables may not being cleaned, it may
  186. // cause segmentfault when free python object inside these global variables
  187. // after python interpreter got freed, so these global variables
  188. // are cleaned here.
  189. // So if exit normally, these global variable will be cleaned twice,
  190. // care be taken to prevent double free in the following functions.
  191. if (!is_cleaned_) {
  192. try {
  193. Clean();
  194. } catch (const std::exception &e) {
  195. MS_LOG(ERROR) << "Exception when cleaning resource. Error info " << e.what();
  196. } catch (...) {
  197. MS_LOG(ERROR) << "Exception when cleaning resource.";
  198. }
  199. }
  200. }
  201. bool Resource::IsTypeInMethodMap(const TypeId &type) {
  202. TypeId type_id = NormalizeTypeId(type);
  203. const MethodMap &method_map = GetMethodMap();
  204. auto iter = method_map.find(static_cast<int>(type_id));
  205. if (iter != method_map.end()) {
  206. return true;
  207. }
  208. return false;
  209. }
  210. Any Resource::GetMethodPtr(const TypeId &type, const std::string &name) {
  211. TypeId type_id = NormalizeTypeId(type);
  212. const MethodMap &method_map = GetMethodMap();
  213. auto iter = method_map.find(static_cast<int>(type_id));
  214. if (iter == method_map.end()) {
  215. MS_LOG(WARNING) << "Object type: " << type_id << " not in the method_map";
  216. return Any();
  217. }
  218. auto iter_map = iter->second.find(name);
  219. if (iter_map == iter->second.end()) {
  220. MS_LOG(WARNING) << "Object type: " << type_id << " have no method: " << name;
  221. return Any();
  222. }
  223. return iter_map->second;
  224. }
  225. void Resource::Clean() {
  226. // AbstractTensor->elements() will be saved in AbstractBasePtrList
  227. args_spec_.clear();
  228. input_ = py::none();
  229. // Context with AbstractBasePtrList may be saved in GraphEvaluator
  230. // some Evaluator like ResolveEvaluator may save Python object in cache,
  231. // it should be cleaned before Python Interpreter destructed.
  232. MS_EXCEPTION_IF_NULL(engine_);
  233. engine_->ClearEvaluatorCache();
  234. // clean static variable to prevent from crash. As static variable is released after
  235. // Python threads is released.
  236. parse::data_converter::ClearObjectCache();
  237. parse::Parser::CleanParserResource();
  238. parse::CleanDataClassToClassMap();
  239. trace::ClearTraceStack();
  240. is_cleaned_ = true;
  241. }
  242. } // namespace pipeline
  243. } // namespace mindspore