Repository navigation
Expand file tree
/
Copy pathcuda_backend.cpp
More file actions
379 lines (320 loc) · 13 KB
/
Copy pathcuda_backend.cpp
File metadata and controls
379 lines (320 loc) · 13 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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cuda_runtime.h>
#include <executorch/runtime/backend/interface.h>
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/evalue.h>
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
// Include our shim layer headers
#include <executorch/backends/aoti/aoti_delegate_handle.h>
#include <executorch/backends/aoti/common_shims.h>
#include <executorch/backends/cuda/runtime/platform/platform.h>
#include <executorch/backends/cuda/runtime/shims/memory.h>
#include <executorch/backends/cuda/runtime/utils.h>
namespace executorch::backends::cuda {
using namespace std;
using namespace aoti;
using executorch::aten::ScalarType;
using executorch::runtime::ArrayRef;
using executorch::runtime::Backend;
using executorch::runtime::BackendExecutionContext;
using executorch::runtime::BackendInitContext;
using executorch::runtime::CompileSpec;
using executorch::runtime::DelegateHandle;
using executorch::runtime::Error;
using executorch::runtime::EValue;
using executorch::runtime::FreeableBuffer;
using executorch::runtime::MemoryAllocator;
using executorch::runtime::NamedDataMap;
using executorch::runtime::Result;
using executorch::runtime::Span;
using executorch::runtime::etensor::Tensor;
class ET_EXPERIMENTAL CudaBackend final
: public ::executorch::runtime::BackendInterface {
private:
Error load_function_pointers_into_handle(
void* so_handle,
AOTIDelegateHandle* handle) const {
#define LOAD_SYMBOL(member, name) \
do { \
auto symbol_res = get_function(so_handle, #name); \
if (!symbol_res.ok()) { \
return symbol_res.error(); \
} \
handle->member = reinterpret_cast<name##Func>(symbol_res.get()); \
} while (0)
LOAD_SYMBOL(create_with_device, AOTInductorModelContainerCreateWithDevice);
LOAD_SYMBOL(delete_container, AOTInductorModelContainerDelete);
LOAD_SYMBOL(get_num_inputs, AOTInductorModelContainerGetNumInputs);
LOAD_SYMBOL(get_num_outputs, AOTInductorModelContainerGetNumOutputs);
LOAD_SYMBOL(run, AOTInductorModelContainerRun);
#undef LOAD_SYMBOL
auto symbol_res =
get_function(so_handle, "AOTInductorModelUpdateConstantsFromBlob");
if (symbol_res.ok()) {
handle->update_constants_from_blob =
reinterpret_cast<AOTInductorModelUpdateConstantsFromBlobFunc>(
symbol_res.get());
} else {
ET_LOG(
Info,
"Failed to load AOTInductorModelUpdateConstantsFromBlob. This .so is probably compiled on an old version of torch (<2.9.0)");
}
return Error::Ok;
}
public:
bool is_available() const override {
return 1;
}
// Once per loaded binary blob
Result<DelegateHandle*> init(
BackendInitContext& context,
FreeableBuffer* processed, // This will be a empty buffer
ArrayRef<CompileSpec> compile_specs // This will be my empty list
) const override {
std::string method_name;
for (const CompileSpec& spec : compile_specs) {
if (std::strcmp(spec.key, "method_name") == 0) {
method_name.assign(
static_cast<const char*>(spec.value.buffer),
spec.value.nbytes); // no nullptr guarantee, so pass size
break;
}
}
std::string so_blob_key =
method_name.empty() ? "so_blob" : method_name + "_so_blob";
const NamedDataMap* named_data_map = context.get_named_data_map();
auto aoti_dso_buffer = named_data_map->get_data(so_blob_key.c_str());
ET_CHECK_OR_RETURN_ERROR(
aoti_dso_buffer.ok(),
Internal,
"Failed to get data for key %s: 0x%x",
so_blob_key.c_str(),
static_cast<uint32_t>(aoti_dso_buffer.error()));
// Generate dynamic temporary file path
filesystem::path temp_dir = filesystem::temp_directory_path();
filesystem::path so_path =
temp_dir / (so_blob_key + to_string(get_process_id()) + ".so");
// Create a temporary file
ofstream outfile(so_path, ios::binary);
// Write the ELF buffer to the temporary file
ET_LOG(
Info,
"Writing %zu bytes to %s",
aoti_dso_buffer->size(),
so_path.c_str());
outfile.write(
static_cast<const char*>(aoti_dso_buffer->data()),
aoti_dso_buffer->size());
ET_CHECK_OR_RETURN_ERROR(
outfile, AccessFailed, "Failed to write to file %s", so_path.c_str());
// Finish writing the file to disk
outfile.close();
// Free the buffer immediately after writing to disk
aoti_dso_buffer->Free();
// Load the lib
Result<void*> lib_handle_res = load_library(so_path);
if (!lib_handle_res.ok()) {
return lib_handle_res.error();
}
void* lib_handle = lib_handle_res.get();
processed->Free();
// Create handle and load function pointers into it
AOTIDelegateHandle* handle = new AOTIDelegateHandle();
handle->so_handle = lib_handle;
handle->so_path = so_path.string();
// Load function pointers specific to this handle's shared library
ET_CHECK_OK_OR_RETURN_ERROR(
load_function_pointers_into_handle(lib_handle, handle));
AOTInductorModelContainerHandle container_handle = nullptr;
ET_CHECK_OK_OR_RETURN_ERROR(
handle->create_with_device(&container_handle, 1, "cuda", nullptr));
ET_LOG(Info, "container_handle = %p", container_handle);
handle->container_handle = container_handle;
// Look into named data map for constant data
std::string weights_blob_key =
method_name.empty() ? "weights_blob" : method_name + "_weights_blob";
auto buffer_res = named_data_map->get_data(weights_blob_key.c_str());
if (buffer_res.ok() && handle->update_constants_from_blob != nullptr) {
ET_LOG(Info, "Found %s in named data map", weights_blob_key.c_str());
const void* weights_blob = buffer_res->data();
// Feed the weights blob into the container. Under the hood it's copying
// weights, so we should free the buffer immediately.
ET_CHECK_OK_OR_RETURN_ERROR(handle->update_constants_from_blob(
handle->container_handle, static_cast<const uint8_t*>(weights_blob)));
buffer_res->Free();
}
// Create a CUDA stream for asynchronous execution
cudaStream_t cuda_stream;
ET_CUDA_CHECK_OR_RETURN_ERROR(cudaStreamCreate(&cuda_stream));
handle->cuda_stream = static_cast<void*>(cuda_stream);
return (DelegateHandle*)handle; // Return the handle post-processing
}
// Once per execution
Error execute(
BackendExecutionContext& context,
DelegateHandle* handle_,
Span<EValue*> args) const override {
AOTIDelegateHandle* handle = (AOTIDelegateHandle*)handle_;
size_t n_inputs;
handle->get_num_inputs(handle->container_handle, &n_inputs);
size_t n_outputs;
handle->get_num_outputs(handle->container_handle, &n_outputs);
ET_CHECK_OR_RETURN_ERROR(
n_inputs + n_outputs == args.size(),
InvalidArgument,
"number of user input %zd and output %zd generated from AOT Inductor does not match ET runner's %zd. Exit.",
n_inputs,
n_outputs,
args.size())
// NOTE: ExecuTorch tensors are always on CPU/host memory
// We need to create GPU copies for CUDA kernel execution
std::vector<AOTITensorHandle> gpu_inputs(
n_inputs); // GPU copies for kernel execution
std::vector<AOTITensorHandle> gpu_outputs(
n_outputs); // GPU tensors for kernel output
// Process input tensors: ExecuTorch provides CPU tensors, create GPU
// copies
for (int i = 0; i < n_inputs; i++) {
// Get tensor dimensions and properties from ExecuTorch CPU tensor
auto cpu_tensor = &(args[i]->toTensor());
auto sizes = cpu_tensor->sizes();
auto scalar_type = cpu_tensor->scalar_type();
// Create GPU tensor with same shape
std::vector<int64_t> sizes_vec(sizes.begin(), sizes.end());
AOTITensorHandle gpu_input_handle;
Error create_err = aoti_torch_empty_strided(
sizes_vec.size(),
sizes_vec.data(),
nullptr, // use default strides
static_cast<int32_t>(scalar_type),
1, // device_type = cuda
0, // device_index = 0
&gpu_input_handle);
ET_CHECK_OR_RETURN_ERROR(
create_err == Error::Ok,
Internal,
"Failed to create GPU tensor for input %d",
i);
gpu_inputs[i] = gpu_input_handle;
// Copy data from CPU to GPU
ET_CHECK_OR_RETURN_ERROR(
aoti_torch_copy_(gpu_inputs[i], cpu_tensor, 0) == Error::Ok,
Internal,
"Failed to copy input %d from CPU to GPU",
i);
}
// Process output tensors: create GPU counterparts for ExecuTorch CPU
// tensors
for (int i = 0; i < n_outputs; i++) {
// Get output tensor dimensions from ExecuTorch CPU tensor
auto cpu_output_tensor = &(args[i + n_inputs]->toTensor());
auto sizes = cpu_output_tensor->sizes();
auto scalar_type = cpu_output_tensor->scalar_type();
// Create GPU tensor with same shape for kernel output
std::vector<int64_t> sizes_vec(sizes.begin(), sizes.end());
AOTITensorHandle gpu_output_handle;
Error create_err = aoti_torch_empty_strided(
sizes_vec.size(),
sizes_vec.data(),
nullptr, // use default strides
static_cast<int32_t>(scalar_type),
1, // device_type = cuda
0, // device_index = 0
&gpu_output_handle);
ET_CHECK_OR_RETURN_ERROR(
create_err == Error::Ok,
Internal,
"Failed to create GPU tensor for output %d",
i);
gpu_outputs[i] = gpu_output_handle;
}
// Run AOTI container with GPU tensors
AOTIRuntimeError error = handle->run(
handle->container_handle,
gpu_inputs.data(), // Use GPU input tensors
n_inputs,
gpu_outputs.data(), // Use GPU output tensors
n_outputs,
handle->cuda_stream, // Pass the actual CUDA stream
nullptr); // proxy_executor_handle can remain nullptr
ET_CHECK_OR_RETURN_ERROR(
error == Error::Ok,
Internal,
"AOTInductorModelContainerRun failed with error code %d",
error);
// Copy GPU output results back to CPU output tensors
for (int i = 0; i < n_outputs; i++) {
auto cpu_output_tensor = &(args[i + n_inputs]->toTensor());
// For DYNAMIC_BOUND tensors we try to resize
ET_CHECK_OK_OR_RETURN_ERROR(
resize_tensor(*cpu_output_tensor, gpu_outputs[i]->sizes()),
"Error resizing tensor at output index %d",
i);
ET_CHECK_OK_OR_RETURN_ERROR(
aoti_torch_copy_(cpu_output_tensor, gpu_outputs[i], 0),
"Failed to copy GPU output %d back to CPU",
i);
}
return Error::Ok;
}
void destroy(DelegateHandle* handle_) const override {
if (handle_ == nullptr) {
return;
}
AOTIDelegateHandle* handle = (AOTIDelegateHandle*)handle_;
// Destroy the CUDA stream if it exists
if (handle->cuda_stream != nullptr) {
cudaStream_t cuda_stream = static_cast<cudaStream_t>(handle->cuda_stream);
cudaError_t stream_err = cudaStreamDestroy(cuda_stream);
ET_CHECK_OR_LOG_ERROR(
stream_err == cudaSuccess,
"Failed to destroy CUDA stream: %s",
cudaGetErrorString(stream_err));
handle->cuda_stream = nullptr;
}
// NOTE: AOTInductorModelContainerDelete does not work correctly with
// multiple .so files. Deleting one container frees shared resources,
// which causes segmentation faults when attempting to delete other
// containers. As a workaround, we skip explicit container deletion
// and defer cleanup to the OS.
// TODO(gasoonjia): Find a proper solution for safe container deletion.
// AOTInductorModelContainerDelete(handle->container_handle);
// Now close the shared library
auto err = Error::Ok;
if (handle->so_handle != nullptr) {
err = close_library(handle->so_handle);
}
// Remove the temporary shared library file
if (!handle->so_path.empty()) {
std::error_code remove_error;
std::filesystem::remove(handle->so_path, remove_error);
ET_CHECK_OR_LOG_ERROR(
!remove_error,
"Failed to remove temporary shared library %s: %s",
handle->so_path.c_str(),
remove_error.message().c_str());
}
delete handle;
clear_all_tensors();
}
};
} // namespace executorch::backends::cuda
namespace executorch::backends {
namespace {
auto cls = cuda::CudaBackend();
executorch::runtime::Backend backend{"CudaBackend", &cls};
static executorch::runtime::Error success_with_compiler =
register_backend(backend);
} // namespace
} // namespace executorch::backends