-
Notifications
You must be signed in to change notification settings - Fork 53
PR: Add finfo and iinfo in the spec #129
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
Conversation
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.
Thanks @steff456. Overall looks good, I propose a few simplifications.
Also noticed:
>>> type(tf.experimental.numpy.finfo(tf.float64).dtype)
<class 'numpy.dtype'>
>>> tf.experimental.numpy.finfo?
...
Note that currently it just forwards to the numpy namesake, while
tensorflow and numpy dtypes may have different properties.
hmm ....
- The largest representable number. | ||
- **min**: _float_ | ||
- The smallest representable number. | ||
- **tiny**: _float_ |
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.
@kgryte do you happen to know if this is guaranteed to be the same across implementations? I.e. it's part of IEEE 754 somehow?
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.
This is what the NumPy docs have in their notes:
Note that tiny is not actually the smallest positive representable value in a NumPy floating point type. As in the IEEE-754 standard [1], NumPy floating point types make use of subnormal numbers to fill the gap between 0 and tiny. However, subnormal numbers may have significantly reduced precision [2].
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.
No. While part of IEEE 754, support for subnormal numbers is less common, especially among older generation GPUs. Often subnormal numbers trigger slow paths, and, if not implemented in hardware, software emulation. More recent NVIDIA GPUs do support subnormal numbers. And among ARM processors, one can flush subnormal numbers to zero.
Based on NumPy docs (via @steff456 ), tiny
refers to the smallest normal number. We should probably be explicit by what we mean by "representable number".
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.
Interesting. So the description is a bit off here. I'm not sure it should be part of the standard. I can imagine it may be useful in a few corner cases, but I've never seen it used in the wild.
Let's see if anyone else has an opinion.
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.
Okay, so there are use cases at the compiled code level at least.
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.
I suppose having this info is useful for meta inspection concerning platform capabilities. For example, if I want to know whether a platform supports subnormals. Apart from meta data, a user would need to compute a quantity which should resolve to a subnormal number and manually check whether the value is zero.
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.
Correct. It can also come up in user code, e.g., in the computation of transcendentals, where, if you know that a platform does not support subnormals, then you can avoid various, often slower, branching logic.
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.
I think if we wanted to be complete, instead of "tiny", we'd have smallest_normal
and smallest_subnormal
. If the latter is 0
, then a platform does not support subnormal numbers.
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.
Those do seem like better names indeed. TF and CuPy just alias numpy.finfo
, which may be wrong. And JAX implements it but docs say "The smallest positive usable number", which is quite uninformative.
I think we can add the new names, but then we should make a PR to NumPy to see if that's accepted. And otherwise we should probably drop it completely.
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.
Thanks @steff456! Added some minor style and punctuation touch-ups to maintain consistency with rest of spec. Otherwise, looks good!
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.
LGTM modulo my one small comment. I'll push a fix for that and merge.
Let's open a separate issue to keep track of the subnormal number attributes.
## Objects in API | ||
|
||
(finfo)= | ||
### finfo(type) |
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.
type
should be a positional argument here, so this should be finfo(type, /)
.
And same for iinfo
.
Currently Currently |
This PR
finfo
, which defines the machine limits for floating point typesiinfo
, which defines the machine limits for integer typesNotes
finfo
class the minimal common set of attributes was defined, reducing the number of attributes currently supported by NumPy, CuPy and JAX.