Skip to content

[https://nvbugs/6272421][fix] avoid host OOM in HfWeightLoader checkpoint prefetch - #15711

Closed
ruodil wants to merge 1 commit into
NVIDIA:mainfrom
ruodil:user/ruodil/fix-prefetch-host-oom
Closed

ruodil wants to merge 1 commit into
NVIDIA:mainfrom
ruodil:user/ruodil/fix-prefetch-host-oom

Conversation

@ruodil

@ruodil ruodil commented Jun 29, 2026 •

Copy link
Copy Markdown
Collaborator

Disposition (2026-10-30): Superseded by #17430, merged as 0946d54. Resolving this branch against current main produces no effective changes (identical Git tree).


Prefetch warmed the OS page cache by reading each safetensors file with a bare f.read(), which allocates an anonymous bytes buffer the size of the entire file. On a multi-GPU node every local rank prefetches concurrently, each with a ThreadPoolExecutor of up to min(cpu*2, 16) workers, so the peak transient anonymous memory is roughly n_local_ranks * workers * shard_size. For large checkpoints (e.g. DeepSeek-R1-0528-FP8 641GB, Nemotron-Ultra-253B 472GB) this reaches several hundred GB of non-reclaimable memory and trips the Linux OOM killer (SIGKILL) during prefetch, before any weights reach the GPU.

Read the file in bounded 16 MiB chunks and discard them instead. This warms the same page cache with O(chunk) resident anonymous memory per thread while preserving the synchronous warm-up before the local MPI barrier.

Summary by CodeRabbit

  • Performance
    • Improved model checkpoint loading by reducing memory usage during file prefetching.
    • Large files are now read in smaller chunks, helping keep startup and loading more efficient and stable.

Description

Test Coverage

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

@ruodil
ruodil requested a review from a team as a code owner June 29, 2026 09:21
@ruodil
ruodil requested a review from brb-nv June 29, 2026 09:21
@coderabbitai

coderabbitai Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

HfWeightLoader._prefetch_one_file is changed to read checkpoint files in fixed 16 MiB chunks in a loop instead of a single f.read() call. A new class constant _PREFETCH_CHUNK_SIZE = 16 * 1024 * 1024 controls the chunk size.

Changes

Chunked file prefetch in HfWeightLoader

Layer / File(s) Summary
Chunked prefetch implementation
tensorrt_llm/_torch/models/checkpoints/hf/weight_loader.py
Adds _PREFETCH_CHUNK_SIZE class constant and replaces the single f.read() in _prefetch_one_file with a loop reading 16 MiB chunks.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The description explains the issue and fix, but it is missing the required Test Coverage section. Add a Test Coverage section listing the tests run or, if none, state that explicitly and note any follow-up validation.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title matches the change and follows the required ticket-plus-type format.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@ruodil
ruodil force-pushed the user/ruodil/fix-prefetch-host-oom branch from 1b718ea to 1178edc Compare June 29, 2026 09:26
@ruodil ruodil changed the title [None][fix] avoid host OOM in HfWeightLoader checkpoint prefetch [https://nvbugs/6272421][fix] avoid host OOM in HfWeightLoader checkpoint prefetch Jun 29, 2026
@ruodil
ruodil requested a review from yufeiwu-nv June 30, 2026 09:00
Prefetch warmed the OS page cache by reading each safetensors file with a
bare f.read(), which copies the entire file into a process-owned anonymous
bytes buffer. On a multi-GPU node every local rank prefetches concurrently,
so the peak transient anonymous memory scales with the number of local ranks
times the shard size. For large checkpoints (e.g. DeepSeek-R1-0528-FP8 641GB,
Nemotron-Ultra-253B 472GB) this reaches hundreds of GB of non-reclaimable
memory and trips the Linux OOM killer (SIGKILL) during prefetch, before any
weights reach the GPU.

Warm the page cache with posix_fadvise(WILLNEED) instead: the kernel reads
the file into the reclaimable, file-backed page cache directly, with no copy
into our address space. This adds zero anonymous memory (so it cannot trigger
the OOM killer; under pressure the kernel just reclaims or caps the cache) and
removes the need for the per-file read buffer, the chunk-size tuning knob, and
the now-pointless ThreadPoolExecutor (the hint is non-blocking).

Signed-off-by: Ruodi Lu <[email protected]>
@ruodil
ruodil force-pushed the user/ruodil/fix-prefetch-host-oom branch from 1178edc to 6f83071 Compare July 21, 2026 08:54
@ruodil
ruodil requested a review from a team as a code owner July 21, 2026 08:54
@ruodil
ruodil requested a review from 2ez4bz July 21, 2026 08:54

@brnguyen2 brnguyen2 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The description and the diff describe two different fixes. The description says the file is read "in bounded 16 MiB chunks and discard[ed]", "preserving the synchronous warm-up before the local MPI barrier" — the code issues posix_fadvise(WILLNEED) and reads nothing. Those have different properties (chunked reads still block until the data is resident; the hint does not), so please update the description to match what's actually implemented, or restore the chunked-read version if that was the intent.

On the implemented approach: it does remove the anonymous allocation that was getting the process SIGKILLed, so the OOM should be fixed. What's unclear is whether prefetch still does its job. WILLNEED returns once readahead is queued, and those pages are reclaimable, so after this change nothing guarantees a shard is resident when _load_weights_in_parallel reaches it — which is what the local_mpi_barrier() and the 90%-of-available-memory gate were built around. Can you post before/after weight-load wall time for one of the large checkpoints in the bug (e.g. the 240 GB FP8 config)? If load time is unchanged the change is clearly good; if it regresses, chunked reads are the better trade.

No test was added. _prefetch_one_file is cheap to cover in tests/unittest/ (nonexistent path, unreadable path, normal path) without a GPU or model weights.

# on a node, could exhaust host RAM and SIGKILL the process during
# prefetch (before any weights reach the GPU). The hint is asynchronous;
# the kernel reads ahead in the background, overlapping with the
# subsequent weight load, and caps itself to available memory.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The comment claims the kernel "caps itself to available memory" and overlaps readahead with the subsequent load. Both are plausible but neither is guaranteed by the posix_fadvise contract — WILLNEED is a hint, the pages are reclaimable, and on some network filesystems it is effectively a no-op. Please trim this to what the syscall actually promises (queue readahead into the page cache, no anonymous allocation) rather than asserting kernel behavior the code doesn't verify.

# whole file.
os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_WILLNEED)
except (AttributeError, OSError) as e:
logger.debug(f"posix_fadvise unavailable, skipping prefetch: {e}")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is the one path where prefetch silently does nothing, and it logs at debug — meanwhile _prefetch_and_load has already printed Prefetching 472.00GB checkpoint files. at info. On a platform or filesystem where posix_fadvise is missing or inert, the logs will read as though a multi-hundred-GB prefetch succeeded while the load pass goes straight to disk. Log the skip at warning (once per process is enough) so this is diagnosable from a normal run.

len(local_file_names))
with ThreadPoolExecutor(max_workers=max_workers) as executor:
list(executor.map(self._prefetch_one_file, local_file_names))
for file_name in local_file_names:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Dropping the thread pool is fine for a non-blocking hint, but it also means this loop is now the only place the design assumption is recorded. If the chunked-read version from the PR description is what ends up merged, this has to go back to concurrent workers — a serial 16 MiB-at-a-time read of a 472 GB checkpoint slice would be far slower than the load it is meant to accelerate. Worth a one-line comment noting the serial loop is only valid because the hint doesn't block.

weight loading reads from memory instead of disk/network.
When multiple ranks run on the same node, each rank warms a disjoint
slice of the files to avoid redundant work; the page cache is shared
node-wide.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Now that the warm-up is a hint rather than a read, splitting files across local ranks doesn't save any work — the syscall is nearly free, and every rank could hint every file. The split only mattered when each rank was doing a real full-file read. Not a bug, but the docstring's stated rationale ("to avoid redundant work") no longer describes why the code is written this way.

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