-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathPythonExtensionGen.cpp
More file actions
574 lines (512 loc) · 20.7 KB
/
PythonExtensionGen.cpp
File metadata and controls
574 lines (512 loc) · 20.7 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include <iostream>
#include <string>
#include "CodeGen_C.h"
#include "Module.h"
#include "PythonExtensionGen.h"
#include "Util.h"
namespace Halide {
namespace Internal {
using std::ostream;
using std::ostringstream;
using std::string;
namespace {
// See normalize_line_endings in CodeGen_C.cpp for rationale.
string normalize_line_endings(const char *s) {
string result;
result.reserve(strlen(s));
for (; *s; ++s) {
if (*s != '\r') {
result += *s;
}
}
return result;
}
string sanitize_name(const string &name) {
ostringstream oss;
for (char c : name) {
if (c == '.' || c == '_') {
oss << "_";
} else if (!isalnum(c)) {
oss << "_" << (int)c;
} else {
oss << c;
}
}
return oss.str();
}
string remove_namespaces(const string &name) {
size_t i = name.find_last_of(':');
if (i == string::npos) {
return name;
} else {
return name.substr(i + 1);
}
}
bool can_convert(const LoweredArgument *arg) {
if (arg->type.is_handle()) {
if (arg->name == "__user_context") {
/* __user_context is a void* pointer to a user supplied memory region.
* We allow the Python callee to pass PyObject* pointers to that. */
return true;
} else {
return false;
}
}
if (arg->type.is_vector()) {
return false;
}
if (arg->type.is_float() && arg->type.bits() != 32 && arg->type.bits() != 64 && arg->type.bits() != 16) {
return false;
}
if ((arg->type.is_int() || arg->type.is_uint()) &&
arg->type.bits() != 1 &&
arg->type.bits() != 8 && arg->type.bits() != 16 &&
arg->type.bits() != 32 && arg->type.bits() != 64) {
return false;
}
return true;
}
std::pair<string, string> print_type(const LoweredArgument *arg) {
// Excluded by can_convert() above:
internal_assert(!arg->type.is_vector());
if (arg->type.is_handle()) {
/* Handles can be any pointer. However, from Python, all you can pass to
* a function is a PyObject*, so we can restrict to that. */
return std::make_pair("O", "PyObject*");
} else if (arg->is_buffer()) {
return std::make_pair("O", "PyObject*");
} else if (arg->type.is_float() && arg->type.bits() == 32) {
return std::make_pair("f", "float");
} else if (arg->type.is_float() && arg->type.bits() == 64) {
return std::make_pair("d", "double");
// } else if (arg->type.is_float() && arg->type.bits() == 16) {
// TODO: can't pass scalar float16 type
} else if (arg->type.bits() == 1) {
// "b" expects an unsigned char, so we assume that bool == uint8.
return std::make_pair("b", "bool");
} else if (arg->type.is_int() && arg->type.bits() == 64) {
return std::make_pair("L", "long long");
} else if (arg->type.is_uint() && arg->type.bits() == 64) {
return std::make_pair("K", "unsigned long long");
} else if (arg->type.is_int()) {
return std::make_pair("i", "int");
} else if (arg->type.is_uint()) {
return std::make_pair("I", "unsigned int");
} else {
return std::make_pair("E", "unknown type");
}
}
const string kModuleRegistrationCode = normalize_line_endings(R"INLINE_CODE(
static_assert(PY_MAJOR_VERSION >= 3, "Python bindings for Halide require Python 3+");
namespace Halide::PythonExtensions {
#define X(name) extern PyObject *name(PyObject *module, PyObject *args, PyObject *kwargs);
HALIDE_PYTHON_EXTENSION_FUNCTIONS
#undef X
} // namespace Halide::PythonExtensions
namespace {
#define _HALIDE_STRINGIFY(x) #x
#define _HALIDE_EXPAND_AND_STRINGIFY(x) _HALIDE_STRINGIFY(x)
#define _HALIDE_CONCAT(x, y) x##y
#define _HALIDE_EXPAND_AND_CONCAT(x, y) _HALIDE_CONCAT(x, y)
PyMethodDef _methods[] = {
#define X(name) {#name, reinterpret_cast<PyCFunction>(Halide::PythonExtensions::name), METH_VARARGS | METH_KEYWORDS, nullptr},
HALIDE_PYTHON_EXTENSION_FUNCTIONS
#undef X
{0, 0, 0, nullptr}, // sentinel
};
PyModuleDef _moduledef = {
PyModuleDef_HEAD_INIT, // base
_HALIDE_EXPAND_AND_STRINGIFY(HALIDE_PYTHON_EXTENSION_MODULE_NAME), // name
nullptr, // doc
-1, // size
_methods, // methods
nullptr, // slots
nullptr, // traverse
nullptr, // clear
nullptr, // free
};
#ifndef HALIDE_PYTHON_EXTENSION_OMIT_ERROR_AND_PRINT_HANDLERS
void _module_halide_error(void *user_context, const char *msg) {
// Most Python code probably doesn't want to log the error text to stderr,
// so we won't do that by default.
#ifdef HALIDE_PYTHON_EXTENSION_LOG_ERRORS_TO_STDERR
PyGILState_STATE s = PyGILState_Ensure();
PySys_FormatStderr("%s\n", msg);
PyGILState_Release(s);
#endif
}
void _module_halide_print(void *user_context, const char *msg) {
PyGILState_STATE s = PyGILState_Ensure();
PySys_FormatStdout("%s", msg);
PyGILState_Release(s);
}
#endif // HALIDE_PYTHON_EXTENSION_OMIT_ERROR_AND_PRINT_HANDLERS
} // namespace
namespace Halide::PythonRuntime {
bool unpack_buffer(PyObject *py_obj,
int py_getbuffer_flags,
const char *name,
int dimensions,
Py_buffer &py_buf,
halide_dimension_t *halide_dim,
halide_buffer_t &halide_buf,
bool &py_buf_valid,
bool &needs_device_free) {
py_buf_valid = false;
needs_device_free = false;
memset(&py_buf, 0, sizeof(py_buf));
if (PyObject_GetBuffer(py_obj, &py_buf, PyBUF_FORMAT | PyBUF_STRIDED_RO | PyBUF_ANY_CONTIGUOUS | py_getbuffer_flags) < 0) {
PyErr_Format(PyExc_ValueError, "Invalid argument %s: Expected %d dimensions, got %d", name, dimensions, py_buf.ndim);
return false;
}
py_buf_valid = true;
if (dimensions && py_buf.ndim != dimensions) {
PyErr_Format(PyExc_ValueError, "Invalid argument %s: Expected %d dimensions, got %d", name, dimensions, py_buf.ndim);
return false;
}
/* We'll get a buffer that's either:
* C_CONTIGUOUS (last dimension varies the fastest, i.e., has stride=1) or
* F_CONTIGUOUS (first dimension varies the fastest, i.e., has stride=1).
* The latter is preferred, since it's already in the format that Halide
* needs. It can can be achieved in numpy by passing order='F' during array
* creation. However, if we do get a C_CONTIGUOUS buffer, flip the dimensions
* (transpose) so we can process it without having to reallocate.
*/
int i, j, j_step;
if (PyBuffer_IsContiguous(&py_buf, 'F')) {
j = 0;
j_step = 1;
} else if (PyBuffer_IsContiguous(&py_buf, 'C')) {
j = py_buf.ndim - 1;
j_step = -1;
} else {
/* Python checks all dimensions and strides, so this typically indicates
* a bug in the array's buffer protocol. */
PyErr_Format(PyExc_ValueError, "Invalid buffer: neither C nor Fortran contiguous");
return false;
}
for (i = 0; i < py_buf.ndim; ++i, j += j_step) {
halide_dim[i].min = 0;
halide_dim[i].stride = (int)(py_buf.strides[j] / py_buf.itemsize); // strides is in bytes
halide_dim[i].extent = (int)py_buf.shape[j];
halide_dim[i].flags = 0;
if (py_buf.suboffsets && py_buf.suboffsets[i] >= 0) {
// Halide doesn't support arrays of pointers. But we should never see this
// anyway, since we specified PyBUF_STRIDED.
PyErr_Format(PyExc_ValueError, "Invalid buffer: suboffsets not supported");
return false;
}
}
if (halide_dim[py_buf.ndim - 1].extent * halide_dim[py_buf.ndim - 1].stride * py_buf.itemsize != py_buf.len) {
PyErr_Format(PyExc_ValueError, "Invalid buffer: length %ld, but computed length %ld",
py_buf.len, py_buf.shape[0] * py_buf.strides[0]);
return false;
}
halide_buf = {};
needs_device_free = true;
if (!py_buf.format) {
halide_buf.type.code = halide_type_uint;
halide_buf.type.bits = 8;
} else {
/* Convert struct type code. See
* https://docs.python.org/2/library/struct.html#module-struct */
char *p = py_buf.format;
while (strchr("@<>!=", *p)) {
p++; // ignore little/bit endian (and alignment)
}
if (*p == 'f' || *p == 'd' || *p == 'e') {
// 'f', 'd', and 'e' are float, double, and half, respectively.
halide_buf.type.code = halide_type_float;
} else if (*p >= 'a' && *p <= 'z') {
// lowercase is signed int.
halide_buf.type.code = halide_type_int;
} else {
// uppercase is unsigned int.
halide_buf.type.code = halide_type_uint;
}
const char *type_codes = "bBhHiIlLqQfde"; // integers and floats
if (*p == '?') {
// Special-case bool, so that it is a distinct type vs uint8_t
// (even though the memory layout is identical)
halide_buf.type.bits = 1;
} else if (strchr(type_codes, *p)) {
halide_buf.type.bits = (uint8_t)py_buf.itemsize * 8;
} else {
// We don't handle 's' and 'p' (char[]) and 'P' (void*)
PyErr_Format(PyExc_ValueError, "Invalid data type for %s: %s", name, py_buf.format);
return false;
}
}
halide_buf.type.lanes = 1;
halide_buf.dimensions = py_buf.ndim;
halide_buf.dim = halide_dim;
halide_buf.host = (uint8_t *)py_buf.buf;
return true;
}
} // namespace Halide::PythonRuntime
extern "C" {
HALIDE_EXPORT_SYMBOL PyObject *_HALIDE_EXPAND_AND_CONCAT(PyInit_, HALIDE_PYTHON_EXTENSION_MODULE_NAME)() {
PyObject *m = PyModule_Create(&_moduledef);
#ifndef HALIDE_PYTHON_EXTENSION_OMIT_ERROR_AND_PRINT_HANDLERS
halide_set_error_handler(_module_halide_error);
halide_set_custom_print(_module_halide_print);
#endif // HALIDE_PYTHON_EXTENSION_OMIT_ERROR_AND_PRINT_HANDLERS
return m;
}
} // extern "C"
)INLINE_CODE");
} // namespace
PythonExtensionGen::PythonExtensionGen(std::ostream &dest)
: dest(dest) {
}
void PythonExtensionGen::compile(const Module &module) {
dest << "#include <string>\n";
dest << "#include <Python.h>\n";
dest << "#include \"HalideRuntime.h\"\n\n";
std::vector<std::string> fnames;
// Emit extern decls of the Halide-generated functions we use directly
// into this file, so that we don't have to #include the relevant .h
// file directly; this simplifies certain compile/build setups (since
// we don't have to build files in tandem and/or get include paths right),
// and should be totally safe, since we are using the same codegen logic
// that would be in the .h file anyway.
if (!module.functions().empty()) {
// The CodeGen_C dtor must run to finish codegen correctly,
// so wrap this in braces
{
CodeGen_C extern_decl_gen(dest, module.target(), CodeGen_C::CPlusPlusExternDecl);
extern_decl_gen.compile(module);
}
dest << normalize_line_endings(R"INLINE_CODE(
namespace Halide::PythonRuntime {
extern bool unpack_buffer(PyObject *py_obj,
int py_getbuffer_flags,
const char *name,
int dimensions,
Py_buffer &py_buf,
halide_dimension_t *halide_dim,
halide_buffer_t &halide_buf,
bool &py_buf_valid,
bool &needs_device_free);
} // namespace Halide::PythonRuntime
namespace {
template<int dimensions>
struct PyHalideBuffer {
// Must allocate at least 1, even if d=0
static constexpr int dims_to_allocate = (dimensions < 1) ? 1 : dimensions;
static constexpr const char* get_raw_halide_runtime_buffer_fn = "_get_raw_halide_buffer_t";
Py_buffer py_buf;
halide_buffer_t* halide_buf = nullptr;
bool py_buf_needs_release = false;
bool needs_device_free = false;
bool unpack_from_halide_buffer(PyObject *py_obj) {
if (!PyObject_HasAttrString(py_obj, get_raw_halide_runtime_buffer_fn)) {
return false;
}
PyObject *py_raw_buffer = PyObject_CallMethod(py_obj, get_raw_halide_runtime_buffer_fn, NULL);
if (!py_raw_buffer) {
PyErr_Clear();
return false;
}
if (!PyLong_Check(py_raw_buffer)) {
Py_DECREF(py_raw_buffer);
return false;
}
uintptr_t py_raw_buffer_ptr = (uintptr_t)PyLong_AsUnsignedLongLong(py_raw_buffer);
Py_DECREF(py_raw_buffer);
if (py_raw_buffer_ptr == 0) {
return false;
}
halide_buf = reinterpret_cast<halide_buffer_t *>(py_raw_buffer_ptr);
return true;
}
bool unpack(PyObject *py_obj, int py_getbuffer_flags, const char *name) {
if (unpack_from_halide_buffer(py_obj)) {
return true;
}
if (Halide::PythonRuntime::unpack_buffer(
py_obj, py_getbuffer_flags, name, dimensions, py_buf,
unpacked_dim, unpacked_buf, py_buf_needs_release,
needs_device_free)) {
halide_buf = &unpacked_buf;
return true;
}
return false;
}
~PyHalideBuffer() {
if (needs_device_free) {
halide_device_free(nullptr, halide_buf);
}
if (py_buf_needs_release) {
PyBuffer_Release(&py_buf);
}
}
PyHalideBuffer() = default;
PyHalideBuffer(const PyHalideBuffer &other) = delete;
PyHalideBuffer &operator=(const PyHalideBuffer &other) = delete;
PyHalideBuffer(PyHalideBuffer &&other) = delete;
PyHalideBuffer &operator=(PyHalideBuffer &&other) = delete;
private:
halide_dimension_t unpacked_dim[dims_to_allocate];
halide_buffer_t unpacked_buf;
};
} // namespace
)INLINE_CODE");
for (const auto &f : module.functions()) {
if (f.linkage == LinkageType::ExternalPlusMetadata) {
compile(f);
fnames.push_back(remove_namespaces(f.name));
}
}
}
dest << "\n";
if (!fnames.empty()) {
dest << "#ifndef HALIDE_PYTHON_EXTENSION_OMIT_MODULE_DEFINITION\n";
dest << "\n";
dest << "#ifndef HALIDE_PYTHON_EXTENSION_MODULE_NAME\n";
dest << "#define HALIDE_PYTHON_EXTENSION_MODULE_NAME " << module.name() << "\n";
dest << "#endif // HALIDE_PYTHON_EXTENSION_MODULE_NAME\n";
dest << "\n";
dest << "#ifndef HALIDE_PYTHON_EXTENSION_FUNCTIONS\n";
dest << "#define HALIDE_PYTHON_EXTENSION_FUNCTIONS";
for (const auto &fname : fnames) {
dest << " X(" << fname << ")";
}
dest << "\n";
dest << "#endif // HALIDE_PYTHON_EXTENSION_FUNCTIONS\n";
dest << "\n";
}
dest << kModuleRegistrationCode;
if (!fnames.empty()) {
dest << "#endif // HALIDE_PYTHON_EXTENSION_OMIT_MODULE_DEFINITION\n";
}
}
void PythonExtensionGen::compile(const LoweredFunc &f) {
const std::vector<LoweredArgument> &args = f.args;
const string basename = remove_namespaces(f.name);
std::vector<string> arg_names(args.size());
for (size_t i = 0; i < args.size(); i++) {
arg_names[i] = sanitize_name(args[i].name);
}
Indentation indent;
indent.indent = 0;
dest << "namespace Halide::PythonExtensions {\n";
dest << "\n";
dest << "namespace {\n";
dest << "\n";
dest << indent << "const char* const " << basename << "_kwlist[] = {\n";
indent.indent += 2;
for (size_t i = 0; i < args.size(); i++) {
dest << indent << "\"" << arg_names[i] << "\",\n";
}
dest << indent << "nullptr\n";
indent.indent -= 2;
dest << indent << "};\n";
dest << "\n";
dest << "} // namespace\n";
dest << "\n";
dest << "// " << f.name << "\n";
dest << "PyObject *" << basename << "(PyObject *module, PyObject *args, PyObject *kwargs) {\n";
indent.indent += 2;
for (const auto &arg : args) {
if (!can_convert(&arg)) {
/* Some arguments can't be converted to Python yet. In those
* cases, just add a dummy function that always throws an
* Exception. */
// TODO: Add support for handles and vectors.
// TODO: might make more sense to simply fail at Halide compile time!
dest << indent << "PyErr_Format(PyExc_NotImplementedError, "
<< "\"Can't convert argument " << arg.name << " from Python\");\n";
dest << indent << "return nullptr;\n";
dest << "}\n";
dest << "} // namespace Halide::PythonExtensions\n";
return;
}
}
for (size_t i = 0; i < args.size(); i++) {
dest << indent << print_type(&args[i]).second << " py_" << arg_names[i] << ";\n";
}
dest << indent << "if (!PyArg_ParseTupleAndKeywords(args, kwargs, \"";
for (const auto &arg : args) {
dest << print_type(&arg).first;
}
dest << "\", (char**)" << basename << "_kwlist\n";
indent.indent += 2;
for (size_t i = 0; i < args.size(); i++) {
dest << indent << ", &py_" << arg_names[i] << "\n";
}
indent.indent -= 2;
dest << indent << ")) {\n";
indent.indent += 2;
dest << indent << "PyErr_Format(PyExc_ValueError, \"Internal error\");\n";
dest << indent << "return nullptr;\n";
indent.indent -= 2;
dest << indent << "}\n";
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer()) {
const auto &name = arg_names[i]; // must use sanitized names here
dest << indent << "PyHalideBuffer<" << (int)args[i].dimensions << "> b_" << name << ";\n";
}
}
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer()) {
const auto &name = arg_names[i]; // must use sanitized names here
dest << indent << "if (!b_" << name << ".unpack(py_" << name << ", "
<< (args[i].is_output() ? "PyBUF_WRITABLE" : "0") << ", "
<< basename << "_kwlist[" << i << "])) return nullptr;\n";
}
}
dest << "\n";
// Mark all input buffers as having a dirty host, so that the Halide call will
// do a lazy-copy-to-GPU if needed.
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer() && args[i].is_input()) {
dest << indent << "b_" << arg_names[i] << ".halide_buf->set_host_dirty();\n";
}
}
dest << indent << "int result;\n";
dest << indent << "Py_BEGIN_ALLOW_THREADS\n";
dest << indent << "result = " << f.name << "(\n";
indent.indent += 2;
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer()) {
dest << indent << "b_" << arg_names[i] << ".halide_buf";
} else {
dest << indent << "py_" << arg_names[i] << "";
}
if (i < args.size() - 1) {
dest << ",";
}
dest << "\n";
}
indent.indent -= 2;
dest << indent << ");\n";
dest << indent << "Py_END_ALLOW_THREADS\n";
// Since the Python Buffer protocol is host-memory-only, we *must*
// flush results back to host, otherwise the output buffer will contain
// random garbage. (We need a better solution for this, see https://github.com/halide/Halide/issues/6868)
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer() && args[i].is_output()) {
dest << indent << "if (result == 0) result = halide_copy_to_host(nullptr, b_" << arg_names[i] << ".halide_buf);\n";
}
}
dest << indent << "if (result != 0) {\n";
indent.indent += 2;
dest << indent << "#ifndef HALIDE_PYTHON_EXTENSION_OMIT_ERROR_AND_PRINT_HANDLERS\n";
dest << indent << "PyErr_Format(PyExc_RuntimeError, \"Halide Runtime Error: %d\", result);\n";
dest << indent << "#else\n";
dest << indent << "PyErr_Format(PyExc_ValueError, \"Halide error %d\", result);\n";
dest << indent << "#endif // HALIDE_PYTHON_EXTENSION_OMIT_ERROR_AND_PRINT_HANDLERS\n";
dest << indent << "return nullptr;\n";
indent.indent -= 2;
dest << indent << "}\n";
dest << "\n";
dest << indent << "Py_INCREF(Py_None);\n";
dest << indent << "return Py_None;\n";
indent.indent -= 2;
dest << "}\n";
dest << "\n";
dest << "} // namespace Halide::PythonExtensions\n";
}
} // namespace Internal
} // namespace Halide