-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements shape and MakeVector Ops in PyTorch
- Shape - Shape_i - Reshape - SpecifyShape - Unbroadcast - MakeVector
- Loading branch information
Showing
6 changed files
with
149 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import torch | ||
|
||
from pytensor.link.pytorch.dispatch.basic import pytorch_funcify | ||
from pytensor.tensor.shape import Reshape, Shape, Shape_i, SpecifyShape, Unbroadcast | ||
|
||
|
||
@pytorch_funcify.register(Reshape) | ||
def pytorch_funcify_Reshape(op, node, **kwargs): | ||
shape = node.inputs[1] | ||
|
||
def reshape(x, shape=shape): | ||
return torch.reshape(x, tuple(shape)) | ||
|
||
return reshape | ||
|
||
|
||
@pytorch_funcify.register(Shape) | ||
def pytorch_funcify_Shape(op, **kwargs): | ||
def shape(x): | ||
return x.shape | ||
|
||
return shape | ||
|
||
|
||
@pytorch_funcify.register(Shape_i) | ||
def pytorch_funcify_Shape_i(op, **kwargs): | ||
i = op.i | ||
|
||
def shape_i(x): | ||
return x.shape[i] | ||
|
||
return shape_i | ||
|
||
|
||
@pytorch_funcify.register(SpecifyShape) | ||
def pytorch_funcify_SpecifyShape(op, node, **kwargs): | ||
def specifyshape(x, *shape): | ||
assert x.ndim == len(shape) | ||
for actual, expected in zip(x.shape, shape): | ||
if expected is None: | ||
continue | ||
if actual != expected: | ||
raise ValueError(f"Invalid shape: Expected {shape} but got {x.shape}") | ||
return x | ||
|
||
return specifyshape | ||
|
||
|
||
@pytorch_funcify.register(Unbroadcast) | ||
def pytorch_funcify_Unbroadcast(op, **kwargs): | ||
def unbroadcast(x): | ||
return x | ||
|
||
return unbroadcast | ||
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import numpy as np | ||
|
||
import pytensor.tensor as pt | ||
from pytensor.compile.ops import DeepCopyOp, ViewOp | ||
from pytensor.configdefaults import config | ||
from pytensor.graph.fg import FunctionGraph | ||
from pytensor.tensor.shape import Shape, Shape_i, Unbroadcast, reshape | ||
from pytensor.tensor.type import iscalar, vector | ||
from tests.link.pytorch.test_basic import compare_pytorch_and_py | ||
|
||
|
||
def test_pytorch_shape_ops(): | ||
x_np = np.zeros((20, 3)) | ||
x = Shape()(pt.as_tensor_variable(x_np)) | ||
x_fg = FunctionGraph([], [x]) | ||
|
||
compare_pytorch_and_py(x_fg, [], must_be_device_array=False) | ||
|
||
x = Shape_i(1)(pt.as_tensor_variable(x_np)) | ||
x_fg = FunctionGraph([], [x]) | ||
|
||
compare_pytorch_and_py(x_fg, [], must_be_device_array=False) | ||
|
||
|
||
def test_pytorch_specify_shape(): | ||
in_pt = pt.matrix("in") | ||
x = pt.specify_shape(in_pt, (4, None)) | ||
x_fg = FunctionGraph([in_pt], [x]) | ||
compare_pytorch_and_py(x_fg, [np.ones((4, 5)).astype(config.floatX)]) | ||
|
||
# When used to assert two arrays have similar shapes | ||
in_pt = pt.matrix("in") | ||
shape_pt = pt.matrix("shape") | ||
x = pt.specify_shape(in_pt, shape_pt.shape) | ||
x_fg = FunctionGraph([in_pt, shape_pt], [x]) | ||
compare_pytorch_and_py( | ||
x_fg, | ||
[np.ones((4, 5)).astype(config.floatX), np.ones((4, 5)).astype(config.floatX)], | ||
) | ||
|
||
|
||
def test_pytorch_Reshape_constant(): | ||
a = vector("a") | ||
x = reshape(a, (2, 2)) | ||
x_fg = FunctionGraph([a], [x]) | ||
compare_pytorch_and_py(x_fg, [np.r_[1.0, 2.0, 3.0, 4.0].astype(config.floatX)]) | ||
|
||
|
||
def test_pytorch_Reshape_shape_graph_input(): | ||
a = vector("a") | ||
shape_pt = iscalar("b") | ||
x = reshape(a, (shape_pt, shape_pt)) | ||
x_fg = FunctionGraph([a, shape_pt], [x]) | ||
compare_pytorch_and_py(x_fg, [np.r_[1.0, 2.0, 3.0, 4.0].astype(config.floatX), 2]) | ||
|
||
|
||
def test_pytorch_compile_ops(): | ||
x = DeepCopyOp()(pt.as_tensor_variable(1.1)) | ||
x_fg = FunctionGraph([], [x]) | ||
|
||
compare_pytorch_and_py(x_fg, []) | ||
|
||
x_np = np.zeros((20, 1, 1)) | ||
x = Unbroadcast(0, 2)(pt.as_tensor_variable(x_np)) | ||
x_fg = FunctionGraph([], [x]) | ||
|
||
compare_pytorch_and_py(x_fg, []) | ||
|
||
x = ViewOp()(pt.as_tensor_variable(x_np)) | ||
x_fg = FunctionGraph([], [x]) | ||
|
||
compare_pytorch_and_py(x_fg, []) |
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