Repository navigation
Studio: fill the H3 conditioner's pinned arenas once, before registering (33% faster cold pin) - #16887
Merged
Conversation
…n a thread pool pin_module_in_place zero-filled each registered arena (faulting its pages), then copied every tensor into it one at a time. The arenas are now placed up front (same largest-first first-fit offsets), and each is filled by its copies plus zeroed gaps on an 8-thread pool before cudaHostRegister, so the copy faults the pages and the separate zero pass is gone. Output is the same tensors at the same offsets, with the same owners replaced; a failed copy now replaces none. registered_pinned_empty takes an optional fill callable in place of its zero fill.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This was referenced Nov 6, 2026
danielhanchen
added a commit
that referenced
this pull request
Nov 6, 2026
…e stubbable package (#16898) ## Summary Since #16793 (`90e1e733c4`), Backend CI `(Python 3.13, g-r)` fails on unrelated PRs. The last hour's staging runs for #16887, #16886 and #16882 all show the same two tests: ``` tests/test_load_download_repos_mapper.py:11: in test_unmapped_lora_base_skips_the_remote_mapper from unsloth.models import loader_utils E ModuleNotFoundError: No module named 'unsloth.models'; 'unsloth' is not a package ``` What #16793 tests (skipping the remote mapper for unknown LoRA bases) is fine. The two new tests guard with `pytest.importorskip("unsloth")` and then `from unsloth.models import ...`. In the backend job, several test files stub `unsloth` at import time as a plain module (`_stub_if_missing` in `test_audio_type_inconclusive.py` and others) when the real package cannot be loaded. If one of those runs earlier in the same xdist worker, `importorskip("unsloth")` returns the stub instead of skipping, and the submodule import fails. Run alone, or in a worker where nothing stubbed it, the tests pass. That is why it depends on how the shard is laid out. ## Change - `studio/backend/tests/test_load_download_repos_mapper.py`: `importorskip` the submodules the tests use (`unsloth.models.loader_utils`, `unsloth.models.loader`). With a stub in place they now skip, the same as with no unsloth at all. With the real package they run unchanged. - `studio/backend/tests/test_backend_tests_stub_heavy_imports.py`: `test_no_test_importorskips_bare_unsloth` fails if any backend test calls `pytest.importorskip("unsloth")` on the bare package, and pins its own detector both ways. No other backend test does this today. The rule is narrowed to `unsloth`, since the `trl` stubs carry the attributes their tests read. ## Testing On `studio/backend`, with a one-line test file that stubs `sys.modules["unsloth"]` as a plain module run first (what the shard does): | | main | this branch | |---|---|---| | stub first, then `test_load_download_repos_mapper.py` | 2 failed (the error above) | 2 skipped | | `test_load_download_repos_mapper.py` alone, real unsloth | 2 passed | 2 passed | | `test_backend_tests_stub_heavy_imports.py` | 6 passed | 7 passed | Putting back the original `importorskip("unsloth")` makes the new guard fail and name `test_load_download_repos_mapper.py`.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
MiniMax-H3's quantized conditioner is pinned eagerly at load (
pin_module_in_place, video_minimax_h3_te.py): its weights are copied from the safetensors mmap into pinned host arenas. Profiling an 8 GB pin showed the copy itself is the smaller part. Most of the time is arena allocation:registered_pinned_emptyzero-fills every byte of a fresh buffer (to fault its pages) and only then are the weights copied in, so every arena byte is written twice.This PR fills each arena once:
_fill_arena: the tensor copies plus zeroed gaps and tail, on an 8-thread pool, before it is registered. The copy faults the pages, so the separate zero pass is gone. Every arena byte has the same value as before.registered_pinned_empty(nbytes, fill=None)takes an optionalfillthat runs instead ofzero_(), beforecudaHostRegister. Other callers are unchanged. The torch pinned-allocator fallback fills after allocating._parameters/_buffersare still replaced rather than.data, so the mmap is still released after the pin.Evidence
Synthetic 8.6 GB safetensors file (Gemma-3-27B-shaped int8 weights plus bf16 tensors), loaded via mmap into a module; each arm runs its own tree's code through the real registered-arena path; 4 repeats per arm, interleaved; cold runs evict the file with
posix_fadvise(DONTNEED). Colab A100-SXM4-40GB VM, 3 vCPU, 83 GB RAM, torch 2.14.1. Median (range):About 10% faster hot and 33% faster cold, both well outside the A/A spread. Running the pin from a worker thread gives the same numbers. An earlier L4 run of the same change gave the same ratios (3.53 vs 3.99 s hot, 4.20 vs 6.24 s cold). Every arm passed a bitwise and
is_pinnedcheck against a fresh reload of the file.Measured and not adopted:
MADV_POPULATE_READon the source (slower),MAP_POPULATE(slower),MADV_HUGEPAGEon the arena (no effect), andpreadvstraight into the arena (faster cold, but it would read the file's bytes rather than memory's).Tests
test_h3_low_vram_stream.py: copies run on the pool with the same placement and byte-identical arenas as a 1-thread run, owners and tied tensors preserved; a failed pooled copy replaces nothing.test_diffusion_registered_slabs.py: the arena contents at register time equal the final contents and only unused bytes are zeroed;fillerrors propagate.test_diffusion_h3_trainerregional-compile cases, 2test_diffusion_segment, 2test_integrated_gpu_no_host_pin) and are unrelated.Not proven