8 #include <torch/csrc/utils/object_ptr.h> 9 #include <torch/csrc/utils/python_numbers.h> 10 #include <torch/csrc/utils/python_compat.h> 14 #include <c10/cuda/CUDAStream.h> 17 #define THPUtils_(NAME) TH_CONCAT_4(THP,Real,Utils_,NAME) 19 #define THPUtils_typename(obj) (Py_TYPE(obj)->tp_name) 21 #if defined(__GNUC__) || defined(__ICL) || defined(__clang__) 22 #define THP_EXPECT(x, y) (__builtin_expect((x), (y))) 24 #define THP_EXPECT(x, y) (x) 27 #if PY_MAJOR_VERSION == 2 28 #define THPUtils_checkReal_FLOAT(object) \ 29 (PyFloat_Check(object) || PyLong_Check(object) || PyInt_Check(object)) 31 #define THPUtils_unpackReal_FLOAT(object) \ 32 (PyFloat_Check(object) ? PyFloat_AsDouble(object) : \ 33 PyLong_Check(object) ? PyLong_AsLongLong(object) : \ 34 PyInt_Check(object) ? PyInt_AsLong(object) : \ 35 (throw std::runtime_error("Could not parse real"), 0)) 37 #define THPUtils_checkReal_INT(object) \ 38 (PyLong_Check(object) || PyInt_Check(object)) 40 #define THPUtils_unpackReal_INT(object) \ 41 (PyLong_Check(object) ? PyLong_AsLongLong(object) : \ 42 PyInt_Check(object) ? PyInt_AsLong(object) : \ 43 (throw std::runtime_error("Could not parse real"), 0)) 45 #define THPUtils_checkReal_FLOAT(object) \ 46 (PyFloat_Check(object) || PyLong_Check(object)) 48 #define THPUtils_unpackReal_FLOAT(object) \ 49 (PyFloat_Check(object) ? PyFloat_AsDouble(object) : \ 50 PyLong_Check(object) ? PyLong_AsLongLong(object) : \ 51 (throw std::runtime_error("Could not parse real"), 0)) 53 #define THPUtils_checkReal_INT(object) \ 56 #define THPUtils_unpackReal_INT(object) \ 57 (PyLong_Check(object) ? PyLong_AsLongLong(object) : \ 58 (throw std::runtime_error("Could not parse real"), 0)) 61 #define THPUtils_unpackReal_BOOL(object) \ 62 (PyBool_Check(object) ? object : \ 63 (throw std::runtime_error("Could not parse real"), Py_False)) 65 #define THPUtils_checkReal_BOOL(object) \ 68 #define THPUtils_newReal_FLOAT(value) PyFloat_FromDouble(value) 70 #define THPUtils_newReal_INT(value) PyInt_FromLong(value) 72 #define THPUtils_newReal_BOOL(value) PyBool_FromLong(value) 74 #define THPDoubleUtils_checkReal(object) THPUtils_checkReal_FLOAT(object) 75 #define THPDoubleUtils_unpackReal(object) (double)THPUtils_unpackReal_FLOAT(object) 76 #define THPDoubleUtils_newReal(value) THPUtils_newReal_FLOAT(value) 77 #define THPFloatUtils_checkReal(object) THPUtils_checkReal_FLOAT(object) 78 #define THPFloatUtils_unpackReal(object) (float)THPUtils_unpackReal_FLOAT(object) 79 #define THPFloatUtils_newReal(value) THPUtils_newReal_FLOAT(value) 80 #define THPHalfUtils_checkReal(object) THPUtils_checkReal_FLOAT(object) 81 #define THPHalfUtils_unpackReal(object) (at::Half)THPUtils_unpackReal_FLOAT(object) 82 #define THPHalfUtils_newReal(value) PyFloat_FromDouble(value) 83 #define THPHalfUtils_newAccreal(value) THPUtils_newReal_FLOAT(value) 85 #define THPBoolUtils_checkReal(object) THPUtils_checkReal_BOOL(object) 86 #define THPBoolUtils_unpackReal(object) THPUtils_unpackReal_BOOL(object) 87 #define THPBoolUtils_newReal(value) THPUtils_newReal_BOOL(value) 88 #define THPBoolUtils_checkAccreal(object) THPUtils_checkReal_BOOL(object) 89 #define THPBoolUtils_unpackAccreal(object) (int64_t)THPUtils_unpackReal_BOOL(object) 90 #define THPBoolUtils_newAccreal(value) THPUtils_newReal_BOOL(value) 91 #define THPLongUtils_checkReal(object) THPUtils_checkReal_INT(object) 92 #define THPLongUtils_unpackReal(object) (int64_t)THPUtils_unpackReal_INT(object) 93 #define THPLongUtils_newReal(value) THPUtils_newReal_INT(value) 94 #define THPIntUtils_checkReal(object) THPUtils_checkReal_INT(object) 95 #define THPIntUtils_unpackReal(object) (int)THPUtils_unpackReal_INT(object) 96 #define THPIntUtils_newReal(value) THPUtils_newReal_INT(value) 97 #define THPShortUtils_checkReal(object) THPUtils_checkReal_INT(object) 98 #define THPShortUtils_unpackReal(object) (short)THPUtils_unpackReal_INT(object) 99 #define THPShortUtils_newReal(value) THPUtils_newReal_INT(value) 100 #define THPCharUtils_checkReal(object) THPUtils_checkReal_INT(object) 101 #define THPCharUtils_unpackReal(object) (char)THPUtils_unpackReal_INT(object) 102 #define THPCharUtils_newReal(value) THPUtils_newReal_INT(value) 103 #define THPByteUtils_checkReal(object) THPUtils_checkReal_INT(object) 104 #define THPByteUtils_unpackReal(object) (unsigned char)THPUtils_unpackReal_INT(object) 105 #define THPByteUtils_newReal(value) THPUtils_newReal_INT(value) 107 #define THPUtils_assert(cond, ...) THPUtils_assertRet(nullptr, cond, __VA_ARGS__) 108 #define THPUtils_assertRet(value, cond, ...) \ 109 if (THP_EXPECT(!(cond), 0)) { THPUtils_setError(__VA_ARGS__); return value; } 110 THP_API
void THPUtils_setError(
const char *format, ...);
111 THP_API
void THPUtils_invalidArguments(
112 PyObject *given_args, PyObject *given_kwargs,
113 const char *function_name,
size_t num_options, ...);
117 bool THPUtils_checkIntTuple(PyObject *arg);
118 std::vector<int> THPUtils_unpackIntTuple(PyObject *arg);
120 void THPUtils_addPyMethodDefs(std::vector<PyMethodDef>& vector, PyMethodDef* methods);
122 int THPUtils_getCallable(PyObject *arg, PyObject **result);
124 #define THWStoragePtr TH_CONCAT_3(TH,Real,StoragePtr) 125 #define THWTensorPtr TH_CONCAT_3(TH,Real,TensorPtr) 126 #define THPStoragePtr TH_CONCAT_3(THP,Real,StoragePtr) 127 #define THPTensorPtr TH_CONCAT_3(THP,Real,TensorPtr) 128 #define THSPTensorPtr TH_CONCAT_3(THSP,Real,TensorPtr) 132 template <
typename T>
133 struct THPUtils_typeTraits {};
135 #include <torch/csrc/generic/utils.h> 136 #include <TH/THGenerateAllTypes.h> 138 #include <torch/csrc/generic/utils.h> 139 #include <TH/THGenerateHalfType.h> 141 #include <torch/csrc/generic/utils.h> 142 #include <TH/THGenerateBoolType.h> 144 THLongStoragePtr THPUtils_unpackSize(PyObject *arg);
145 bool THPUtils_tryUnpackLongs(PyObject *arg, THLongStoragePtr& result);
146 std::vector<int64_t> THPUtils_unpackLongs(PyObject *arg);
147 bool THPUtils_tryUnpackLongVarArgs(PyObject *args,
int ignore_first, THLongStoragePtr& result);
148 PyObject * THPUtils_dispatchStateless(PyObject *tensor,
const char *name, PyObject *args, PyObject *kwargs);
150 template<
typename _real,
typename =
void>
151 struct mod_traits {};
153 template<
typename _real>
154 struct mod_traits<_real, typename
std::enable_if<std::is_floating_point<_real>::value>::type> {
155 static _real mod(_real a, _real b) {
return fmod(a, b); }
158 template<
typename _real>
159 struct mod_traits<_real, typename
std::enable_if<std::is_integral<_real>::value>::type> {
160 static _real mod(_real a, _real b) {
return a % b; }
163 void setBackCompatBroadcastWarn(
bool warn);
164 bool getBackCompatBroadcastWarn();
166 void setBackCompatKeepdimWarn(
bool warn);
167 bool getBackCompatKeepdimWarn();
168 bool maybeThrowBackCompatKeepdimWarn(
char *func);
172 std::vector<c10::optional<at::cuda::CUDAStream>> THPUtils_PySequence_to_CUDAStreamList(PyObject *obj);