Skip to content

Register native pinned host memory with CUDA for GPU DMA - #8283

Merged
sfc-gh-truwase merged 3 commits into
masterfrom
tjruwase/native-pin-device-register
Aug 23, 2026
Merged

sfc-gh-truwase merged 3 commits into
masterfrom
tjruwase/native-pin-device-register

Conversation

@sfc-gh-truwase

Copy link
Copy Markdown
Collaborator

Summary

  • Native pin (posix_memalign + mlock) is device-independent and sufficient for DeepNVMe, but the GPU DMA engine does not treat mlock pages as pinned. After allocation, optionally call cudaHostRegister (and cudaHostUnregister before free) via accelerator hooks so copy_(..., non_blocking=True) can DMA.
  • Default-on via DS_PIN_MEMORY_REGISTER_DEVICE (1/true/yes/on; 0 keeps mlock-only). Registration failure logs once and continues with mlock. CPU accelerators no-op.
  • Adds benchmarks/pin_memory/h2d_d2h_bench.py (torch vs native-unregistered vs native-registered).

H200 H2D/D2H (1 GPU)

Host tunji-h200-n1g2-ds-pin-0, NVIDIA H200, --sizes-mib 4 64 256 --warmup 10 --iters 50, autorun job-20260820T155549Z.

Arm Size (MiB) H2D (GB/s) D2H (GB/s) torch.is_pinned
torch 4 53.20 52.96 True
torch 64 55.32 52.49 True
torch 256 55.41 52.30 True
native-unregistered 4 16.95 14.05 False
native-unregistered 64 16.45 15.45 False
native-unregistered 256 9.98 16.42 False
native-registered 4 53.16 52.78 True
native-registered 64 55.29 54.79 True
native-registered 256 55.40 54.94 True

Registered native matches torch bandwidth (~53–55 GB/s). mlock without cudaHostRegister stays in the ~10–17 GB/s pageable range and reports torch.is_pinned=False.

Test plan

  • pre-commit run --files on touched paths
  • pytest tests/unit/v1/pin_memory/test_pin_memory.py 16 passed (register on/off, CPU no-op, CUDA cudart mock, register failure keeps mlock, GC unregister, invalid env)
  • GPU autorun job-20260820T155549Z: pin_memory + accelerator tests 25 passed, H2D/D2H bench exit 0

Made with Cursor

mlock alone is page-locked for AIO but not device-pinned; cudaHostRegister after alloc restores torch-class H2D/D2H bandwidth.

Signed-off-by: Olatunji Ruwase <tunji.ruwase@snowflake.com>
Co-authored-by: Cursor <cursoragent@cursor.com>

@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: e6bea83b5d

ℹ️ 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".

Comment thread deepspeed/utils/pin_memory.py Resolved
cudaHostUnregister must succeed before returning the allocation, or the driver can keep a registration on recycled host pages.

Signed-off-by: Olatunji Ruwase <tunji.ruwase@snowflake.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@delock

delock commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

Hi @sfc-gh-truwase I'm generally okay with this PR. The only thing I want to add is XPU implementation of register_host_memory, which should be added from a seperate PR. I'll approve your PR.

@PKUWZP
PKUWZP self-requested a review August 22, 2026 04:51
@sfc-gh-truwase

Copy link
Copy Markdown
Collaborator Author

Hi @sfc-gh-truwase I'm generally okay with this PR. The only thing I want to add is XPU implementation of register_host_memory, which should be added from a seperate PR. I'll approve your PR.

@delock, good call. Please let me know when this is done.

@sfc-gh-truwase
sfc-gh-truwase added this pull request to the merge queue Aug 23, 2026
Merged via the queue into master with commit cb26080 Aug 23, 2026
14 checks passed
@sfc-gh-truwase
sfc-gh-truwase deleted the tjruwase/native-pin-device-register branch August 23, 2026 23:04
@delock

delock commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

@nelyahu @hipudding @Andy666G @siqi654321 @frozenleaves @rogerxfeng8 Note this PR requires accelerator backend to implement register_host_memory and unregister_host_memory https://github.com/deepspeedai/DeepSpeed/pull/8283/changes#diff-14f0c55fe0552744caf8a206b10fcd4b9d78729aa95e30038fcc2f2c6156d514R336

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>
sfc-gh-abkulkarni pushed a commit to sfc-gh-abkulkarni/DeepSpeed that referenced this pull request Sep 18, 2026
…eepspeedai#8537)

implement register_host_memory and unregister_host_memory for supa deepspeedai#8283

Signed-off-by: frozenleaves <914814442@qq.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.

4 participants