Skip to content
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

Fix TensorType to return new TensorTypes for shapes instead of TensorType instances #228

Merged
merged 3 commits into from
Dec 7, 2022

Conversation

abock
Copy link
Contributor

@abock abock commented Dec 2, 2022

This ensures that TensorType is never actually instantiated and instead new TensorType subclasses are created (and cached) for each shape.

This is a stepping stone to a fully generic TensorType where the dtype and shape are modeled into the type itself, but it appears that won't be easily realized until we can depend on Python 3.11.

Importantly, this adds unit tests to ensure if we can move to a proper generic that behavior will remain the same.

Fixes #219

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

lintrunner found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

@codecov
Copy link

codecov bot commented Dec 2, 2022

Codecov Report

Merging #228 (fcd664e) into main (ed41071) will increase coverage by 0.08%.
The diff coverage is 98.83%.

@@            Coverage Diff             @@
##             main     #228      +/-   ##
==========================================
+ Coverage   75.57%   75.66%   +0.08%     
==========================================
  Files          89       90       +1     
  Lines        7248     7274      +26     
==========================================
+ Hits         5478     5504      +26     
  Misses       1770     1770              
Impacted Files Coverage Δ
onnxscript/test/onnx_types_test.py 97.22% <97.22%> (ø)
onnxscript/onnx_types.py 96.15% <100.00%> (+0.53%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

onnxscript/test/onnx_types_test.py Fixed Show fixed Hide fixed
onnxscript/test/onnx_types_test.py Fixed Show fixed Hide fixed
@abock abock force-pushed the abock/uninstantiated-tensor-types branch 3 times, most recently from 87c8432 to 2fb12b6 Compare December 2, 2022 03:50
Copy link
Contributor

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

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

LGTM🚀

from __future__ import annotations

from typing import Optional, Tuple, Union
from abc import ABC
Copy link
Contributor

Choose a reason for hiding this comment

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

I would import abc and use abc.ABC

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any particular reason?

Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly because of this https://github.com/microsoft/onnx-script/wiki/Coding-style#avoid-relative-imports-import-only-modules, as suggested by https://google.github.io/styleguide/pyguide.html#23-packages.

ABC is usually unique enough, but I think maintaining a single rule of importing only modules can save us from deciding what to import as modules and what not to.

@abock abock force-pushed the abock/uninstantiated-tensor-types branch from 2fb12b6 to 8eb6213 Compare December 2, 2022 15:30
@abock abock force-pushed the abock/uninstantiated-tensor-types branch 2 times, most recently from 4ec4b4c to b7a7c97 Compare December 2, 2022 18:08
onnxscript/test/onnx_types_test.py Fixed Show fixed Hide fixed
onnxscript/test/onnx_types_test.py Fixed Show fixed Hide fixed
onnxscript/test/onnx_types_test.py Fixed Show fixed Hide fixed
onnxscript/test/onnx_types_test.py Fixed Show fixed Hide fixed
Copy link
Collaborator

@gramalingam gramalingam left a comment

Choose a reason for hiding this comment

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

LGTM, thanks. One thing I am not entirely clear is how well other tools, like mypy, will be when dealing with onnxscript code with type annotations, but that's a different question, may be worth talking about in our meeting. E.g., would using something like Annotated[TensorType, Dtype(dtype), Shape(shape)] work better with other type-checkers and tools?

@abock abock force-pushed the abock/uninstantiated-tensor-types branch 2 times, most recently from 559f507 to d4ef07a Compare December 2, 2022 19:24
@abock
Copy link
Contributor Author

abock commented Dec 2, 2022

As for tooling, I am very unimpressed with mypy in particular. There is an actual bug in mypy that is causing it to not see the __class_getitem__ and then complain that the type is not generic (it is not) or subscriptable (it is).

Python 3.11 provides support for variadic generics and syntax sugar to promote Python literals to Literal[T]. We could make it fully generic today via TypeVarTuple and Unpack through typing_extensions, but the usage syntax is gross.

Today:

def F(tensor: FLOAT[Literal[128], Literal["M"], Literal[1024]]): ...

def G(tensor: FLOAT[Literal[Ellipsis]]): ...

With 3.11:

def F(tensor: FLOAT[128, "M", 1024]): ...

def G(tensor: FLOAT[...]): ...

@abock abock force-pushed the abock/uninstantiated-tensor-types branch 6 times, most recently from b9df365 to 08dc10d Compare December 2, 2022 21:40
abock and others added 2 commits December 7, 2022 16:27
This ensures that TensorType is never actually instantiated and instead
new TensorType subclasses are created (and cached) for each shape.

This is a stepping stone to a fully generic TensorType where the dtype
and shape are modeled into the type itself, but it appears that won't
be easily realized until we can depend on Python 3.11.

Importantly, this adds unit tests to ensure if we can move to a proper
generic that behavior will remain the same.

Fixes #219
@justinchuby justinchuby force-pushed the abock/uninstantiated-tensor-types branch from 08dc10d to fcd664e Compare December 7, 2022 16:32
@justinchuby justinchuby merged commit ed8dfb6 into main Dec 7, 2022
@justinchuby justinchuby deleted the abock/uninstantiated-tensor-types branch December 7, 2022 16:50
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.

bug: tensor types are not recognized as types
3 participants