|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/backends/aoti/common_shims.h> |
| 10 | +#include <executorch/backends/aoti/utils.h> |
| 11 | +#include <executorch/backends/cuda/runtime/shims/memory.h> |
| 12 | +#include <executorch/backends/cuda/runtime/shims/tensor_attribute.h> |
| 13 | +#include <executorch/backends/cuda/runtime/shims/utils.h> |
| 14 | +#include <executorch/runtime/platform/log.h> |
| 15 | +#include <cstdint> |
| 16 | +#include <cstdlib> // For posix_memalign |
| 17 | +#include <memory> |
| 18 | +#include <unordered_set> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +// CUDA error checking macro |
| 22 | +#define ET_CUDA_CHECK_OR_RETURN_ERROR(EXPR) \ |
| 23 | + do { \ |
| 24 | + const cudaError_t err = EXPR; \ |
| 25 | + if (err == cudaSuccess) { \ |
| 26 | + break; \ |
| 27 | + } \ |
| 28 | + ET_LOG( \ |
| 29 | + Error, \ |
| 30 | + "%s:%d CUDA error: %s", \ |
| 31 | + __FILE__, \ |
| 32 | + __LINE__, \ |
| 33 | + cudaGetErrorString(err)); \ |
| 34 | + return Error::Internal; \ |
| 35 | + } while (0) |
| 36 | + |
| 37 | +// Kernel launch check macro |
| 38 | +#define ET_CUDA_KERNEL_LAUNCH_CHECK_OR_RETURN_ERROR() \ |
| 39 | + ET_CUDA_CHECK_OR_RETURN_ERROR(cudaGetLastError()) |
| 40 | + |
| 41 | +namespace executorch { |
| 42 | +namespace backends { |
| 43 | +namespace cuda { |
| 44 | + |
| 45 | +using executorch::aten::SizesType; |
| 46 | +using executorch::aten::StridesType; |
| 47 | +using executorch::backends::aoti::dtype_to_element_size; |
| 48 | +using executorch::backends::aoti::dtype_to_scalar_type; |
| 49 | + |
| 50 | +// Global storage for tensors and their metadata |
| 51 | +std::unordered_set<std::shared_ptr<Tensor>> tensors; |
| 52 | + |
| 53 | +extern "C" { |
| 54 | + |
| 55 | +AOTITorchError aoti_torch_empty_strided( |
| 56 | + int64_t ndim, |
| 57 | + const int64_t* sizes_ptr, |
| 58 | + const int64_t* strides_ptr, |
| 59 | + int32_t dtype, |
| 60 | + int32_t device_type, |
| 61 | + int32_t device_index, |
| 62 | + Tensor** ret_new_tensor) { |
| 63 | + // Check that device_index is always 0 |
| 64 | + if (device_index != 0) { |
| 65 | + ET_LOG(Error, "device_index must be 0, got: %d", device_index); |
| 66 | + return Error::InvalidArgument; |
| 67 | + } |
| 68 | + |
| 69 | + // This requires us to reserve CUDA memory and put it into a ETensor |
| 70 | + void* ptr; |
| 71 | + int64_t numel = 1; |
| 72 | + for (int64_t i = 0; i < ndim; i++) { |
| 73 | + numel *= sizes_ptr[i]; |
| 74 | + } |
| 75 | + |
| 76 | + AOTITorchError dtype_error = validate_dtype(dtype); |
| 77 | + if (dtype_error != Error::Ok) { |
| 78 | + return dtype_error; |
| 79 | + } |
| 80 | + |
| 81 | + size_t element_size = dtype_to_element_size(dtype); |
| 82 | + if (element_size == 0) { |
| 83 | + ET_LOG(Error, "Invalid element size for dtype: %d", dtype); |
| 84 | + return Error::InvalidArgument; |
| 85 | + } |
| 86 | + int64_t nbytes = numel * element_size; |
| 87 | + |
| 88 | + if (device_type == 1) { // cuda |
| 89 | + ET_CUDA_CHECK_OR_RETURN_ERROR(cudaMallocManaged(&ptr, nbytes)); |
| 90 | + } else if (device_type == 0) { // cpu |
| 91 | + // Ensure 16-byte alignment for CPU memory to match CUDA requirements |
| 92 | + int result = posix_memalign(&ptr, 16, nbytes); |
| 93 | + if (result != 0) { |
| 94 | + ET_LOG(Error, "Failed to allocate aligned CPU memory"); |
| 95 | + return Error::MemoryAllocationFailed; |
| 96 | + } |
| 97 | + if (ptr == nullptr) { |
| 98 | + ET_LOG(Error, "Failed to call posix_memalign"); |
| 99 | + return Error::MemoryAllocationFailed; |
| 100 | + } |
| 101 | + } else { |
| 102 | + ET_LOG( |
| 103 | + Error, |
| 104 | + "Need to implement empty_strided for non-CUDA non-CPU device type %d", |
| 105 | + device_type); |
| 106 | + return Error::NotImplemented; |
| 107 | + } |
| 108 | + |
| 109 | + // ETensor sizes |
| 110 | + auto sizes = convert_sizes_to_vector(ndim, sizes_ptr); |
| 111 | + |
| 112 | + // ETensor strides |
| 113 | + auto strides = convert_strides_to_vector(ndim, sizes_ptr, strides_ptr); |
| 114 | + |
| 115 | + // ETensor creation with dynamic shape support for edge cases |
| 116 | + auto tensor = executorch::extension::from_blob( |
| 117 | + ptr, sizes, strides, dtype_to_scalar_type(dtype)); |
| 118 | + |
| 119 | + // Store the tensor so it doesn't get destroyed |
| 120 | + tensors.insert(tensor); |
| 121 | + *ret_new_tensor = tensor.get(); |
| 122 | + |
| 123 | + return Error::Ok; |
| 124 | +} |
| 125 | + |
| 126 | +// TODO(gasoonjia): reuse aoti_torch_delete_tensor_object to destory tensors |
| 127 | +void clear_all_tensors() { |
| 128 | + tensors.clear(); |
| 129 | +} |
| 130 | + |
| 131 | +} // extern "C" |
| 132 | + |
| 133 | +} // namespace cuda |
| 134 | +} // namespace backends |
| 135 | +} // namespace executorch |
0 commit comments