Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9f5c14a
[ET Device Support] Schema changes: device info on Tensor and buffer-…
Gasoonjia Feb 18, 2026
af472d7
[ET Device Support] TensorImpl carries device info
Gasoonjia Feb 18, 2026
2b66985
[ET Device Support] DeviceAllocator interface and DeviceAllocatorRegi…
Gasoonjia Feb 18, 2026
d630230
Update base for Update on "[ET Device Support] DeviceAllocator interf…
Gasoonjia Feb 18, 2026
8127b3b
Update on "[ET Device Support] DeviceAllocator interface and DeviceAl…
Gasoonjia Feb 18, 2026
531dfb7
Update base for Update on "[ET Device Support] DeviceAllocator interf…
Gasoonjia Mar 11, 2026
76d7f72
Update on "[ET Device Support] DeviceAllocator interface and DeviceAl…
Gasoonjia Mar 11, 2026
f9e07cc
Update base for Update on "[ET Device Support] DeviceAllocator interf…
Gasoonjia Mar 19, 2026
ed81943
Update on "[ET Device Support] DeviceAllocator interface and DeviceAl…
Gasoonjia Mar 19, 2026
228bdeb
[ET Device Support] Add NonConstBufferDevice schema for per-buffer de…
Gasoonjia Mar 19, 2026
670bb21
Update base for Update on "[ET Device Support] Add NonConstBufferDevi…
Gasoonjia Mar 19, 2026
c2cb7ac
Update on "[ET Device Support] Add NonConstBufferDevice schema for pe…
Gasoonjia Mar 19, 2026
466a90c
Update base for Update on "[ET Device Support] Add NonConstBufferDevi…
Gasoonjia Mar 20, 2026
bd98755
Update on "[ET Device Support] Add NonConstBufferDevice schema for pe…
Gasoonjia Mar 20, 2026
747dbaa
[ET Device Support] Device-aware memory planning: separate buffers pe…
Gasoonjia Mar 20, 2026
1c84955
Update base for Update on "[ET Device Support] Device-aware memory pl…
Gasoonjia Mar 24, 2026
66c7d4e
Update on "[ET Device Support] Device-aware memory planning: separate…
Gasoonjia Mar 24, 2026
0829c5d
[ET Device Support] Emitter reads non_const_buffer_device from graph …
Gasoonjia Mar 24, 2026
9dc075b
[ET Device Support] DeviceMemoryBuffer RAII class for device memory l…
Gasoonjia Mar 24, 2026
1fdbbd1
[ET Device Support] MethodMeta: expose per-buffer device placement API
Gasoonjia Mar 24, 2026
6af3059
[ET Device Support] MemoryManager: add per-buffer device metadata
Gasoonjia Mar 24, 2026
c003658
[ET Device Support] Module: allocate device memory for planned buffers
Gasoonjia Mar 24, 2026
0fcfa83
[ET Device Support] CudaAllocator: device memory allocator for CUDA b…
Gasoonjia Mar 24, 2026
a332757
Update base for Update on "[ET Device Support] CudaAllocator: device …
Gasoonjia Apr 6, 2026
6c5df5d
Update on "[ET Device Support] CudaAllocator: device memory allocator…
Gasoonjia Apr 6, 2026
7ff22e2
[ET Device Support] Define AOT device copy ops registry
Gasoonjia Apr 6, 2026
d72bdfa
Update base for Update on "[ET Device Support] Define et_copy runtime…
Gasoonjia Apr 8, 2026
86f7edf
Update base for Update on "[ET Device Support] Define et_copy runtime…
Gasoonjia May 14, 2026
451049c
Update (base update)
Gasoonjia May 18, 2026
c61a90b
Update (base update)
Gasoonjia May 21, 2026
0ccb75e
Update (base update)
Gasoonjia May 21, 2026
c1dc5e4
Update (base update)
Gasoonjia May 22, 2026
ba9987a
Update (base update)
Gasoonjia May 27, 2026
38bd893
Update (base update)
Gasoonjia May 27, 2026
ae54cf6
[ET Device Support] Define et_copy runtime h2d and d2h copy ops (#18729)
Gasoonjia May 28, 2026
066fc4c
Merge branch 'main' into gh/gasoonjia/159/base
Gasoonjia May 28, 2026
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
24 changes: 24 additions & 0 deletions backends/cuda/runtime/shims/tests/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,27 @@ def define_common_targets():
cuda_shim_cpp_unittest("aoti_torch_new_tensor_handle")
cuda_shim_cpp_unittest("aoti_torch_item_bool")
cuda_shim_cpp_unittest("aoti_torch_assign_tensors_out")

cpp_unittest(
name = "test_op__device_copy",
srcs = ["test_op__device_copy.cpp"],
deps = [
"//executorch/backends/cuda/runtime:cuda_backend",
"//executorch/kernels/portable:generated_lib",
"//executorch/kernels/portable:generated_lib_headers",
"//executorch/kernels/portable/cpu:op__device_copy",
"//executorch/runtime/core:device_allocator",
"//executorch/runtime/core/exec_aten:lib",
"//executorch/runtime/core/portable_type:portable_type",
"//executorch/runtime/kernel:kernel_runtime_context",
"//executorch/runtime/platform:platform",
],
external_deps = [
("cuda", None, "cuda-lazy"),
],
preprocessor_flags = ["-DCUDA_AVAILABLE=1"],
keep_gpu_sections = True,
remote_execution = re_test_utils.remote_execution(
platform = "gpu-remote-execution",
),
)
195 changes: 195 additions & 0 deletions backends/cuda/runtime/shims/tests/test_op__device_copy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <cuda_runtime.h>
#include <executorch/kernels/portable/Functions.h>
#include <executorch/runtime/core/device_allocator.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/core/portable_type/tensor_impl.h>
#include <executorch/runtime/kernel/kernel_runtime_context.h>
#include <executorch/runtime/platform/runtime.h>
#include <gtest/gtest.h>

#if (defined(__has_feature) && __has_feature(address_sanitizer)) || \
defined(__SANITIZE_ADDRESS__)
#include <sanitizer/lsan_interface.h>
#define EXECUTORCH_CUDA_DEVICE_COPY_HAS_LSAN_INTERFACE 1
#else
#define EXECUTORCH_CUDA_DEVICE_COPY_HAS_LSAN_INTERFACE 0
#endif

#include <cstdint>
#include <memory>
#include <vector>

using executorch::aten::ScalarType;
using executorch::aten::Tensor;
using executorch::aten::TensorImpl;
using executorch::runtime::Error;
using executorch::runtime::get_device_allocator;
using executorch::runtime::KernelRuntimeContext;
using executorch::runtime::TensorShapeDynamism;
using executorch::runtime::etensor::DeviceIndex;
using executorch::runtime::etensor::DeviceType;

namespace {

struct CudaDeleter {
void operator()(void* ptr) const {
if (ptr != nullptr) {
cudaFree(ptr);
}
}
};

using CudaPtr = std::unique_ptr<void, CudaDeleter>;

CudaPtr allocate_cuda(size_t nbytes) {
void* ptr = nullptr;
const cudaError_t err = cudaMalloc(&ptr, nbytes);
EXPECT_EQ(err, cudaSuccess) << "cudaMalloc failed";
return CudaPtr(ptr);
}

bool is_cuda_available() {
#if EXECUTORCH_CUDA_DEVICE_COPY_HAS_LSAN_INTERFACE
__lsan_disable();
#endif
int device_count = 0;
const cudaError_t err = cudaGetDeviceCount(&device_count);
#if EXECUTORCH_CUDA_DEVICE_COPY_HAS_LSAN_INTERFACE
__lsan_enable();
#endif
return err == cudaSuccess && device_count > 0;
}

std::vector<float> copy_cuda_to_host(const void* device_ptr, size_t numel) {
std::vector<float> host(numel);
const cudaError_t err = cudaMemcpy(
host.data(), device_ptr, numel * sizeof(float), cudaMemcpyDeviceToHost);
EXPECT_EQ(err, cudaSuccess) << "cudaMemcpy D2H failed";
return host;
}

void copy_host_to_cuda(const std::vector<float>& host, void* device_ptr) {
const cudaError_t err = cudaMemcpy(
device_ptr,
host.data(),
host.size() * sizeof(float),
cudaMemcpyHostToDevice);
EXPECT_EQ(err, cudaSuccess) << "cudaMemcpy H2D failed";
}

class CudaDeviceCopyOpTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
executorch::runtime::runtime_init();
ASSERT_NE(get_device_allocator(DeviceType::CUDA), nullptr)
<< "Linking cuda_backend should auto-register the CUDA allocator";
}

void SetUp() override {
if (!is_cuda_available()) {
GTEST_SKIP() << "CUDA not available, skipping CUDA device copy op tests";
}
}

Tensor& op_h2d_copy_out(const Tensor& self, Tensor& out) {
return torch::executor::et_copy::_h2d_copy_outf(context_, self, out);
}

Tensor& op_d2h_copy_out(const Tensor& self, Tensor& out) {
return torch::executor::et_copy::_d2h_copy_outf(context_, self, out);
}

KernelRuntimeContext context_;
};

} // namespace

TEST_F(CudaDeviceCopyOpTest, H2dCopyUsesRegisteredCudaAllocator) {
std::vector<float> src_data = {1.0f, 2.0f, 3.0f, 4.0f};
auto device_data = allocate_cuda(src_data.size() * sizeof(float));
ASSERT_NE(device_data.get(), nullptr);

int32_t sizes[] = {static_cast<int32_t>(src_data.size())};
uint8_t dim_order[] = {0};
int32_t strides[] = {1};

TensorImpl src_impl(
ScalarType::Float,
1,
sizes,
src_data.data(),
dim_order,
strides,
TensorShapeDynamism::STATIC,
DeviceType::CPU,
0);
Tensor src(&src_impl);

TensorImpl dst_impl(
ScalarType::Float,
1,
sizes,
device_data.get(),
dim_order,
strides,
TensorShapeDynamism::STATIC,
DeviceType::CUDA,
0);
Tensor dst(&dst_impl);

Tensor& result = op_h2d_copy_out(src, dst);

EXPECT_EQ(context_.failure_state(), Error::Ok);
EXPECT_EQ(&result, &dst);
EXPECT_EQ(copy_cuda_to_host(device_data.get(), src_data.size()), src_data);
}

TEST_F(CudaDeviceCopyOpTest, D2hCopyUsesRegisteredCudaAllocator) {
const std::vector<float> expected = {5.0f, 6.0f, 7.0f, 8.0f};
auto device_data = allocate_cuda(expected.size() * sizeof(float));
ASSERT_NE(device_data.get(), nullptr);
copy_host_to_cuda(expected, device_data.get());

std::vector<float> dst_data(expected.size(), 0.0f);
int32_t sizes[] = {static_cast<int32_t>(expected.size())};
uint8_t dim_order[] = {0};
int32_t strides[] = {1};

TensorImpl src_impl(
ScalarType::Float,
1,
sizes,
device_data.get(),
dim_order,
strides,
TensorShapeDynamism::STATIC,
DeviceType::CUDA,
0);
Tensor src(&src_impl);

TensorImpl dst_impl(
ScalarType::Float,
1,
sizes,
dst_data.data(),
dim_order,
strides,
TensorShapeDynamism::STATIC,
DeviceType::CPU,
0);
Tensor dst(&dst_impl);

Tensor& result = op_d2h_copy_out(src, dst);

EXPECT_EQ(context_.failure_state(), Error::Ok);
EXPECT_EQ(&result, &dst);
EXPECT_EQ(dst_data, expected);
}
154 changes: 154 additions & 0 deletions kernels/portable/cpu/op__device_copy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* Runtime kernels for et_copy._h2d_copy and et_copy._d2h_copy ops.
*
* These ops transfer tensor data between CPU and device memory using
* the DeviceAllocator interface. The device type is inferred from the
* tensor metadata (out.device_type() for H2D, self.device_type() for D2H),
* which was set during AOT serialization by PropagateDevicePass.
*/

#include <executorch/runtime/core/device_allocator.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/kernel/kernel_includes.h>

namespace torch {
namespace executor {
namespace native {

using Tensor = executorch::aten::Tensor;
using DeviceAllocator = executorch::runtime::DeviceAllocator;
using Error = executorch::runtime::Error;

/**
* Copies tensor data from host (CPU) memory to device memory.
*
* self: source tensor on CPU
* out: destination tensor on device (memory-planned by runtime)
*
* The device type and index are inferred from out's TensorImpl metadata.
*/
Tensor&
_h2d_copy_out(KernelRuntimeContext& ctx, const Tensor& self, Tensor& out) {
auto device_type = out.unsafeGetTensorImpl()->device_type();
auto device_index = out.unsafeGetTensorImpl()->device_index();

ET_KERNEL_CHECK_MSG(
ctx,
self.unsafeGetTensorImpl()->device_type() ==
executorch::runtime::etensor::DeviceType::CPU,
InvalidArgument,
out,
"_h2d_copy: source tensor must be on CPU, got device_type=%d",
static_cast<int>(self.unsafeGetTensorImpl()->device_type()));

ET_KERNEL_CHECK_MSG(
ctx,
device_type != executorch::runtime::etensor::DeviceType::CPU,
InvalidArgument,
out,
"_h2d_copy: destination tensor must be on a non-CPU device");

auto nbytes = self.nbytes();
ET_KERNEL_CHECK_MSG(
ctx,
nbytes == out.nbytes(),
InvalidArgument,
out,
"_h2d_copy: size mismatch: self.nbytes()=%zu, out.nbytes()=%zu",
nbytes,
out.nbytes());

DeviceAllocator* allocator =
executorch::runtime::get_device_allocator(device_type);
ET_KERNEL_CHECK_MSG(
ctx,
allocator != nullptr,
NotFound,
out,
"_h2d_copy: no device allocator registered for device_type=%d",
static_cast<int>(device_type));

Error err = allocator->copy_host_to_device(
out.mutable_data_ptr(), self.const_data_ptr(), nbytes, device_index);
ET_KERNEL_CHECK_MSG(
ctx,
err == Error::Ok,
Internal,
out,
"_h2d_copy: copy_host_to_device failed");

return out;
}

/**
* Copies tensor data from device memory to host (CPU) memory.
*
* self: source tensor on device
* out: destination tensor on CPU (memory-planned by runtime)
*
* The device type and index are inferred from self's TensorImpl metadata.
*/
Tensor&
_d2h_copy_out(KernelRuntimeContext& ctx, const Tensor& self, Tensor& out) {
auto device_type = self.unsafeGetTensorImpl()->device_type();
auto device_index = self.unsafeGetTensorImpl()->device_index();

ET_KERNEL_CHECK_MSG(
ctx,
device_type != executorch::runtime::etensor::DeviceType::CPU,
InvalidArgument,
out,
"_d2h_copy: source tensor must be on a non-CPU device");

ET_KERNEL_CHECK_MSG(
ctx,
out.unsafeGetTensorImpl()->device_type() ==
executorch::runtime::etensor::DeviceType::CPU,
InvalidArgument,
out,
"_d2h_copy: destination tensor must be on CPU, got device_type=%d",
static_cast<int>(out.unsafeGetTensorImpl()->device_type()));

auto nbytes = self.nbytes();
ET_KERNEL_CHECK_MSG(
ctx,
nbytes == out.nbytes(),
InvalidArgument,
out,
"_d2h_copy: size mismatch: self.nbytes()=%zu, out.nbytes()=%zu",
nbytes,
out.nbytes());

DeviceAllocator* allocator =
executorch::runtime::get_device_allocator(device_type);
ET_KERNEL_CHECK_MSG(
ctx,
allocator != nullptr,
NotFound,
out,
"_d2h_copy: no device allocator registered for device_type=%d",
static_cast<int>(device_type));

Error err = allocator->copy_device_to_host(
out.mutable_data_ptr(), self.const_data_ptr(), nbytes, device_index);
ET_KERNEL_CHECK_MSG(
ctx,
err == Error::Ok,
Internal,
out,
"_d2h_copy: copy_device_to_host failed");

return out;
}

} // namespace native
} // namespace executor
} // namespace torch
10 changes: 10 additions & 0 deletions kernels/portable/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,16 @@
- arg_meta: null
kernel_name: torch::executor::zeros_out

- func: et_copy::_h2d_copy.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)
kernels:
- arg_meta: null
kernel_name: torch::executor::_h2d_copy_out

- func: et_copy::_d2h_copy.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)
kernels:
- arg_meta: null
kernel_name: torch::executor::_d2h_copy_out

- func: dim_order_ops::_empty_dim_order.out(int[] size, *, int[]? dim_order=None, Tensor(a!) out) -> Tensor(a!)
kernels:
- arg_meta: null
Expand Down
Loading
Loading