Skip to content

Studio: fill the H3 conditioner's pinned arenas once, before registering (33% faster cold pin) - #16887

Merged
danielhanchen merged 1 commit into
mainfrom
studio-h3-conditioner-pin-fast
Nov 6, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-h3-conditioner-pin-fast

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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_empty zero-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:

  • Arena placement (largest first, first fit, 512-byte alignment, power-of-two arena sizes) is computed up front, unchanged.
  • Each arena is then filled by _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 optional fill that runs instead of zero_(), before cudaHostRegister. Other callers are unchanged. The torch pinned-allocator fallback fills after allocating.
  • Owners are replaced only after every copy succeeded, so a failed copy now leaves the module untouched (before, some owners could already be replaced). The caller still falls back to streaming unpinned.
  • _parameters / _buffers are 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):

base base A/A head head A/A
hot page cache 3.795 s (3.78-4.32) 3.772 s (3.74-3.79) 3.402 s (3.39-3.41) 3.402 s (3.38-3.47)
cold page cache 6.052 s (6.01-6.23) 6.050 s (6.03-6.08) 4.070 s (4.01-4.12) 4.022 s (3.99-4.13)

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_pinned check against a fresh reload of the file.

Measured and not adopted: MADV_POPULATE_READ on the source (slower), MAP_POPULATE (slower), MADV_HUGEPAGE on the arena (no effect), and preadv straight 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; fill errors propagate.
  • The four new tests fail on base and pass on head. H3, registered-slab, integrated-GPU, segment and pin-arena suites: 1092 passed; 7 tests fail identically on base and head (3 test_diffusion_h3_trainer regional-compile cases, 2 test_diffusion_segment, 2 test_integrated_gpu_no_host_pin) and are unrelated.

Not proven

  • The 7 s vs 29 s run-to-run swing seen on a G4 with the real conditioner was not reproduced: repeat spread here was 1-3%. Its cause (memory pressure, a concurrent denoiser slab pin) is still open; this PR removes a full write pass but does not claim to remove that variance.
  • Only 3-vCPU Colab VMs were used, not a G4 or a many-core host, and not the real 27 GB conditioner in a full Studio load. Linear scaling suggests roughly 1.3 s hot and 6.6 s cold saved for 27 GB, which is an extrapolation.

…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.
@danielhanchen danielhanchen self-assigned this Nov 6, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T08:06:25.817409Z a49a83a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: a49a83a1ff

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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`.
@danielhanchen
danielhanchen merged commit ccc8a82 into main Nov 6, 2026
@danielhanchen
danielhanchen deleted the studio-h3-conditioner-pin-fast branch November 6, 2026 12:39
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.

1 participant