Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP][FX] Add Interpreter and Transformer
ghstack-source-id: 9d200c9512549670d0c78e7ba86bb38cf3f79b4a Pull Request resolved: #50420
- Loading branch information
James Reed
committed
Jan 29, 2021
1 parent
dbfaf96
commit 964ab86
Showing
6 changed files
with
528 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,17 @@ | ||
import torch | ||
import torch.fx | ||
from torch.fx.node import Node | ||
from typing import Any | ||
|
||
from typing import Dict | ||
class ShapeProp(torch.fx.Interpreter): | ||
def run_node(self, n : Node) -> Any: | ||
result = super().run_node(n) | ||
|
||
class ShapeProp: | ||
def __init__(self, mod): | ||
self.mod = mod | ||
self.graph = mod.graph | ||
self.modules = dict(self.mod.named_modules()) | ||
if isinstance(result, torch.Tensor): | ||
n.shape = result.shape # type: ignore | ||
n.dtype = result.dtype # type: ignore | ||
|
||
def propagate(self, *args): | ||
args_iter = iter(args) | ||
env : Dict[str, Node] = {} | ||
|
||
def load_arg(a): | ||
return torch.fx.node.map_arg(a, lambda n: env[n.name]) | ||
|
||
def fetch_attr(target : str): | ||
target_atoms = target.split('.') | ||
attr_itr = self.mod | ||
for i, atom in enumerate(target_atoms): | ||
if not hasattr(attr_itr, atom): | ||
raise RuntimeError(f"Node referenced nonexistant target {'.'.join(target_atoms[:i])}") | ||
attr_itr = getattr(attr_itr, atom) | ||
return attr_itr | ||
|
||
for node in self.graph.nodes: | ||
if node.op == 'placeholder': | ||
result = next(args_iter) | ||
elif node.op == 'get_attr': | ||
result = fetch_attr(node.target) | ||
elif node.op == 'call_function': | ||
result = node.target(*load_arg(node.args), **load_arg(node.kwargs)) | ||
elif node.op == 'call_method': | ||
self_obj, *args = load_arg(node.args) | ||
kwargs = load_arg(node.kwargs) | ||
result = getattr(self_obj, node.target)(*args, **kwargs) | ||
elif node.op == 'call_module': | ||
result = self.modules[node.target](*load_arg(node.args), **load_arg(node.kwargs)) | ||
elif node.op == 'output': | ||
return load_arg(node.args[0]) | ||
return result | ||
|
||
if isinstance(result, torch.Tensor): | ||
node.shape = result.shape | ||
node.dtype = result.dtype | ||
|
||
env[node.name] = result | ||
|
||
return None | ||
def propagate(self, *args): | ||
return super().run(*args) |
Oops, something went wrong.