Skip to content
14 changes: 14 additions & 0 deletions backends/cuda/runtime/shims/memory_slim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ AOTITorchError aoti_torch_empty_strided(
return Error::Ok;
}

AOTITorchError aoti_torch_delete_tensor_object(Tensor* tensor) {
ET_CHECK_OR_RETURN_ERROR(
tensor != nullptr,
InvalidArgument,
"aoti_torch_delete_tensor_object: tensor is null");

// SlimTensor uses SharedPtr for storage, so simply deleting the tensor
// will automatically handle reference counting and free the underlying
// storage when no more references exist.
delete tensor;

return Error::Ok;
}

} // extern "C"

} // namespace executorch::backends::cuda
12 changes: 12 additions & 0 deletions backends/cuda/runtime/shims/memory_slim.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ AOTI_SHIM_EXPORT AOTITorchError aoti_torch_empty_strided(
int32_t device_index,
Tensor** ret_new_tensor);

/**
* Deletes a tensor object and frees associated resources.
*
* For SlimTensor, the underlying storage uses SharedPtr-based reference
* counting. When the last tensor referencing the storage is deleted,
* the memory is automatically freed.
*
* @param tensor Pointer to the tensor to delete (must not be null)
* @return AOTITorchError error code (Error::Ok on success)
*/
AOTI_SHIM_EXPORT AOTITorchError aoti_torch_delete_tensor_object(Tensor* tensor);

} // extern "C"

} // namespace executorch::backends::cuda
1 change: 1 addition & 0 deletions backends/cuda/runtime/shims/tests/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ def define_common_targets():
# SlimTensor-based shim tests
cuda_shim_slim_cpp_unittest("aoti_torch_empty_strided")
cuda_shim_slim_cpp_unittest("aoti_torch_create_tensor_from_blob_v2")
cuda_shim_slim_cpp_unittest("aoti_torch_delete_tensor_object")
Loading
Loading