/usr/local/lib64/python3.6/site-packages/torch/fx/__pycache__
Edit: /usr/local/lib64/python3.6/site-packages/torch/fx/__pycache__/interpreter.cpython-36.pyc (17660B)
3
EgmH @ s d dl mZ d dlmZ d dlmZmZmZmZm Z d dl
mZ d dlm
Z
d dlmZ ddlmZmZmZmZmZmZmZ ed d
G dd dZed d
G d
d deZdS ) )GraphModule)Graph)ArgumentNodeTargetmap_arg
map_aggregate)Proxy)Tracer)
compatibility )AnyDictIteratorListOptionalTupleUnionT)is_backward_compatiblec @ s e Zd ZdZeddd&eedddZeddddee e
ef ed d
dZedde
edd
dZ
edddeedf e eef edddZedddeedf e eef edddZedddeedf e eef edddZedddeedf e eef edddZedddeedf e eef edddZedddeedf e eef edddZeddeddd Zedde
eee f dd!d"Zeddee
ed#d$d%ZdS )'Interpretera
An Interpreter executes an FX graph Node-by-Node. This pattern
can be useful for many things, including writing code
transformations as well as analysis passes.
Methods in the Interpreter class can be overridden to customize
the behavior of execution. The map of overrideable methods
in terms of call hierarchy::
run()
+-- run_node
+-- placeholder()
+-- get_attr()
+-- call_function()
+-- call_method()
+-- call_module()
+-- output()
Example:
Suppose we want to swap all instances of ``torch.neg`` with
``torch.sigmoid`` and vice versa (including their ``Tensor``
method equivalents). We could subclass Interpreter like so::
class NegSigmSwapInterpreter(Interpreter):
def call_function(self, target : Target,
args : Tuple, kwargs : Dict) -> Any:
if target == torch.sigmoid:
return torch.neg(*args, **kwargs)
return super().call_function(n)
def call_method(self, target : Target,
args : Tuple, kwargs : Dict) -> Any:
if target == 'neg':
call_self, *args_tail = args
return call_self.sigmoid(*args_tail, **kwargs)
return super().call_method(n)
def fn(x):
return torch.sigmoid(x).neg()
gm = torch.fx.symbolic_trace(fn)
input = torch.randn(3, 4)
result = NegSigmSwapInterpreter(gm).run(input)
torch.testing.assert_allclose(result, torch.neg(input).sigmoid())
Args:
module (GraphModule): The module to be executed
garbage_collect_values (bool): Whether to delete values after their last
use within the Module's execution. This ensures optimal memory usage during
execution. This can be disabled to, for example, examine all of the intermediate
values in the execution by looking at the ``Interpreter.env`` attribute.
T)r )modulegarbage_collect_valuesc s t |tst|_tjj _i _|_jri i _ t
t
dfddxBtjjj
D ]0 t j fdd t j fdd qfW d S )N)nuserc s( | kr$| | <