forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
335 lines (296 loc) · 9.7 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <c10/util/irange.h>
#include <torch/csrc/python_headers.h>
#include <torch/csrc/THP.h>
#include <torch/csrc/utils/python_strings.h>
#include <torch/csrc/utils/invalid_arguments.h>
#include <torch/csrc/autograd/variable.h>
#include <torch/csrc/DynamicTypes.h>
// NOLINTNEXTLINE(bugprone-suspicious-include)
#include <torch/csrc/generic/utils.cpp>
#include <TH/THGenerateAllTypes.h>
// NOLINTNEXTLINE(bugprone-suspicious-include)
#include <torch/csrc/generic/utils.cpp>
#include <TH/THGenerateComplexTypes.h>
// NOLINTNEXTLINE(bugprone-suspicious-include)
#include <torch/csrc/generic/utils.cpp>
#include <TH/THGenerateHalfType.h>
// NOLINTNEXTLINE(bugprone-suspicious-include)
#include <torch/csrc/generic/utils.cpp>
#include <TH/THGenerateBFloat16Type.h>
#include <torch/csrc/WindowsTorchApiMacro.h>
// NOLINTNEXTLINE(bugprone-suspicious-include)
#include <torch/csrc/generic/utils.cpp>
#include <TH/THGenerateBoolType.h>
#include <algorithm>
#include <cstdarg>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
int THPUtils_getCallable(PyObject *arg, PyObject **result) {
if (!PyCallable_Check(arg))
return 0;
*result = arg;
return 1;
}
THLongStoragePtr THPUtils_unpackSize(PyObject *arg) {
THLongStoragePtr result;
if (!THPUtils_tryUnpackLongs(arg, result)) {
std::string msg = "THPUtils_unpackSize() expects a torch.Size (got '";
msg += Py_TYPE(arg)->tp_name;
msg += "')";
throw std::runtime_error(msg);
}
return result;
}
bool THPUtils_tryUnpackLongs(PyObject *arg, THLongStoragePtr& result) {
bool tuple = PyTuple_Check(arg);
bool list = PyList_Check(arg);
if (tuple || list) {
// NOLINTNEXTLINE(bugprone-branch-clone)
int nDim = tuple ? PyTuple_GET_SIZE(arg) : PyList_GET_SIZE(arg);
THLongStoragePtr storage(THLongStorage_newWithSize(nDim));
for (int i = 0; i != nDim; ++i) {
PyObject* item = tuple ? PyTuple_GET_ITEM(arg, i) : PyList_GET_ITEM(arg, i);
if (!THPUtils_checkLong(item)) {
return false;
}
THLongStorage_set(storage, i, THPUtils_unpackLong(item));
}
result = std::move(storage);
return true;
}
return false;
}
std::vector<int64_t> THPUtils_unpackLongs(PyObject *arg) {
bool tuple = PyTuple_Check(arg);
bool list = PyList_Check(arg);
if (tuple || list) {
// NOLINTNEXTLINE(bugprone-branch-clone)
int nDim = tuple ? PyTuple_GET_SIZE(arg) : PyList_GET_SIZE(arg);
std::vector<int64_t> sizes(nDim);
for (int i = 0; i != nDim; ++i) {
PyObject* item = tuple ? PyTuple_GET_ITEM(arg, i) : PyList_GET_ITEM(arg, i);
if (!THPUtils_checkLong(item)) {
std::ostringstream oss;
oss << "expected int at position " << i << ", but got: " << THPUtils_typename(item);
throw std::runtime_error(oss.str());
}
sizes[i] = THPUtils_unpackLong(item);
}
return sizes;
}
throw std::runtime_error("Expected tuple or list");
}
bool THPUtils_tryUnpackLongVarArgs(PyObject *args, int ignore_first, THLongStoragePtr& result) {
Py_ssize_t length = PyTuple_Size(args) - ignore_first;
if (length < 1) {
return false;
}
PyObject *first_arg = PyTuple_GET_ITEM(args, ignore_first);
if (length == 1 && THPUtils_tryUnpackLongs(first_arg, result)) {
return true;
}
// Try to parse the numbers
result = THLongStorage_newWithSize(length);
for (Py_ssize_t i = 0; i < length; ++i) {
PyObject *arg = PyTuple_GET_ITEM(args, i + ignore_first);
if (!THPUtils_checkLong(arg)) {
return false;
}
THLongStorage_set(result, i, THPUtils_unpackLong(arg));
}
return true;
}
bool THPUtils_checkIntTuple(PyObject *arg)
{
if (!PyTuple_Check(arg)) {
return false;
}
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(arg); ++i) {
if (!THPUtils_checkLong(PyTuple_GET_ITEM(arg, i))) {
return false;
}
}
return true;
}
std::vector<int> THPUtils_unpackIntTuple(PyObject *arg)
{
if (!THPUtils_checkIntTuple(arg)) {
throw std::runtime_error("Couldn't unpack int tuple");
}
std::vector<int> values(PyTuple_GET_SIZE(arg));
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(arg); ++i) {
values[i] = (int)THPUtils_unpackLong(PyTuple_GET_ITEM(arg, i));
}
return values;
}
void THPUtils_setError(const char *format, ...)
{
static const size_t ERROR_BUFFER_SIZE = 1000;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
char buffer[ERROR_BUFFER_SIZE];
va_list fmt_args;
va_start(fmt_args, format);
vsnprintf(buffer, ERROR_BUFFER_SIZE, format, fmt_args);
va_end(fmt_args);
PyErr_SetString(PyExc_RuntimeError, buffer);
}
void THPUtils_addPyMethodDefs(std::vector<PyMethodDef>& vector, PyMethodDef* methods)
{
if (!vector.empty()) {
// remove nullptr terminator
vector.pop_back();
}
while (true) {
vector.push_back(*methods);
if (!methods->ml_name) {
break;
}
methods++;
}
}
static const char* classOrTypename(PyObject* obj) {
if (PyType_Check(obj)) {
return ((PyTypeObject*)obj)->tp_name;
}
return Py_TYPE(obj)->tp_name;
}
PyObject * THPUtils_dispatchStateless(
PyObject *tensor, const char *name, PyObject *args, PyObject *kwargs)
{
THPObjectPtr methods(PyObject_GetAttrString(tensor, THP_STATELESS_ATTRIBUTE_NAME));
if (!methods) {
return PyErr_Format(
PyExc_TypeError,
"Type %s doesn't implement stateless methods",
classOrTypename(tensor));
}
THPObjectPtr method(PyObject_GetAttrString(methods, name));
if (!method) {
return PyErr_Format(
PyExc_TypeError,
"Type %s doesn't implement stateless method %s",
classOrTypename(tensor),
name);
}
return PyObject_Call(method.get(), args, kwargs);
}
void THPUtils_invalidArguments(PyObject *given_args, PyObject *given_kwargs,
const char *function_name, size_t num_options, ...) {
std::vector<std::string> option_strings;
va_list option_list;
va_start(option_list, num_options);
for(const auto i : c10::irange(num_options))
option_strings.emplace_back(va_arg(option_list, const char*));
va_end(option_list);
PyErr_SetString(PyExc_TypeError, torch::format_invalid_args(
given_args, given_kwargs, function_name, option_strings).c_str());
}
template<>
void THPPointer<THPGenerator>::free() {
if (ptr)
Py_DECREF(ptr);
}
template class THPPointer<THPGenerator>;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static bool backCompatBroadcastWarn = false;
void setBackCompatBroadcastWarn(bool warn) {
backCompatBroadcastWarn = warn;
}
bool getBackCompatBroadcastWarn() {
return backCompatBroadcastWarn;
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static bool backCompatKeepdimWarn = false;
void setBackCompatKeepdimWarn(bool warn) {
backCompatKeepdimWarn = warn;
}
bool getBackCompatKeepdimWarn() {
return backCompatKeepdimWarn;
}
bool maybeThrowBackCompatKeepdimWarn(char *func) {
if(getBackCompatKeepdimWarn()) {
std::ostringstream ss;
ss << "backwards compatibility: call to \"" << func
<< "\" uses default value for keepdim which has changed default to False. Consider passing as kwarg.",
PyErr_WarnEx(PyExc_UserWarning, ss.str().c_str(), 1);
}
return true;
}
template<>
void THPPointer<THTensor>::free() {
if (ptr) {
THTensor_free(LIBRARY_STATE ptr);
}
}
template<>
void THPPointer<THPStorage>::free() {
if (ptr)
Py_DECREF(ptr);
}
template class THPPointer<THPStorage>;
namespace torch { namespace gdb {
/* ~~~ misc debugging utilities ~~~
*
* torch::gdb::* functions are NOT meant to be called by general pytorch code,
* but only from within a gdb session. As such, utils.h does not contain any
* declaration for those.
*/
// This is a helper needed by the torch-tensor-repr gdb command.
// Return an human-readable representation of the given Tensor. The resulting
// string is stored into a malloc()ed buffer. The caller is responsible to
// free() it. We use malloc() instead of new[] because it's much easier to
// call free than delete[] from withing gdb.
// Currently the code for computing the repr of a tensor is written in Python,
// so we need to wrap the Tensor into a Python object first.
char *tensor_repr(at::Tensor tensor) {
PyGILState_STATE gil = PyGILState_Ensure();
// NOLINTNEXTLINE(modernize-use-nullptr)
PyObject *pytensor = NULL;
// NOLINTNEXTLINE(modernize-use-nullptr)
PyObject *repr = NULL;
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
Py_ssize_t bufsize;
// NOLINTNEXTLINE(modernize-use-nullptr)
const char *buf = NULL;
// NOLINTNEXTLINE(modernize-use-nullptr)
char *result = NULL;
pytensor = THPVariable_Wrap(at::Tensor(tensor));
if (!pytensor)
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
goto error;
repr = PyObject_Repr(pytensor);
if (!repr)
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
goto error;
buf = PyUnicode_AsUTF8AndSize(repr, &bufsize);
if (!buf)
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
goto error;
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
result = static_cast<char*>(malloc(bufsize + 1)); // account for the trailing \0
if (!result) {
fprintf(stderr, "cannot allocate memory for the result\n");
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
goto error;
}
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.strcpy)
strcpy(result, buf);
Py_XDECREF(pytensor);
Py_XDECREF(repr);
PyGILState_Release(gil);
return result;
error:
fprintf(stderr, "torch::gdb::tensor_repr: unexpected error\n");
if (PyErr_Occurred())
PyErr_Print();
Py_XDECREF(pytensor);
Py_XDECREF(repr);
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
free(result);
PyGILState_Release(gil);
// NOLINTNEXTLINE(modernize-use-nullptr)
return NULL;
}
}} // namespace torch::gdb