mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/30486 Fixes: https://github.com/pytorch/pytorch/issues/29252 There is some incorrect code in the handling of parsing python numbers that led to issue #29252: When we allow interpretation of a zero-dim numpy integer value as a scalar in pytorch, we incorrectly parse the int as a float. This PR also fixes the issue described in the "FIXME" here: https://github.com/pytorch/pytorch/pull/27628/files#diff-f539198dd366265fb8dc2d661bc5d5bcR1487 Test Plan: Added a unit test based on the example given in the issue. Differential Revision: D18932520 Pulled By: nairbv fbshipit-source-id: f6416f28dfd73ac72c1042042851d76beb5fcf65
169 lines
4.3 KiB
C++
169 lines
4.3 KiB
C++
#pragma once
|
|
|
|
#include <torch/csrc/Exceptions.h>
|
|
#include <torch/csrc/jit/tracer.h>
|
|
#include <torch/csrc/python_headers.h>
|
|
#include <torch/csrc/utils/object_ptr.h>
|
|
#include <torch/csrc/utils/tensor_numpy.h>
|
|
#include <cstdint>
|
|
#include <stdexcept>
|
|
|
|
// largest integer that can be represented consecutively in a double
|
|
const int64_t DOUBLE_INT_MAX = 9007199254740992;
|
|
|
|
inline PyObject* THPUtils_packInt64(int64_t value) {
|
|
#if PY_MAJOR_VERSION == 2
|
|
if (sizeof(long) == sizeof(int64_t)) {
|
|
return PyInt_FromLong(static_cast<long>(value));
|
|
} else if (value <= INT32_MAX && value >= INT32_MIN) {
|
|
return PyInt_FromLong(static_cast<long>(value));
|
|
}
|
|
#endif
|
|
return PyLong_FromLongLong(value);
|
|
}
|
|
|
|
inline PyObject* THPUtils_packUInt64(uint64_t value) {
|
|
#if PY_MAJOR_VERSION == 2
|
|
if (value <= INT32_MAX) {
|
|
return PyInt_FromLong(static_cast<long>(value));
|
|
}
|
|
#endif
|
|
return PyLong_FromUnsignedLongLong(value);
|
|
}
|
|
|
|
inline PyObject* THPUtils_packDoubleAsInt(double value) {
|
|
#if PY_MAJOR_VERSION == 2
|
|
if (value <= INT32_MAX && value >= INT32_MIN) {
|
|
return PyInt_FromLong(static_cast<long>(value));
|
|
}
|
|
#endif
|
|
return PyLong_FromDouble(value);
|
|
}
|
|
|
|
inline bool THPUtils_checkLong(PyObject* obj) {
|
|
#ifdef USE_NUMPY
|
|
if (torch::utils::is_numpy_int(obj)) {
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
#if PY_MAJOR_VERSION == 2
|
|
return (PyLong_Check(obj) || PyInt_Check(obj)) && !PyBool_Check(obj);
|
|
#else
|
|
return PyLong_Check(obj) && !PyBool_Check(obj);
|
|
#endif
|
|
}
|
|
|
|
inline int64_t THPUtils_unpackLong(PyObject* obj) {
|
|
int overflow;
|
|
long long value = PyLong_AsLongLongAndOverflow(obj, &overflow);
|
|
if (value == -1 && PyErr_Occurred()) {
|
|
throw python_error();
|
|
}
|
|
if (overflow != 0) {
|
|
throw std::runtime_error("Overflow when unpacking long");
|
|
}
|
|
return (int64_t)value;
|
|
}
|
|
|
|
inline bool THPUtils_checkIndex(PyObject *obj) {
|
|
if (PyBool_Check(obj)) {
|
|
return false;
|
|
}
|
|
if (THPUtils_checkLong(obj)) {
|
|
return true;
|
|
}
|
|
torch::jit::tracer::NoWarn no_warn_guard;
|
|
auto index = THPObjectPtr(PyNumber_Index(obj));
|
|
if (!index) {
|
|
PyErr_Clear();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
inline int64_t THPUtils_unpackIndex(PyObject* obj) {
|
|
if (!THPUtils_checkLong(obj)) {
|
|
auto index = THPObjectPtr(PyNumber_Index(obj));
|
|
if (index == nullptr) {
|
|
throw python_error();
|
|
}
|
|
// NB: This needs to be called before `index` goes out of scope and the
|
|
// underlying object's refcount is decremented
|
|
return THPUtils_unpackLong(index.get());
|
|
}
|
|
return THPUtils_unpackLong(obj);
|
|
}
|
|
|
|
inline bool THPUtils_unpackBool(PyObject* obj) {
|
|
if (obj == Py_True) {
|
|
return true;
|
|
} else if (obj == Py_False) {
|
|
return false;
|
|
} else {
|
|
throw std::runtime_error("couldn't convert python object to boolean");
|
|
}
|
|
}
|
|
|
|
inline bool THPUtils_checkDouble(PyObject* obj) {
|
|
#ifdef USE_NUMPY
|
|
if (torch::utils::is_numpy_scalar(obj)) {
|
|
return true;
|
|
}
|
|
#endif
|
|
#if PY_MAJOR_VERSION == 2
|
|
return PyFloat_Check(obj) || PyLong_Check(obj) || PyInt_Check(obj);
|
|
#else
|
|
return PyFloat_Check(obj) || PyLong_Check(obj);
|
|
#endif
|
|
}
|
|
|
|
inline bool THPUtils_checkScalar(PyObject* obj) {
|
|
#ifdef USE_NUMPY
|
|
if (torch::utils::is_numpy_scalar(obj)) {
|
|
return true;
|
|
}
|
|
#endif
|
|
#if PY_MAJOR_VERSION == 2
|
|
return PyFloat_Check(obj) || PyLong_Check(obj) || PyInt_Check(obj) || PyComplex_Check(obj);
|
|
#else
|
|
return PyFloat_Check(obj) || PyLong_Check(obj) || PyComplex_Check(obj);
|
|
#endif
|
|
}
|
|
|
|
inline double THPUtils_unpackDouble(PyObject* obj) {
|
|
if (PyFloat_Check(obj)) {
|
|
return PyFloat_AS_DOUBLE(obj);
|
|
}
|
|
if (PyLong_Check(obj)) {
|
|
int overflow;
|
|
long long value = PyLong_AsLongLongAndOverflow(obj, &overflow);
|
|
if (overflow != 0) {
|
|
throw std::runtime_error("Overflow when unpacking double");
|
|
}
|
|
if (value > DOUBLE_INT_MAX || value < -DOUBLE_INT_MAX) {
|
|
throw std::runtime_error("Precision loss when unpacking double");
|
|
}
|
|
return (double)value;
|
|
}
|
|
#if PY_MAJOR_VERSION == 2
|
|
if (PyInt_Check(obj)) {
|
|
return (double)PyInt_AS_LONG(obj);
|
|
}
|
|
#endif
|
|
double value = PyFloat_AsDouble(obj);
|
|
if (value == -1 && PyErr_Occurred()) {
|
|
throw python_error();
|
|
}
|
|
return value;
|
|
}
|
|
|
|
inline std::complex<double> THPUtils_unpackComplexDouble(PyObject *obj) {
|
|
Py_complex value = PyComplex_AsCComplex(obj);
|
|
if (value.real == -1.0 && PyErr_Occurred()) {
|
|
throw python_error();
|
|
}
|
|
|
|
return std::complex<double>(value.real, value.imag);
|
|
}
|