-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Avoid the builtin numbers
module.
#144788
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
Comments
Thanks for this. I agree with all your points. I actually ran into the same issues with The first alternative seems to be the most appropriate to me (also probably the easiest to compile). As you pointed out Protocols can be slow for instance check (which people will ultimately do). @malfet If we remove all refs to |
yeah lets use our builtin number |
Since torch still supports 3.9, one question is how to replace the
① seems to be the one that's easiest to replace once 3.9 reaches EOL this year. I tried also the following, but mypy doesn't like it for 3.10+ if sys.version_info < (3, 10):
_Number = (bool, int, float)
else:
_Number: TypeAlias = Number |
I think the tuple (1) is what most people expected |
…ber`. (pytorch#145086) Fixes pytorch#144788 (partial) Pull Request resolved: pytorch#145086 Approved by: https://github.com/malfet
@ezyang This should probably be reopened, since #145086 was only a partial fix that only covers the module After this, there are 22 files left that use the One issue that needs to be addressed: complex numbers (which But likely this means that we need both a real-valued type, which is the current I used the name |
Uh oh!
There was an error while loading. Please reload this page.
🚀 The feature, motivation and pitch
Currently, torch uses the builtin
numbers
module in a few places (only ~40 hits). However, thenumbers
module is problematic for multiple reasons:The
numbers
module is incompatible with type annotations (see int is not a Number? python/mypy#3186, example: mypy-playground).numbers.Number
,numbers.Real
, etc. is a terrible idea.isinstance(x, Number)
forces us to addtype: ignore
comments inside the else branch.torch.distributions
module ([typing] Add static type hints totorch.distributions
. #144196), since this is the place where most of the uses ofnumbers
are found, see: [typing] Add type hints to__init__
methods intorch.distributions
. #144197 (comment)Since it's just an abstract base class, it requires users to do
Number.register(my_number_type)
to ensureisinstance
succeeds.Internally,
torch.tensor
doesn't seem to care if something is anumbers.Number
, in fact, the supported types appear to betorch.SymBool
,torch.SymInt
andtorch.SymFloat
numpy
scalarsnumpy.int32
,numpy.int64
,numpy.float32
, etc.bool
,int
,float
,complex
__bool__
,__int__
,__index__
,__float__
or__complex__
(requires specifyingdtype
)(see
/torch/csrc/utils/tensor_new.cpp
andtorch/_refs/__init__.py
)demo
Alternatives
There are 3 main alternatives:
Union
type of the supported types (tuple
for python 3.9).torch
already provides for example liketorch.types.Number
andtorch._prims_common.Number
Protocol
types liketyping.SupportsFloat
Tensor
, since it implements__float__
, is aSupportsFloat
itself, which could require changing some exisiting if-else tests.Protocol
type.Additional context
One concern could be speed of `isinstance(x, Number)`, below is a comparison between the approaches.
One can see that
isinstance(val, SupportsFloat)
is roughly twice as slow asisinstance(val, Real)
for a positive, but can be a lot slower for a negative. TheUnion
can be a lot faster, but the speed depends on the order of the members (if we putfloat
last, the first run takes ~90ns, since the argument is checked sequentially against the provided types).cc @fritzo @neerajprad @alicanb @nikitaved @ezyang @malfet @xuzhao9 @gramster
The text was updated successfully, but these errors were encountered: