Skip to content

Studio: retry on the GPU with --fit off when llama.cpp's fit step crashes - #16421

Draft
debojyoti-dev wants to merge 15 commits into
unslothai:mainfrom
debojyoti-dev:fix/vulkan-fit-step-crash-16395
Draft

debojyoti-dev wants to merge 15 commits into
unslothai:mainfrom
debojyoti-dev:fix/vulkan-fit-step-crash-16395

Conversation

@debojyoti-dev

@debojyoti-dev debojyoti-dev commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Studio can fall back to CPU when llama.cpp crashes during its own memory-fitting step, even though loading without fitting may succeed. This adds one GPU retry with fitting disabled before the existing recovery continues.

The retry requires a startup failure whose last child log comes from the fitting step. It preserves explicit user fit and context settings, excludes capability and ROCm-library failures, and retains CPU fallback if the retry fails. Managed Auto context, including Studio's internally emitted -c 0, is bounded to the fit-context floor or 8192 instead of the model's native window. Separate KV caches retain that floor per serving slot, and retry commands normalize the managed fit flag so later flash-attention recovery can re-enable fitting.

Fixes #16395

Validation:

The reporter's Windows 10 RX 5700 XT and driver have not been tested. The crash/recovery simulations establish the launcher sequence; they do not establish that the driver can allocate and decode after fitting is disabled. The new tests also reproduce the previously broken flash-attention recovery and per-slot context allocation; they fail before the corrections and pass afterward.

…shes

On an RX 5700 XT with the auto-selected Vulkan build, llama-server dies
with 0xC0000005 while llama.cpp's fit step is still querying device
memory, before any weights load. The existing --fit off retry only runs
when Unsloth placed the model itself, so a launch that handed placement
to --fit on went straight to the CPU fallback.

When a fitter Unsloth turned on is the last thing the child logged
before crashing, retry once with --fit off, pinning -c to the --fit-ctx
floor when the argv names no context so the retry does not jump to the
model's native window. A user's own -fit/--fit or LLAMA_ARG_FIT is left
alone, and a retry that also crashes still reaches the CPU fallback.

Fixes unslothai#16395
@danielhanchen

Copy link
Copy Markdown
Member

(Python 3.13, rest) fails here on studio/backend/tests/test_tp_vision_regression.py::test_fit_off_retry_skipped_on_a_tensor_capability_crash. It passes on main. The test reads LlamaCppBackend.load_model's source and expects _fit_retry_allowed, _startup_crashed and not _capability_crash within the 1000 characters before run_cmd = [*run_cmd, "--fit", "off"]:

tests/test_tp_vision_regression.py:566
    assert "_fit_retry_allowed" in guard and "_startup_crashed" in guard

This PR adds a longer log message and new conditions ahead of that retry, so the guard now sits outside the window. The fix is either to keep the capability-crash skip next to the retry, or to update the test to find the retry's actual if condition, for example via ast, rather than a fixed character window.

The other reds here (Source lint and workflow-trigger lint risky loader, (Python 3.13, g-r), rest-2) come from this branch's base predating fixes now on main (#16428, #16430), and should clear on a merge of main.

danielhanchen added a commit that referenced this pull request Nov 1, 2026
… their copy (#16431)

## Summary

`Chat UI Tests (extra)` fails on PRs based on current main (#16418,
#16421, #16413) in the chat width driver:

```
[chat-width] FAIL in step "measure the 'Standard' preset across 7 viewports" after 30.7s: TimeoutError: Locator.click: Timeout 30000ms exceeded.
  - waiting for get_by_role("option", name="Standard", exact=True)
```

#16408 (`8da4ba2f1c`, customizable new-chat greeting) renamed the chat
width option label from "Standard" to "Default"
(`settings.appearance.custom.chatWidth.standard`) in every locale. The
product change is intended. `tests/studio/playwright_chat_width.py`
clicked the option by its English text, so it waited for an option that
no longer exists.

## Change

- **`playwright_chat_width.py`:** the driver picks each preset by its
position in the open listbox, not by its copy. The component lists the
values as `["standard", "wide", "full"]`. The driver's own names for the
presets (`CHAT_WIDTH_PRESETS`) are unchanged, so the measurements and
messages read the same. I did not switch it to the `_en_catalog` reader,
because `test_en_catalog.py` pins every catalog-reading driver to the
composer workflow, and this one runs in `studio-ui-smoke.yml`.
- **New `tests/studio/test_chat_width_driver_presets.py`:** checks that
`appearance-custom-controls.tsx` still maps over `["standard", "wide",
"full"]` in that order, and that the driver has one preset per option. A
reorder or a new option then fails here instead of measuring the wrong
preset.

## Testing

- The new test, `test_en_catalog.py` and
`test_playwright_suites_run_in_ci.py`: 54 passed.
- Sabotage: with the component's order swapped to `["wide", "standard",
"full"]`, the new test fails.
- The driver itself needs a live Studio. It runs in this PR's own `Chat
UI Tests (extra)` job, and I will only merge once that is green.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@danielhanchen danielhanchen self-assigned this Nov 2, 2026
@danielhanchen

danielhanchen commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Review record for #16421, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#691.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror f172a79bd (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

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

Reviewed commit: f172a79bd0

No triage reaction was recorded on the mirror

Round 2: reviewed mirror e0fb059aed (this PR's 9caabfae8)

chatgpt-codex-connector[bot] (on the mirror)

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

Reviewed commit: e0fb059aed

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror e0fb059aed (this PR's 9caabfae8).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@AssabTigleG

Copy link
Copy Markdown

#16395 (comment)

  1. [Q6_K] LFM3-1.7B-Heretic-Abliterated-GGUF
  2. [Q4_K_M] Qwable-9B-Claude-Fable-5-GGUF
  3. [Q4_K_M] LFM2.5-8B-A1B-Uncensored-GGUF
  4. [Q4_K_M] Mica-v0.1-4B
  5. [PTQ1_0] Swift-Bonsai-2-GGUF

DriverVersion Name
32.0.21033.2001 AMD Radeon RX 5700 XT

{"timestamp": "2026-11-03T03:27:13.861876Z", "level": "warning", "event": "llama-server crashed during startup (exit code 3221225477) with forced --fit off; the fit estimate was optimistic, retrying once with --fit on so it can offload. Crash log: H:\\Unsloth\\studio\\logs\\llama-server\\llama-1793676429-port-50058-try0.log"}
{"timestamp": "2026-11-03T03:27:17.537369Z", "level": "warning", "event": "llama-server hard-crashed at startup (exit 3221225477) with flash attention on; retrying once with --flash-attn off (keeps vision and MTP)."}
{"timestamp": "2026-11-03T03:27:21.209130Z", "level": "warning", "event": "llama-server crashed during startup (exit code 3221225477) with forced --fit off; the fit estimate was optimistic, retrying once with --fit on so it can offload. Crash log: H:\\Unsloth\\studio\\logs\\llama-server\\llama-1793676437-noflash-port-50058-try0.log"}
{"timestamp": "2026-11-03T03:27:24.913136Z", "level": "warning", "event": "The auto-selected Vulkan backend hard-crashed during startup; retrying once with llama.cpp devices disabled."}
{"timestamp": "2026-11-03T03:27:29.665005Z", "level": "warning", "event": "llama-server loaded successfully on CPU after the auto-selected Vulkan backend crashed. GPU acceleration is disabled for this model session."}

llama-1793676444-cpu-port-50058-try0.log

llama-1793676441-noflash-port-50058-try1.log

llama-1793676437-noflash-port-50058-try0.log

llama-1793676433-port-50058-try1.log

llama-1793676429-port-50058-try0.log

server-20261103-082243-pid6292.log

backend-backend-1793676160739-2-s01.log

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 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-13T17:27:50.617651Z 5614c88 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 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: 60806997c5

ℹ️ 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 +32089 to +32092
run_cmd = [
*(self._without_flags(run_cmd, {"-fit", "--fit"}) or run_cmd),
"--fit",
"off",

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 Remove retry-added locks before the new fit-off relaunch

When keep_resident=True, no_ram_reserve=False, and the initial launch is fully GPU-offloaded, _mem_managed is empty, but the existing fit-on recovery can subsequently append _retry_lock. In the supported fit-off → fit-on → no-flash fit-on → no-flash fit-off recovery sequence, this new rung then marks mlock inapplicable while the cleanup below only strips _mem_managed, leaving the retry-added lock on the final child. That pins a complete host copy of an otherwise GPU-resident model and can defeat recovery on RAM-constrained hosts. Track and remove the managed lock actually present on run_cmd, including locks added by earlier recovery rungs.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 61953c9: page-locks added by earlier recovery rungs are tracked and stripped by the fit-off rung.

and not _hip_rocr_mismatch
and self._fit_step_retry_eligible(run_cmd, extra_args, env)
)
if _fit_step_crashed or (

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 Restore residency when the no-flash retry re-enables fitting

For a managed fit-on load with keep_resident=True and no_ram_reserve=False, this newly reachable branch strips the page lock and sets _mem_host_resident and _memory_mlock_applicable false. If the fit-off attempt also crashes, _enable_managed_fit_for_no_flash() re-enables fitting on that stripped argv without reinstating the lock or bookkeeping. A successful no-flash fit-on launch can therefore place weights in host RAM without the requested residency guarantee, and later settings checks incorrectly consider the missing lock satisfied. Reapply the host-memory policy whenever a subsequent recovery switches the fitter back on.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 61953c9: the no-flash fit-on relaunch re-arms the page-lock and its bookkeeping when the fitter is turned back on.

…m when no-flash re-enables the fitter

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@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: 61953c922a

ℹ️ 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 +31440 to +31442
rearmed = _rearm_resident_lock(
flipped, "--flash-attn off retry; it runs the fitter again"
)

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 Drop managed DirectIO when no-flash recovery re-enables fitting

On Windows with no_ram_reserve=True, a managed fit-on crash can now reach the fit-off rung and append --load-mode dio. If that child also crashes, this no-flash path re-enables fitting but leaves managed DIO and its applicability flag intact. The upstream help reproduced in studio/backend/tests/test_load_mode_caps_unknown.py:199 describes dio as “use DirectIO if available”; _drop_managed_dio() documents that it buffers CPU layers and requires confirmed full offload. A fitter that places weights in host RAM can therefore exhaust RAM where file-backed paging would allow recovery. Strip managed DIO and refresh the memory bookkeeping when re-enabling fitting, matching the ordinary fit-on recovery.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 5614c88: the no-flash fit-on relaunch now drops Unsloth's managed DirectIO (record kept for the CPU replay), with a regression test.

@danielhanchen

Copy link
Copy Markdown
Member

@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: 5614c8859b

ℹ️ 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 +31448 to +31451
flipped = self._drop_managed_dio(
flipped,
"the --flash-attn off retry runs the fitter again",
clear_record = False,

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 Restore managed DirectIO on subsequent fit-off retries

On Windows with no_ram_reserve=True and confirmed discrete-GPU offload, the fit-on → fit-off → no-flash fit-on → no-flash fit-off recovery sequence loses managed DirectIO on the final child. This call correctly strips --load-mode dio for the no-flash fit-on attempt, but the preceding fit-off retry already set _mem_host_resident=False, and _rearm_resident_lock() leaves it false under No RAM Reserve. The subsequent fit-off retry therefore skips its DIO policy block; the retained _memory_dio_flags would also suppress reinsertion. The captured upstream help describes dio as “use DirectIO if available” (studio/backend/tests/test_load_mode_caps_unknown.py:199); without it, Windows retains the full host mapping despite the no-reserve setting. Re-evaluate managed DIO against the actual retry argv whenever fitting is disabled, independently of the host-residency transition.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not changing: the final child still loads and runs; without DirectIO it keeps a reclaimable mmap page cache, which costs no correctness and needs four consecutive startup crashes under No RAM Reserve to reach.

danielhanchen added a commit that referenced this pull request Nov 13, 2026
…al extra (#17687)

## Summary

`Core (HF=latest + TRL=latest)` fails on every PR since TRL 1.18.0 was
released today (for example on the staging runs for #17521, #17534 and
#16421):

```
FAILED tests/_trl_trainer_discovery_shim.py::test_unsloth_patches_experimental_trainers_via_thin_wrappers
AssertionError: experimental *Trainer classes declared in source but not importable: [('async_grpo', 'HarnessAsyncGRPOTrainer')]
```

TRL 1.18 added `trl/experimental/async_grpo/openenv_harness.py`, which
defines `HarnessAsyncGRPOTrainer` and imports the optional `openenv`
package at module level. The CI job does not install `openenv`, so that
one submodule cannot be imported. The inline test (in
`consolidated-tests-ci.yml`) finds every `class *Trainer` in
`trl.experimental.*` by regex, walks the package's submodules to locate
each one, and swallows submodule import errors. So the class is reported
as "declared in source but not importable", as if TRL's public surface
had drifted.

TRL is right, and Unsloth does not patch this trainer. The test already
skips a whole experimental package whose `__init__` needs an optional
dependency (`IMPORT-SKIP`), but it had no equivalent for a single
submodule.

Reproduced with TRL 1.18.0 and transformers 5.23.0: 1 failed, 4 passed.

## Change

In the submodule walk, a `ModuleNotFoundError` for a non-`trl` module
counts as an optional-extra skip, but only when the submodule that
failed is the file declaring the class. It is logged as an `IMPORT-SKIP`
line naming the module and the missing package. Every other case still
lands in `missing`:

- a class that is not defined anywhere;
- a submodule failing for another reason;
- a missing `trl.*` module.

## Testing

Running the inline test extracted from the workflow:

- TRL 1.18.0: 5 passed, with `IMPORT-SKIP
trl.experimental.async_grpo.openenv_harness.HarnessAsyncGRPOTrainer:
needs the optional openenv`.
- TRL 1.17.1: 5 passed, unchanged.
- Sabotage: a `trl/experimental/async_grpo/zz_ghost.py` that raises
`RuntimeError` before declaring `GhostTrainer` still fails with
`[('async_grpo', 'GhostTrainer')]`.
- The workflow YAML parses, and the workflow-content tests
(`test_cache_budget_discipline.py`, `test_pip_cache_naming.py`,
`test_zoo_suite_parallel_isolation.py`, `test_trl_cap_sites.py`): 295
passed, 3 skipped.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@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: 5614c8859b

ℹ️ 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 +31403 to +31404
if not _mem_should_mlock or _mem_host_resident:
return run

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 residency applicability even when locking is disabled

On a discrete GPU, with both memory toggles initially off, a managed fit-on → fit-off → no-flash fit-on recovery leaves _memory_mlock_applicable=False: the fit-off rung clears it, and this guard skips restoring it because no lock was requested at launch. If the successful no-flash fitter leaves weights in host RAM, turning Keep Resident on afterward is incorrectly treated as already satisfied by memory_state_satisfies_settings(). Consequently, both the settings reload hint and duplicate-load comparator skip the reload needed to apply residency. Update the host-residency/applicability bookkeeping whenever fitting is re-enabled, independently of whether the current settings require adding a lock.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not changing: with Keep Resident off nothing is emitted or owed, and the existing --fit on rung uses the same gate; the only effect is a reload prompt after toggling the setting post three startup crashes.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 5614c8859b

ℹ️ 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
danielhanchen marked this pull request as draft November 15, 2026 07:21

This branch has not been deployed

No deployments
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] Can't use vulkan on my RX 5700 XT - Windows 10 22H2

3 participants