Skip to content

Commit 3b0aa75

Browse files
authored
[CustomDevice] register Copy for custom device (#44200)
* [CustomDevice] register Copy for custom device * [CustomDevice] register Copy for custom device * [CustomDevice] register Copy for custom device * merge and add uts * merge and add uts * fix for blocking and unittests coverage
1 parent db864f0 commit 3b0aa75

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

paddle/phi/core/tensor_utils.cc

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,9 @@ void Copy(const Context& dev_ctx,
200200
paddle::memory::Copy(
201201
dst_cuda_pinned_place, dst_ptr, src_gpu_place, src_ptr, size, stream);
202202
#endif
203-
}
204203
#ifdef PADDLE_WITH_XPU
205-
else if (paddle::platform::is_xpu_place(src_place) && // NOLINT
206-
paddle::platform::is_cpu_place(dst_place)) {
204+
} else if (paddle::platform::is_xpu_place(src_place) && // NOLINT
205+
paddle::platform::is_cpu_place(dst_place)) {
207206
paddle::memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size);
208207
} else if (paddle::platform::is_cpu_place(src_place) &&
209208
paddle::platform::is_xpu_place(dst_place)) {
@@ -216,11 +215,40 @@ void Copy(const Context& dev_ctx,
216215
return;
217216
}
218217
paddle::memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size);
218+
#endif
219+
#ifdef PADDLE_WITH_CUSTOM_DEVICE
220+
} else if (paddle::platform::is_custom_place(src_place) && // NOLINT
221+
paddle::platform::is_cpu_place(dst_place)) {
222+
auto stream =
223+
blocking
224+
? nullptr
225+
: reinterpret_cast<const paddle::platform::CustomDeviceContext&>(
226+
dev_ctx)
227+
.stream();
228+
paddle::memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size, stream);
229+
} else if (paddle::platform::is_cpu_place(src_place) && // NOLINT
230+
paddle::platform::is_custom_place(dst_place)) {
231+
auto stream =
232+
blocking
233+
? nullptr
234+
: reinterpret_cast<const paddle::platform::CustomDeviceContext&>(
235+
dev_ctx)
236+
.stream();
237+
paddle::memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size, stream);
238+
} else if (paddle::platform::is_custom_place(src_place) && // NOLINT
239+
paddle::platform::is_custom_place(dst_place)) {
240+
auto stream =
241+
blocking
242+
? nullptr
243+
: reinterpret_cast<const paddle::platform::CustomDeviceContext&>(
244+
dev_ctx)
245+
.stream();
246+
paddle::memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size, stream);
247+
#endif
219248
} else {
220249
PADDLE_THROW(phi::errors::Unimplemented(
221250
"Copy from %s to %s is not supported.", src_place, dst_place));
222251
}
223-
#endif
224252
}
225253

226254
template <typename Context>
@@ -363,4 +391,11 @@ template void Copy(const XPUContext& dev_ctx,
363391
DenseTensor* dst);
364392
#endif
365393

394+
#ifdef PADDLE_WITH_CUSTOM_DEVICE
395+
template void Copy(const CustomContext& dev_ctx,
396+
const DenseTensor& src,
397+
Place dst_place,
398+
bool blocking,
399+
DenseTensor* dst);
400+
#endif
366401
} // namespace phi

python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_custom_device(self):
3939
self._test_custom_device_dataloader()
4040
self._test_custom_device_mnist()
4141
self._test_eager_backward_api()
42+
self._test_eager_copy_to()
4243
self._test_custom_device_dataloader()
4344
self._test_custom_device_mnist()
4445

@@ -133,6 +134,32 @@ def _test_eager_backward_api(self):
133134

134135
self.assertTrue(x_tensor.grad.place.is_custom_place())
135136

137+
def _test_eager_copy_to(self):
138+
import paddle
139+
x = np.random.random([2, 2]).astype("float32")
140+
# cpu -> custom
141+
cpu_tensor = paddle.to_tensor(x,
142+
dtype='float32',
143+
place=paddle.CPUPlace())
144+
custom_cpu_tensor = cpu_tensor._copy_to(
145+
paddle.CustomPlace('custom_cpu', 0), True)
146+
self.assertTrue(np.array_equal(custom_cpu_tensor, x))
147+
self.assertTrue(custom_cpu_tensor.place.is_custom_place())
148+
# custom -> custom
149+
another_custom_cpu_tensor = custom_cpu_tensor._copy_to(
150+
paddle.CustomPlace('custom_cpu', 0), True)
151+
self.assertTrue(np.array_equal(another_custom_cpu_tensor, x))
152+
self.assertTrue(another_custom_cpu_tensor.place.is_custom_place())
153+
# custom -> cpu
154+
another_cpu_tensor = custom_cpu_tensor._copy_to(paddle.CPUPlace(), True)
155+
self.assertTrue(np.array_equal(another_cpu_tensor, x))
156+
self.assertTrue(another_cpu_tensor.place.is_cpu_place())
157+
# custom -> custom self
158+
another_custom_cpu_tensor = another_custom_cpu_tensor._copy_to(
159+
paddle.CustomPlace('custom_cpu', 0), True)
160+
self.assertTrue(np.array_equal(another_custom_cpu_tensor, x))
161+
self.assertTrue(another_custom_cpu_tensor.place.is_custom_place())
162+
136163
def tearDown(self):
137164
del os.environ['CUSTOM_DEVICE_ROOT']
138165

0 commit comments

Comments
 (0)