frontend.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. import torch
  2. import sys
  3. import ast
  4. import dataclasses
  5. import inspect
  6. import string
  7. import re
  8. from collections import namedtuple
  9. from textwrap import dedent
  10. from typing import List, Tuple # noqa: F401
  11. from torch._C._jit_tree_views import (
  12. ClassDef, Ident, Stmt, Decl, Def, Var,
  13. EmptyTypeAnnotation, Param, ExprStmt, Assign,
  14. Delete, Return, Raise, Assert, AugAssign, While,
  15. For, If, Pass, Break, Continue, Apply, Dots, Select,
  16. TrueLiteral, FalseLiteral, NoneLiteral, Starred,
  17. ListLiteral, TupleLiteral, DictLiteral, Const,
  18. StringLiteral, ListComp, Attribute, BinOp, UnaryOp,
  19. SliceExpr, Subscript, TernaryIf, With, WithItem, Property,
  20. DictComp,
  21. )
  22. from torch._sources import get_source_lines_and_file, parse_def, make_source_context
  23. from torch._sources import ParsedDef as _ParsedDef
  24. from torch.jit._dataclass_impls import DATACLASS_MAGIC_METHODS
  25. from torch.jit._monkeytype_config import monkeytype_trace, get_qualified_name
  26. from torch._jit_internal import should_drop, _is_drop_fn, is_static_fn, FunctionModifiers # noqa: F401
  27. from torch import _jit_internal
  28. import torch.jit.annotations
  29. _IS_ASTUNPARSE_INSTALLED = False
  30. try:
  31. import astunparse # type: ignore[import]
  32. _IS_ASTUNPARSE_INSTALLED = True
  33. except ImportError:
  34. pass
  35. # Borrowed from cPython implementation
  36. # https://github.com/python/cpython/blob/561612d8456cfab5672c9b445521113b847bd6b3/Lib/textwrap.py#L411#
  37. _reserved_prefix = '__jit'
  38. _reserved_names = {'print'}
  39. _identifier_chars = set(string.ascii_lowercase + string.ascii_uppercase + string.digits)
  40. def is_reserved_name(name):
  41. return name.startswith(_reserved_prefix) or name in _reserved_names
  42. pretty_node_names = {
  43. ast.FunctionDef: "function definitions",
  44. ast.For: "for loops",
  45. ast.Delete: "del statements",
  46. ast.ClassDef: "class definitions",
  47. ast.With: "with statements",
  48. ast.Raise: "raise statements",
  49. ast.Assert: "assertions",
  50. ast.Import: "import statements",
  51. ast.ImportFrom: "import statements",
  52. ast.Global: "global variables",
  53. ast.Break: "break statements",
  54. ast.Continue: "continue statements",
  55. }
  56. node_start_tokens = {
  57. ast.FunctionDef: "def",
  58. ast.For: "for",
  59. ast.Delete: "del",
  60. ast.ClassDef: "class",
  61. ast.With: "with",
  62. ast.Raise: "raise",
  63. ast.Assert: "assert",
  64. ast.Import: "import",
  65. ast.ImportFrom: "from",
  66. ast.Global: "global",
  67. ast.Break: "break",
  68. ast.Continue: "continue",
  69. }
  70. pretty_node_names.update({
  71. ast.AsyncFunctionDef: "async function definitions",
  72. ast.AsyncFor: "async for loops",
  73. ast.AsyncWith: "async with statements",
  74. ast.Try: "try blocks",
  75. ast.Nonlocal: "nonlocal variables",
  76. })
  77. node_start_tokens.update({
  78. ast.AsyncFunctionDef: "async def",
  79. ast.AsyncFor: "async for",
  80. ast.AsyncWith: "async with",
  81. ast.Try: "try",
  82. ast.Nonlocal: "nonlocal",
  83. })
  84. pretty_node_names.update({
  85. ast.AnnAssign: "annotated assignments",
  86. })
  87. # NB: no specific token for AnnAssign
  88. class FrontendError(Exception):
  89. def __init__(self, source_range, msg):
  90. self.source_range = source_range
  91. self.msg = msg
  92. # This has to be instantiated here so the ErrorReport is accurate to the
  93. # call stack when the FrontendError was raised
  94. self.error_report = torch._C.ErrorReport(self.source_range)
  95. def __str__(self):
  96. return self.msg + self.error_report.what().lstrip()
  97. class NotSupportedError(FrontendError):
  98. pass
  99. class UnsupportedNodeError(NotSupportedError):
  100. def __init__(self, ctx, offending_node, reason=''):
  101. # If we don't have a specific token, we default to length of 1
  102. node_type = type(offending_node)
  103. range_len = len(node_start_tokens.get(node_type, ' '))
  104. source_range = ctx.make_range(offending_node.lineno,
  105. offending_node.col_offset,
  106. offending_node.col_offset + range_len)
  107. feature_name = pretty_node_names.get(node_type, node_type.__name__)
  108. msg = "{} {}aren't supported".format(feature_name, reason + ' ' if reason else '')
  109. super().__init__(source_range, msg)
  110. class FrontendTypeError(FrontendError):
  111. pass
  112. def build_withitems(ctx, items):
  113. items = [build_withitem(ctx, i) for i in items]
  114. return list(items)
  115. def build_stmts(ctx, stmts):
  116. stmts = [build_stmt(ctx, s) for s in stmts]
  117. return list(filter(None, stmts))
  118. def get_class_properties(cls, self_name):
  119. """
  120. Get a list of Property objects representing the properties of a class.
  121. Args:
  122. cls: The class to get properties of.
  123. self_name: The name of the class that the properties should belong to.
  124. Returns:
  125. A list of Property objects corresponding to the properties of cls. Property
  126. here refers to the subclass of TreeView.
  127. """
  128. props = inspect.getmembers(
  129. cls, predicate=lambda m: isinstance(m, property))
  130. # Any property that should not compiled must be in this list on the Module.
  131. unused_properties = getattr(cls, "__jit_unused_properties__", [])
  132. # Create Property TreeView objects from inspected property objects.
  133. properties = []
  134. for prop in props:
  135. if prop[0] not in unused_properties and not should_drop(prop[1].fget):
  136. getter = get_jit_def(prop[1].fget, f"__{prop[0]}_getter", self_name=self_name)
  137. setter = get_jit_def(prop[1].fset, f"__{prop[0]}_setter", self_name=self_name) if prop[1].fset else None
  138. properties.append(Property(getter.range(), Ident(getter.range(), prop[0]), getter, setter))
  139. return properties
  140. def get_class_assigns(ctx, cls_ast):
  141. assigns = []
  142. def maybe_build_assign(builder, entry):
  143. nonlocal assigns
  144. try:
  145. assigns.append(builder(ctx, entry))
  146. except NotSupportedError:
  147. pass
  148. for entry in cls_ast.body:
  149. if isinstance(entry, ast.Assign):
  150. maybe_build_assign(StmtBuilder.build_Assign, entry)
  151. elif isinstance(entry, ast.AnnAssign):
  152. maybe_build_assign(StmtBuilder.build_AnnAssign, entry)
  153. return assigns
  154. def get_jit_class_def(cls, self_name):
  155. # Get defs for each method within the current class independently
  156. # TODO: proper overriding analysis when implementing class inheritance
  157. methods = inspect.getmembers(
  158. cls,
  159. predicate=lambda m: (inspect.ismethod(m) or inspect.isfunction(m))
  160. and not is_static_fn(cls, m.__name__)
  161. and m.__name__ in cls.__dict__
  162. and not _is_drop_fn(m)
  163. )
  164. def is_classmethod(fn):
  165. return inspect.ismethod(fn) and getattr(fn, "__self__", None) == cls
  166. # Get and parse the source code for this class
  167. sourcelines, file_lineno, filename = get_source_lines_and_file(cls, torch._C.ErrorReport.call_stack())
  168. source = ''.join(sourcelines)
  169. dedent_src = dedent(source)
  170. py_ast = ast.parse(dedent_src)
  171. class_ast = py_ast.body[0]
  172. assert isinstance(class_ast, ast.ClassDef)
  173. # Special case for dataclasses. In general we need access to the source code for
  174. # an object in order to JIT compile it. But the dataclasses module dynamically synthesizes
  175. # magic methods for classes, and we can't get the source code for these methods. As a
  176. # workaround, we synthesize TorchScript-friendly implementations ourselves.
  177. if dataclasses.is_dataclass(cls):
  178. # Detect whether the user manually implemented any of the magic methods. If they did,
  179. # we don't want to synthesize/override them.
  180. overrides = {
  181. method.name
  182. for method in class_ast.body
  183. if isinstance(method, ast.FunctionDef) and method.name in DATACLASS_MAGIC_METHODS
  184. }
  185. for i, (name, _) in enumerate(methods):
  186. # Is this a magic method we can synthesize?
  187. synthesizer_fn = DATACLASS_MAGIC_METHODS.get(name)
  188. if synthesizer_fn and name not in overrides:
  189. parsed_def = synthesizer_fn(cls)
  190. methods[i] = name, parsed_def
  191. func = getattr(cls, name)
  192. _jit_internal.loader.cache(func, parsed_def.source)
  193. method_defs = [
  194. get_jit_def(obj, name, self_name=self_name, is_classmethod=is_classmethod(obj))
  195. for (name, obj) in methods
  196. ]
  197. properties = get_class_properties(cls, self_name)
  198. leading_whitespace_len = len(source.split('\n', 1)[0]) - len(dedent_src.split('\n', 1)[0])
  199. ctx = make_source_context(source, filename, file_lineno, leading_whitespace_len, False)
  200. assigns = get_class_assigns(ctx, class_ast)
  201. return build_class_def(ctx, class_ast, method_defs, properties, self_name, assigns)
  202. def get_jit_def(fn, def_name, self_name=None, is_classmethod=False):
  203. """
  204. Build a JIT AST (TreeView) from the given function.
  205. Args:
  206. fn: A function object to compile or a pre-parsed ParsedDef object
  207. def_name: The name to give to the resulting AST object. This is not
  208. always the same as `fn.__name__`, for example:
  209. def _forward(self):
  210. ...
  211. forward = _forward
  212. In this case, the `__name__` attribute of the function object is "_forward",
  213. but we want the result AST to have the name "forward".
  214. self_name: If this function is a method, what the type name of `self` is.
  215. """
  216. parsed_def = parse_def(fn) if not isinstance(fn, _ParsedDef) else fn
  217. type_line = torch.jit.annotations.get_type_line(parsed_def.source)
  218. fn_def = parsed_def.ast.body[0]
  219. if is_classmethod:
  220. arg_name = fn_def.args.args[0].arg
  221. # Insert a statement that assigns the first argument to the class
  222. assign_stmt = ast.parse(f"{arg_name} = {self_name}").body[0]
  223. fn_def.body.insert(0, assign_stmt)
  224. # Swap out the function signature and body if it is unused
  225. if should_drop(fn):
  226. unused_fn_def = ast.parse("def unused_fn(self: Any):\n\traise RuntimeError(\"Cannot call @unused methods\")")
  227. if len(unused_fn_def.body) != 1 or not isinstance(unused_fn_def.body[0], ast.FunctionDef):
  228. raise RuntimeError(f"Expected a single top-level function: {parsed_def.filename}:{parsed_def.file_lineno}")
  229. unused_def = unused_fn_def.body[0]
  230. fn_def.body = unused_def.body
  231. # kwarg/vararg not supported by `build_def`
  232. fn_def.args.kwarg = fn_def.args.vararg = None
  233. for arg in fn_def.args.args + fn_def.args.kwonlyargs:
  234. # Replace potentially unsupported type annotations by "Any"
  235. arg.annotation = unused_def.args.args[0].annotation
  236. if _is_drop_fn(fn):
  237. # Dropping potentially unsupported return type annotation for jit._drop
  238. fn_def.returns = None
  239. fn_def.type_comment = None
  240. # If MonkeyType is installed, get all the consolidated type traces
  241. # for the arguments from type_trace_db
  242. type_trace_db = torch.jit._script._get_type_trace_db()
  243. pdt_arg_types = None
  244. if monkeytype_trace and not isinstance(fn, _ParsedDef):
  245. qualname = get_qualified_name(fn)
  246. pdt_arg_types = type_trace_db.get_args_types(qualname)
  247. return build_def(parsed_def.ctx, fn_def, type_line, def_name, self_name=self_name, pdt_arg_types=pdt_arg_types)
  248. # TODO: more robust handling of recognizing ignore context manager
  249. def is_torch_jit_ignore_context_manager(stmt):
  250. # checks if the statement is torch.jit.ignore context manager
  251. if isinstance(stmt.items[0].context_expr, ast.Call):
  252. # extract torch part
  253. function = stmt.items[0].context_expr.func
  254. if isinstance(function, ast.Attribute):
  255. attr_name = function.attr
  256. attr_value = function.value
  257. if attr_name == "_IgnoreContextManager" and isinstance(attr_value, ast.Attribute):
  258. # there should be at most two nested attributes (e.g torch.jit._IgnoreContextManager)
  259. if attr_value.attr == "jit" and isinstance(attr_value.value, ast.Name):
  260. if attr_value.value.id == "torch":
  261. return True
  262. return False
  263. class Builder:
  264. def __call__(self, ctx, node):
  265. method = getattr(self, 'build_' + node.__class__.__name__, None)
  266. if method is None:
  267. raise UnsupportedNodeError(ctx, node)
  268. return method(ctx, node)
  269. def build_class_def(ctx, py_def, methods, properties, self_name, assigns):
  270. r = ctx.make_range(py_def.lineno, py_def.col_offset,
  271. py_def.col_offset + len("class"))
  272. return ClassDef(Ident(r, self_name), [Stmt(method) for method in methods], properties, assigns)
  273. def build_def(ctx, py_def, type_line, def_name, self_name=None, pdt_arg_types=None):
  274. body = py_def.body
  275. r = ctx.make_range(py_def.lineno,
  276. py_def.col_offset,
  277. py_def.col_offset + len("def"))
  278. param_list = build_param_list(ctx, py_def.args, self_name, pdt_arg_types)
  279. return_type = None
  280. if getattr(py_def, 'returns', None) is not None:
  281. return_type = build_expr(ctx, py_def.returns)
  282. decl = Decl(r, param_list, return_type)
  283. is_method = self_name is not None
  284. if type_line is not None:
  285. type_comment_decl = torch._C.parse_type_comment(type_line)
  286. decl = torch._C.merge_type_from_type_comment(decl, type_comment_decl, is_method)
  287. return Def(Ident(r, def_name),
  288. decl,
  289. build_stmts(ctx, body))
  290. _vararg_kwarg_err = ("Compiled functions can't take variable number of arguments "
  291. "or use keyword-only arguments with defaults")
  292. def build_param_list(ctx, py_args, self_name, pdt_arg_types=None):
  293. if py_args.kwarg is not None:
  294. expr = py_args.kwarg
  295. ctx_range = ctx.make_range(expr.lineno, expr.col_offset - 1, expr.col_offset + len(expr.arg))
  296. raise NotSupportedError(ctx_range, _vararg_kwarg_err)
  297. if py_args.vararg is not None:
  298. expr = py_args.vararg
  299. ctx_range = ctx.make_range(expr.lineno, expr.col_offset - 1, expr.col_offset + len(expr.arg))
  300. raise NotSupportedError(ctx_range, _vararg_kwarg_err)
  301. if len(py_args.kw_defaults) > 0:
  302. # kw_defaults is a list of the values for the kwargs (which default to None),
  303. # so they don't actually have line numbers.
  304. for arg in py_args.kw_defaults:
  305. if arg is not None:
  306. ctx_range = build_expr(ctx, arg).range()
  307. raise NotSupportedError(ctx_range, _vararg_kwarg_err)
  308. # List of Tuple of args and type as inferred by profile directed typing
  309. arg_and_types = [(arg, pdt_arg_types[arg.arg] if pdt_arg_types and bool(pdt_arg_types[arg.arg]) else None)
  310. for arg in py_args.args]
  311. arg_and_types_kwonlyargs = [(arg, pdt_arg_types[arg.arg] if pdt_arg_types and bool(pdt_arg_types[arg.arg])
  312. else None) for arg in py_args.kwonlyargs]
  313. result = [build_param(ctx, arg, self_name, kwarg_only=False, pdt_arg_type=arg_type)
  314. for arg, arg_type in arg_and_types]
  315. result += [build_param(ctx, arg, self_name, kwarg_only=True, pdt_arg_type=arg_type)
  316. for arg, arg_type in arg_and_types_kwonlyargs]
  317. return result
  318. def build_param(ctx, py_arg, self_name, kwarg_only, pdt_arg_type=None):
  319. # NB: In Python3 py_arg is a pair of (str arg, expr? annotation)
  320. name = py_arg.arg
  321. r = ctx.make_range(py_arg.lineno, py_arg.col_offset, py_arg.col_offset + len(name))
  322. if getattr(py_arg, 'annotation', None) is not None:
  323. annotation_expr = build_expr(ctx, py_arg.annotation)
  324. elif pdt_arg_type:
  325. annotation_expr = Var(Ident(r, pdt_arg_type))
  326. elif self_name is not None and name == 'self':
  327. annotation_expr = Var(Ident(r, self_name))
  328. else:
  329. annotation_expr = EmptyTypeAnnotation(r)
  330. return Param(annotation_expr, Ident(r, name), kwarg_only)
  331. def build_ignore_context_manager(ctx, stmt):
  332. InputType = namedtuple('InputType', ['name', 'ann'])
  333. OutputType = namedtuple('OutputType', ['name', 'ann'])
  334. def process_ins_outs(args):
  335. # parse the context manager to figure out inputs and outputs
  336. # with their annotated types
  337. # TODO: add input, output validator
  338. inputs = []
  339. outputs = []
  340. for arg in args:
  341. var_name = arg.arg
  342. var_ann = arg.value.value
  343. var_decl_type, var_ann = var_ann.split(":")
  344. if var_decl_type == "inp":
  345. inputs.append(InputType(var_name, var_ann))
  346. if var_decl_type == "out":
  347. outputs.append(OutputType(var_name, var_ann))
  348. return inputs, outputs
  349. def create_unique_name_ext(ctx, stmt):
  350. # extension will be based on the full path filename plus
  351. # the line number of original context manager
  352. fn = re.sub(r'[^a-zA-Z0-9_]', '_', ctx.filename)
  353. return f"{fn}_{stmt.lineno}"
  354. def build_return_ann_stmt(outputs):
  355. return_type_ann = ""
  356. return_statement_str = "return "
  357. if len(outputs) == 0:
  358. return_type_ann += " -> None"
  359. if len(outputs) == 1:
  360. return_type_ann = " -> " + outputs[0].ann
  361. return_statement_str += outputs[0].name
  362. if len(outputs) > 1:
  363. return_type_ann = " -> Tuple"
  364. return_type_ann += "[" + ", ".join([var.ann for var in outputs]) + "]"
  365. return_statement_str += ", ".join([var.name for var in outputs])
  366. return return_type_ann, return_statement_str
  367. def build_args(args):
  368. return ", ".join([arg.name for arg in args])
  369. inputs, outputs = process_ins_outs(stmt.items[0].context_expr.keywords)
  370. # build the replacement function str with given inputs and outputs
  371. ignore_function_name = "func_ignore_" + create_unique_name_ext(ctx, stmt)
  372. ignore_function_str = "\ndef " + ignore_function_name
  373. ignore_function_str += "(" + ", ".join([var.name + " :" + var.ann for var in inputs]) + ")"
  374. return_ann, return_stmt = build_return_ann_stmt(outputs)
  375. ignore_function_str += return_ann + ": pass"
  376. # first create the functionDef object from just declaration
  377. ignore_function = ast.parse(ignore_function_str).body[0]
  378. # dump the body of context manager to dummy function
  379. ignore_function.body = stmt.body # type: ignore[attr-defined]
  380. # insert return statement to the function
  381. return_stmt = ast.parse(return_stmt).body[0]
  382. ignore_function.body.append(return_stmt) # type: ignore[attr-defined]
  383. # registers the custom function in the global context
  384. ignore_func_str = "@torch.jit.ignore\n" + astunparse.unparse(ignore_function)
  385. ignore_func_str += "\nglobals()[\"{}\"] = {}".format(ignore_function_name, ignore_function_name)
  386. exec(ignore_func_str) # noqa: P204
  387. # build the statements as:
  388. # <out_1>, <out_2>, ... = torch.jit.frontend.<func>(<in_1>, <in_2>)
  389. assign_str_lhs = build_args(outputs)
  390. # this function will be registered in torch.jit.frontend module by default
  391. assign_str_rhs = "torch.jit.frontend.{}(".format(ignore_function_name) + build_args(inputs) + ")"
  392. if len(outputs) > 0:
  393. assign_str = assign_str_lhs + " = " + assign_str_rhs
  394. else:
  395. assign_str = assign_str_rhs
  396. assign_ast = ast.parse(assign_str).body[0]
  397. return assign_ast
  398. def get_default_args(fn):
  399. if fn is None:
  400. return {}
  401. signature = inspect.signature(fn)
  402. return {
  403. k: v.default
  404. for k, v in signature.parameters.items()
  405. if v.default is not inspect.Parameter.empty
  406. }
  407. def get_default_args_for_class(cls):
  408. """
  409. Get default arguments for all methods in a class (except for static methods).
  410. Args:
  411. cls: type - The class type to inspect for default arguments.
  412. Returns:
  413. A Dict[str, Dict[str, Any]] which maps each method name to a Dict[str, Any]
  414. that maps each argument name to its default value.
  415. """
  416. # Get methods (except static methods because those are compiled separately as
  417. # if they were independent script functions).
  418. methods = inspect.getmembers(
  419. cls,
  420. predicate=lambda m: (inspect.ismethod(m) or inspect.isfunction(m))
  421. and not is_static_fn(cls, m.__name__)
  422. and m.__name__ in cls.__dict__
  423. )
  424. # Get method defaults. Property defaults do not need to be considered
  425. # because setters cannot be invoked without a value.
  426. defaults = {method_name: get_default_args(method_impl) for method_name, method_impl in methods}
  427. return defaults
  428. class WithItemBuilder(Builder):
  429. @staticmethod
  430. def build_withitem(ctx, item):
  431. lineno = item.context_expr.lineno
  432. start = item.context_expr.col_offset
  433. end = start + len(pretty_node_names[ast.With])
  434. op_vars = item.optional_vars
  435. r = ctx.make_range(lineno, start, end)
  436. return WithItem(r, build_expr(ctx, item.context_expr), build_expr(ctx, op_vars) if op_vars else None)
  437. class StmtBuilder(Builder):
  438. augassign_map = {
  439. ast.Add: '+',
  440. ast.Sub: '-',
  441. ast.Mult: '*',
  442. ast.Div: '/',
  443. ast.Mod: '%',
  444. ast.BitOr: '|',
  445. ast.BitAnd: '&',
  446. ast.BitXor: '^',
  447. ast.LShift: '<<',
  448. ast.RShift: '>>',
  449. ast.Pow: '**',
  450. }
  451. @staticmethod
  452. def build_Expr(ctx, stmt):
  453. value = stmt.value
  454. if value.__class__.__name__ == 'Str':
  455. # If a statement is a string literal expression,
  456. # then it is a docstring. Just ignore it.
  457. return None
  458. else:
  459. return ExprStmt(build_expr(ctx, value))
  460. @staticmethod
  461. def build_Assign(ctx, stmt):
  462. rhs = build_expr(ctx, stmt.value)
  463. lhs = [build_expr(ctx, x) for x in stmt.targets]
  464. return Assign(lhs, rhs)
  465. @staticmethod
  466. def build_AnnAssign(ctx, stmt):
  467. if stmt.value is None:
  468. raise UnsupportedNodeError(ctx, stmt, reason='without assigned value')
  469. # Disallow type annotations on instance attributes outside of __init__
  470. if type(stmt.target) == ast.Attribute and \
  471. stmt.target.value.id == "self" and ctx.funcname != "__init__": # type: ignore[attr-defined]
  472. start = stmt.col_offset
  473. end = start + len(f"self.{stmt.target.attr}")
  474. if hasattr(stmt.annotation, 'id'):
  475. end += len(f": {stmt.annotation.id}")
  476. sr = ctx.make_range(stmt.lineno, start, end)
  477. raise ValueError("Type annotations on instance attributes must be declared in "
  478. f"__init__, not '{ctx.funcname}': {sr}")
  479. rhs = build_expr(ctx, stmt.value)
  480. lhs = build_expr(ctx, stmt.target)
  481. the_type = build_expr(ctx, stmt.annotation)
  482. return Assign([lhs], rhs, the_type)
  483. @staticmethod
  484. def build_Delete(ctx, stmt):
  485. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("del"))
  486. return Delete(r, [build_expr(ctx, target) for target in stmt.targets])
  487. @staticmethod
  488. def build_Return(ctx, stmt):
  489. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("return"))
  490. return Return(r, None if stmt.value is None else build_expr(ctx, stmt.value))
  491. @staticmethod
  492. def build_Raise(ctx, stmt):
  493. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("raise"))
  494. expr = build_expr(ctx, stmt.exc)
  495. return Raise(r, expr)
  496. @staticmethod
  497. def build_Assert(ctx, stmt):
  498. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("assert"))
  499. test = build_expr(ctx, stmt.test)
  500. msg = build_expr(ctx, stmt.msg) if stmt.msg is not None else None
  501. return Assert(r, test, msg)
  502. @staticmethod
  503. def build_AugAssign(ctx, stmt):
  504. lhs = build_expr(ctx, stmt.target)
  505. rhs = build_expr(ctx, stmt.value)
  506. op = type(stmt.op)
  507. if op in StmtBuilder.augassign_map:
  508. op_token = StmtBuilder.augassign_map[op]
  509. else:
  510. raise NotSupportedError(
  511. find_before(ctx, rhs.range().start, '=', offsets=(-1, 0)),
  512. "unsupported kind of augmented assignment: " + op.__name__)
  513. return AugAssign(lhs, op_token, rhs)
  514. @staticmethod
  515. def build_While(ctx, stmt):
  516. if stmt.orelse:
  517. # TODO: try to recover the location of else:? Python doesn't give us useful
  518. # annotations in this case
  519. raise NotSupportedError(None, "else branches of while loops aren't supported")
  520. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("while"))
  521. return While(r, build_expr(ctx, stmt.test),
  522. build_stmts(ctx, stmt.body))
  523. @staticmethod
  524. def build_For(ctx, stmt):
  525. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("for"))
  526. if stmt.orelse:
  527. raise NotSupportedError(r, "else branches of for loops aren't supported")
  528. return For(
  529. r, [build_expr(ctx, stmt.target)],
  530. [build_expr(ctx, stmt.iter)], build_stmts(ctx, stmt.body))
  531. @staticmethod
  532. def build_If(ctx, stmt):
  533. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("if"))
  534. return If(r, build_expr(ctx, stmt.test),
  535. build_stmts(ctx, stmt.body),
  536. build_stmts(ctx, stmt.orelse))
  537. @staticmethod
  538. def build_Print(ctx, stmt):
  539. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("print"))
  540. if stmt.dest:
  541. raise NotSupportedError(r, "print statements with non-default destinations aren't supported")
  542. args = [build_expr(ctx, val) for val in stmt.values]
  543. return ExprStmt(Apply(Var(Ident(r, "print")), args, []))
  544. @staticmethod
  545. def build_Pass(ctx, stmt):
  546. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("pass"))
  547. return Pass(r)
  548. @staticmethod
  549. def build_Break(ctx, stmt):
  550. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("break"))
  551. return Break(r)
  552. @staticmethod
  553. def build_Continue(ctx, stmt):
  554. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("continue"))
  555. return Continue(r)
  556. @staticmethod
  557. def build_With(ctx, stmt):
  558. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset + len("with"))
  559. # Handle ignore context manager
  560. if is_torch_jit_ignore_context_manager(stmt):
  561. if not _IS_ASTUNPARSE_INSTALLED:
  562. raise RuntimeError("torch.jit._IgnoreContextManager requires installing Python library `astunparse`,\
  563. please install it in your Python environment")
  564. assign_ast = build_ignore_context_manager(ctx, stmt)
  565. return build_stmt(ctx, assign_ast)
  566. return With(r, build_withitems(ctx, stmt.items), build_stmts(ctx, stmt.body))
  567. class ExprBuilder(Builder):
  568. binop_map = {
  569. ast.Add: '+',
  570. ast.Sub: '-',
  571. ast.Mult: '*',
  572. ast.Div: '/',
  573. ast.Pow: '**',
  574. ast.Mod: '%',
  575. ast.FloorDiv: '//',
  576. ast.BitAnd: '&',
  577. ast.BitXor: '^',
  578. ast.BitOr: '|',
  579. ast.LShift: '<<',
  580. ast.RShift: '>>',
  581. }
  582. binop_map[ast.MatMult] = '@'
  583. unop_map = {
  584. ast.Not: 'not',
  585. ast.USub: '-',
  586. ast.Invert: '~',
  587. }
  588. boolop_map = {
  589. ast.And: 'and',
  590. ast.Or: 'or',
  591. }
  592. cmpop_map = {
  593. ast.Eq: '==',
  594. ast.NotEq: '!=',
  595. ast.LtE: '<=',
  596. ast.Lt: '<',
  597. ast.GtE: '>=',
  598. ast.Gt: '>',
  599. ast.Is: 'is',
  600. ast.IsNot: 'is not',
  601. ast.In: 'in',
  602. ast.NotIn: 'not in',
  603. }
  604. @staticmethod
  605. def build_Attribute(ctx, expr):
  606. base = build_expr(ctx, expr.value)
  607. # expr.attr is just a string, so it's not annotated in any way, so we have
  608. # to build the range manually
  609. source = ctx.source.encode('utf-8')
  610. def get_char(index):
  611. return chr(source[index])
  612. start_pos = base.range().end + 1
  613. while get_char(start_pos) in string.whitespace: # Skip whitespace
  614. start_pos += 1
  615. end_pos = start_pos + len(expr.attr)
  616. name_range = ctx.make_raw_range(start_pos, end_pos)
  617. return Select(base, Ident(name_range, expr.attr))
  618. @staticmethod
  619. def build_Call(ctx, expr):
  620. func = build_expr(ctx, expr.func)
  621. args = [build_expr(ctx, py_arg) for py_arg in expr.args]
  622. if hasattr(expr, 'starargs') and expr.starargs:
  623. stararg_expr = build_expr(ctx, expr.starargs)
  624. args += [Starred(stararg_expr.range(), stararg_expr)]
  625. kwargs = []
  626. for kw in expr.keywords:
  627. kw_expr = build_expr(ctx, kw.value)
  628. # XXX: we could do a better job at figuring out the range for the name here
  629. if not kw.arg:
  630. raise NotSupportedError(kw_expr.range(), 'keyword-arg expansion is not supported')
  631. kwargs.append(Attribute(Ident(kw_expr.range(), kw.arg), kw_expr))
  632. return Apply(func, args, kwargs)
  633. @staticmethod
  634. def build_Ellipsis(ctx, expr):
  635. r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + 3) # len("...") == 3
  636. return Dots(r)
  637. @staticmethod
  638. def build_Name(ctx, expr):
  639. r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + len(expr.id))
  640. if expr.id.startswith(_reserved_prefix):
  641. raise NotSupportedError(r, "names of variables used in JIT-ed functions "
  642. "can't start with " + _reserved_prefix)
  643. if expr.id == "True":
  644. return TrueLiteral(r)
  645. elif expr.id == "False":
  646. return FalseLiteral(r)
  647. elif expr.id == "None":
  648. return NoneLiteral(r)
  649. elif expr.id == "Ellipsis":
  650. return Dots(r)
  651. return Var(Ident(r, expr.id))
  652. @staticmethod
  653. def build_NameConstant(ctx, expr):
  654. r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + len(str(expr.value)))
  655. if expr.value is True:
  656. return TrueLiteral(r)
  657. elif expr.value is False:
  658. return FalseLiteral(r)
  659. elif expr.value is None:
  660. return NoneLiteral(r)
  661. elif expr.value == Ellipsis:
  662. return Dots(r)
  663. else:
  664. raise ValueError("Name constant value unsupported: " + str(expr.value))
  665. @staticmethod
  666. def build_BinOp(ctx, expr):
  667. lhs = build_expr(ctx, expr.left)
  668. rhs = build_expr(ctx, expr.right)
  669. op = type(expr.op)
  670. if op == ast.Div and not ctx.uses_true_division:
  671. err_range = ctx.make_raw_range(lhs.range().end, rhs.range().start)
  672. raise FrontendError(err_range, 'Division of ints in TorchScript uses Python 3 true '
  673. 'division semantics. Please put `from __future__ '
  674. 'import division` at the top of your file')
  675. op_token = ExprBuilder.binop_map.get(op)
  676. if op_token is None:
  677. err_range = ctx.make_raw_range(lhs.range().end, rhs.range().start)
  678. raise NotSupportedError(err_range, "unsupported binary operator: " + op.__name__)
  679. return BinOp(op_token, lhs, rhs)
  680. @staticmethod
  681. def build_UnaryOp(ctx, expr):
  682. sub_expr = build_expr(ctx, expr.operand)
  683. op = type(expr.op)
  684. op_token = ExprBuilder.unop_map.get(op)
  685. if op_token is None:
  686. raise NotSupportedError(expr.range(), "unsupported unary operator: " + op.__name__)
  687. r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + len(op_token))
  688. return UnaryOp(r, op_token, sub_expr)
  689. @staticmethod
  690. def build_BoolOp(ctx, expr):
  691. if len(expr.values) < 2:
  692. raise AssertionError("expected at least 2 values in BoolOp, but got " + str(len(expr.values)))
  693. sub_exprs = [build_expr(ctx, sub_expr) for sub_expr in expr.values]
  694. op = type(expr.op)
  695. op_token = ExprBuilder.boolop_map.get(op)
  696. if op_token is None:
  697. err_range = ctx.make_raw_range(sub_exprs[0].range().end, sub_exprs[1].range().start)
  698. raise NotSupportedError(err_range, "unsupported boolean operator: " + op.__name__)
  699. lhs = sub_exprs[0]
  700. for rhs in sub_exprs[1:]:
  701. lhs = BinOp(op_token, lhs, rhs)
  702. return lhs
  703. @staticmethod
  704. def build_IfExp(ctx, expr):
  705. return TernaryIf(build_expr(ctx, expr.test),
  706. build_expr(ctx, expr.body),
  707. build_expr(ctx, expr.orelse))
  708. @staticmethod
  709. def build_Compare(ctx, expr):
  710. operands = [build_expr(ctx, e) for e in [expr.left] + list(expr.comparators)]
  711. result = None
  712. for lhs, op_, rhs in zip(operands, expr.ops, operands[1:]):
  713. op = type(op_)
  714. op_token = ExprBuilder.cmpop_map.get(op)
  715. r = ctx.make_raw_range(lhs.range().end, rhs.range().start)
  716. if op_token is None:
  717. raise NotSupportedError(r, "unsupported comparison operator: " + op.__name__)
  718. if op == ast.NotIn:
  719. # NB: `not in` is just `not( in )`, so we don't introduce new tree view
  720. # but just make it a nested call in our tree view structure
  721. in_expr = BinOp('in', lhs, rhs)
  722. cmp_expr = UnaryOp(r, 'not', in_expr)
  723. else:
  724. cmp_expr = BinOp(op_token, lhs, rhs)
  725. if result is None:
  726. result = cmp_expr
  727. else:
  728. result = BinOp('and', result, cmp_expr)
  729. return result
  730. @staticmethod
  731. def build_Subscript(ctx, expr):
  732. def build_SliceExpr(ctx, base, slice_expr):
  733. lower = build_expr(ctx, slice_expr.lower) if slice_expr.lower is not None else None
  734. upper = build_expr(ctx, slice_expr.upper) if slice_expr.upper is not None else None
  735. step = build_expr(ctx, slice_expr.step) if slice_expr.step is not None else None
  736. return SliceExpr(base.range(), lower, upper, step)
  737. def build_Index(ctx, base, index_expr):
  738. if isinstance(index_expr.value, ast.Tuple):
  739. raise NotSupportedError(base.range(),
  740. "slicing multiple dimensions with "
  741. "tuples not supported yet")
  742. return build_expr(ctx, index_expr.value)
  743. def build_ExtSlice(ctx, base, extslice):
  744. sub_exprs = []
  745. for expr in extslice.dims:
  746. sub_type = type(expr)
  747. if sub_type is ast.Index:
  748. sub_exprs.append(build_Index(ctx, base, expr))
  749. elif sub_type is ast.Slice:
  750. sub_exprs.append(build_SliceExpr(ctx, base, expr))
  751. elif sub_type is ast.Ellipsis:
  752. sub_exprs.append(Dots(base.range()))
  753. else:
  754. raise NotSupportedError(base.range(),
  755. "slicing multiple dimensions with "
  756. "{} not supported".format(sub_type))
  757. return sub_exprs
  758. base = build_expr(ctx, expr.value)
  759. sub_type = type(expr.slice)
  760. if sub_type is ast.Index:
  761. if isinstance(expr.slice.value, ast.Tuple):
  762. # N-dimensional indexing using Tuple: x[(i, j, k)] is equivalent to x[i, j, k]
  763. # XXX: Indexing using a list is **different**! It triggers advanced indexing.
  764. indices = [build_expr(ctx, index_expr) for index_expr in expr.slice.value.elts]
  765. if not indices:
  766. # `col_offset` is an int, but `end_col_offset` is
  767. # `Optional[int]`. The magic number is here to make
  768. # sure we can parse `()` on any machine
  769. r = ctx.make_range(expr.lineno,
  770. expr.slice.value.col_offset,
  771. expr.slice.value.col_offset + 2)
  772. tup = TupleLiteral(r, [])
  773. indices.append(tup)
  774. return Subscript(base, indices)
  775. else:
  776. return Subscript(base, [build_expr(ctx, expr.slice.value)])
  777. elif sub_type is ast.Slice:
  778. return Subscript(base, [build_SliceExpr(ctx, base, expr.slice)])
  779. elif sub_type is ast.ExtSlice:
  780. return Subscript(base, build_ExtSlice(ctx, base, expr.slice))
  781. elif sys.version_info >= (3, 9): # In Python3.9 array indicies are not wrapped in ast.Index
  782. if sub_type is ast.Tuple:
  783. # N-dimensional indexing using Tuple: x[(i, j, k)] is equivalent to x[i, j, k]
  784. indices = []
  785. for index_expr in expr.slice.elts:
  786. if isinstance(index_expr, ast.Slice):
  787. indices.append(build_SliceExpr(ctx, base, index_expr))
  788. else:
  789. indices.append(build_expr(ctx, index_expr))
  790. # Special-case logic for `typing.Tuple[()]`
  791. if not indices:
  792. # See note above r.e. magic number
  793. r = ctx.make_range(expr.lineno,
  794. expr.slice.col_offset,
  795. expr.slice.col_offset + 2)
  796. tup = TupleLiteral(r, [])
  797. indices.append(tup)
  798. return Subscript(base, indices)
  799. return Subscript(base, [build_expr(ctx, expr.slice)])
  800. else: # Ellipsis (can only happen in Python 2)
  801. raise NotSupportedError(base.range(), "ellipsis is not supported")
  802. @staticmethod
  803. def build_List(ctx, expr):
  804. return ListLiteral(ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + 1),
  805. [build_expr(ctx, e) for e in expr.elts])
  806. @staticmethod
  807. def build_Tuple(ctx, expr):
  808. return TupleLiteral(ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + 1),
  809. [build_expr(ctx, e) for e in expr.elts])
  810. @staticmethod
  811. def build_Dict(ctx, expr):
  812. range = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + 1)
  813. if expr.keys and not expr.keys[0]:
  814. raise NotSupportedError(range, "Dict expansion (e.g. `{**dict}`) is not supported")
  815. return DictLiteral(range, [build_expr(ctx, e) for e in expr.keys],
  816. [build_expr(ctx, e) for e in expr.values])
  817. @staticmethod
  818. def build_Num(ctx, expr):
  819. value = str(expr.n)
  820. r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + len(value))
  821. return Const(r, value)
  822. @staticmethod
  823. def build_Constant(ctx, expr):
  824. value = expr.value
  825. if value is None or isinstance(value, bool):
  826. # NB: this check has to happen before the int check because bool is
  827. # a subclass of int
  828. return ExprBuilder.build_NameConstant(ctx, expr)
  829. if isinstance(value, (int, float, complex)):
  830. return ExprBuilder.build_Num(ctx, expr)
  831. elif isinstance(value, str):
  832. return ExprBuilder.build_Str(ctx, expr)
  833. elif isinstance(value, type(Ellipsis)):
  834. return ExprBuilder.build_Ellipsis(ctx, expr)
  835. else:
  836. error_range = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + len(str(value)))
  837. raise FrontendError(error_range, "Unknown Constant expression type")
  838. @staticmethod
  839. def build_Str(ctx, expr):
  840. value = str(expr.s)
  841. r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + len(value) + 1)
  842. return StringLiteral(r, value)
  843. @staticmethod
  844. def build_JoinedStr(ctx, expr):
  845. s = ''
  846. args = []
  847. for value in expr.values:
  848. r = ctx.make_range(value.lineno, value.col_offset, value.col_offset + 1)
  849. if isinstance(value, ast.FormattedValue):
  850. if value.conversion != -1:
  851. raise NotSupportedError(r, 'Don\'t support conversion in JoinedStr')
  852. if value.format_spec is not None:
  853. raise NotSupportedError(r, 'Don\'t support formatting in JoinedStr')
  854. s += '{}'
  855. args.append(build_expr(ctx, value.value))
  856. elif isinstance(value, ast.Str):
  857. s += value.s
  858. else:
  859. raise NotSupportedError(r, 'Unsupported value in JoinedStr')
  860. r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + 1)
  861. return Apply(Select(StringLiteral(r, s), Ident(r, 'format')), args, [])
  862. @staticmethod
  863. def build_ListComp(ctx, stmt):
  864. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset)
  865. if (len(stmt.generators) != 1):
  866. raise NotSupportedError(r, "Only a single generator is currently supported")
  867. if (len(stmt.generators[0].ifs) != 0):
  868. raise NotSupportedError(r, "Comprehension ifs are not supported yet")
  869. elt_expr = build_expr(ctx, stmt.elt)
  870. target_expr = build_expr(ctx, stmt.generators[0].target)
  871. iter_expr = build_expr(ctx, stmt.generators[0].iter)
  872. return ListComp(r, elt_expr, target_expr, iter_expr)
  873. @staticmethod
  874. def build_GeneratorExp(ctx, stmt):
  875. # Convert Generator expression to ListComp
  876. return ExprBuilder.build_ListComp(ctx, stmt)
  877. @staticmethod
  878. def build_DictComp(ctx, stmt):
  879. r = ctx.make_range(stmt.lineno, stmt.col_offset, stmt.col_offset)
  880. if (len(stmt.generators) != 1):
  881. raise NotSupportedError(r, "Only a single generator is currently supported")
  882. if (len(stmt.generators[0].ifs) != 0):
  883. raise NotSupportedError(r, "Comprehension ifs are not supported yet")
  884. key_expr = build_expr(ctx, stmt.key)
  885. value_expr = build_expr(ctx, stmt.value)
  886. target_expr = build_expr(ctx, stmt.generators[0].target)
  887. iter_expr = build_expr(ctx, stmt.generators[0].iter)
  888. return DictComp(r, key_expr, value_expr, target_expr, iter_expr)
  889. @staticmethod
  890. def build_Starred(ctx, expr):
  891. r = ctx.make_range(expr.lineno, expr.col_offset, expr.col_offset + 1)
  892. return Starred(r, build_expr(ctx, expr.value))
  893. build_expr = ExprBuilder()
  894. build_stmt = StmtBuilder()
  895. build_withitem = WithItemBuilder()
  896. def find_before(ctx, pos, substr, offsets=(0, 0)):
  897. new_pos = ctx.source[:pos].rindex(substr)
  898. return ctx.make_raw_range(new_pos + offsets[0], new_pos + len(substr) + offsets[1])