-
Notifications
You must be signed in to change notification settings - Fork 101
fix(inference): materialise checkpoint tensors in host memory before H2D copy (17.7x faster load on GB300) #150
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
Open
rwagwani
wants to merge
3
commits into
NVIDIA:main
Choose a base branch
from
rwagwani:fix/checkpoint-load-mmap-h2d
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -270,7 +270,84 @@ def _normalize_diffusers_target_key(name: str) -> str: | |
| return name.removeprefix("model.net.").replace("_orig_mod.", "").replace("_checkpoint_wrapped_module.", "") | ||
|
|
||
|
|
||
| class _DiffusersHuggingFaceStorageReader(HuggingFaceStorageReader): | ||
| class _MmapSafeReadMixin: | ||
| """Materialize each safetensors slice into anonymous RAM before the H2D copy. | ||
|
|
||
| ``HuggingFaceStorageReader._process_read_request`` copies tensors straight from the | ||
| ``mmap``-backed safetensors slice onto the GPU, so the transfer handles a page fault | ||
| per tensor. On Grace that dominates checkpoint load: a Cosmos3-Super reasoner load | ||
| spends ~1200s here with zero disk I/O, the data already resident in page cache. | ||
|
|
||
| The copy is applied conditionally -- see ``_materialize_enabled`` for why. | ||
| """ | ||
|
|
||
| _materialize_cache = None | ||
|
|
||
| @classmethod | ||
| def _materialize_enabled(cls) -> bool: | ||
| """Whether to stage tensors through anonymous host memory before the H2D copy. | ||
|
|
||
| Defaults on for aarch64 and off elsewhere, overridable in either direction with | ||
| COSMOS_MATERIALIZE_CHECKPOINT=1/0. | ||
|
|
||
| The staging copy removes a slow file-backed mmap H2D path on Grace, but where that | ||
| path is already fast the copy is pure overhead: on 8x A100 (Cosmos3-Super, 8 ranks) | ||
| applying it unconditionally measured ~13.2% slower, because concurrent ranks contend | ||
| for host memory bandwidth. The architecture is only a proxy for "is the mmap H2D path | ||
| slow here", so it is used as a default rather than as a hard condition. | ||
| """ | ||
| if cls._materialize_cache is None: | ||
| import os | ||
| import platform | ||
|
|
||
| override = os.environ.get("COSMOS_MATERIALIZE_CHECKPOINT") | ||
| if override is not None: | ||
| cls._materialize_cache = override.strip().lower() not in ( | ||
| "0", | ||
| "false", | ||
| "no", | ||
| "off", | ||
| "", | ||
| ) | ||
| else: | ||
| cls._materialize_cache = platform.machine().lower() in ("aarch64", "arm64") | ||
|
Collaborator
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. Should be all default off? Since not necessarily would benefit in all aarch64/arm64 case? Still think this is highly related with the storage system. |
||
| return cls._materialize_cache | ||
|
|
||
| def _process_read_request(self, f, req, planner) -> None: # noqa: D102 | ||
| slices = tuple(slice(offset, offset + length) for offset, length in zip(req.storage_offsets, req.lengths)) | ||
| tensor = f.get_slice(req.storage_index.fqn)[slices] | ||
| target_tensor = planner.resolve_tensor(req).detach() | ||
|
|
||
| if target_tensor.size() != tensor.size(): | ||
| raise AssertionError(f"req {req.storage_index} mismatch sizes {target_tensor.size()} vs {tensor.size()}") | ||
|
|
||
| if target_tensor.is_cuda and self._materialize_enabled(): | ||
| # Materialise into anonymous host memory before the H2D copy. | ||
| # | ||
| # The base implementation copies straight from mmap-backed safetensors | ||
| # storage into a CUDA tensor, so the transfer handles a page fault per | ||
| # tensor. On Grace this dominates load time (Cosmos3-Super load: ~1200s | ||
| # down to ~70-85s with this change, measured across two boots; zero disk | ||
| # I/O either way -- the data is already in page cache). | ||
| # | ||
| # Pre-faulting in place is not sufficient: measured on a 4.61 GiB shard, | ||
| # an mmap source still costs 9.00 ms/tensor after faults are pre-paid, | ||
| # versus 1.61 ms/tensor from the heap. Pinning the staging buffer is not | ||
| # necessary either -- with a heap source, pageable (1.61) and pinned | ||
| # (1.69) are equivalent. A single clone() does both jobs: the memcpy | ||
| # faults the pages sequentially and leaves the data resident and | ||
| # anonymous. It is freed as soon as the H2D copy completes. | ||
| tensor = tensor.contiguous().clone() | ||
|
|
||
| target_tensor.copy_(tensor) | ||
| planner.commit_tensor(req, target_tensor) | ||
|
|
||
|
|
||
| class _MmapSafeHuggingFaceStorageReader(_MmapSafeReadMixin, HuggingFaceStorageReader): | ||
| """Plain HF safetensors reader with the mmap-H2D staging copy.""" | ||
|
|
||
|
|
||
| class _DiffusersHuggingFaceStorageReader(_MmapSafeReadMixin, HuggingFaceStorageReader): | ||
| """Hugging Face safetensors reader that follows diffusers' root weight map.""" | ||
|
|
||
| def __init__(self, checkpoint_path: Path) -> None: | ||
|
|
@@ -561,7 +638,7 @@ def from_pretrained_dcp( | |
| return model | ||
| state_dict = get_model_state_dict(model) | ||
| _raise_on_missing_vision_keys(checkpoint_path, state_dict) | ||
| storage_reader = HuggingFaceStorageReader(str(checkpoint_path)) | ||
| storage_reader = _MmapSafeHuggingFaceStorageReader(str(checkpoint_path)) | ||
| case _: | ||
| assert_never(checkpoint_type) | ||
| dcp.load(state_dict=state_dict, storage_reader=storage_reader) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could have default 0 in the env.get?