|  | 
|  | 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/runtime/platform/log.h> | 
|  | 11 | +#include <cstdint> | 
|  | 12 | + | 
|  | 13 | +namespace executorch { | 
|  | 14 | +namespace backends { | 
|  | 15 | +namespace aoti { | 
|  | 16 | + | 
|  | 17 | +namespace internal { | 
|  | 18 | + | 
|  | 19 | +// Global storage for tensor metadata | 
|  | 20 | +std::unordered_map<Tensor*, std::vector<int64_t>> tensor_to_sizes; | 
|  | 21 | +std::unordered_map<Tensor*, std::vector<int64_t>> tensor_to_strides; | 
|  | 22 | +} // namespace internal | 
|  | 23 | + | 
|  | 24 | +extern "C" { | 
|  | 25 | + | 
|  | 26 | +// Autograd mode functions | 
|  | 27 | +int32_t aoti_torch_grad_mode_is_enabled() { | 
|  | 28 | +  // No autograd ever | 
|  | 29 | +  return false; | 
|  | 30 | +} | 
|  | 31 | + | 
|  | 32 | +void aoti_torch_grad_mode_set_enabled(bool enabled) { | 
|  | 33 | +  if (enabled) { | 
|  | 34 | +    throw std::runtime_error("Cannot enable autograd"); | 
|  | 35 | +  } | 
|  | 36 | +} | 
|  | 37 | + | 
|  | 38 | +// Tensor attribute operations | 
|  | 39 | +AOTITorchError aoti_torch_get_data_ptr( | 
|  | 40 | +    AOTITensorHandle tensor, | 
|  | 41 | +    void** ret_data_ptr) { | 
|  | 42 | +  *ret_data_ptr = tensor->mutable_data_ptr(); | 
|  | 43 | +  return Error::Ok; | 
|  | 44 | +} | 
|  | 45 | + | 
|  | 46 | +AOTITorchError aoti_torch_get_storage_offset( | 
|  | 47 | +    AOTITensorHandle tensor, | 
|  | 48 | +    int64_t* ret_storage_offset) { | 
|  | 49 | +  // Storage offset is always 0 in ET | 
|  | 50 | +  *ret_storage_offset = 0; | 
|  | 51 | + | 
|  | 52 | +  return Error::Ok; | 
|  | 53 | +} | 
|  | 54 | + | 
|  | 55 | +AOTITorchError aoti_torch_get_strides( | 
|  | 56 | +    AOTITensorHandle tensor, | 
|  | 57 | +    int64_t** ret_strides) { | 
|  | 58 | +  auto it = internal::tensor_to_strides.find(tensor); | 
|  | 59 | +  if (it == internal::tensor_to_strides.end()) { | 
|  | 60 | +    std::vector<int64_t> strides(tensor->dim()); | 
|  | 61 | +    auto tensor_strides = tensor->strides(); | 
|  | 62 | +    for (int i = 0; i < tensor->dim(); i++) { | 
|  | 63 | +      strides[i] = tensor_strides[i]; | 
|  | 64 | +    } | 
|  | 65 | +    it = internal::tensor_to_strides.emplace(tensor, std::move(strides)).first; | 
|  | 66 | +  } | 
|  | 67 | + | 
|  | 68 | +  // For 0D tensors, data() returns nullptr on empty vectors, but we need to | 
|  | 69 | +  // return a valid pointer | 
|  | 70 | +  if (it->second.empty()) { | 
|  | 71 | +    static int64_t empty_strides_placeholder = 0; | 
|  | 72 | +    *ret_strides = &empty_strides_placeholder; | 
|  | 73 | +  } else { | 
|  | 74 | +    *ret_strides = it->second.data(); | 
|  | 75 | +  } | 
|  | 76 | + | 
|  | 77 | +  return Error::Ok; | 
|  | 78 | +} | 
|  | 79 | + | 
|  | 80 | +AOTITorchError aoti_torch_get_dtype( | 
|  | 81 | +    AOTITensorHandle tensor, | 
|  | 82 | +    int32_t* ret_dtype) { | 
|  | 83 | +  *ret_dtype = static_cast<int32_t>(tensor->scalar_type()); | 
|  | 84 | + | 
|  | 85 | +  return Error::Ok; | 
|  | 86 | +} | 
|  | 87 | + | 
|  | 88 | +AOTITorchError aoti_torch_get_sizes( | 
|  | 89 | +    AOTITensorHandle tensor, | 
|  | 90 | +    int64_t** ret_sizes) { | 
|  | 91 | +  auto it = internal::tensor_to_sizes.find(tensor); | 
|  | 92 | +  if (it == internal::tensor_to_sizes.end()) { | 
|  | 93 | +    std::vector<int64_t> sizes(tensor->dim()); | 
|  | 94 | +    auto tensor_sizes = tensor->sizes(); | 
|  | 95 | +    for (int i = 0; i < tensor->dim(); i++) { | 
|  | 96 | +      sizes[i] = tensor_sizes[i]; | 
|  | 97 | +    } | 
|  | 98 | +    it = internal::tensor_to_sizes.emplace(tensor, std::move(sizes)).first; | 
|  | 99 | +  } | 
|  | 100 | + | 
|  | 101 | +  // For 0D tensors, data() returns nullptr on empty vectors, but we need to | 
|  | 102 | +  // return a valid pointer | 
|  | 103 | +  if (it->second.empty()) { | 
|  | 104 | +    static int64_t empty_sizes_placeholder = 0; | 
|  | 105 | +    *ret_sizes = &empty_sizes_placeholder; | 
|  | 106 | +  } else { | 
|  | 107 | +    *ret_sizes = it->second.data(); | 
|  | 108 | +  } | 
|  | 109 | + | 
|  | 110 | +  return Error::Ok; | 
|  | 111 | +} | 
|  | 112 | + | 
|  | 113 | +AOTITorchError aoti_torch_get_storage_size( | 
|  | 114 | +    AOTITensorHandle tensor, | 
|  | 115 | +    int64_t* ret_size) { | 
|  | 116 | +  return Error::NotSupported; | 
|  | 117 | +} | 
|  | 118 | + | 
|  | 119 | +AOTITorchError aoti_torch_get_device_index( | 
|  | 120 | +    AOTITensorHandle tensor, | 
|  | 121 | +    int32_t* ret_device_index) { | 
|  | 122 | +  // Let's assume all tensors AOTI using are on CUDA:0 | 
|  | 123 | +  *ret_device_index = 0; | 
|  | 124 | +  return Error::Ok; | 
|  | 125 | +} | 
|  | 126 | + | 
|  | 127 | +AOTITorchError aoti_torch_get_dim(AOTITensorHandle tensor, int64_t* ret_dim) { | 
|  | 128 | +  *ret_dim = static_cast<int64_t>(tensor->dim()); | 
|  | 129 | +  return Error::Ok; | 
|  | 130 | +} | 
|  | 131 | + | 
|  | 132 | +// Device and layout utility functions | 
|  | 133 | +int32_t aoti_torch_device_type_cpu() { | 
|  | 134 | +  // Let's say cpu is 0 for ET as well | 
|  | 135 | +  return 0; | 
|  | 136 | +} | 
|  | 137 | + | 
|  | 138 | +__attribute__((__visibility__("default"))) int32_t aoti_torch_layout_strided() { | 
|  | 139 | +  // ET only support strided layout, the return value will always be 0, a.k.a | 
|  | 140 | +  // at::Layout::Strided; | 
|  | 141 | +  return 0; | 
|  | 142 | +} | 
|  | 143 | + | 
|  | 144 | +// Dtype constants - these return the PyTorch dtype codes | 
|  | 145 | +// Currently only float32 is supported, but using robust enum-based approach | 
|  | 146 | +__attribute__((__visibility__("default"))) int32_t aoti_torch_dtype_float32() { | 
|  | 147 | +  return 6; // PyTorch's float32 dtype code | 
|  | 148 | +} | 
|  | 149 | + | 
|  | 150 | +// Cleanup functions | 
|  | 151 | +void cleanup_tensor_metadata() { | 
|  | 152 | +  internal::tensor_to_sizes.clear(); | 
|  | 153 | +  internal::tensor_to_strides.clear(); | 
|  | 154 | +} | 
|  | 155 | + | 
|  | 156 | +} // extern "C" | 
|  | 157 | + | 
|  | 158 | +} // namespace aoti | 
|  | 159 | +} // namespace backends | 
|  | 160 | +} // namespace executorch | 
0 commit comments