-
Notifications
You must be signed in to change notification settings - Fork 135
Adding dot for xtensor #1450
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
Open
AllenDowney
wants to merge
1
commit into
pymc-devs:labeled_tensors
Choose a base branch
from
AllenDowney:add_xtensor_dot_take2
base: labeled_tensors
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Adding dot for xtensor #1450
Changes from all commits
Commits
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import pytensor.xtensor.rewriting.basic | ||
import pytensor.xtensor.rewriting.indexing | ||
import pytensor.xtensor.rewriting.math | ||
import pytensor.xtensor.rewriting.reduction | ||
import pytensor.xtensor.rewriting.shape | ||
import pytensor.xtensor.rewriting.vectorization |
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,40 @@ | ||
from pytensor.graph import node_rewriter | ||
from pytensor.tensor import tensordot | ||
from pytensor.xtensor.basic import tensor_from_xtensor, xtensor_from_tensor | ||
from pytensor.xtensor.math import XDot | ||
from pytensor.xtensor.rewriting.utils import register_lower_xtensor | ||
|
||
|
||
@register_lower_xtensor | ||
@node_rewriter(tracks=[XDot]) | ||
def lower_dot(fgraph, node): | ||
"""Rewrite XDot to tensor.dot. | ||
|
||
This rewrite converts an XDot operation to a tensor-based dot operation, | ||
handling dimension alignment and contraction. | ||
""" | ||
[x, y] = node.inputs | ||
[out] = node.outputs | ||
|
||
# Convert inputs to tensors | ||
x_tensor = tensor_from_xtensor(x) | ||
y_tensor = tensor_from_xtensor(y) | ||
|
||
# Get dimensions to contract | ||
if node.op.dims is None: | ||
# Contract over all matching dimensions | ||
x_dims = set(x.type.dims) | ||
y_dims = set(y.type.dims) | ||
contract_dims = tuple(x_dims & y_dims) | ||
else: | ||
contract_dims = node.op.dims | ||
|
||
# Get axes to contract for each input | ||
x_axes = [x.type.dims.index(dim) for dim in contract_dims] | ||
y_axes = [y.type.dims.index(dim) for dim in contract_dims] | ||
|
||
# Perform dot product | ||
out_tensor = tensordot(x_tensor, y_tensor, axes=(x_axes, y_axes)) | ||
|
||
# Convert back to xtensor | ||
return [xtensor_from_tensor(out_tensor, out.type.dims)] |
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 |
---|---|---|
|
@@ -151,3 +151,34 @@ def test_cast(): | |
yc64 = x.astype("complex64") | ||
with pytest.raises(TypeError, match="Casting from complex to real is ambiguous"): | ||
yc64.astype("float64") | ||
|
||
|
||
def test_dot(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks promising, but needs some tests calling dot with specific dims? |
||
"""Test basic dot product operations.""" | ||
# Test matrix-matrix dot product | ||
x = xtensor("x", dims=("a", "b"), shape=(2, 3)) | ||
y = xtensor("y", dims=("b", "c"), shape=(3, 4)) | ||
z = x.dot(y) | ||
assert z.type.dims == ("a", "c") | ||
assert z.type.shape == (2, 4) | ||
|
||
fn = xr_function([x, y], z) | ||
x_test = DataArray(np.ones((2, 3)), dims=("a", "b")) | ||
y_test = DataArray(np.ones((3, 4)), dims=("b", "c")) | ||
z_test = fn(x_test, y_test) | ||
expected = x_test.dot(y_test) | ||
xr_assert_allclose(z_test, expected) | ||
|
||
# Test matrix-vector dot product | ||
x = xtensor("x", dims=("a", "b"), shape=(2, 3)) | ||
y = xtensor("y", dims=("b",), shape=(3,)) | ||
z = x.dot(y) | ||
assert z.type.dims == ("a",) | ||
assert z.type.shape == (2,) | ||
|
||
fn = xr_function([x, y], z) | ||
x_test = DataArray(np.ones((2, 3)), dims=("a", "b")) | ||
y_test = DataArray(np.ones(3), dims=("b",)) | ||
z_test = fn(x_test, y_test) | ||
expected = x_test.dot(y_test) | ||
xr_assert_allclose(z_test, expected) |
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.
These checks are better placed in
make_node