Skip to content

XPU: register native pinned host memory with the SYCL runtime - #8322

Closed
delock wants to merge 3 commits into
masterfrom
gma/xpu-pin-memory
Closed

delock wants to merge 3 commits into
masterfrom
gma/xpu-pin-memory

Conversation

@delock

@delock delock commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #8315.

Summary

  • Implement register_host_memory / unregister_host_memory in XPU_Accelerator via SYCL's prepare_for_device_copy / release_from_device_copy (the SYCL analogue of cudaHostRegister/cudaHostUnregister). Previously XPU inherited the base-class no-ops, so DS_PIN_MEMORY_BACKEND=native buffers stayed mlock-only on Intel GPUs.
  • Honors the existing DS_PIN_MEMORY_REGISTER_DEVICE opt-out and falls back to "log once and continue with mlock" when the symbols cannot be resolved.
  • Make benchmarks/pin_memory/h2d_d2h_bench.py accelerator-agnostic (it hardcoded a CUDA check and torch's CUDA-only pin_memory=True path) so the arms can be measured on XPU.

Implementation note: resolving the SYCL entry points

Neither SYCL nor torch exposes a C entry point for these functions, so the mangled C++ symbols are resolved with ctypes. Two constraints, both found the hard way on an Arc Pro B60:

Which library. Opening the libsycl.so development symlink dlopens a second SYCL runtime alongside the one torch already loaded. Both end up with their own driver state and the process aborts at interpreter shutdown with UR_RESULT_ERROR_UNINITIALIZED. We therefore only ever bind, with RTLD_NOLOAD, to the runtime already mapped into the process.

Not by soname. The soname is version-dependent -- oneAPI 2025.x ships libsycl.so.8, 2026.x ships libsycl.so.9 (both are present on the test machine). Hardcoding .so.8 would silently disable registration after a oneAPI upgrade. The path is instead read from /proc/self/maps, which tracks the version and also disambiguates between multiple oneAPI installs.

The mangled names themselves are stable: they live in SYCL's _V1 inline ABI namespace and are byte-identical in libsycl.so.8 and libsycl.so.9, verified with nm on both. Symbols disappearing is handled as the mlock-only fallback rather than an error.

Two tests pin this down (test_xpu_sycl_lookup_reuses_torch_runtime, test_xpu_sycl_lookup_without_mapped_runtime) so the lookup cannot regress to loading a second runtime or a hardcoded soname.

The sycl_queue attribute torch exposes as an int is a sycl::queue* -- confirmed against torch's own AOTI runtime, which dereferences it as *queue_ptr -- and pointer/reference share an ABI, so passing it to a const sycl::queue& parameter is sound.

Testing

Intel Arc Pro B60 (4 cards, ZE_AFFINITY_MASK=3), torch 2.10.0+xpu, oneAPI 2025.3 + 2026.0 both installed:

  • pytest tests/unit/v1/pin_memory/test_pin_memory.py -- 21 passed.
  • benchmarks/pin_memory/h2d_d2h_bench.py runs all three arms to completion and exits 0 (it aborted at shutdown before the RTLD_NOLOAD fix).

Bandwidth on this particular machine is not a useful signal: the host is a VM whose PCIe path tops out around 1.8 GB/s, and all arms -- including plain pageable -- already sit at that ceiling, so registration has no headroom to show a gain:

arm,size_mib,h2d_gbps,d2h_gbps
torch,256,1.82,1.81
native-unregistered,256,1.80,1.78
native-registered,256,1.61,1.81

The path is nevertheless exercised: a standalone probe against non-mlocked malloc memory (which is not at the mlock-assisted ceiling) moved D2H from 1.53 to 1.78 GB/s after prepare_for_device_copy, matching torch-pinned. Confirming the intended H2D/D2H win would need a bare-metal Intel GPU host with a full-bandwidth PCIe link.

Known limitation

SYCL reports registration failures by throwing sycl::exception, which cannot cross the ctypes boundary -- a bad address returns normally rather than raising. So unlike the CUDA path (which checks the cudaError_t), a failed registration here degrades silently to mlock-only speed instead of being reported. Making this observable would need a small C++ shim that catches the exception and returns a status code; I left it out to keep the change dependency-free, but am happy to add it if reviewers prefer.

delock added 2 commits August 26, 2026 16:20
Native pinned buffers (DS_PIN_MEMORY_BACKEND=native) were mlock-only on XPU
because XPU_Accelerator inherited the no-op register_host_memory hooks, so
H2D/D2H stayed at pageable bandwidth. Implement the hooks via SYCL's
prepare_for_device_copy/release_from_device_copy and make the pin_memory
benchmark accelerator-agnostic so the gain can be measured on Intel GPUs.

Signed-off-by: Ma, Guokai <guokai.ma@intel.com>
Resolving prepare_for_device_copy through the libsycl.so development symlink
dlopen'd a second SYCL runtime alongside the libsycl.so.8 torch already had
loaded. Both ended up with their own driver state and the process aborted at
interpreter shutdown with UR_RESULT_ERROR_UNINITIALIZED. Look the symbols up
on the soname with RTLD_NOLOAD so we only ever bind to torch's instance.

Signed-off-by: Ma, Guokai <guokai.ma@intel.com>
@delock
delock marked this pull request as draft August 26, 2026 08:45

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6e0a597d9d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


# DeepSpeed Team

import ctypes

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add the required commit sign-off

This is a non-merge commit, but its message contains no Signed-off-by trailer, 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 👍 / 👎.

Comment on lines +278 to +280
prepare, _ = funcs
prepare(address, num_bytes, sycl_queue)
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Document the new XPU registration path

When DS_PIN_MEMORY_BACKEND=native is used on XPU, this call now registers the allocation with SYCL, but docs/code-docs/source/memory.rst still explicitly says native allocations are not registered with XPU and describes device registration as CUDA-only (lines 340–347 and 390–405). This leaves XPU users incorrectly warned that asynchronous copies will block and omits the new fallback behavior, so update the existing pin-memory documentation with this feature.

AGENTS.md reference: AGENTS.md:L26-L26

Useful? React with 👍 / 👎.

…oname

The soname is version-dependent: oneAPI 2025.x ships libsycl.so.8 while
2026.x ships libsycl.so.9, so hardcoding .so.8 silently disables host
registration after a oneAPI upgrade. Read the path of the libsycl the process
already mapped from /proc/self/maps, which both tracks the version and
disambiguates between multiple oneAPI installs on one machine.

Signed-off-by: Ma, Guokai <guokai.ma@intel.com>
@delock

delock commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

This feature (register host memory) depends on oneapi 2026.2. Need to wait release of oneapi > 2026.1

@delock delock closed this Aug 26, 2026
nithin-aashik-mcw pushed a commit to nithin-aashik-mcw/DeepSpeed that referenced this pull request Sep 18, 2026
…pspeedai#8532)

Fixes deepspeedai#8531.

Follow-up to deepspeedai#8283; XPU counterpart tracked in deepspeedai#8315 (attempted in
deepspeedai#8322).

## Summary

- Implement `register_host_memory` / `unregister_host_memory` on
`NPU_Accelerator`via the NPU runtime API torch_npu ships:
`torch.npu.npurt()` returns the runtime-API module with
`npuHostRegister` / `npuHostUnregister`, thin wrappers over
`aclrtHostRegisterV2` / `aclrtHostUnregister`. Previously
`NPU_Accelerator` inherited the base-class no-ops, so
`DS_PIN_MEMORY_BACKEND=native` buffers stayed mlock-only on Ascend NPUs
and asynchronous copies ran at pageable bandwidth.
- Degrades to "log once and continue with mlock" on every failure path
and keeps honoring the existing `DS_PIN_MEMORY_REGISTER_DEVICE` opt-out.
- Extends `tests/unit/v1/pin_memory/test_pin_memory.py` with NPU cases
mirroring the CUDA cudart-mock tests, including the availability
fallbacks.
- Makes `benchmarks/pin_memory/h2d_d2h_bench.py` accelerator-agnostic
(it hardcoded a CUDA check and torch's `pin_memory=True` fast path) so
the before/after bandwidth could be measured on NPU.
- Documents the NPU registration path in
`docs/code-docs/source/memory.rst`.

## Implementation notes

**The npurt binding.** torch_npu exposes `torch.npu.npurt()`, which
initializes the runtime if needed and returns the runtime-API module.
Using it avoids binding `libascendcl.so` ourselves and raises a clear
`RuntimeError` when the build lacks the module. Two defenses gate the
hook, both falling back to mlock-only with a one-time warning:

- no `torch.npu.npurt` attribute (the hook checks for its presence),
- `npurt()` failing to initialize the runtime.

**Flag choice: `ACL_HOST_REG_MAPPED`.** The `flags` parameter of
`npuHostRegister` is the CANN `ACL_HOST_REG_*` bitmask (per the npurt
docstring and `acl_rt.h`): `MAPPED` (0x2) page-locks the range and adds
a device mapping, `PINNED` (0x10000000) only page-locks. Measured
through the bench methodology at 64 MiB (10 process-level A/B runs):
`PINNED`-only registrations fell back to mlock-only H2D speed (~8-9
GB/s) in 5/5 runs, while `MAPPED` kept ~23 GB/s (5/6). `MAPPED`
additionally requires 4K-aligned addresses (a 64B-offset probe fails
with `ACL_ERROR_RT_DRV_INTERNAL_ERROR`, 507899), which DeepSpeed's
native allocator guarantees (`posix_memalign(..., sysconf(_SC_PAGESIZE),
...)`); sizes need no page alignment (probed 400 B to 1 MiB+3, rc=0). As
a guard against unaligned callers, the hook extends the range down to
the page boundary, padding the size accordingly, instead of relying on
the driver's opaque internal error.

**Failure semantics.** A non-zero return code from `npuHostRegister`
logs once and returns `False`, so `NativePinnedMemory` keeps the buffer
mlock-only and does not track the address. A failed `npuHostUnregister`
raises (like the CUDA hook), which makes `NativePinnedMemory` keep the
allocation alive so the driver never holds a registration for pages
later reused by `malloc`.

## Bandwidth on Ascend 910B4

`benchmarks/pin_memory/h2d_d2h_bench.py`, one full run of the deepspeedai#8283
methodology (`--sizes-mib 4 64 --warmup 10 --iters 50`), CANN 8.5.0,
torch 2.10.0+cpu, torch_npu 2.10.0, Ascend 910B4 (aarch64), shared box:

| Arm | Size (MiB) | H2D (GB/s) | D2H (GB/s) | torch.is_pinned |
|-----|------------|------------|------------|-----------------|
| torch | 4 | 10.4 | 21.1 | True |
| torch | 64 | 16.5 | 21.0 | True |
| native-unregistered | 4 | 9.7 | 9.4 | False |
| native-unregistered | 64 | 21.1 | 19.6 | False |
| native-registered | 4 | **22.7** | 20.6 | True |
| native-registered | 64 | **23.4** | 20.9 | True |

After registration, `torch.is_pinned()` on a `torch.frombuffer` view of
the range returns `True` — the torch_npu runtime recognizes the
registered range — so the DMA path is confirmed to engage, not just
inferred from the numbers.

Registration lifts H2D bandwidth from ~9.7 to ~22.7 GB/s at 4 MiB (2.3x)
and holds ~23.4 GB/s at 64 MiB, with D2H improving as well (9.4 → 20.6
and 19.6 → 20.9 GB/s). Registered copies match or exceed the torch
`pin_memory()` arm at every size.

## Test plan

- [x] `pre-commit run --files` on touched paths — yapf, flake8,
check-torchdist, check-torchcuda, check-license, codespell all pass
- [x] `pytest tests/unit/v1/pin_memory/test_pin_memory.py` — **24
passed** on Ascend 910B4 (18 existing incl. the CUDA cudart-mock tests +
6 new NPU cases: npurt register/unregister calls, unaligned address
extended to the page boundary, non-zero rc degrades to mlock, unregister
failure raises, missing npurt is a no-op, availability gate table)
- [x] `benchmarks/pin_memory/h2d_d2h_bench.py` on Ascend 910B4 — all
three arms run to completion and exit 0 at 4/64 MiB

## Known limitation

Some torch_npu builds may ship without the npurt module. The `hasattr`
check degrades to mlock-only with a one-time warning, so registration is
only available when the binding is present.

---------

Signed-off-by: Zizhao Tang <1601677496@qq.com>
Co-authored-by: Ma, Guokai <guokai.ma@gmail.com>
@tohtana tohtana added needs-gpu-test GPU testing is recommended to validate the changes in this PR. and removed needs-gpu-test GPU testing is recommended to validate the changes in this PR. labels Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REQUEST] XPU: register native pinned host memory with the device runtime (follow-up to #8283)

2 participants