-
Notifications
You must be signed in to change notification settings - Fork 136
Implement dot for XTensorVariables #1475
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
ricardoV94
merged 12 commits into
pymc-devs:labeled_tensors
from
AllenDowney:add_xtensor_dot_take3
Jun 19, 2025
+329
−2
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1a4755e
Adding dot for xtensor
AllenDowney 28796dc
First pass at xtensor.dot
AllenDowney 7c9214d
Lint
AllenDowney 18501bf
Adding shape checking at rewrite time
AllenDowney 23b1799
Cleanup
AllenDowney 497c974
Compose XDot and Sum
AllenDowney e81657e
Generalizing XDot
AllenDowney e44ca9f
Now with more einsum
AllenDowney 6ce20d0
Cleanup
AllenDowney ed6bbd6
Handle symbolic shapes
AllenDowney 9f33117
Use specify_shape (and better error tests)
AllenDowney 1caf977
Moving shape check to make_node
AllenDowney 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,47 @@ | ||
from string import ascii_lowercase | ||
|
||
from pytensor.graph import node_rewriter | ||
from pytensor.tensor import einsum | ||
from pytensor.tensor.shape import specify_shape | ||
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) | ||
|
||
# Collect all dimension names across inputs and output | ||
all_dims = list( | ||
dict.fromkeys(x.type.dims + y.type.dims + out.type.dims) | ||
) # preserve order | ||
if len(all_dims) > len(ascii_lowercase): | ||
raise ValueError("Too many dimensions to map to einsum subscripts") | ||
|
||
dim_to_char = dict(zip(all_dims, ascii_lowercase)) | ||
|
||
# Build einsum string | ||
x_subs = "".join(dim_to_char[d] for d in x.type.dims) | ||
y_subs = "".join(dim_to_char[d] for d in y.type.dims) | ||
out_subs = "".join(dim_to_char[d] for d in out.type.dims) | ||
einsum_str = f"{x_subs},{y_subs}->{out_subs}" | ||
|
||
# Perform the einsum operation | ||
out_tensor = einsum(einsum_str, x_tensor, y_tensor) | ||
|
||
# Reshape to match the output shape | ||
out_tensor = specify_shape(out_tensor, out.type.shape) | ||
|
||
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
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.
I'm gonna merge because I need it for testing, but the xarray dot allows arbitrary number of operands, which we should try to support eventually. Will open an issue
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.
should be an easy implementation. we can come back to it.
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.
#1485