Skip to content

Studio: read lookup tables lazily when needed to enable the MoE cache - #16444

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
oobabooga:studio-moe-cache-lazy-table
Nov 2, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
oobabooga:studio-moe-cache-lazy-table

Conversation

@oobabooga

Copy link
Copy Markdown
Member

Problem

Qwen3.8-Flash-Next UD-IQ4_XS on a host with 94 GiB of RAM and a 48 GB card launches without llama.cpp's MoE cache, so decode runs the overflowing layers' experts on the CPU through the --fit split:

Lookup tables: keeping the 26.8 GB lookup table in RAM (--lazy-mode off); free RAM covers it beside the weights and the KV cache.
MoE cache: system RAM cannot hold the 55.4 GB of routed experts pinned; launching without --moe-cache-mib.

Root cause

The lookup-table decision runs first and keeps the 26.8 GB per_layer_token_embd table resident whenever RAM covers it beside the weights. The MoE cache gate then charges that table, the 55.4 GB of pinned experts and the prompt cache to host RAM, and the sum does not fit. Nothing reconsiders the table decision, although llama.cpp can read the table lazily under --load-mode none too, which frees the RAM the experts need.

Fix

When the cache gate fails only because Unsloth's own --lazy-mode off keeps the table resident, the gate prices the launch again with the table read lazily. If the pinned experts and the prompt cache fit, --lazy-mode off is dropped at the point where --moe-cache-mib auto is actually added, after the effective loader and fitter checks, so a launch that ends up without the cache keeps its resident table. The load mode stays none, so the experts stay pinned.

New log line on that path (it follows the earlier table decision's line, which it reverses):

MoE cache: reading the 26.8 GB lookup table lazily so system RAM can pin the 55.4 GB of routed experts.

Behavior boundaries

  • A host with the RAM for both keeps the resident table and the pinned cache, as before.
  • A host without enough RAM to pin the experts even with a lazy table keeps its existing lookup-table decision and launches without the cache.
  • A user's own --lazy-mode or --load-mode (per-model setting, extra argument or env) is untouched. A user's mmap keeps the cache off and the resident table.
  • Models without a lazily readable table are unaffected.
  • Prompt reads fetch table rows from disk on demand, so they are slightly slower (see Measurements).
  • Pinning the experts leaves less RAM free; during the patched runs the kernel's memory compaction thread (kcompactd) used about one core.

Measurements

Qwen3.8-Flash-Next UD-IQ4_XS with the separate MTP drafter, RTX 6000 Ada 48 GB, i9-14900K, 94 GiB RAM. Studio Auto with the context set to 32768, greedy, thinking off, one request at a time. Before is 0455cae42c, after is this change on top of it; the two were launched alternately, and each figure is the median of 4 requests.

Launch flag Before After
--load-mode none none
--lazy-mode off not passed
--moe-cache-mib not passed auto
other --parallel 4 -c 32768 --ubatch-size 2048 --spec-type draft-mtp same

Decode, tokens/s:

Case Before After Change
Short story (512 generated) 40.0 71.2 1.78x
Code (512 generated) 51.1 92.9 1.82x
Code edit, 1.2K prompt (512 generated) 52.3 101.3 1.94x
After a 4K prompt (128 generated) 46.4 67.1 1.45x
After a 16K prompt (256 generated) 46.2 92.2 1.99x

Prompt reads, tokens/s:

Case Before After Change
4K prompt 1,255 1,098 -12%
16K prompt 1,283 1,210 -6%

Available RAM after the load: 26.2 to 32.7 GiB before, 25.0 to 26.1 GiB after.

Verification

  • test_a_resident_table_that_crowds_out_the_pinned_experts_is_read_lazily_for_the_cache: the cache path with a lazy table, plus the ample-RAM and short-RAM cases. Fails on the base commit.
  • test_a_user_mmap_keeps_the_resident_table_when_the_cache_stays_off: mmap from the per-model setting, extra arguments and LLAMA_ARG_LOAD_MODE keeps --lazy-mode off and no cache. Fails if the flag is dropped at the RAM gate instead of where the cache is added.
  • tests/test_llama_weight_placement.py, tests/test_lookup_table_fit.py and tests/test_llama_cpp_placement.py: 717 passed on 681111a2e2.
  • Measured before the branch was rebased onto 681111a2e2; the patches are identical and none of the commits in between touch the lookup-table, load-mode or MoE cache code.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 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 ⚠️ Failed 2026-11-02T02:02:54.075921Z
We couldn’t complete this review. Comment @codex review to try again.
7d80fd9 PR opened
ℹ️ 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.

@oobabooga

Copy link
Copy Markdown
Member Author

Reached Codex review convergence at oobabooga#443.

@danielhanchen
danielhanchen merged commit df96104 into unslothai:main Nov 2, 2026
danielhanchen added a commit that referenced this pull request Nov 2, 2026
## Summary

`Repo tests (CPU, gloo-tp-pp)` failed on #16444 and #16451 in
`tests/test_pp_env_gloo.py`, which #16407 added. Neither PR touches
pipeline parallel. Both failures are the same case, with an identical
value:

```
test_pp_env_gradient_accumulation_and_microbatches[gemma3_ga1_bs4]
base_model.model.model.layers.0.mlp.gate_proj.lora_A.weight: Tensor-likes are not close!
Mismatched elements: 1 / 64 (1.6%)
Greatest absolute difference: 1.5791505575180054e-05 at index (2, 2) (up to 1e-05 allowed)
```

The identical value means the result depends on the runner's CPU, not on
chance, so any run that lands on that runner type fails.

The adapter comparison runs after several optimizer steps, so it carries
the fp32 reduction-order noise of a 2-stage split against the
single-process reference. The file already records that noise for the
grad norms ("3e-5 absolute ... between two identical reference runs").
On this host, the largest adapter difference across all five cases of
the test is 3.7e-6, which is why 1e-5 looked safe.

## Change

`tests/test_pp_env_gloo.py`: in
`test_pp_env_gradient_accumulation_and_microbatches` only, the
trained-adapter comparison takes `atol = 5e-5`, about 3x the CI value.
`rtol` is unchanged, and so is every other `_same_tensors` call:
save_pretrained, Trainer.save_model and export.

The `_close` call just before it is also unchanged. It pins the step
count and every loss at rel 1e-5. It pins the first grad norm at rel
1e-5 and the rest at 1e-4.

## Testing

- `tests/test_pp_env_gloo.py`: 14 passed, 1 skipped.
- Sabotage: I temporarily scaled every `lora_A` by `1 + 1e-4` in the
pipeline-parallel ranks only. `[ga1_bs4]` and `[gemma3_ga1_bs4]` both
still fail, as they do at `1 + 1e-3`. So a real divergence much smaller
than any pipeline bug would be still fails the test.
danielhanchen added a commit that referenced this pull request Nov 2, 2026
…ard (#16447)

## Summary

Since #16407, `Repo tests (CPU, gloo)` sits right at its step cap. On
main (`9341351954`) it ran 1922 s of the 2100 s cap. On #16444, against
the same base, it hit the cap and was killed (`exit code 124`) after 145
of its 153 tests had passed.

#16407 grew `tests/test_parallel_cp_compose_gloo.py` to 27 tests. I
timed the shard per file locally (`-n 4 --dist loadgroup --durations=0`,
the CI selection). That one file takes 1143 of the shard's 3605
test-seconds, about a third:

| File | test-seconds | share |
|---|---|---|
| `test_parallel_cp_compose_gloo.py` | 1143 | 31.7% |
| `test_tp_env_gloo.py` | 796 | 22.1% |
| `test_tp_replica_gloo.py` | 533 | 14.8% |
| `test_tp_full_finetune_gloo.py` | 429 | 11.9% |
| the other 12 files | 704 | 19.5% |

`gloo-tp-pp` cannot absorb it, because it already runs 1397 s on main.

The file also cannot run four tests at a time on its own. Each test
spawns 2 to 8 gloo ranks. In this PR's first two runs of a dedicated
shard at `-n 4`, the runner was shut down mid-run both times (`The
runner has received a shutdown signal`, exit 143). Measured locally as
summed PSS over the pytest process tree, the peak is:

| workers | peak memory | local wall time |
|---|---|---|
| 4 | about 20 GB | 291 s |
| 2 | about 12 GB | 440 s |

A hosted runner has 16 GB. In the mixed `gloo` shard, the file's tests
were interleaved with lighter ones, so they rarely overlapped four at a
time.

## Change

`.github/workflows/studio-backend-ci.yml`:
- New `gloo-cp` shard: `tests/test_parallel_c*gloo*.py`, with `workers:
2`.
- The repo-cpu pytest command reads `-n ${{ matrix.workers || 4 }}`, so
every other shard keeps `-n 4`.
- The `gloo` shard's `tests/test_p[!p]*gloo*.py` becomes
`tests/test_parallel_[!c]*gloo*.py`. Its other globs are unchanged.
- Comments record why the split and the worker count exist. They also
note that a future `*gloo*` file named `test_p[!ap]*` would match no
shard; `tests/test_ci_repo_cpu_shards.py` reports that as a dropped
file.

`tests/studio/test_backend_ci_parallel_isolation.py`:
- Its command scan substitutes each shard's `workers` (or the
expression's default), the way it already substitutes `selection`.
Before that, its existing "no unexpanded `${{`" assertion caught the new
expression.
- The pinned counts move from 11 to 12 parallel runs, and from 8 to 9
repo-root shards.

## Testing

- With main (now including #16446) merged in, these pass (176 passed):
  - `tests/test_ci_repo_cpu_shards.py`
  - `tests/test_ci_backend_pytest_shards.py`
  - `tests/studio/test_backend_ci_parallel_isolation.py`
  - `tests/test_rendezvous_port_everywhere.py`
  - `tests/studio/test_cache_budget_discipline.py`
- `tests/test_parallel_cp_compose_gloo.py` at `-n 2`: 23 passed, 3
skipped.
- A first attempt, `test_p[!ap]*gloo*.py`, matched no file, and the
guard's unexpanded-pattern check caught it. Hence the explicit
`test_parallel_` globs.
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.

2 participants