Skip to content

Unsloth Studio: explain why Model Memory skips the RAM lock when a model is fully on the GPU - #9571

Merged
danielhanchen merged 83 commits into
mainfrom
fix-9549-model-memory-applicable
Sep 29, 2026
Merged

danielhanchen merged 83 commits into
mainfrom
fix-9549-model-memory-applicable

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

With a GGUF fully offloaded to a discrete GPU there is no copy of the weights in system RAM, so "Keep model in GPU memory" skips --mlock on purpose. The panel never said so (#9549).

  • ModelMemoryResponse gains mlock_applicable, read from the launch record the backend already keeps. True with nothing loaded.
  • The panel explains the case in one line and re-reads after a model load.
  • New locale key in all 12 locales. No change to how llama-server is launched.

Model memory panel before and after

Fixes #9549

With the model fully offloaded to a discrete GPU there is no copy of the
weights in host RAM, so apply_model_memory_policy skips the lock on purpose
and residency is carried by the idle-unload veto alone. Nothing said so. The
toggle read on, mlock_active read false, reload_required read false, and the
panel stayed quiet, so a user who turned both switches on and watched nothing
change had no way to find out why.

Measured on a gfx1151 ROCm host across the 2x2 of toggles crossed with an
empty and a --mlock extras list: five of the eight launches are byte-identical
and the argv falls into three classes, so the policy is behaving as documented
and only the reporting was missing.

_active_launch_placement already carried mlock_applicable for the reload check
and threw it away; the response now keeps it, and the panel explains the case
in one line without touching the switches, which are saved policy for the next
load and not a description of this one.

Two smaller things in the same area:

- The section read the settings once on mount and never subscribed, so a field
  describing the RUNNING launch would have been stale as soon as a model was
  loaded. It now forces the read and follows the existing change bus.
- noRamReserveHint promised to "stream the weights into VRAM instead of
  keeping a full copy in RAM", which reads as a promise that RAM use goes
  away. It removes the reservation, not the mapping; the mapped pages stay
  against the process until the OS reclaims them, and on Windows that is the
  number the reporter was looking at.

Fixes #9549
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@mahiatlinux
mahiatlinux self-requested a review August 29, 2026 22:43
@danielhanchen danielhanchen self-assigned this Sep 21, 2026
Resolved against main's Windows no-reserve DirectIO rewrite (#10618), which
had rebuilt the same Model Memory machinery this branch touches. Main owns the
machinery, this branch owns the reporting payload:

- llama_server_args: main's `settings=` / `gpu_offload_confirmed=` / `env=`
  signatures throughout; this branch's `model_memory_settings=` was the same
  idea under a longer name. `memory_state_satisfies_settings` keeps main's
  DirectIO body but reads the caller's snapshot locals, and takes `settings`
  LAST so every existing positional caller still binds `direct_io`.
- llama_cpp: main's `_record_memory_state`, `_mem_*` locals, widened
  `probe_vulkan` gate, richer `_memory_mlock_applicable` and fail-closed
  `type_known` parse. `_vulkan_targets_are_igpus` goes back to main's "no
  answer folds into not integrated": paired with the fail-closed parse, this
  branch's inversion would have page-locked a host-sized copy of a discrete
  full offload on any legacy five-column probe, which is the reservation #9549
  reports. `_memory_policy_active` unions main's `_mem_managed_is_effective`
  with this branch's `_load_mode_policy_suppressed`.
- The --fit off retry is hand-ported rather than merged: main's DirectIO arm
  with this branch's rebuild-and-replace, its "block no longer on argv" guard,
  and applicability taken from the recorded state so a restored reserving mode
  keeps something to lock.
- routes/settings: main's 7-tuple `_active_launch_placement` carrying this
  branch's media/STT-resident branch, the `reserves_ram` fold-in, the
  `placement=` threading, and the new `mlock_applicable` / `mlock_skip_reason`
  helpers. Dropped the `not want_mlock` short-circuit: it predates the
  contract both docstrings state, and hid a user's own --mlock.
- Frontend: union in chat-api and the dictation adapter; the locales take
  main's `noRamReserve` wording, which already says required CPU buffers can
  still use RAM, and keep this branch's two new keys.
- Tests updated to main's API and to the resolutions above.
pre-commit-ci Bot and others added 2 commits September 21, 2026 07:11
… scope

The resync line assigned _mem_policy_argv inside the nested retry, which makes
it a local of that function and shadows the block built in load_model, so the
guard that reads it a few lines earlier raised UnboundLocalError. Caught by
ruff F823 in CI. The block is not read again after this arm, so dropping the
assignment restores the reviewed behaviour rather than adding a nonlocal.
danielhanchen and others added 3 commits September 21, 2026 09:32
…robe

The launch hands _weights_in_host_memory the fit's own Vulkan reading so the
classifier does not spawn a second probe. It was handed _shared_gpu_ids, which
is that reading INTERSECTED with the devices the planner kept, and the two
questions are not the same one:

  _shared_gpu_ids  answers for the devices the planner kept
  the classifier   is asked about the ordinals the CHILD ends up on

A pass-through --device wins over the planner's selection, so the child can run
on an iGPU the planner dropped. Absent from the narrowed set, that iGPU reads as
a discrete card, the weights read as not host-backed, and the page-lock a Keep
Resident user asked for is withdrawn without a word. Enumerated over probe rows
x selected ordinals x derivation, 150 cells: 12 disagreed with the probe and all
12 in that direction.

The narrowed set stays as it is for the pricing consumers. The classifier now
gets the reading only when the intersection lost nothing, which is the only case
where it can answer for an ordinal the planner did not keep; otherwise it is
None and the probe is asked, exactly as before this branch.

Also drops _memory_launch_pending from the two test backend factories: main
renamed that attribute to _memory_pending_launch, so the key was dead and named
after an API that no longer exists.
Three reporting defects in the retry arm, all found by tracing the arm's
branches rather than by a failing test:

- The rebuild ran before the "is the block still on the argv" guard, so a retry
  that keeps the page-lock because the block moved still emitted whatever
  apply_load_mode_policy logs while deciding. The operator read "Don't reserve
  system RAM drops the requested load mode" for a decision nothing applied. The
  guard is asked first now, and the rebuild only runs where its answer is used.

- The arm that keeps the lock BECAUSE the rebuilt policy still reserves host RAM
  logged nothing at all, which in the log is indistinguishable from the policy
  having emitted nothing in the first place. Those two lead to opposite
  conclusions about the child, so it says which one happened.

- The comment on the iGPU snapshot claimed the RAM guard reads None as "not
  measured". It does not: every pricing consumer coerces None to the empty set.
  Only the page-lock classifier reads the difference, which is what the comment
  now says.

No cell of the policy matrix moves: the arm's conditions are the same three, in
the same order, with the rebuild hoisted into the branch that uses it.
@danielhanchen

danielhanchen commented Sep 21, 2026 •

Copy link
Copy Markdown
Member Author

Rescoped to the core fix: 17 files, +176 / -14 against main (was ~2,400 lines). The dropped launch-path changes had turned --load-mode none into mmap+mlock on discrete full offloads (slower load, 333 MB locked); nothing under core/inference is touched now.

Verified on a B200: with Keep Resident on and a GGUF fully on the GPU, the panel shows the new line and the API returns mlock_applicable: false. Backend 2584 passed (3 failures also fail on main: gguf not installed here). Frontend tests, typecheck and locale parity clean.

Not tested: the reporter's Windows ROCm host.

…omoting it to mmap+mlock

A reserving load mode (--load-mode none, --no-mmap) on a full GPU offload keeps no
host copy of the weights, so it is not a reason to page-lock. Promote only when the
preview actually locks, report mlock_applicable from the launch record alone, and
restore main's APU-retry assertion that a user's --no-mmap survives.
pre-commit-ci Bot and others added 4 commits September 28, 2026 12:51
Keep only what #9549 needs: the settings response reports whether the running
llama.cpp launch has a host copy to page-lock (read from the launch record main
already keeps), and the panel says so in one line, re-reading after a model load.
Launch-path changes, ungoverned-runner reporting and the frontend write queue
and polling go back to main's behaviour.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 17ae386693

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

Comment on lines +73 to +75
const unsubscribe = subscribeModelLifecycle(({ loading }) => {
if (!loading) {
refresh();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Refresh the memory state after explicit unloads

This subscription only observes load lifecycle events, so ejecting the active chat model through /api/inference/unload never triggers a refresh. If a fully offloaded model displayed the new notice while the settings page remained mounted, ejecting it from the loaded-models control leaves mlockApplicable false and the panel continues claiming the model is fully on the GPU even though no model is loaded. Subscribe to the successful unload/ejection path as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not taking this one: Settings is a modal dialog, so the chat eject control cannot be used while this panel is mounted, and the panel re-reads on every open. The unloads reachable inside Settings (embedding, SystemOne) are other runtimes this note does not describe.

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 17ae386693

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0b0803fd7c

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

) => {
setIsSaving(true);
setError(null);
latestRef.current += 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Block lifecycle refreshes for the whole save

When a model lifecycle completion occurs after this counter is incremented but before the awaited PUT finishes, its refresh receives a newer ID and its GET can read the pre-save settings. If that GET resolves after the PUT, it passes the guard, overwrites the successful save response, and republishes the stale value to other Model Memory subscribers, so the switches and model-config notices can revert until another refresh. Fresh evidence beyond the earlier report is that the fix advances the counter only before the await, while the lifecycle callback remains enabled throughout the save; serialize or suppress these refreshes until the PUT settles.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not taking this one: it needs a load to finish inside the PUT and the server to read settings before the save commits yet answer after it. That window is below the bar for a panel note, and the republication to other subscribers is main's existing loadModelMemorySettings behaviour, not new here.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 0b0803fd7c

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] AMD: Unsloth Studio loads models into system RAM despite VRAM-only settings (W7900 + W7500, Strix Halo)

4 participants