Caffe2 - C++ API
A deep learning, cross platform ML framework
utils.h
1 #ifndef THP_UTILS_H
2 #define THP_UTILS_H
3 
4 #include <vector>
5 #include <string>
6 #include <type_traits>
7 #include <ATen/ATen.h>
8 #include <torch/csrc/utils/object_ptr.h>
9 #include <torch/csrc/utils/python_numbers.h>
10 #include <torch/csrc/utils/python_compat.h>
11 
12 #ifdef USE_CUDA
13 #include <THC/THC.h>
14 #include <c10/cuda/CUDAStream.h>
15 #endif
16 
17 #define THPUtils_(NAME) TH_CONCAT_4(THP,Real,Utils_,NAME)
18 
19 #define THPUtils_typename(obj) (Py_TYPE(obj)->tp_name)
20 
21 #if defined(__GNUC__) || defined(__ICL) || defined(__clang__)
22 #define THP_EXPECT(x, y) (__builtin_expect((x), (y)))
23 #else
24 #define THP_EXPECT(x, y) (x)
25 #endif
26 
27 #if PY_MAJOR_VERSION == 2
28 #define THPUtils_checkReal_FLOAT(object) \
29  (PyFloat_Check(object) || PyLong_Check(object) || PyInt_Check(object))
30 
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))
36 
37 #define THPUtils_checkReal_INT(object) \
38  (PyLong_Check(object) || PyInt_Check(object))
39 
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))
44 #else /* PY_MAJOR_VERSION == 2 */
45 #define THPUtils_checkReal_FLOAT(object) \
46  (PyFloat_Check(object) || PyLong_Check(object))
47 
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))
52 
53 #define THPUtils_checkReal_INT(object) \
54  PyLong_Check(object)
55 
56 #define THPUtils_unpackReal_INT(object) \
57  (PyLong_Check(object) ? PyLong_AsLongLong(object) : \
58  (throw std::runtime_error("Could not parse real"), 0))
59 #endif
60 
61 #define THPUtils_unpackReal_BOOL(object) \
62  (PyBool_Check(object) ? object : \
63  (throw std::runtime_error("Could not parse real"), Py_False))
64 
65 #define THPUtils_checkReal_BOOL(object) \
66  PyBool_Check(object)
67 
68 #define THPUtils_newReal_FLOAT(value) PyFloat_FromDouble(value)
69 // TODO: handle int overflows for py2
70 #define THPUtils_newReal_INT(value) PyInt_FromLong(value)
71 
72 #define THPUtils_newReal_BOOL(value) PyBool_FromLong(value)
73 
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)
84 
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)
106 
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, ...);
114 
115 #ifdef _THP_CORE
116 
117 bool THPUtils_checkIntTuple(PyObject *arg);
118 std::vector<int> THPUtils_unpackIntTuple(PyObject *arg);
119 
120 void THPUtils_addPyMethodDefs(std::vector<PyMethodDef>& vector, PyMethodDef* methods);
121 
122 int THPUtils_getCallable(PyObject *arg, PyObject **result);
123 
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)
129 
130 typedef THPPointer<THPGenerator> THPGeneratorPtr;
131 
132 template <typename T>
133 struct THPUtils_typeTraits {};
134 
135 #include <torch/csrc/generic/utils.h>
136 #include <TH/THGenerateAllTypes.h>
137 
138 #include <torch/csrc/generic/utils.h>
139 #include <TH/THGenerateHalfType.h>
140 
141 #include <torch/csrc/generic/utils.h>
142 #include <TH/THGenerateBoolType.h>
143 
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);
149 
150 template<typename _real, typename = void>
151 struct mod_traits {};
152 
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); }
156 };
157 
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; }
161 };
162 
163 void setBackCompatBroadcastWarn(bool warn);
164 bool getBackCompatBroadcastWarn();
165 
166 void setBackCompatKeepdimWarn(bool warn);
167 bool getBackCompatKeepdimWarn();
168 bool maybeThrowBackCompatKeepdimWarn(char *func);
169 
170 // NB: This is in torch/csrc/cuda/utils.cpp, for whatever reason
171 #ifdef USE_CUDA
172 std::vector<c10::optional<at::cuda::CUDAStream>> THPUtils_PySequence_to_CUDAStreamList(PyObject *obj);
173 #endif
174 
175 #endif /* _THP_CORE */
176 
177 #endif