Skip to content

Commit

Permalink
Handle C++ exceptions raised during finfo/iinfo calls (#109743)
Browse files Browse the repository at this point in the history
Partially fixes #109737
Pull Request resolved: #109743
Approved by: https://github.com/albanD
ghstack dependencies: #109744
  • Loading branch information
malfet authored and pytorchmergebot committed Sep 22, 2023
1 parent d7dfa91 commit f092eec
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions torch/csrc/TypeInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static PyObject* THPDTypeInfo_bits(THPDTypeInfo* self, void*) {
}

static PyObject* THPFInfo_eps(THPFInfo* self, void*) {
HANDLE_TH_ERRORS
return AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND4(
at::kHalf,
at::ScalarType::BFloat16,
Expand All @@ -124,9 +125,11 @@ static PyObject* THPFInfo_eps(THPFInfo* self, void*) {
std::numeric_limits<
at::scalar_value_type<scalar_t>::type>::epsilon());
});
END_HANDLE_TH_ERRORS
}

static PyObject* THPFInfo_max(THPFInfo* self, void*) {
HANDLE_TH_ERRORS
return AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND4(
at::kHalf,
at::ScalarType::BFloat16,
Expand All @@ -138,9 +141,11 @@ static PyObject* THPFInfo_max(THPFInfo* self, void*) {
return PyFloat_FromDouble(
std::numeric_limits<at::scalar_value_type<scalar_t>::type>::max());
});
END_HANDLE_TH_ERRORS
}

static PyObject* THPFInfo_min(THPFInfo* self, void*) {
HANDLE_TH_ERRORS
return AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND4(
at::kHalf,
at::ScalarType::BFloat16,
Expand All @@ -153,9 +158,11 @@ static PyObject* THPFInfo_min(THPFInfo* self, void*) {
std::numeric_limits<
at::scalar_value_type<scalar_t>::type>::lowest());
});
END_HANDLE_TH_ERRORS
}

static PyObject* THPIInfo_max(THPIInfo* self, void*) {
HANDLE_TH_ERRORS
if (at::isIntegralType(self->type, /*includeBool=*/false)) {
return AT_DISPATCH_INTEGRAL_TYPES(self->type, "max", [] {
return THPUtils_packInt64(std::numeric_limits<scalar_t>::max());
Expand All @@ -165,9 +172,11 @@ static PyObject* THPIInfo_max(THPIInfo* self, void*) {
return AT_DISPATCH_QINT_AND_SUB_BYTE_TYPES(self->type, "max", [] {
return THPUtils_packInt64(std::numeric_limits<underlying_t>::max());
});
END_HANDLE_TH_ERRORS
}

static PyObject* THPIInfo_min(THPIInfo* self, void*) {
HANDLE_TH_ERRORS
if (at::isIntegralType(self->type, /*includeBool=*/false)) {
return AT_DISPATCH_INTEGRAL_TYPES(self->type, "min", [] {
return THPUtils_packInt64(std::numeric_limits<scalar_t>::lowest());
Expand All @@ -177,16 +186,20 @@ static PyObject* THPIInfo_min(THPIInfo* self, void*) {
return AT_DISPATCH_QINT_AND_SUB_BYTE_TYPES(self->type, "min", [] {
return THPUtils_packInt64(std::numeric_limits<underlying_t>::lowest());
});
END_HANDLE_TH_ERRORS
}

static PyObject* THPIInfo_dtype(THPIInfo* self, void*) {
HANDLE_TH_ERRORS
auto primary_name = torch::utils::getDtypeNames(self->type).first;
return AT_DISPATCH_INTEGRAL_TYPES(self->type, "dtype", [&primary_name] {
return PyUnicode_FromString(primary_name.data());
});
END_HANDLE_TH_ERRORS
}

static PyObject* THPFInfo_smallest_normal(THPFInfo* self, void*) {
HANDLE_TH_ERRORS
return AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND4(
at::kHalf,
at::ScalarType::BFloat16,
Expand All @@ -198,6 +211,7 @@ static PyObject* THPFInfo_smallest_normal(THPFInfo* self, void*) {
return PyFloat_FromDouble(
std::numeric_limits<at::scalar_value_type<scalar_t>::type>::min());
});
END_HANDLE_TH_ERRORS
}

static PyObject* THPFInfo_tiny(THPFInfo* self, void*) {
Expand All @@ -206,6 +220,7 @@ static PyObject* THPFInfo_tiny(THPFInfo* self, void*) {
}

static PyObject* THPFInfo_resolution(THPFInfo* self, void*) {
HANDLE_TH_ERRORS
return AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND4(
at::kHalf,
at::ScalarType::BFloat16,
Expand All @@ -219,9 +234,11 @@ static PyObject* THPFInfo_resolution(THPFInfo* self, void*) {
-std::numeric_limits<
at::scalar_value_type<scalar_t>::type>::digits10));
});
END_HANDLE_TH_ERRORS
}

static PyObject* THPFInfo_dtype(THPFInfo* self, void*) {
HANDLE_TH_ERRORS
auto primary_name = torch::utils::getDtypeNames(self->type).first;
return AT_DISPATCH_FLOATING_AND_COMPLEX_TYPES_AND4(
at::kHalf,
Expand All @@ -231,10 +248,12 @@ static PyObject* THPFInfo_dtype(THPFInfo* self, void*) {
self->type,
"dtype",
[&primary_name] { return PyUnicode_FromString(primary_name.data()); });
END_HANDLE_TH_ERRORS
}

PyObject* THPFInfo_str(THPFInfo* self) {
std::ostringstream oss;
const auto dtypeStr = THPFInfo_dtype(self, nullptr);
oss << "finfo(resolution="
<< PyFloat_AsDouble(THPFInfo_resolution(self, nullptr));
oss << ", min=" << PyFloat_AsDouble(THPFInfo_min(self, nullptr));
Expand All @@ -243,19 +262,23 @@ PyObject* THPFInfo_str(THPFInfo* self) {
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)) << ")";

return THPUtils_packString(oss.str().c_str());
if (dtypeStr != nullptr) {
oss << ", dtype=" << PyUnicode_AsUTF8(dtypeStr) << ")";
}
return !PyErr_Occurred() ? THPUtils_packString(oss.str().c_str()) : nullptr;
}

PyObject* THPIInfo_str(THPIInfo* self) {
std::ostringstream oss;

const auto dtypeStr = THPIInfo_dtype(self, nullptr);
oss << "iinfo(min=" << PyLong_AsDouble(THPIInfo_min(self, nullptr));
oss << ", max=" << PyLong_AsDouble(THPIInfo_max(self, nullptr));
oss << ", dtype=" << PyUnicode_AsUTF8(THPIInfo_dtype(self, nullptr)) << ")";
if (dtypeStr) {
oss << ", dtype=" << PyUnicode_AsUTF8(dtypeStr) << ")";
}

return THPUtils_packString(oss.str().c_str());
return !PyErr_Occurred() ? THPUtils_packString(oss.str().c_str()) : nullptr;
}

// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-avoid-c-arrays)
Expand Down

0 comments on commit f092eec

Please sign in to comment.