-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CodeGen_PyTorch.cpp
221 lines (193 loc) · 7.41 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
#include <iostream>
#include "CodeGen_C.h"
#include "CodeGen_PyTorch.h"
#include "Module.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 \"HalidePyTorchCudaHelpers.h\"\n";
}
stream << "#include \"HalideBuffer.h\"\n";
stream << "#include \"HalidePyTorchHelpers.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()) {
// Don't put non-external function declarations in headers.
// We need to be consistent with CodeGen_C::compile.
if (f.linkage == LinkageType::Internal) {
continue;
}
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() << "struct UserContext { int device_id; CUcontext *cuda_context; cudaStream_t *stream; } user_ctx;\n";
stream << get_indent() << "user_ctx.device_id = device_id;\n";
stream << get_indent() << "user_ctx.cuda_context = &ctx;\n";
stream << get_indent() << "user_ctx.stream = &stream;\n";
stream << get_indent() << "void* __user_context = (void*) &user_ctx;\n\n";
} else {
stream << get_indent() << "void* __user_context = nullptr;\n\n";
}
stream << get_indent() << "// Check tensors have contiguous memory and are on the correct device\n";
for (auto &buffer_arg : buffer_args) {
stream << get_indent();
stream
<< "HLPT_CHECK_CONTIGUOUS("
<< c_print_name(buffer_arg.name)
<< ");\n";
if (is_cuda) {
stream << get_indent();
stream
<< "HLPT_CHECK_DEVICE("
<< c_print_name(buffer_arg.name)
<< ", device_id);\n";
}
}
stream << "\n";
stream << get_indent() << "// Wrap tensors in Halide buffers\n";
for (auto &buffer_arg : buffer_args) {
if (!buffer_arg.is_buffer()) {
continue;
}
stream << get_indent();
std::string tp = type_to_c_type(buffer_arg.type, false);
stream
<< "Halide::Runtime::Buffer<" << tp << "> "
<< c_print_name(buffer_arg.name);
if (is_cuda) {
stream
<< "_buffer = Halide::PyTorch::wrap_cuda<" << tp << ">(";
} else {
stream
<< "_buffer = Halide::PyTorch::wrap<" << tp << ">(";
}
stream
<< c_print_name(buffer_arg.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 (auto &buffer_arg : buffer_args) {
if (buffer_arg.is_buffer()) {
stream << get_indent();
stream
<< "AT_ASSERTM(!"
<< c_print_name(buffer_arg.name) << "_buffer.host_dirty(),"
<< "\"device not synchronized for buffer "
<< c_print_name(buffer_arg.name)
<< ", make sure all update stages are explicitly computed on GPU."
<< "\");\n";
stream << get_indent();
stream
<< c_print_name(buffer_arg.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(nullptr);\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";
}
}
} // namespace Internal
} // namespace Halide