Repository navigation
XPU: register native pinned host memory with the SYCL runtime #8322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
|
|
||
| # DeepSpeed Team | ||
|
|
||
| import ctypes | ||
| import torch | ||
| from deepspeed.accelerator.abstract_accelerator import DeepSpeedAccelerator | ||
| import functools | ||
|
|
@@ -15,6 +16,57 @@ | |
| except ImportError as e: | ||
| oneccl_imported_p = False | ||
|
|
||
| # Mangled names of ``sycl::ext::oneapi::experimental::prepare_for_device_copy(const void*, size_t, const queue&)`` | ||
| # and ``release_from_device_copy(const void*, const queue&)``. These are the SYCL | ||
| # equivalent of ``cudaHostRegister``/``cudaHostUnregister``. Neither SYCL nor torch | ||
| # exposes a C entry point for them, so the C++ symbols are resolved directly. The | ||
| # names live in SYCL's ``_V1`` inline ABI namespace and are stable across the | ||
| # libsycl.so.8 (oneAPI 2025.x) and libsycl.so.9 (oneAPI 2026.x) runtimes. | ||
| SYCL_PREPARE_FOR_DEVICE_COPY = "_ZN4sycl3_V13ext6oneapi12experimental23prepare_for_device_copyEPKvmRKNS0_5queueE" | ||
| SYCL_RELEASE_FROM_DEVICE_COPY = "_ZN4sycl3_V13ext6oneapi12experimental24release_from_device_copyEPKvRKNS0_5queueE" | ||
|
|
||
| # Only ever bind to the SYCL runtime torch already initialized. Opening a second | ||
| # one (e.g. via the ``libsycl.so`` development symlink, which may resolve to a | ||
| # different oneAPI install) leaves both with their own driver state and aborts at | ||
| # interpreter shutdown with UR_RESULT_ERROR_UNINITIALIZED. | ||
| RTLD_NOLOAD = 0x00004 | ||
|
|
||
|
|
||
| def _mapped_sycl_runtime_path(): | ||
| """Path of the libsycl the current process already mapped, or None. | ||
|
|
||
| Read from /proc/self/maps rather than hardcoding a soname: the soname is | ||
| version-dependent (oneAPI 2025.x ships libsycl.so.8, 2026.x ships .so.9) and | ||
| the path also disambiguates between several oneAPI installs on one machine. | ||
| """ | ||
| try: | ||
| with open("/proc/self/maps") as maps: | ||
| for line in maps: | ||
| path = line.rstrip("\n").rpartition(" ")[2] | ||
| if "/libsycl.so" in path: | ||
| return path | ||
| except OSError: | ||
| return None | ||
| return None | ||
|
|
||
|
|
||
| def _sycl_host_copy_funcs(): | ||
| """Resolve the SYCL host-registration functions, or return None when unavailable.""" | ||
| runtime_path = _mapped_sycl_runtime_path() | ||
| if runtime_path is None: | ||
| return None | ||
| try: | ||
| libsycl = ctypes.CDLL(runtime_path, mode=RTLD_NOLOAD) | ||
| prepare = getattr(libsycl, SYCL_PREPARE_FOR_DEVICE_COPY) | ||
| release = getattr(libsycl, SYCL_RELEASE_FROM_DEVICE_COPY) | ||
| except (OSError, AttributeError): | ||
| return None | ||
| prepare.restype = None | ||
| prepare.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_void_p] | ||
| release.restype = None | ||
| release.argtypes = [ctypes.c_void_p, ctypes.c_void_p] | ||
| return prepare, release | ||
|
|
||
|
|
||
| class XPU_Accelerator(DeepSpeedAccelerator): | ||
|
|
||
|
|
@@ -234,6 +286,34 @@ def _torch_pin_memory(self, tensor): | |
| def _torch_is_pinned(self, tensor): | ||
| return tensor.is_pinned(device=self.current_device_name()) | ||
|
|
||
| def register_host_memory(self, address, num_bytes): | ||
| # Resolve the queue first: it initializes torch's XPU runtime, which is | ||
| # what loads libsycl.so.8 and makes the RTLD_NOLOAD lookup below succeed. | ||
| sycl_queue = self._sycl_queue() | ||
| funcs = _sycl_host_copy_funcs() | ||
| if funcs is None: | ||
| from deepspeed.utils import logger | ||
| logger.warning_once("SYCL host-memory registration is unavailable (prepare_for_device_copy not found in " | ||
| "the loaded SYCL runtime); native pinned memory stays mlock-only.") | ||
| return False | ||
| prepare, _ = funcs | ||
| prepare(address, num_bytes, sycl_queue) | ||
| return True | ||
|
Comment on lines
+299
to
+301
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When AGENTS.md reference: AGENTS.md:L26-L26 Useful? React with 👍 / 👎. |
||
|
|
||
| def unregister_host_memory(self, address): | ||
| sycl_queue = self._sycl_queue() | ||
| funcs = _sycl_host_copy_funcs() | ||
| if funcs is None: | ||
| return None | ||
| _, release = funcs | ||
| release(address, sycl_queue) | ||
|
|
||
| def _sycl_queue(self): | ||
| # prepare_for_device_copy registers with the queue's SYCL context, so any | ||
| # queue on the current device yields the same registration. | ||
| torch.xpu.init() | ||
| return torch.xpu.current_stream().sycl_queue | ||
|
|
||
| def op_builder_dir(self): | ||
| try: | ||
| # is op_builder from deepspeed or a 3p version? this should only succeed if it's deepspeed | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a non-merge commit, but its message contains no
Signed-off-bytrailer, so it violates the repository's commit requirement. Add the sign-off using the configured Git name and email before merging.AGENTS.md reference: AGENTS.md:L8-L8
Useful? React with 👍 / 👎.