Skip to content

WIP: experiment with first class dim objects #1517

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

aseyboldt
Copy link
Member

@aseyboldt aseyboldt commented Jul 2, 2025

Very much still an experiment! I'm still working on this, but thought it might be helpful to already share what I have so far...

In this version of named-dims, we use objects to represent dimensions instead of plain strings. This allows us to ensure that array axes with shared dimensions are always compatible.

A dimension variable stores the length of the dimension at runtime.
The type of the dimension provides the identity that is used to ensure tensors don't use duplicate dimensions.

# A dimension variable
>>> foo = px.dim("foo")

# We can inspect the dim type (the final .dim attribute should be hidden a little when we go further with this)
>>> foo.type.dim
BaseDim(size=None, name='foo', uuid=UUID('930aff02-e5d7-4a25-89da-486c9d5b2412'))

We can then create tensors with given dimensions:

>>> x = px.xtensor("x", dims=[foo, foo.clone_dim()])
>>> x.type.dims
(BaseDim(size=None, name='foo', uuid=UUID('930aff02-e5d7-4a25-89da-486c9d5b2412')),
 CloneDim(size=None, name=None, base=BaseDim(size=None, name='foo', uuid=UUID('930aff02-e5d7-4a25-89da-486c9d5b2412')), uuid=UUID('01ba2ef7-6d73-4082-b769-b5cb7909a16d')))

(Obvious todo: improve pretty printing!)
(Currently those are the dim.type.dim objects, I think we could just use dim.type directly to make it cleaner)

foo.clone_dim() prodvides a new dimension, that has the same shape as foo, but a different identity, so that we can build a tensor out of foo and foo.clone_dim(). Different instances of foo.clone_dim() are treated as different dimensions.

We can recover a dimension variable from an xtensor that has this dimension (because it implicitly stores the size of the dimension, and also stores the dimension type):

>>> x.dims[1].dprint();
FromTensor{dim_type=<pytensor.xtensor.type.DimType object at 0x7f3d372b3080>} [id A]
 └─ x [id B]

@OriolAbril @ricardoV94


📚 Documentation preview 📚: https://pytensor--1517.org.readthedocs.build/en/1517/

@@ -38,6 +41,184 @@
from pytensor.tensor.variable import TensorConstantSignature, TensorVariable


# I think uint64 would make more sense, but some code in tensor/rewrites/shape
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numpy uses int64 for shape internally so we end up doing it also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants