Skip to content

fix(dynamicemb): size HostVMMTensor mapping to need, grow via mremap - #425

Merged
shijieliu merged 1 commit into
NVIDIA:mainfrom
jiashuy:fix_host_vmm_bug
Jun 17, 2026
Merged

shijieliu merged 1 commit into
NVIDIA:mainfrom
jiashuy:fix_host_vmm_bug

Conversation

@jiashuy

@jiashuy jiashuy commented Jun 16, 2026 •

Copy link
Copy Markdown
Collaborator

HostVMMTensor reserved the entire machine's physical RAM as an anonymous mmap for every host tensor, intending it as a VA window that extend() would grow into in place. With many tables (e.g. the test_alignment suite), these multi-TB reservations accumulate and exhaust the process virtual address space, so mmap eventually fails with ENOMEM even for tiny tensors:

RuntimeError: mmap virtual memory failed (size=2164192202752 bytes):
Cannot allocate memory

Map only the bytes actually backed (page-aligned) and grow the mapping on demand in extend() via mremap(MREMAP_MAYMOVE). Relocation is safe: callers rebuild value-buffer pointers after extend() per the get_table_ptrs() contract. CUDA registration is re-done around the remap, with rollback to the previous size on mremap/mlock/cudaHostRegister failure. Add _GNU_SOURCE for mremap and include errno/size in the mmap error message.

Fixes the host path of test/unit_tests/test_alignment.py; verified passing at WORLD_SIZE=1/4/8 plus the test_vmm_tensor.py extend stress test.

Description

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@jiashuy

jiashuy commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator Author

/build

@greptile-apps

greptile-apps Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a virtual-address exhaustion bug in HostVMMTensor: the constructor previously mmap'd the entire machine's physical RAM as a VA reservation for every tensor, causing ENOMEM failures (even for tiny tensors) when many embedding tables were created simultaneously. The fix maps only the bytes actually needed at construction time and adds mremap(MREMAP_MAYMOVE)-based growth in extend(), with careful rollback-on-failure via the new restore_to_size() helper.

  • Replaces the whole-RAM mmap reservation with a minimal mmap(m_size) and adds _GNU_SOURCE + mremap(MREMAP_MAYMOVE) growth in extend(); CUDA registration is dropped before remapping and re-acquired after, with rollback to the previous size on any failure.
  • Constructor error handling is improved: cudaHostRegister/cudaHostGetDevicePointer failures now correctly call munlock+munmap before throwing, and madvise failures are downgraded from fatal to a logged warning.
  • Destructor is fixed to use m_reserved (the true mapping extent) rather than m_size for munlock/munmap, eliminating the previous partial-unmap bug on failed extend().

Confidence Score: 5/5

The core logic is correct: m_reserved is updated before any throwable operation, the destructor uses m_reserved throughout, and all failure paths invoke restore_to_size() which correctly shrinks back, re-registers with CUDA, and refreshes the device pointer.

The fundamental correctness of the change is sound. All failure paths in extend() either leave the buffer unchanged or roll back via restore_to_size(). The remaining observations are minor inefficiencies and an encapsulation gap — none affect correctness or safety.

corelib/dynamicemb/src/vmm_tensor.cu — the extend() + restore_to_size() interaction is the only path that warrants a close read; the constructor and destructor changes are straightforward.

Important Files Changed

Filename Overview
corelib/dynamicemb/src/vmm_tensor.cu Core fix replaces multi-TB mmap reservation with minimal allocation + mremap growth; recovery logic and destructor are sound. Minor issues: restore_to_size() is public but is an internal helper, getTotalPhysicalMemory() reads /proc/meminfo on every extend() call, and mlock() in extend() re-locks the already-locked old range unnecessarily.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant Caller
    participant HVT as HostVMMTensor
    participant OS as OS mmap/mremap/mlock
    participant CUDA as CUDA Runtime

    Note over Caller,CUDA: extend() happy path
    Caller->>HVT: extend(new_numel)
    HVT->>CUDA: cudaHostUnregister(m_addr_h)
    CUDA-->>HVT: OK
    HVT->>OS: mremap(old_addr, old_size, new_bytes, MREMAP_MAYMOVE)
    OS-->>HVT: new_addr
    HVT->>OS: madvise tail non-fatal
    HVT->>OS: memset loop to fault in new pages
    HVT->>OS: mlock(m_addr_h, new_bytes)
    OS-->>HVT: OK
    HVT->>CUDA: cudaHostRegister(m_addr_h, new_bytes)
    CUDA-->>HVT: OK
    HVT->>CUDA: cudaHostGetDevicePointer
    CUDA-->>HVT: m_addr_d updated
    HVT-->>Caller: returns

    Note over Caller,CUDA: extend() failure path with rollback
    Caller->>HVT: extend(new_numel)
    HVT->>CUDA: cudaHostUnregister
    HVT->>OS: mremap succeeds
    HVT->>OS: mlock fails
    HVT->>HVT: restore_to_size(old_size)
    HVT->>OS: mremap shrink back to old_size
    HVT->>CUDA: cudaHostRegister at old_size
    CUDA-->>HVT: m_addr_d refreshed
    HVT-->>Caller: throws runtime_error
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant Caller
    participant HVT as HostVMMTensor
    participant OS as OS mmap/mremap/mlock
    participant CUDA as CUDA Runtime

    Note over Caller,CUDA: extend() happy path
    Caller->>HVT: extend(new_numel)
    HVT->>CUDA: cudaHostUnregister(m_addr_h)
    CUDA-->>HVT: OK
    HVT->>OS: mremap(old_addr, old_size, new_bytes, MREMAP_MAYMOVE)
    OS-->>HVT: new_addr
    HVT->>OS: madvise tail non-fatal
    HVT->>OS: memset loop to fault in new pages
    HVT->>OS: mlock(m_addr_h, new_bytes)
    OS-->>HVT: OK
    HVT->>CUDA: cudaHostRegister(m_addr_h, new_bytes)
    CUDA-->>HVT: OK
    HVT->>CUDA: cudaHostGetDevicePointer
    CUDA-->>HVT: m_addr_d updated
    HVT-->>Caller: returns

    Note over Caller,CUDA: extend() failure path with rollback
    Caller->>HVT: extend(new_numel)
    HVT->>CUDA: cudaHostUnregister
    HVT->>OS: mremap succeeds
    HVT->>OS: mlock fails
    HVT->>HVT: restore_to_size(old_size)
    HVT->>OS: mremap shrink back to old_size
    HVT->>CUDA: cudaHostRegister at old_size
    CUDA-->>HVT: m_addr_d refreshed
    HVT-->>Caller: throws runtime_error
Loading

Reviews (2): Last reviewed commit: "fix(dynamicemb): size HostVMMTensor mapp..." | Re-trigger Greptile

Comment thread corelib/dynamicemb/src/vmm_tensor.cu Resolved
Comment thread corelib/dynamicemb/src/vmm_tensor.cu Resolved
@JacoCheung

JacoCheung commented Jun 16, 2026 •

Copy link
Copy Markdown
Collaborator

❌ Pipeline #54914686 -- failed

Job Status Log
pre_check ✅ success view
train_build_x86 ✅ success view
train_build_arm64 ✅ success view
tritonserver_build_x86 ✅ success view
prepare-jet-b200-smoke ✅ success view
prepare-jet-b200-inference ✅ success view
prepare-jet-cw-dfw-e2e-benchmark ✅ success view
build_whl ✅ success view
dynamicemb_test_fwd_bwd_8gpus ✅ success view
dynamicemb_test_load_dump_8gpus ✅ success view
unit_test_1gpu_a100 ✅ success view
unit_test_1gpu_h100 ❌ failed view
unit_test_4gpu ✅ success view
unit_test_tp_4gpu ❌ failed view
L20_unit_test_1gpu ✅ success view
inference_unit_test_1gpu ✅ success view
inference_test_1gpu ❌ failed view

Result: 14/17 jobs passed

View full pipeline

HostVMMTensor reserved the entire machine's physical RAM as an anonymous
mmap for every host tensor, intending it as a VA window that extend()
would grow into in place. With many tables (e.g. the test_alignment
suite), these multi-TB reservations accumulate and exhaust the process
virtual address space, so mmap eventually fails with ENOMEM even for tiny
tensors:

  RuntimeError: mmap virtual memory failed (size=2164192202752 bytes):
  Cannot allocate memory

Map only the bytes actually backed (page-aligned) and grow the mapping on
demand in extend() via mremap(MREMAP_MAYMOVE). Relocation is safe: callers
rebuild value-buffer pointers after extend() per the get_table_ptrs()
contract.

Robustness:
- restore_to_size() centralizes rollback: on any failure after the mapping
  has grown (mremap/mlock/cudaHostRegister/cudaHostGetDevicePointer), drop the
  CUDA registration, shrink the mapping back, and re-register, leaving the
  object consistent (m_size == m_reserved == old size) and safe to reuse/retry.
- Record the grown extent (m_reserved) immediately after a successful mremap so
  the destructor's munmap always covers the whole mapping (no tail leak).
- Free the mmap'd + mlocked buffer if CUDA registration fails in the
  constructor (the destructor does not run on a failed construction).
- Treat madvise(MADV_WILLNEED) as the advisory hint it is: non-fatal in both
  the constructor and extend() (memset + mlock are the real fault-in/pin),
  warning with errno instead of aborting or silently ignoring it.
- Destructor is best-effort (never throws); add _GNU_SOURCE for mremap and
  include errno/size in the mmap error message.

Fixes the host path of test/unit_tests/test_alignment.py; verified passing
at WORLD_SIZE=1/4/8 plus the test_vmm_tensor.py extend stress test.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@jiashuy
jiashuy force-pushed the fix_host_vmm_bug branch from 0c12c7a to ffe3351 Compare June 17, 2026 06:33
@jiashuy
jiashuy requested a review from shijieliu June 17, 2026 06:34
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.

3 participants