-
Notifications
You must be signed in to change notification settings - Fork 971
Expand file tree
/
Copy pathop__device_copy.cpp
More file actions
155 lines (135 loc) · 4.5 KB
/
op__device_copy.cpp
File metadata and controls
155 lines (135 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* 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/extension/kernel_util/make_boxed_from_unboxed_functor.h>
#include <executorch/runtime/core/device_allocator.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/kernel/kernel_includes.h>
namespace executorch::runtime::native {
using executorch::aten::Tensor;
using executorch::runtime::KernelRuntimeContext;
/**
* 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() == 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 != 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 = 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 != 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() == 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 = 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 executorch::runtime::native
EXECUTORCH_LIBRARY(
et_copy,
"_h2d_copy.out",
executorch::runtime::native::_h2d_copy_out);
EXECUTORCH_LIBRARY(
et_copy,
"_d2h_copy.out",
executorch::runtime::native::_d2h_copy_out);