ENH: Convert finfo.tiny to finfo.smallest_normal (#76292)

Fixes #70909, by a straightforward search and replace discussed in #70909.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/76292
Approved by: https://github.com/mruberry
This commit is contained in:
Rohit Goswami
2022-05-20 00:59:48 +00:00
committed by PyTorch MergeBot
parent 7892a45741
commit c915fbe201
4 changed files with 22 additions and 12 deletions

View File

@ -19,22 +19,23 @@ A :class:`torch.finfo` is an object that represents the numerical properties of
A :class:`torch.finfo` provides the following attributes:
========== ===== ========================================
Name Type Description
========== ===== ========================================
bits int The number of bits occupied by the type.
eps float The smallest representable number such that ``1.0 + eps != 1.0``.
max float The largest representable number.
min float The smallest representable number (typically ``-max``).
tiny float The smallest positive normal number. See notes.
resolution float The approximate decimal resolution of this type, i.e., ``10**-precision``.
========== ===== ========================================
=============== ===== ==========================================================================
Name Type Description
=============== ===== ==========================================================================
bits int The number of bits occupied by the type.
eps float The smallest representable number such that ``1.0 + eps != 1.0``.
max float The largest representable number.
min float The smallest representable number (typically ``-max``).
tiny float The smallest positive normal number. Equivalent to ``smallest_normal``.
smallest_normal float The smallest positive normal number. See notes.
resolution float The approximate decimal resolution of this type, i.e., ``10**-precision``.
=============== ===== ==========================================================================
.. note::
The constructor of :class:`torch.finfo` can be called without argument, in which case the class is created for the pytorch default dtype (as returned by :func:`torch.get_default_dtype`).
.. note::
`tiny` returns the smallest *normal* number, but there are smaller
`smallest_normal` returns the smallest *normal* number, but there are smaller
subnormal numbers. See https://en.wikipedia.org/wiki/Denormal_number
for more information.

View File

@ -63,6 +63,7 @@ class TestDTypeInfo(TestCase):
self.assertEqual(xinfo.min, -3.38953e+38)
self.assertEqual(xinfo.eps, 0.0078125)
self.assertEqual(xinfo.tiny, 1.17549e-38)
self.assertEqual(xinfo.tiny, xinfo.smallest_normal)
self.assertEqual(xinfo.resolution, 0.01)
self.assertEqual(xinfo.dtype, "bfloat16")
torch.set_default_dtype(x.dtype)

View File

@ -87,6 +87,7 @@ class finfo:
max: _float
eps: _float
tiny: _float
smallest_normal: _float
resolution: _float
dtype: str

View File

@ -168,13 +168,18 @@ static PyObject* THPIInfo_dtype(THPIInfo* self, void*) {
});
}
static PyObject* THPFInfo_tiny(THPFInfo* self, void*) {
static PyObject* THPFInfo_smallest_normal(THPFInfo* self, void*) {
return AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND2(at::kHalf, at::ScalarType::BFloat16, self->type, "min", [] {
return PyFloat_FromDouble(
std::numeric_limits<at::scalar_value_type<scalar_t>::type>::min());
});
}
static PyObject* THPFInfo_tiny(THPFInfo* self, void*) {
// see gh-70909, essentially the array_api prefers smallest_normal over tiny
return THPFInfo_smallest_normal(self, nullptr);
}
static PyObject* THPFInfo_resolution(THPFInfo* self, void*) {
return AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND2(at::kHalf, at::ScalarType::BFloat16, self->type, "digits10", [] {
return PyFloat_FromDouble(
@ -197,6 +202,7 @@ PyObject* THPFInfo_str(THPFInfo* self) {
oss << ", min=" << PyFloat_AsDouble(THPFInfo_min(self, nullptr));
oss << ", max=" << PyFloat_AsDouble(THPFInfo_max(self, nullptr));
oss << ", eps=" << PyFloat_AsDouble(THPFInfo_eps(self, nullptr));
oss << ", smallest_normal=" << PyFloat_AsDouble(THPFInfo_smallest_normal(self, nullptr));
oss << ", tiny=" << PyFloat_AsDouble(THPFInfo_tiny(self, nullptr));
oss << ", dtype=" << PyUnicode_AsUTF8(THPFInfo_dtype(self, nullptr)) << ")";
@ -222,6 +228,7 @@ static struct PyGetSetDef THPFInfo_properties[] = {
{"eps", (getter)THPFInfo_eps, nullptr, nullptr, nullptr},
{"max", (getter)THPFInfo_max, nullptr, nullptr, nullptr},
{"min", (getter)THPFInfo_min, nullptr, nullptr, nullptr},
{"smallest_normal", (getter)THPFInfo_smallest_normal, nullptr, nullptr, nullptr},
{"tiny", (getter)THPFInfo_tiny, nullptr, nullptr, nullptr},
{"resolution", (getter)THPFInfo_resolution, nullptr, nullptr, nullptr},
{"dtype", (getter)THPFInfo_dtype, nullptr, nullptr, nullptr},