XTensorVariable indexing update#1438
Merged
ricardoV94 merged 3 commits intopymc-devs:labeled_tensorsfrom Jun 4, 2025
Merged
Conversation
4e105b4 to
e5fbf7a
Compare
e5fbf7a to
04f057c
Compare
OriolAbril
approved these changes
Jun 3, 2025
Member
OriolAbril
left a comment
There was a problem hiding this comment.
I was also unable to find a combination of indexes/setter data that I think should work but doesn't.
This one already works but I'd suggest adding as a test case too:
x = xtensor("x", shape=(5, 5), dims=("a", "b"))
y = xtensor("y", shape=(3,), dims=("d",))
idx = xtensor("idx", dtype=int, shape=(None,), dims=("d",))
# define "d" dimension by slicing the "a" dimension
# so we can set y into x
orthogonal_update1 = x[idx].set(y)
fn = xr_function([x, idx, y], [orthogonal_update1])
x_test = np.abs(xr_random_like(x))
y_test = -np.abs(xr_random_like(y))
idx_test = DataArray([0, 2, 3], dims=("d",))
result = fn(x_test, idx_test, y_test)
x_test[idx_test] = y_test
expected_result = [x_test]
xr_assert_allclose(result, expected_result)
Member
Author
Will do! |
04f057c to
2ad906a
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This builds on top of #1429 to support index assignment and increment
x[idx] = yandx[idx] += y, following xarray indexing and broadcasting semantics.As with regular TensorVariables, due to immutability/hashability constraints of PyTensor, it is not possible to use the python native operations, so one has to write
z = x[idx].set(y)andz = x[idx].inc(y)📚 Documentation preview 📚: https://pytensor--1438.org.readthedocs.build/en/1438/