-
Notifications
You must be signed in to change notification settings - Fork 5
fx shape propagation avoids allocating expensive default summation intermediates, computes intermediate shapes formally #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Linux-cpp-lisp
merged 11 commits into
Linux-cpp-lisp:main
from
mister-bailey:efficient_opt_einsum
Nov 7, 2021
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c5730ff
fx shape propogation avoids allocating expensive default summation in…
mister-bailey 134254d
flake8
mariogeiger 1db7896
tensordot
mariogeiger acaa9e3
torch.fx.Interpreter from scratch
mariogeiger 51f6579
fix?
mariogeiger 2ffd300
remove 1.8.0 version of get_shape
mariogeiger a82279b
fix?
mariogeiger 1aff1a3
fix
mariogeiger 11857f7
check dtype
mariogeiger 259ea74
Update opt_einsum_fx/_efficient_shape_prop.py
mariogeiger e159c75
expand trick and propagate device
mariogeiger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from typing import Any, NamedTuple | ||
|
||
import opt_einsum | ||
import torch | ||
from torch.fx.node import Node | ||
|
||
from ._fuse import _EINSUM_FUNCS | ||
|
||
|
||
class SimpleMeta(NamedTuple): | ||
""" | ||
The full ShapeProp defines and uses a NamedTuple to | ||
store a whole bunch of metadata about the tensors | ||
going into and out of the Node op. But we don't | ||
have most of that info, and anyway, I don't think | ||
most of it's used in opt_einsum or opt_einsum_fx. | ||
(These are only concerned with computing a summation | ||
order.) | ||
|
||
Rather than give dummy or default values, which I | ||
only *assume* would be fine, I'm defining a NamedTuple | ||
with only the values we actually know. So if I'm wrong | ||
we will get a very clear error message, rather than | ||
some invisible error. | ||
""" | ||
|
||
shape: torch.Size | ||
dtype: torch.dtype | ||
|
||
|
||
class EfficientShapeProp(torch.fx.Interpreter): | ||
""" | ||
Like ShapeProp, traverses a graph Node-by-Node | ||
and records the shape and type of the result | ||
into each Node. | ||
|
||
Except we treat 'einsum' as a special case. | ||
We don't actually execute 'einsum' on tensors, | ||
since the einsums will typically not be optimized | ||
yet (ShapeProp is called before optimization), | ||
and inefficient summation order can create | ||
enormous intermediate tensors, which often creates | ||
needless out-of-memory errors. | ||
|
||
So we override 'run_node' only for 'einsums'. | ||
It's straightforward to determine the shape of the | ||
result just from the output indices. | ||
|
||
(The call to opt_einsum that will typically follow | ||
this, also doesn't actually build the tensors | ||
during its exploration.) | ||
""" | ||
|
||
def run_node(self, n: Node) -> Any: | ||
if n.op == "call_function" and n.target in _EINSUM_FUNCS: | ||
args, kwargs = self.fetch_args_kwargs_from_env(n) | ||
equation, *operands = args | ||
shapes = [op.shape for op in operands] | ||
|
||
assert len({op.dtype for op in operands}) == 1 | ||
meta = SimpleMeta(einsum_shape(equation, *shapes), operands[0].dtype) | ||
result = torch.zeros((1,) * len(meta.shape), dtype=meta.dtype, device=operands[0].device).expand(meta.shape) | ||
elif n.op == "call_function" and n.target == torch.tensordot: | ||
args, kwargs = self.fetch_args_kwargs_from_env(n) | ||
shape_a = [dim for i, dim in enumerate(args[0].shape) if i not in kwargs['dims'][0]] | ||
shape_b = [dim for i, dim in enumerate(args[1].shape) if i not in kwargs['dims'][1]] | ||
|
||
assert len({op.dtype for op in args}) == 1 | ||
meta = SimpleMeta(shape_a + shape_b, args[0].dtype) | ||
result = torch.zeros((1,) * len(meta.shape), dtype=meta.dtype, device=args[0].device).expand(meta.shape) | ||
else: | ||
result = super().run_node(n) | ||
|
||
if isinstance(result, torch.Tensor): | ||
meta = SimpleMeta(result.shape, result.dtype) | ||
else: | ||
meta = None | ||
|
||
n.meta = dict() | ||
n.meta['tensor_meta'] = meta | ||
n.meta['type'] = type(result) | ||
|
||
return result | ||
|
||
def propagate(self, *args): | ||
return super().run(*args) | ||
|
||
|
||
def einsum_shape(subscripts, *shapes): | ||
""" | ||
Given an einsum equation and input shapes, returns the output | ||
shape of the einsum. | ||
|
||
Args: | ||
subscripts: the einsum formula | ||
shapes: the input shapes | ||
""" | ||
Shaped = NamedTuple('Shaped', [('shape', tuple)]) | ||
input_subscripts, output_subscript, _ = opt_einsum.parser.parse_einsum_input( | ||
(subscripts,) + tuple(Shaped(shape) for shape in shapes) | ||
) | ||
dims = { | ||
i: dim | ||
for ii, shape in zip(input_subscripts.split(','), shapes) | ||
for i, dim in zip(ii, shape) | ||
} | ||
return tuple(dims[i] for i in output_subscript) |
This file contains hidden or 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 hidden or 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,28 +1,14 @@ | ||
from typing import Optional | ||
from packaging import version | ||
|
||
import torch | ||
from torch import fx | ||
|
||
_TORCH_IS_GE_19: bool = version.parse(torch.__version__) >= version.parse("1.9.0") | ||
|
||
# The torch FX APIs are not stable, so we need helper wrappers | ||
|
||
if _TORCH_IS_GE_19: | ||
|
||
def get_shape(n: fx.Node) -> Optional[torch.Size]: | ||
"""Get the shape of a node after ``ShapeProp``""" | ||
try: | ||
return n.meta["tensor_meta"].shape | ||
except KeyError: | ||
return None | ||
|
||
|
||
else: | ||
|
||
def get_shape(n: fx.Node) -> Optional[torch.Size]: | ||
"""Get the shape of a node after ``ShapeProp``""" | ||
try: | ||
return n.shape | ||
except AttributeError: | ||
return None | ||
def get_shape(n: fx.Node) -> Optional[torch.Size]: | ||
"""Get the shape of a node after ``ShapeProp``""" | ||
try: | ||
return n.meta["tensor_meta"].shape | ||
except KeyError: | ||
return None | ||
except AttributeError: | ||
return None |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.