-
Notifications
You must be signed in to change notification settings - Fork 129
Named tensors with typed spaces #477
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
base: main
Are you sure you want to change the base?
Conversation
PyTensor variables shouldn't show up in the attributes of Variable types. |
had a few more thoughts on this, and found that also for unordered spaces we need to know which index a dimension has in the underlying array. With that information one can then index into Now the question is where this information should be kept. Either the Maybe it's enough to keep a |
class XTensorFromTensor(Op): | ||
__props__ = ("dims",) | ||
|
||
def __init__(self, dims: Iterable[DimLike]): |
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.
dims should be Sequence since Iterable can exhaust...
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.
What do you mean by "exhaust" here?
class XElemwise(Op): | ||
__props__ = ("scalar_op",) | ||
|
||
def __init__(self, scalar_op): |
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.
missing type hints
... | ||
|
||
|
||
class Dim(DimLike): |
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.
As far as I remember, it is a bad practice to inherit a base class from the protocol
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.
Dim can be just a dataclass instead
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.
According to the explanation in the PEP I would disagree: https://peps.python.org/pep-0544/#explicitly-declaring-implementation
By inheriting the protocol, we enable type checkers to warn about incomplete/incorrect implementations.
I took the branch from #407 and added a
pytensor.xtensor.spaces
module that defines types to distinguish between "unordered spaces" (BaseSpace
) and "ordered spaces" (OrderedSpace
).BaseSpace
andOrderedSpace
are similar to sets & tuples, but do not implement some operations that would mess up interpreting them asdims
.One idea here is to apply the mathematical operations not only to the variables, but also to their spaces.
For example:
This matches broadcasting in
xarray
:However,
xarray.DataArray.dims
are tuples, and the commutative rule does not apply to addition ofxarray.DataArray
variables'dims
:In contrast, with this PR the resulting dims become an unordered space, and the resulting
XTensorType
are equal:This was basic math, but we could introduce
XOp
s withXOp.infer_space
methods that can implement broadcasting rules for any operation:Similarly, this should allow us to implement dot products requiring
OrderedSpace
inputs to produce anOrderedSpace
output, or aspecify_dimorder
XOp
that orders aBaseSpace
into anOrderedSpace
.Looking at the
(None, None, None)
shape from the code block above, I wonder if we should typeXTensorType.shape
as aMapping[DimLike, int | ScalarVariable]
🤔