From 8251ec9ac24e5252aabc5580effa4f4d64847293 Mon Sep 17 00:00:00 2001 From: "Yu, Guangye" <106960996+guangyey@users.noreply.github.com> Date: Thu, 19 Dec 2024 18:51:34 -0800 Subject: [PATCH] Add lazy init to empty/resize (#1183) # Motivation As titled. # Additional Context Align to CUDA. (cherry picked from commit cd0873c40eddba4c675093898d0447b8c4e702d3) --- src/ATen/native/xpu/sycl/ResizeKernel.cpp | 3 ++- src/ATen/xpu/EmptyTensor.cpp | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ATen/native/xpu/sycl/ResizeKernel.cpp b/src/ATen/native/xpu/sycl/ResizeKernel.cpp index 237a1c213..f1ee7f944 100644 --- a/src/ATen/native/xpu/sycl/ResizeKernel.cpp +++ b/src/ATen/native/xpu/sycl/ResizeKernel.cpp @@ -25,8 +25,9 @@ void resize_bytes_xpu(StorageImpl* storage, size_t size_bytes) { c10::xpu::XPUGuard guard(device.index()); at::DataPtr data = allocator->allocate(size_bytes); if (storage->data_ptr()) { - auto q = at::xpu::getCurrentSYCLQueue(); + at::globalContext().lazyInitDevice(c10::DeviceType::XPU); + auto q = at::xpu::getCurrentSYCLQueue(); q.memcpy( data.get(), storage->data(), std::min(storage->nbytes(), size_bytes)); } diff --git a/src/ATen/xpu/EmptyTensor.cpp b/src/ATen/xpu/EmptyTensor.cpp index 3f5e998f8..6411bb221 100644 --- a/src/ATen/xpu/EmptyTensor.cpp +++ b/src/ATen/xpu/EmptyTensor.cpp @@ -54,6 +54,7 @@ TensorBase empty_strided_xpu( IntArrayRef stride, ScalarType dtype, c10::optional device_opt) { + at::globalContext().lazyInitDevice(c10::DeviceType::XPU); const auto device = device_or_default(device_opt); TORCH_INTERNAL_ASSERT(device.is_xpu()); const c10::DeviceGuard device_guard(device);