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.

parser.py 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. # This is the Python adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  2. #
  3. # Copyright 2020 Huawei Technologies Co., Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # ============================================================================
  17. """The module of parser python object, called by c++."""
  18. import ast
  19. import types
  20. import inspect
  21. from textwrap import dedent
  22. from dataclasses import is_dataclass
  23. import asttokens
  24. import mindspore.nn as nn
  25. from mindspore import log as logger
  26. from mindspore import ops
  27. from mindspore.common.dtype import pytype_to_dtype
  28. from mindspore.common.api import _MindSporeFunction
  29. from .namespace import CellNamespace, ClosureNamespace, ClassMemberNamespace
  30. from .resources import parse_object_map, convert_object_map, trope_ns, SYMBOL_UNDEFINE, NO_IMPLEMENT
  31. # define return value
  32. RET_SUCCESS = 0
  33. RET_FAILURE = 0xFF
  34. # define resolve type
  35. RESOLVE_TYPE_NONE = 0 # resolve None
  36. RESOLVE_TYPE_FUNCTION = 1 # resolve function
  37. RESOLVE_TYPE_METHOD = 2 # resolve class method
  38. RESOLVE_TYPE_CLASS_TYPE = 3 # resolve class type
  39. RESOLVE_TYPE_CLASS_INSTANCE = 4 # resolve the class instance of common class
  40. RESOLVE_TYPE_INVALID = 0xFF
  41. # define the class instance detail type
  42. # When the type is RESOLVE_TYPE_CLASS_INSTANCE
  43. CLASS_INSTANCE_TYPE_CELL = 0 # class instance type is Cell
  44. CLASS_INSTANCE_TYPE_PRIMITIVE = 1 # class instance type is Primitive
  45. CLASS_INSTANCE_TYPE_INVALID = 0xFF
  46. # Ast main type
  47. AST_MAIN_TYPE_STMT = 0 # ast.Stmt
  48. AST_MAIN_TYPE_EXPR = 1 # ast.Expr
  49. AST_MAIN_TYPE_SLICE = 2 # ast.Slice
  50. AST_MAIN_TYPE_UNKNOWN = 0xFF # unknown
  51. # Ast sub type
  52. AST_SUB_TYPE_AND = 3 # ast.And
  53. AST_SUB_TYPE_OR = 4 # ast.Or
  54. AST_SUB_TYPE_NAME = 5 # ast.Name
  55. AST_SUB_TYPE_TUPLE = 6 # ast.Tuple
  56. AST_SUB_TYPE_SUBSCRIPT = 7 # ast.Subscript
  57. AST_SUB_TYPE_STARRED = 8 # ast.Starred
  58. AST_SUB_TYPE_UNKNOWN = 0xFF # unknown
  59. # Process expr statement white list
  60. # add as needed, eg: "clear", "extend", "insert", "remove", "reverse"
  61. parse_expr_statement_white_list = (
  62. "append",
  63. )
  64. def create_slice_obj(start, end, step):
  65. """Create slice object"""
  66. return slice(start, end, step)
  67. def parse_cb(func, parse_method=None):
  68. """Implements the function of parse."""
  69. return Parser(func, parse_method)
  70. def get_parse_method_of_class(obj, parse_method=None):
  71. """
  72. Het parse method of class.
  73. Args:
  74. obj(Object): Instance of class.
  75. parse_method(str): Save the method name. Cell object has default method named 'construct'.
  76. Returns:
  77. Function, obj's method.
  78. """
  79. method = None
  80. method_name = None
  81. if parse_method is not None:
  82. method_name = parse_method
  83. else:
  84. if isinstance(obj, nn.Cell):
  85. if obj.enable_hook:
  86. method_name = "_hook_construct"
  87. else:
  88. method_name = "construct"
  89. if method_name is not None:
  90. if hasattr(obj, method_name):
  91. method = getattr(obj, method_name)
  92. return method
  93. def get_bprop_method_of_class(obj, parse_method=None):
  94. """
  95. Get bprop method of class.
  96. Args:
  97. obj (Object): Instance of class.
  98. parse_method(str): Save the method name. Cell object has default method named 'bprop'.
  99. Returns:
  100. Function, obj's method.
  101. """
  102. method = None
  103. if isinstance(obj, nn.Cell):
  104. method_name = "bprop"
  105. if hasattr(obj, method_name):
  106. method = getattr(obj, method_name)
  107. return method
  108. def resolve_symbol(namespace, symbol):
  109. """
  110. Resolve a symbol.
  111. Note:
  112. Can't get function when use closure function. So save the fn on namespace.
  113. Args:
  114. namespace (Object): Symbol's namespace.
  115. symbol (str): Need resolve symbol.
  116. Returns:
  117. Object, resolve result of symbol.
  118. """
  119. # All exceptions need to be caught in this function
  120. try:
  121. resolve_ = namespace[symbol]
  122. # list and dict is not hashable ,it can not be key for the map, just return the result
  123. if isinstance(resolve_, (list, dict)):
  124. return resolve_
  125. # dataclass may not be hashable
  126. if getattr(resolve_, "__hash__") is None:
  127. return resolve_
  128. # If need trope the obj
  129. if resolve_ in convert_object_map:
  130. resolve_ = convert_object_map.get(resolve_)
  131. logger.debug("convert resolve = %r", resolve_)
  132. if resolve_ == NO_IMPLEMENT:
  133. raise NotImplementedError("not implemented for ", str(symbol))
  134. except Exception as e:
  135. if isinstance(e, NotImplementedError):
  136. raise e
  137. resolve_ = None
  138. logger.debug("resolve exception occurred, value = %r", e)
  139. logger.debug("resolve type is invalid, namespace = %s, symbol = %s",
  140. namespace.__str__(), symbol)
  141. if isinstance(resolve_, _MindSporeFunction):
  142. logger.debug("resolve class _MindSporeFunction, resolve fn instead.")
  143. resolve_ = resolve_.fn
  144. return resolve_
  145. def generate_scope(obj):
  146. """Generate the scope for every cell object in the network."""
  147. if isinstance(obj, nn.Cell):
  148. obj.generate_scope()
  149. def get_scope_name(obj):
  150. """Returns the scope of a cell object in one network."""
  151. if isinstance(obj, nn.Cell):
  152. return obj.get_scope()
  153. return None
  154. def get_object_key(obj):
  155. """Return the function key: module + name."""
  156. obj_key = ""
  157. if hasattr(obj, "__name__"):
  158. if hasattr(obj, "cell_init_args"):
  159. obj_key = "%s_ID" % (str(obj.__class__.__name__) + str(obj.__name__) + obj.cell_init_args)
  160. obj_id = "%s_ID%d" % (str(obj.__class__.__name__) + str(obj.__name__), id(obj))
  161. else:
  162. if hasattr(obj, "cell_init_args"):
  163. obj_key = "%s_ID" % (str(obj.__class__.__name__) + obj.cell_init_args)
  164. obj_id = "%s_ID%d" % (str(obj.__class__.__name__), id(obj))
  165. logger.debug("obj_key %s obj_id = %s", obj_key, obj_id)
  166. # method has same id of different instance
  167. if isinstance(obj, types.MethodType):
  168. method_instance = obj.__self__
  169. instance_id = "%s_ID%d" % (str(method_instance.__class__.__name__), id(method_instance))
  170. obj_id = instance_id + obj_id
  171. return obj_id, obj_key
  172. def get_default_input(obj):
  173. if hasattr(obj, '__parameter__'):
  174. return obj.default_input
  175. if isinstance(obj, tuple):
  176. convert = lambda x: x.default_input if hasattr(x, '__parameter__') else x
  177. args = tuple(convert(x) for x in obj)
  178. return args
  179. return obj
  180. def is_class_member(node):
  181. """Check the attr is class member variable."""
  182. type_ = node.__class__.__name__
  183. if type_ == "Attribute":
  184. if not hasattr(node.value, "id"):
  185. return False
  186. id_ = node.value.id
  187. if id_ == "self":
  188. return True
  189. return False
  190. def get_obj_id(obj):
  191. """Get the obj id."""
  192. return str(id(obj))
  193. def get_obj_type(obj):
  194. """Get the obj type."""
  195. obj_type = RESOLVE_TYPE_INVALID
  196. if obj is None:
  197. obj_type = RESOLVE_TYPE_NONE
  198. elif isinstance(obj, types.FunctionType):
  199. obj_type = RESOLVE_TYPE_FUNCTION
  200. elif isinstance(obj, types.MethodType):
  201. obj_type = RESOLVE_TYPE_METHOD
  202. elif isinstance(obj, type):
  203. obj_type = RESOLVE_TYPE_CLASS_TYPE
  204. elif _is_class_instance(obj):
  205. obj_type = RESOLVE_TYPE_CLASS_INSTANCE
  206. else:
  207. # here for ndarray, just print its shape (in case of the array to large and print many data in screen)
  208. is_ndarray = type(obj).__name__ == 'ndarray' and hasattr(obj, 'shape')
  209. raise TypeError(f'Invalid object with type `{type(obj)}` and {"shape" if is_ndarray else "value"} '
  210. f'`{obj.shape if is_ndarray else obj}`.')
  211. return obj_type
  212. def get_class_instance_type(obj):
  213. """Get the class instance detail type."""
  214. # check the obj type
  215. logger.debug("Get the class type(%r)", obj)
  216. class_type = CLASS_INSTANCE_TYPE_INVALID
  217. if _is_class_instance(obj):
  218. if isinstance(obj, nn.Cell):
  219. class_type = CLASS_INSTANCE_TYPE_CELL
  220. elif isinstance(obj, ops.Primitive):
  221. class_type = CLASS_INSTANCE_TYPE_PRIMITIVE
  222. # Add the other type base requirement
  223. return class_type
  224. def _is_class_instance(obj):
  225. """Confirm the obj is class instance."""
  226. return isinstance(obj, (nn.Cell, ops.Primitive)) or _is_dataclass_instance(obj)
  227. def _is_dataclass_instance(obj):
  228. """check whether a class is an instance of a dataclass (and not a dataclass itself)"""
  229. return is_dataclass(obj) and not isinstance(obj, type)
  230. def create_obj_instance(cls_type, args_tuple=None):
  231. """Create python instance."""
  232. obj = None
  233. if isinstance(cls_type, type):
  234. # check the type, now only support nn.Cell and Primitive
  235. if issubclass(cls_type, (nn.Cell, ops.Primitive)):
  236. if args_tuple is not None:
  237. obj = cls_type(*args_tuple)
  238. else:
  239. obj = cls_type()
  240. return obj
  241. def get_module_namespace(obj):
  242. """Get the module's namespace."""
  243. logger.debug("get module namespace, module = %r", obj)
  244. mod_namespace = None
  245. if isinstance(obj, types.ModuleType):
  246. mod_namespace = CellNamespace(obj.__name__)
  247. else:
  248. logger.warning("Module(%r) is invalid, get namespace failure!", obj)
  249. return mod_namespace
  250. def get_class_member_namespace_symbol(obj):
  251. """Get obj class member type."""
  252. logger.debug("get class instance namespace, object = %r", obj)
  253. class_namespace = ClassMemberNamespace(obj)
  254. logger.debug("class namesapce = %r", class_namespace)
  255. return class_namespace
  256. def get_dataclass_attributes(cls):
  257. """Get attributes of dataclass."""
  258. fields = cls.__dataclass_fields__
  259. attributes = {name: pytype_to_dtype(field.type)
  260. for name, field in fields.items()}
  261. return attributes
  262. def get_dataclass_methods(cls):
  263. """Get functions of dataclass."""
  264. methods = {name: getattr(cls, name)
  265. for name in dir(cls)
  266. if isinstance(getattr(cls, name), (types.FunctionType,))}
  267. return methods
  268. class Parser:
  269. """
  270. Parser python code to ast tree.
  271. Args:
  272. fn(FunctionType/MethodType): Need parse object instance.
  273. parse_method(ExtendInfoOfParseObj): Extend information for parse the function.
  274. """
  275. def __init__(self, fn: (types.FunctionType, types.MethodType), parse_method=None) -> None:
  276. self.fn = fn
  277. self.parse_method = parse_method
  278. _, self.line_offset = inspect.getsourcelines(self.fn)
  279. self.filename: str = inspect.getfile(self.fn)
  280. # Used to resolve the function's globals Namespace.
  281. self.global_namespace = CellNamespace(fn.__module__)
  282. self.function_module = fn.__module__
  283. # Used to resolve the function's nonlocals.
  284. self.closure_namespace = ClosureNamespace(fn)
  285. self.function_name = fn.__name__
  286. self.col_offset = 0
  287. def parse(self):
  288. """Parse the function or method."""
  289. logger.debug("fn = %r", self.fn)
  290. tree = None
  291. if isinstance(self.fn, (types.FunctionType, types.MethodType)):
  292. original_src = inspect.getsource(self.fn)
  293. src = dedent(original_src)
  294. self.col_offset = \
  295. len(original_src.split('\n')[0]) - len(src.split('\n')[0])
  296. logger.debug("get source = %s", src)
  297. tree = asttokens.ASTTokens(src, parse=True).tree
  298. else:
  299. logger.error("Fn type is invalid")
  300. return tree
  301. def get_args(self, node):
  302. """Get the arg of parse object."""
  303. args = []
  304. # process position args
  305. for arg in node.args.args:
  306. args.append(arg)
  307. # process kwonlyargs: kwonlyargs is append after position args
  308. if node.args.kwonlyargs:
  309. for kwarg in node.args.kwonlyargs:
  310. args.append(kwarg)
  311. # process vararg: vararg is append after kwonlyargs
  312. if node.args.vararg:
  313. args.append(node.args.vararg)
  314. # process kwarg: kwarg is append after vararg
  315. if node.args.kwarg:
  316. args.append(node.args.kwarg)
  317. return args
  318. def get_args_default_values(self, node):
  319. """get the args'default values of parse object."""
  320. nondefaults = [None] * (len(node.args.args) - len(node.args.defaults))
  321. defaults = nondefaults + node.args.defaults + node.args.kw_defaults
  322. if node.args.vararg:
  323. defaults.append(None)
  324. if node.args.kwarg:
  325. defaults.append(None)
  326. return defaults
  327. def get_node_type(self, node):
  328. """Process an ast node."""
  329. method_name = f'{node.__class__.__name__}'
  330. node_type = [method_name]
  331. # judge the ast main type
  332. if isinstance(node, ast.stmt):
  333. node_type.append(AST_MAIN_TYPE_STMT)
  334. elif isinstance(node, (ast.expr, ast.slice)) or node is None:
  335. # ast.slice and ast.expr should be expr
  336. node_type.append(AST_MAIN_TYPE_EXPR)
  337. else:
  338. node_type.append(AST_MAIN_TYPE_UNKNOWN)
  339. return node_type
  340. def get_ast_type(self, node):
  341. """Get the ast type."""
  342. ast_type = AST_SUB_TYPE_UNKNOWN
  343. if isinstance(node, ast.And):
  344. ast_type = AST_SUB_TYPE_AND
  345. elif isinstance(node, ast.Or):
  346. ast_type = AST_SUB_TYPE_OR
  347. elif isinstance(node, ast.Name):
  348. ast_type = AST_SUB_TYPE_NAME
  349. elif isinstance(node, ast.Tuple):
  350. ast_type = AST_SUB_TYPE_TUPLE
  351. elif isinstance(node, ast.Subscript):
  352. ast_type = AST_SUB_TYPE_SUBSCRIPT
  353. elif isinstance(node, ast.Starred):
  354. ast_type = AST_SUB_TYPE_STARRED
  355. else:
  356. ast_type = AST_SUB_TYPE_UNKNOWN
  357. return ast_type
  358. def get_namespace_symbol(self, var: str):
  359. """Get symbol type and namespace and symbol."""
  360. if var in self.closure_namespace:
  361. ops_info = (self.closure_namespace, var)
  362. logger.debug("in closure_namespace")
  363. elif var in self.global_namespace:
  364. ops_info = (self.global_namespace, var)
  365. logger.debug("in global_namespace")
  366. else:
  367. ops_info = parse_object_map.get(SYMBOL_UNDEFINE)
  368. ops_info = [ops_info[0], var]
  369. return ops_info
  370. def get_operation_namespace_symbol(self, var: str):
  371. """Get operation namespace and symbol."""
  372. ops_info = (trope_ns, var)
  373. logger.debug("get operation ops info = %r", ops_info)
  374. return ops_info
  375. def get_ast_namespace_symbol(self, obj):
  376. """Get obj type and namespace and symbol."""
  377. # step 1:get symbol from object map
  378. ops_info = parse_object_map.get(type(obj), SYMBOL_UNDEFINE)
  379. logger.debug("ops info = %r", ops_info)
  380. return ops_info
  381. def get_location(self, node):
  382. """
  383. Get location of node start and end line no.
  384. Args:
  385. node: AST op node or tuple or List. This is a node in the ANF diagram,
  386. here is the code location to get this node.
  387. Returns:
  388. List, [fileName, linestart, colstart, lineend, colend].
  389. """
  390. ret = [self.filename]
  391. err_exit = 0
  392. if isinstance(node, (list, tuple)):
  393. node_size = len(node)
  394. if node_size == 0:
  395. err_exit = 1
  396. else:
  397. start_node = node[0]
  398. end_node = node[-1]
  399. else:
  400. start_node = node
  401. end_node = node
  402. if err_exit == 0:
  403. if hasattr(start_node, "lineno") and \
  404. hasattr(end_node, "col_offset"):
  405. start_lineno, start_colno = start_node.first_token.start
  406. end_lineno, end_colno = end_node.last_token.end
  407. start_lineno += self.line_offset - 1
  408. start_colno += self.col_offset
  409. end_lineno += self.line_offset - 1
  410. end_colno += self.col_offset
  411. ret = ret + [start_lineno, start_colno, end_lineno, end_colno]
  412. else:
  413. ret = ret + [0, 0, 0, 0]
  414. return ret
  415. def expand_expr_statement(self, node):
  416. """
  417. Process the expr statement and expand it.
  418. Returns:
  419. tuple, (True, expr.value, x)/(False, None, None).
  420. """
  421. if isinstance(node, ast.Expr) and hasattr(node, "value"):
  422. expr_value = node.value
  423. if isinstance(expr_value, ast.Call):
  424. func = expr_value.func
  425. if isinstance(func, ast.Attribute) and \
  426. hasattr(func, "attr") and \
  427. hasattr(func, "value"):
  428. method = func.attr
  429. target = func.value
  430. if method in parse_expr_statement_white_list:
  431. logger.debug("Expand expr, target:%s, method:%s", target, method)
  432. return True, expr_value, target
  433. return True, expr_value
  434. return False, None, None