Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions backends/cuda/runtime/shims/memory_slim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,67 @@ AOTITorchError aoti_torch_new_tensor_handle(
return Error::Ok;
}

AOTITorchError aoti_torch__reinterpret_tensor(
Tensor* self,
int64_t ndim,
const int64_t* sizes_ptr,
const int64_t* strides_ptr,
int64_t storage_offset,
Tensor** ret_new_tensor) {
ET_CHECK_OR_RETURN_ERROR(
self != nullptr,
InvalidArgument,
"aoti_torch__reinterpret_tensor: self is null");

ET_CHECK_OR_RETURN_ERROR(
ret_new_tensor != nullptr,
InvalidArgument,
"aoti_torch__reinterpret_tensor: ret_new_tensor is null");

ET_CHECK_OR_RETURN_ERROR(
ndim >= 0,
InvalidArgument,
"aoti_torch__reinterpret_tensor: ndim must be non-negative, got %lld",
static_cast<long long>(ndim));

ET_CHECK_OR_RETURN_ERROR(
!(sizes_ptr == nullptr && ndim > 0),
InvalidArgument,
"aoti_torch__reinterpret_tensor: sizes_ptr is null but ndim > 0");

IntArrayRef sizes(sizes_ptr, static_cast<size_t>(ndim));
IntArrayRef strides(strides_ptr, static_cast<size_t>(ndim));

// Create a new tensor view using as_strided. This creates a tensor that
// shares the same underlying storage but with different sizes, strides,
// and storage offset. SlimTensor::as_strided() handles this via copy
// constructor which shares the SharedPtr<Storage>.
*ret_new_tensor =
new Tensor(self->as_strided(sizes, strides, storage_offset));

return Error::Ok;
}

AOTITorchError
aoti_torch_copy_(Tensor* self, Tensor* src, int32_t non_blocking) {
(void)non_blocking; // SlimTensor::copy_() is always synchronous for now

ET_CHECK_OR_RETURN_ERROR(
self != nullptr, InvalidArgument, "aoti_torch_copy_: self is null");

ET_CHECK_OR_RETURN_ERROR(
src != nullptr, InvalidArgument, "aoti_torch_copy_: src is null");

// SlimTensor::copy_() handles:
// - Same numel validation
// - Same dtype validation
// - CPU-CPU, CPU-CUDA, CUDA-CPU, CUDA-CUDA copies
// - Contiguous fast path and non-contiguous element-wise copy
self->copy_(*src);

return Error::Ok;
}

} // extern "C"

} // namespace executorch::backends::cuda
37 changes: 37 additions & 0 deletions backends/cuda/runtime/shims/memory_slim.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,43 @@ AOTI_SHIM_EXPORT AOTITorchError aoti_torch_delete_tensor_object(Tensor* tensor);
AOTI_SHIM_EXPORT AOTITorchError
aoti_torch_new_tensor_handle(Tensor* orig_handle, Tensor** new_handle);

/**
* Creates a reinterpreted view of a tensor with new sizes, strides, and offset.
*
* This is equivalent to torch.as_strided() - it creates a new tensor that
* shares the same underlying storage but with different view parameters.
*
* @param self Original tensor to reinterpret (must not be null)
* @param ndim Number of dimensions for the new view
* @param sizes_ptr Pointer to array of dimension sizes
* @param strides_ptr Pointer to array of strides for each dimension
* @param storage_offset Storage offset in number of elements
* @param ret_new_tensor Output parameter for the reinterpreted tensor view
* @return AOTITorchError error code (Error::Ok on success)
*/
AOTI_SHIM_EXPORT AOTITorchError aoti_torch__reinterpret_tensor(
Tensor* self,
int64_t ndim,
const int64_t* sizes_ptr,
const int64_t* strides_ptr,
int64_t storage_offset,
Tensor** ret_new_tensor);

/**
* Copies data from source tensor to destination tensor.
*
* Handles all device combinations (CPU-CPU, CPU-CUDA, CUDA-CPU, CUDA-CUDA)
* and supports tensors with different strides. The destination tensor must
* already be allocated with sufficient storage.
*
* @param self Destination tensor (must not be null)
* @param src Source tensor to copy from (must not be null)
* @param non_blocking If true, the copy may be asynchronous (currently ignored)
* @return AOTITorchError error code (Error::Ok on success)
*/
AOTI_SHIM_EXPORT AOTITorchError
aoti_torch_copy_(Tensor* self, Tensor* src, int32_t non_blocking);

} // extern "C"

} // namespace executorch::backends::cuda
2 changes: 2 additions & 0 deletions backends/cuda/runtime/shims/tests/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ def define_common_targets():
cuda_shim_slim_cpp_unittest("aoti_torch_create_tensor_from_blob_v2")
cuda_shim_slim_cpp_unittest("aoti_torch_delete_tensor_object")
cuda_shim_slim_cpp_unittest("aoti_torch_new_tensor_handle")
cuda_shim_slim_cpp_unittest("aoti_torch__reinterpret_tensor")
cuda_shim_slim_cpp_unittest("aoti_torch_copy_")
Loading
Loading