-
Notifications
You must be signed in to change notification settings - Fork 132
Add Pytorch implementation of Blockwise #988
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,32 @@ | ||
import torch | ||
import torch.compiler | ||
|
||
from pytensor.graph import FunctionGraph | ||
from pytensor.link.pytorch.dispatch import pytorch_funcify | ||
from pytensor.tensor.blockwise import Blockwise | ||
|
||
|
||
@pytorch_funcify.register(Blockwise) | ||
def funcify_Blockwise(op: Blockwise, node, *args, **kwargs): | ||
batched_dims = op.batch_ndim(node) | ||
core_node = op._create_dummy_core_node(node.inputs) | ||
core_fgraph = FunctionGraph(inputs=core_node.inputs, outputs=core_node.outputs) | ||
inner_func = pytorch_funcify(core_fgraph, squeeze_output=len(node.outputs) == 1) | ||
|
||
for _ in range(batched_dims): | ||
inner_func = torch.vmap(inner_func) | ||
ricardoV94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@torch.compiler.disable(recursive=False) | ||
def batcher(*inputs): | ||
op._check_runtime_broadcast(node, inputs) | ||
# broadcast on batched_dims | ||
all_batched_dims = tuple(t.shape[:batched_dims] for t in inputs) | ||
batched_shape = torch.broadcast_shapes(*all_batched_dims) | ||
broadcast_inputs = [ | ||
torch.broadcast_to(i, batched_shape + i.shape[batched_dims:]) | ||
for i in inputs | ||
] | ||
res = inner_func(*broadcast_inputs) | ||
return res | ||
|
||
return batcher |
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,53 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pytensor | ||
import pytensor.tensor as pt | ||
from pytensor.graph.basic import Apply | ||
from pytensor.graph.op import Op | ||
from pytensor.tensor.blockwise import Blockwise | ||
|
||
|
||
torch = pytest.importorskip("torch") | ||
basic = pytest.importorskip("pytensor.link.pytorch.dispatch.basic") | ||
|
||
|
||
class TestOp(Op): | ||
gufunc_signature = "(m,n),(n,p)->(m,p)" | ||
|
||
def __init__(self, final_shape): | ||
Ch0ronomato marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super().__init__() | ||
self.final_shape = final_shape | ||
self.call_shapes = [] | ||
|
||
def make_node(self, *args): | ||
return Apply(self, list(args), [pt.matrix("_", shape=self.final_shape)]) | ||
|
||
def perform(self, *_): | ||
raise RuntimeError("In perform") | ||
|
||
|
||
@basic.pytorch_funcify.register(TestOp) | ||
def evaluate_test_op(op, **_): | ||
@torch.compiler.disable(recursive=False) | ||
def func(a, b): | ||
op.call_shapes.extend(map(torch.Tensor.size, [a, b])) | ||
return a @ b | ||
|
||
return func | ||
|
||
|
||
def test_blockwise_broadcast(): | ||
_x = np.random.rand(5, 1, 2, 3) | ||
_y = np.random.rand(3, 3, 2) | ||
|
||
x = pt.tensor4("x", shape=(5, 1, 2, 3)) | ||
y = pt.tensor3("y", shape=(3, 3, 2)) | ||
op = TestOp((2, 2)) | ||
z = Blockwise(op)(x, y) | ||
|
||
f = pytensor.function([x, y], z, mode="PYTORCH") | ||
res = f(_x, _y) | ||
assert tuple(res.shape) == (5, 3, 2, 2) | ||
np.testing.assert_allclose(res, _x @ _y) | ||
assert op.call_shapes == [(2, 3), (3, 2)] |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
squeeze_output always works no?