|
17 | 17 | #include <executorch/backends/aoti/slim/c10/core/Device.h> |
18 | 18 | #include <executorch/backends/aoti/slim/core/slim_tensor.h> |
19 | 19 | #include <executorch/backends/aoti/slim/core/storage.h> |
| 20 | +#include <executorch/backends/aoti/slim/factory/from_blob.h> |
| 21 | +#include <executorch/backends/aoti/slim/util/array_ref_util.h> |
20 | 22 |
|
21 | 23 | namespace executorch::backends::cuda { |
22 | 24 |
|
@@ -314,4 +316,42 @@ inline void delete_slimtensor_vector( |
314 | 316 | tensors.clear(); |
315 | 317 | } |
316 | 318 |
|
| 319 | +/** |
| 320 | + * Creates a non-owning SlimTensor that wraps an external data pointer, using |
| 321 | + * the shape, strides and dtype of the given ETensor as metadata. |
| 322 | + * |
| 323 | + * Common helper for the CUDA backend, where we frequently need to create a |
| 324 | + * SlimTensor view of memory (e.g., a GPU buffer) that mirrors the layout of a |
| 325 | + * CPU/GPU ETensor. The returned tensor does NOT own the data; the caller must |
| 326 | + * keep it alive for the SlimTensor's lifetime. |
| 327 | + * |
| 328 | + * @param data_ptr Pointer to memory the SlimTensor should reference. |
| 329 | + * @param etensor ETensor whose sizes/strides/dtype define the SlimTensor's |
| 330 | + * metadata. |
| 331 | + * @param device Device where data_ptr resides (defaults to the default |
| 332 | + * CUDA device). |
| 333 | + * @return A heap-allocated SlimTensor; the caller takes ownership. |
| 334 | + */ |
| 335 | +inline executorch::backends::aoti::slim::SlimTensor* |
| 336 | +make_slimtensor_from_blob_with_etensor_metadata( |
| 337 | + void* data_ptr, |
| 338 | + const executorch::runtime::etensor::Tensor* etensor, |
| 339 | + const executorch::backends::aoti::slim::c10::Device& device = |
| 340 | + executorch::backends::aoti::slim::DEFAULT_CUDA_DEVICE) { |
| 341 | + auto sizes = etensor->sizes(); |
| 342 | + auto strides = etensor->strides(); |
| 343 | + std::vector<int64_t> sizes_vec(sizes.begin(), sizes.end()); |
| 344 | + std::vector<int64_t> strides_vec(strides.begin(), strides.end()); |
| 345 | + |
| 346 | + return new executorch::backends::aoti::slim::SlimTensor( |
| 347 | + executorch::backends::aoti::slim::from_blob( |
| 348 | + data_ptr, |
| 349 | + executorch::backends::aoti::slim::makeArrayRef(sizes_vec), |
| 350 | + executorch::backends::aoti::slim::makeArrayRef(strides_vec), |
| 351 | + static_cast<executorch::backends::aoti::slim::c10::ScalarType>( |
| 352 | + etensor->scalar_type()), |
| 353 | + device, |
| 354 | + /*storage_offset=*/0)); |
| 355 | +} |
| 356 | + |
317 | 357 | } // namespace executorch::backends::cuda |
0 commit comments