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 (#50420)
Summary: Pull Request resolved: #50420 Test Plan: Imported from OSS Reviewed By: zdevito Differential Revision: D25880330 Pulled By: jamesr66a fbshipit-source-id: 27d34888e36e39924821fed891d79f969237a104
- Loading branch information
1 parent
0831984
commit 609f76f
Showing
6 changed files
with
548 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.