/usr/local/lib64/python3.6/site-packages/torch/fx/experimental/__pycache__
Edit: /usr/local/lib64/python3.6/site-packages/torch/fx/experimental/__pycache__/rewriter.cpython-36.pyc (4813B)
3
EgO @ s d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm Z m
Z
mZmZm
Z
d dlmZ d dlmZ d dlmZ d dlZG dd de jZG d d
d
eZe ejje
f e ejje
f ddd
ZdS ) N)FunctionType)castUnionCallableDictOptionalAny)Tracer)Graph)normalize_source_linesc @ s. e Zd ZdZedddZdd Zdd Zd S )
AST_Rewriterax
Take a FunctionType object representing a `forward` method, then
perform an AST rewrite to swap out nodes that are not symbolically
traceable with a callsite to the FX alternative.
To support swapping out an AST node, define a new `visit` method on
that node. For more details, see:
https://docs.python.org/3/library/ast.html#ast.NodeTransformer
)fnc C s t j|\}}t|}dj|}tj|}tj|}tj| j |}t
|dd}tj|j} t
| j }
t|| tt
| j |
}t|dkst| |d }dd }
|
||jdS )N exec r c S s8 t | j|| j| j| jd}tj|| }tj| j|_|S )z?Based on https://stackoverflow.com/a/13503277/2988730 (@unutbu))nameZargdefsZclosure) r __code____name____defaults____closure__ functoolsupdate_wrappercopy__kwdefaults__)fglobalsg r J/usr/local/lib64/python3.6/site-packages/torch/fx/experimental/rewriter.pychange_func_globals. s
z1AST_Rewriter.rewrite.
.change_func_globals)r )inspectgetsourcelinesr jointextwrapdedentastparsefix_missing_locationsvisitcompiler __globals__setkeysr listlenAssertionError)selfr
sourcelines_sourceZnormalized_strZ
source_astZdest_astcodeZglobals_dictZkeys_beforeZnew_keysZfn_compiledr r r r rewrite s
zAST_Rewriter.rewritec C sr t jddd}t|t jst|j}t|t js4t|jr@|jnt jddd}|j |g|_
t j|d}t j||S )z
Swap out the Assert node (Python's `assert`) with a callsite to the
symbolically-traceable torch._assert function
ztorch._assert()eval)moder N)valuekind)r8 )
r% r&