forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeGen_PyTorch.cpp
391 lines (327 loc) · 12.3 KB
/
CodeGen_PyTorch.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
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
#include <iostream>
#include "CodeGen_C.h"
#include "CodeGen_PyTorch.h"
#include "IROperator.h"
#include "Param.h"
#include "Util.h"
#include "Var.h"
namespace Halide {
namespace Internal {
CodeGen_PyTorch::CodeGen_PyTorch(std::ostream &s)
: IRPrinter(s) {
}
void CodeGen_PyTorch::compile(const Module &module) {
const Target target = module.target();
if (target.has_feature(Target::CUDA)) {
if (!target.has_feature(Target::UserContext)) {
user_error << "Compile a PyTorch wrapper for a CUDA op requires the "
"UserContext feature to properly manage the GPU memory. "
"Please add \"-user_context\" to the generator's target options.\n";
}
stream << "#include \"ATen/cuda/CUDAContext.h\"\n";
stream << "#include \"HalideBuffer.h\"\n";
stream << "#include \"HalidePyTorchCudaHelpers.h\"\n";
stream << "#include \"HalidePyTorchHelpers.h\"\n";
stream << "#include \"torch/extension.h\"\n";
} else {
stream << "#include \"HalideBuffer.h\"\n";
stream << "#include \"HalidePyTorchHelpers.h\"\n";
stream << "#include \"torch/extension.h\"\n";
}
stream << "\n";
// 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.
{
CodeGen_C extern_decl_gen(stream, module.target(), CodeGen_C::CPlusPlusExternDecl);
extern_decl_gen.compile(module);
}
for (const auto &f : module.functions()) {
if (target.has_feature(Target::CUDA)) {
compile(f, true);
} else {
compile(f, false);
}
}
}
void CodeGen_PyTorch::compile(const LoweredFunc &f, bool is_cuda) {
// Don't put non-external function declarations in headers.
std::vector<std::string> namespaces;
std::string simple_name = extract_namespaces(f.name, namespaces);
if (!namespaces.empty()) {
for (const auto &ns : namespaces) {
stream << "namespace " << ns << " {\n";
}
stream << "\n";
}
const std::vector<LoweredArgument> &args = f.args;
std::vector<LoweredArgument> buffer_args;
stream << "HALIDE_FUNCTION_ATTRS\n";
stream << "inline int " << simple_name << "_th_(";
for (size_t i = 0; i < args.size(); i++) {
if (args[i].name == "__user_context") {
continue;
} else if (args[i].is_buffer()) {
buffer_args.push_back(args[i]);
stream
<< "at::Tensor &"
<< c_print_name(args[i].name);
} else {
stream
<< type_to_c_type(args[i].type, true)
<< c_print_name(args[i].name);
}
if (i < args.size() - 1)
stream << ", ";
}
stream << ") {\n";
indent += 4;
if (is_cuda) {
stream << get_indent() << "// Setup CUDA\n";
stream << get_indent() << "int device_id = at::cuda::current_device();\n";
stream << get_indent() << "CUcontext ctx = 0;\n";
stream << get_indent() << "CUresult res = cuCtxGetCurrent(&ctx);\n";
stream << get_indent() << "AT_ASSERTM(res == 0, \"Could not acquire CUDA context\");\n";
stream << get_indent() << "cudaStream_t stream = at::cuda::getCurrentCUDAStream(device_id);\n";
stream << get_indent() << "Halide::PyTorch::UserContext user_ctx(device_id, &ctx, &stream);\n";
stream << get_indent() << "void* __user_context = (void*) &user_ctx;\n\n";
}
stream << get_indent() << "// Check tensors have contiguous memory and are on the correct device\n";
for (size_t i = 0; i < buffer_args.size(); i++) {
stream << get_indent();
stream
<< "HLPT_CHECK_CONTIGUOUS("
<< c_print_name(buffer_args[i].name)
<< ");\n";
if (is_cuda) {
stream << get_indent();
stream
<< "HLPT_CHECK_DEVICE("
<< c_print_name(buffer_args[i].name)
<< ", device_id);\n";
}
}
stream << "\n";
stream << get_indent() << "// Wrap tensors in Halide buffers\n";
for (size_t i = 0; i < buffer_args.size(); i++) {
if (!buffer_args[i].is_buffer())
continue;
stream << get_indent();
std::string tp = type_to_c_type(buffer_args[i].type, false);
stream
<< "Halide::Runtime::Buffer<" << tp << "> "
<< c_print_name(buffer_args[i].name)
<< "_buffer = Halide::PyTorch::wrap<" << tp << ">("
<< c_print_name(buffer_args[i].name)
<< ");\n";
}
stream << "\n";
stream << get_indent() << "// Run Halide pipeline\n";
stream << get_indent() << "int err = " << simple_name << "(";
for (size_t i = 0; i < args.size(); i++) {
if (args[i].is_buffer()) {
stream
<< c_print_name(args[i].name)
<< "_buffer";
} else {
stream << c_print_name(args[i].name);
}
if (i < args.size() - 1)
stream << ", ";
}
stream << ");\n";
stream << "\n";
stream << get_indent() << "AT_ASSERTM(err == 0, \"Halide call failed\");\n";
if (is_cuda) {
stream << get_indent() << "// Make sure data is on device\n";
for (size_t i = 0; i < buffer_args.size(); i++) {
if (buffer_args[i].is_buffer()) {
stream << get_indent();
stream
<< "AT_ASSERTM(!"
<< c_print_name(buffer_args[i].name) << "_buffer.host_dirty(),"
<< "\"device not synchronized for buffer "
<< c_print_name(buffer_args[i].name)
<< ", make sure all update stages are excplicitly computed on GPU."
<< "\");\n";
stream << get_indent();
stream
<< c_print_name(buffer_args[i].name) << "_buffer"
<< ".device_detach_native();\n";
}
}
stream << "\n";
}
// TODO(mgharbi): this is not very well documented
if (get_env_variable("FLUSH_MEMOIZE_CACHE") == "1") {
stream << get_indent() << "// Flush cache\n";
if (is_cuda) {
stream << get_indent() << "halide_memoization_cache_cleanup(__user_context);\n";
} else {
stream << get_indent() << "halide_memoization_cache_cleanup(NULL);\n";
}
}
stream << get_indent() << "return 0;\n";
indent -= 4;
stream << "}\n";
if (!namespaces.empty()) {
stream << "\n";
for (size_t i = namespaces.size(); i > 0; i--) {
stream << "} // namespace " << namespaces[i - 1] << "\n";
}
stream << "\n";
}
}
void CodeGen_PyTorch::test() {
// Dummy Halide pipeline
LoweredArgument buffer_arg("buf", Argument::OutputBuffer, Int(32), 3, ArgumentEstimates{});
LoweredArgument float_arg("alpha", Argument::InputScalar, Float(32), 0, ArgumentEstimates{});
LoweredArgument int_arg("beta", Argument::InputScalar, Int(32), 0, ArgumentEstimates{});
std::vector<LoweredArgument> args = {buffer_arg, float_arg, int_arg};
Var x("x");
Param<float> alpha("alpha");
Param<int> beta("beta");
Expr e = Add::make(alpha, Cast::make(Float(32), beta));
Stmt s = Store::make("buf", e, x, Parameter(), const_true(), ModulusRemainder());
Expr buf = Variable::make(Handle(), "buf.buffer");
s = LetStmt::make("buf", Call::make(Handle(), Call::buffer_get_host, {buf}, Call::Extern), s);
std::ostringstream source;
std::ostringstream source_cuda;
{
// TODO(mgharbi): test that Target("host-cuda") raises an exception since
// we require the "user_context" feature when using CUDA
Module m("", Target("host"));
m.append(LoweredFunc("test1", args, s, LinkageType::External));
CodeGen_PyTorch(source).compile(m);
}
{
Module m("", Target("host-cuda-user_context"));
m.append(LoweredFunc("test1", args, s, LinkageType::External));
CodeGen_PyTorch(source_cuda).compile(m);
}
std::string src = source.str() + "\n" + source_cuda.str();
// The correct source concatenates CPU and GPU headers
std::string correct_src =
R"GOLDEN_CODE(#include "HalideBuffer.h"
#include "HalidePyTorchHelpers.h"
#include "torch/extension.h"
struct halide_buffer_t;
struct halide_filter_metadata_t;
#ifndef HALIDE_MUST_USE_RESULT
#ifdef __has_attribute
#if __has_attribute(nodiscard)
#define HALIDE_MUST_USE_RESULT [[nodiscard]]
#elif __has_attribute(warn_unused_result)
#define HALIDE_MUST_USE_RESULT __attribute__((warn_unused_result))
#else
#define HALIDE_MUST_USE_RESULT
#endif
#else
#define HALIDE_MUST_USE_RESULT
#endif
#endif
#ifndef HALIDE_FUNCTION_ATTRS
#define HALIDE_FUNCTION_ATTRS
#endif
#ifdef __cplusplus
extern "C" {
#endif
HALIDE_FUNCTION_ATTRS
int test1(struct halide_buffer_t *_buf_buffer, float _alpha, int32_t _beta);
#ifdef __cplusplus
} // extern "C"
#endif
HALIDE_FUNCTION_ATTRS
inline int test1_th_(at::Tensor &_buf, float _alpha, int32_t _beta) {
// Check tensors have contiguous memory and are on the correct device
HLPT_CHECK_CONTIGUOUS(_buf);
// Wrap tensors in Halide buffers
Halide::Runtime::Buffer<int32_t> _buf_buffer = Halide::PyTorch::wrap<int32_t>(_buf);
// Run Halide pipeline
int err = test1(_buf_buffer, _alpha, _beta);
AT_ASSERTM(err == 0, "Halide call failed");
return 0;
}
#include "ATen/cuda/CUDAContext.h"
#include "HalideBuffer.h"
#include "HalidePyTorchCudaHelpers.h"
#include "HalidePyTorchHelpers.h"
#include "torch/extension.h"
struct halide_buffer_t;
struct halide_filter_metadata_t;
#ifndef HALIDE_MUST_USE_RESULT
#ifdef __has_attribute
#if __has_attribute(nodiscard)
#define HALIDE_MUST_USE_RESULT [[nodiscard]]
#elif __has_attribute(warn_unused_result)
#define HALIDE_MUST_USE_RESULT __attribute__((warn_unused_result))
#else
#define HALIDE_MUST_USE_RESULT
#endif
#else
#define HALIDE_MUST_USE_RESULT
#endif
#endif
#ifndef HALIDE_FUNCTION_ATTRS
#define HALIDE_FUNCTION_ATTRS
#endif
#ifdef __cplusplus
extern "C" {
#endif
HALIDE_FUNCTION_ATTRS
int test1(struct halide_buffer_t *_buf_buffer, float _alpha, int32_t _beta);
#ifdef __cplusplus
} // extern "C"
#endif
HALIDE_FUNCTION_ATTRS
inline int test1_th_(at::Tensor &_buf, float _alpha, int32_t _beta) {
// Setup CUDA
int device_id = at::cuda::current_device();
CUcontext ctx = 0;
CUresult res = cuCtxGetCurrent(&ctx);
AT_ASSERTM(res == 0, "Could not acquire CUDA context");
cudaStream_t stream = at::cuda::getCurrentCUDAStream(device_id);
Halide::PyTorch::UserContext user_ctx(device_id, &ctx, &stream);
void* __user_context = (void*) &user_ctx;
// Check tensors have contiguous memory and are on the correct device
HLPT_CHECK_CONTIGUOUS(_buf);
HLPT_CHECK_DEVICE(_buf, device_id);
// Wrap tensors in Halide buffers
Halide::Runtime::Buffer<int32_t> _buf_buffer = Halide::PyTorch::wrap<int32_t>(_buf);
// Run Halide pipeline
int err = test1(_buf_buffer, _alpha, _beta);
AT_ASSERTM(err == 0, "Halide call failed");
// Make sure data is on device
AT_ASSERTM(!_buf_buffer.host_dirty(),"device not synchronized for buffer _buf, make sure all update stages are excplicitly computed on GPU.");
_buf_buffer.device_detach_native();
return 0;
}
)GOLDEN_CODE";
if (src != correct_src) {
int diff = 0;
while (src[diff] == correct_src[diff]) {
diff++;
}
int diff_end = diff + 1;
while (diff > 0 && src[diff] != '\n') {
diff--;
}
while (diff_end < (int)src.size() && src[diff_end] != '\n') {
diff_end++;
}
internal_error
<< "Correct source code:\n"
<< correct_src
<< "Actual source code:\n"
<< src
<< "Difference starts at:" << diff << "\n"
<< "Correct: " << correct_src.substr(diff, diff_end - diff) << "\n"
<< "Actual: " << src.substr(diff, diff_end - diff) << "\n";
}
std::cout << "CodeGen_PyTorch test passed\n";
}
} // namespace Internal
} // namespace Halide