Skip to content

Studio: keep Inkling 2 compute regrowth free under --fit on - #18941

Merged
danielhanchen merged 7 commits into
mainfrom
studio-inkling2-fit-target-regrowth
Nov 22, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
studio-inkling2-fit-target-regrowth

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #18919. When Studio hands an Inkling 2 load to llama.cpp's fitter (--fit on), the fitter measures only the load-time compute reserve and leaves --fit-target free on each device (1024 MiB by default). The Inkling 2 banded compute buffer grows past that reserve after the first prompt. At the -ub 2048 raise Studio applies when MoE experts sit in RAM, the measured growth is 1.5 to 2.5 GB, more than the margin, so a session can OOM mid-prompt after a clean load.

This adds the expected growth to the --fit-target margin, for Inkling 2 --fit on launches only:

  • Growth = runtime estimate minus a calibrated lower bound of llama.cpp's load-time reserve, at the launched micro-batch, K/V type and context.
  • The runtime side is Studio: price Inkling 2 quantized KV by what the bundled llama.cpp runs #18919's banded estimate, unchanged, with a floor for the margin only, because past about 262K cells at -ub 2048 the estimate reads under the measured runtime (524K: 5572 vs 5836 MiB).
  • The reserve bound is fitted under every Inkling-2-Mini reading we have on both builds, for ub 512 to 2048 and 32K to 1M cells. On How to merge lora adapter with pretrained weights? #399 it is the larger of an activation floor and the 4 B per cell shared mask. On older builds it is the activations plus 16 B per cell.
  • Both sides use Mini's activation estimate as a fixed scale, so the growth follows the micro-batch and context the way Studio: price Inkling 2 quantized KV by what the bundled llama.cpp runs #18919's runtime term does, not a larger model's activation size.
  • With no -c (Manual + Auto), the fitter picks the context, so the largest growth from the --fit-ctx floor up to the native length is used.
  • It is added on top of the margin Studio already emits: the budget slider delta, the small-card trim, the per-device list and the mid-session OOM headroom. A user's own --fit-target still wins.
  • It is 0 where there is no banded path: Metal, Vulkan, SYCL, pre-How to merge lora adapter with pretrained weights? #399 quantized K/V, or no flash attention.
  • Non-Inkling 2 models, including Inkling 1, never reach the new code.

Measured growth vs emitted

Inkling-2-Mini UD-IQ1_S, CUDA, -np 4 --kv-unified -fa on, slot 0 filled to 90% of the context. The compute buffer is read from the load log and from memory_breakdown_print at exit. The ub 2048 rows and the 32K ub 1024 row are --fit on launches on the #399 build b12628; the other rows are earlier --fit off readings. The 32K ub 1024 row reproduces the earlier --fit off reading (1080 vs 1081 MiB).

build ub ctx K/V reserve at load after prompt measured growth emitted growth pad
#399 512 32K f16 / q8_0 258 747 489 617 +128
#399 512 128K f16 382 837 455 689 +234
#399 512 128K q8_0 541 837 296 689 +393
#399 512 512K f16 1078 1077 0 421 +422
#399 512 1M f16 2102 2101 0 173 +174
#399 1024 32K f16 514 1594 1080 1249 +169
#399 1024 1M f16 4206 4205 0 393 +394
#399 2048 32K f16 / q8_0 1035 3582 2547 2690 +143
#399 2048 128K f16 / q8_0 1443 3925 2482 2866 +384
#399 2048 256K f16 2275 4492 2217 2397 +180
#399 2048 512K f16 4323 5836 1513 1681 +168
b12602 512 32K f16 541 811 270 379 +109
b12602 512 128K f16 1357 1497 140 323 +183

All values are MiB. The emitted growth is added on top of the existing margin (1024 MiB by default), so the real headroom is larger than the pad shown.

Inkling-2-Small launch matrix

432 simulated launches (sparse real-header GGUFs, 24 / 48 / 80 / 96 / 180 GB and 2x80 GB cards, auto and fixed context, f16 and q8_0, shipped b12602 and #399 builds), #18919 head versus this branch:

  • 421 launches go --fit on. 315 now carry a larger --fit-target. The other 106 are q8_0 on the shipped build, which runs dense attention and has no banded growth.
  • How to merge lora adapter with pretrained weights? #399 build at -ub 2048, f16 and q8_0: +2978 to +3250 MiB (margin 4002 to 4274 MiB), 159 launches.
  • How to merge lora adapter with pretrained weights? #399 build at the default micro-batch: +601 to +689 MiB (margin 1337 to 1713 MiB), 51 launches.
  • Shipped build, f16 at -ub 2048: +1964 to +2204 MiB, 78 launches.
  • Shipped build, f16 at the default micro-batch: +347 to +540 MiB, 27 launches.
  • Every launch still loads. Outside --fit-target, all 432 argv are identical.

Non-Inkling models

384 launches of qwen3moe, llama, inkling, gemma3, deepseek4 and glm4moe (8 / 24 / 48 GB and mixed cards, 1 to 80 GiB weights, f16 and q8_0, GPU and Manual + Auto, 276 of them --fit on) produce byte-identical argv on #18919 head and on this branch.

Tests

studio/backend/tests/test_inkling2_fit_target_regrowth.py:

  • for every measured row, emitted growth >= measured growth + 100 MiB, and the reserve bound <= the measured reserve;
  • Studio: price Inkling 2 quantized KV by what the bundled llama.cpp runs #18919's runtime estimate is unchanged;
  • the growth does not scale with a larger model's activations;
  • an unknown context takes the largest growth;
  • no growth without a banded path;
  • the launched --fit-target with the default margin, the small-card trim and the per-device list;
  • non-Inkling 2 argv is unchanged even with the growth forced to 4096 MiB.

On #18919 head, 5 of the 49 tests pass (the unchanged cases) and the rest fail. The neighbouring fit, placement, compute buffer, OOM respawn, sidecar and Inkling suites give the same results, with the same failing test IDs, as #18919 head.

Not covered

Test fix

test_mixed_cards_keep_their_own_margins_plus_the_headroom (from #18920) reached torch.cuda through the datacenter and integrated-GPU probes on a host with visible GPUs, so the CPU-test guard failed it at teardown. It now fakes the device info the way test_auto_fit_target_trim.py does. On CI runners with no GPU it already passed.

Review fixes

  • On the currently shipped llama.cpp build (pre-How to merge lora adapter with pretrained weights? #399 banded path), the margin counted Studio: price Inkling 2 quantized KV by what the bundled llama.cpp runs #18919's upper-bound load reserve as post-prompt growth past about 262K cells, which kept up to 4.7 GB free that the measurements show is never used (for example 5228 MiB on 12 and 16 GB cards at 1M context). Growth on that build is now priced from the runtime term only; every measured row keeps its value.
  • The budget delta is floored first (the same 512 MiB floor the emitter applies), then the regrowth is added on top, for the single margin and every per-device entry, so a low budget slider can no longer eat the reserve.
  • The mixed-card respawn test also stays off torch.cuda when run alone.

A base vs head launch matrix (516 pairs: 8 to 24 GB cards, 2 x 24 GB and 24 + 8 GB, sparse header-only GGUFs) loads on both sides, and nothing differs outside --fit-target.

llama.cpp's fitter sizes only the load-time compute reserve, but the
Inkling 2 banded buffer grows past it after the first prompt. Add the
estimated growth (runtime minus reserve at the launched micro-batch,
from the same banded estimate) to every --fit-target margin Studio
passes for an Inkling 2 --fit on launch.
…bound

The growth kept free is now the runtime estimate less a lower bound of
llama.cpp's load-time reserve fitted under every Inkling-2-Mini reading
(ub 512-2048, 32K-1M cells, both builds), with a runtime floor where the
banded estimate reads under the measured runtime past ~262K cells. The
#18919 estimate itself is unchanged.
…query

On a host with visible GPUs the test reached torch.cuda through the datacenter and
integrated-GPU probes, and the CPU-test guard failed it at teardown. Fake the device
info the same way test_auto_fit_target_trim does.
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T20:51:17.199324Z 606618a 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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…alone

Hardware detection also names the cards through torch.cuda when the test runs
by itself on a host with visible GPUs, so fake that query too.
@danielhanchen

Copy link
Copy Markdown
Member Author

The (Python 3.13, a-f) shard of the staging run fails studio/backend/tests/test_batch_sizes_per_load.py::test_the_recorded_micro_batch_is_derived_from_the_slots_that_launched with expected thirteen re-derivations, found 14. It passes on main: this PR adds a fourteenth _ubatch_for_slots(...) call to LlamaCppBackend.load_model, and the test counts them by AST.

If the new call is intended (the Inkling 2 regrowth re-derivation), raise the count to 14 and add it to the list in the comment above the assertion, so the guard keeps describing every re-derivation.

The other reds on that run are not from this PR: the Core legs (PEP 604 unions in diffusion_lanczos_triton.py) and seam-guard (video_vae_multigpu) are red on main and fixed by #18945, and the aiter failure in the rest shard is fixed on main by #18942.

danielhanchen added a commit that referenced this pull request Nov 22, 2026
…r the two-card Wan decode with the VAE seam guard (main reds) (#18945)

## Summary

Two checks are red on `main` and fail on every PR that runs them, for
example the staging run for #18941:

| Failing check | Introduced by | Which side was wrong | Fix |
|---|---|---|---|
|
`tests/test_python39_compatibility.py::test_studio_evaluated_unions_do_not_grow`
(`Core`, all three combos): "36 studio files now evaluate PEP 604 unions
on the floor, against 35 recorded" | #18917 added
`core/inference/diffusion_lanczos_triton.py` | Code. Its public entry
point declares `crop`, `mean` and `std` as `tuple[...] \| None`, which
is evaluated when the function is defined and raises `TypeError` on
Python 3.9. | Those three parameters become `Optional[tuple[...]]`. No
`from __future__ import annotations`: the module's Triton kernels read
their `tl.constexpr` annotations, which that import would turn into
strings. |
|
`studio/backend/tests/test_vae_tile_geometry.py::test_every_tiled_decode_patch_is_probed`
(`seam-guard`): "['video_vae_multigpu'] rebind a VAE's tiled_decode but
expose no install(vae) this guard applies" | #18829 added
`core/inference/video_vae_multigpu.py` | Test registry. The module
mirrors the stock `AutoencoderKLWan.tiled_decode` body (checked by AST
fingerprint) and only shares its tiles between two cards. It keeps the
stock tile grid, and
`test_video_vae_multigpu.py::test_split_tiles_match_the_stock_tiled_decode_bitwise`
proves it. Its `install(pipe, partner)` takes a pipeline, not a VAE, so
the guard cannot apply it. | Listed in `GEOMETRY_PRESERVING` with that
reason, which is the remedy the guard's message names for a module that
keeps the stock tiles. |

Neither fix changes behaviour.

## Testing

- `tests/test_python39_compatibility.py`: fails on `main` with the
message above, 8 passed with this change.
- `tests/test_vae_tile_geometry.py` and
`tests/test_video_vae_multigpu.py`:
`test_every_tiled_decode_patch_is_probed` fails on `main`. With this
change, 55 passed, 2 skipped, 2 xfailed (the recorded known seams).
- `tests/test_diffusion_ref_preprocess.py` and
`tests/test_diffusion_ref_preprocess_gate.py`, the suites that exercise
the Lanczos module: 33 passed.
- `ruff check --select F` on the Lanczos module: clean.
…growth

On builds before #399, #18919's 18 B/cell load-time reserve rate is a planner
bound that overtakes its 15 B/cell runtime rate at long contexts, as the
measured reserve does (growth 270 -> 140 MiB from 32K to 128K). The --fit on
margin read that surplus as post-prompt growth, so it rose again past ~262K
cells (4332 MiB at 1M, -ub 2048) and a Manual + Auto launch with no -c kept
that maximum free whatever context the fitter picked. Price only the runtime
term against the reserve bound there; every measured row keeps its value.
A budget slider above the default gives a negative margin delta, which the
emitter floors at 512 MiB. The regrowth was added to the raw delta, so the
negative balance ate it: a 48 GB card at a 100% budget (delta -1474 MiB) kept
2239 MiB free against 2547 MiB of measured growth. Floor the delta first, for
the broadcast margin and for every per-device entry, then add the growth.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 606618a0f6

ℹ️ 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".

…atch re-derivations

The regrowth margin sizes its growth at the micro-batch the launched slots get, so
load_model now derives it fourteen times; the guard lists the new site.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 22, 2026
@danielhanchen
danielhanchen merged commit e2107d0 into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the studio-inkling2-fit-target-regrowth branch November 22, 2026 23:10
danielhanchen added a commit that referenced this pull request Nov 22, 2026
…, and read checked-in sources as UTF-8 (main reds) (#18952)

## Summary

Two checks are red on `main` and fail on every PR that runs them, for
example the staging run for #18941:

| Failing check | Introduced by | Which side was wrong | Fix |
|---|---|---|---|
|
`tests/studio/install/test_mlx_install.py::test_mlx_image_engine_pins_leave_the_mlx_stack_alone`
(`studio-install` shard): `assert
'mflux==0.24.0'.startswith('mflux==0.22.')` | #18933 bumped mflux to
0.24.0 (0.22.0 kept for Python 3.10) | Test. The bump is intended. The
test pinned a version literal of its own, so every mflux bump breaks it.
| The test now checks the installer pin against the MLX engine itself:
`_MLX_DIFFUSION_PINS` must be exactly `mflux==<MFLUX_VERSION>` for the
running interpreter, and that version must be in
`MFLUX_COMPATIBLE_VERSIONS`. Both are read from
`mlx_diffusion_convert.py`'s source, since importing it needs numpy and
the installer tests do not have it. The check is stricter than before:
an installer pin that drifts from what the engine accepts at runtime now
fails. |
|
`tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding`
(`rest-2` shard): 3 reads with the platform default encoding | #18923
(`tests/test_adapter_unc_base_guard.py:30` and `:91`) and
`tests/test_inkling2_support.py:367` | Tests. On Windows a bare
`read_text()` uses the locale code page. | `encoding = "utf-8"` on those
three reads. |

Only tests change.

## Testing

- `tests/studio/install/test_mlx_install.py`,
`tests/test_source_read_encoding.py`,
`tests/test_adapter_unc_base_guard.py`,
`tests/test_inkling2_support.py`: 3 failed on `main`. With this change,
92 passed, 4 skipped.
- Sabotage: with `_MFLUX_PIN` changed to `mflux==0.23.0`, a version the
engine does not expect, the mflux test fails with `assert
'mflux==0.23.0' == 'mflux==0.24.0'`.
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