-
Notifications
You must be signed in to change notification settings - Fork 48
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
Unified generic Tensor class #311
Comments
|
I have the same question. A parametric Tensor type would be perfect, the main question is whether we enable a way to specify dimensions. I thought there was an earlier proposal to use multiple generic-parameters when it becomes available? |
|
Re @BowenBao 's question:
In the existing system, INT64 represents a tensor (with int64 element types). In the proposed system, we should really say |
Some options I came up with @gramalingam @xadupre Tensor[TReal, Shape["M", 1]] # Shape can be a non-generic with __class_get_item__ defined; mypy will still fail
Tensor[TReal, "M", 1] # Supported only after 3.11: python will fail in runtime if before 3.11
Tensor[TReal, Literal["M"], Literal[1]] # mypy will not fail but it will also not do anything useful
Tensor[INT64["M"]] # reusing existing; mypy will fail; doesn't support typevars @script(
shape={
"self": ("M", "N"),
}
)
def aten_add(self: Tensor[TReal], other: Tensor[TReal], alpha: float = 1.0) -> Tensor[TReal]: cons: verbose, duplication |
It could be great to able to indicate the output is of the exact same type of the input, not only real. |
Yes. This is why I think typevars are great |
Another option for shapes might be:
Yes, using type-vars to indicate type-constraints is useful. One issue @abock ran into using type-vars (in his generation of signatures for all ONNX ops) was getting intellisense to be useful for usage of type-vars. May not be an issue if we use predefined vars like Another detail: using |
Yes, type-vars in Python typing bring much from the world of traditional generics ala C# - we can express those constraints. I think it's worth revisiting seeing exactly which distinct type unions fall out of the codegen and use that to inform what our generic unions could be and how they could be named. With sensible naming, the IntelliSense issue may be acceptable today, and I expect rich support for typings in the IDEs to improve over time as well. |
I suggest we go with |
Will the use of strings (like "M") work with mypy (which used to complain about this ... it allowed int constants, but not string constants, as "dependent types") Edited: Ok I notice it is implied above in an earlier message that this will fail mypy, unlike the more verbose |
It will still fail mypy the same way we do now. However since Python 3.12(or 3.11?) will make this valid, I am hoping mypy will catch up |
Also reference: https://github.com/google/jaxtyping |
Create turn Tensor to a generic class to unify function definition and evaluator interfaces.
Tensor to represent a symbolic tensor by default, optionally made concrete by initializing with an numpy array
Usage
typing definition:
In function definition:
Traced only functions:
Evaluators operate on concrete Tensors
cc @gramalingam @fatcat-z @xiaowuhu @BowenBao @titaiwangms @abock
The text was updated successfully, but these errors were encountered: