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

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