Skip to content

[Feature] AMD NPU: large model downloads check free disk space first, can be cancelled and resumed, refuse a second Unsloth Studio process, and show Verifying/Finishing instead of a frozen 99% - #17526

Merged
danielhanchen merged 7 commits into
unslothai:mainfrom
LeoBorcherding:studio/npu-download-robustness
Nov 14, 2026

Conversation

@LeoBorcherding

@LeoBorcherding LeoBorcherding commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

NPU model downloads (18 to 22 GB) had no free-space check, no way to cancel, and nothing stopping two Unsloth Studio processes on one home writing the same .partial. A full disk mid-write surfaced as a generic error. Resuming rehashed every kept file with no progress event, and the bar sat at 99% while Lemonade's /v1/pull registered the files, because Unsloth Studio drops Lemonade's own events there.

  • Before downloading, the bytes still needed are compared with shutil.disk_usage of the models folder. Too little space fails early, naming GB needed and GB free. ENOSPC mid-write now reads "Out of disk space while downloading ", and the partial is kept.
  • POST /api/npu/models/{id}/download/cancel stops the pull at its next chunk (including during a rehash or a retry wait) and keeps the .partial files, so Download resumes. The Hub NPU catalog row gets a Cancel button. A cancel ends quietly, with no error toast.
  • Each model's download takes an OS file lock (fcntl.flock / msvcrt.locking) on a file beside its folder. A second process gets "already downloading in another Unsloth Studio process (PID n)". A dead process's lock is released by the OS, so there's no stale-lock cleanup.
  • A verifying event is emitted before rehashing a kept file, and a finishing event before Lemonade's register step. The UI shows "Verifying N%" and "Finishing".

Tests

test_flm_files.py: free-space refusal, ENOSPC message with partial kept, cancel then resume from the kept byte, cancel during a rehash, a real second process holding the lock refused and then let through once it's killed, a same-process double download refused, verifying events on resume. test_npu_chat_route.py: the cancel route and the cancelled event. test_npu_backend.py: the finishing event, and a cancelled download keeps its partial and never calls /v1/pull. Frontend: label phases, cancel API, phase tracking and a quiet cancel in npu-catalog-store.

Tested on Windows 11, Ryzen 9 9950X + 2x R9700, no NPU: test_flm_files.py + test_npu_chat_route.py (112 passed) against a local range-capable HTTP server, the multi-account route matrix, frontend typecheck and node tests. The test_npu_backend.py additions skip on Windows and have not run yet (Linux CI will). Not run on a Ryzen AI NPU.

Screenshots

NPU spoofed: fake Lemonade/FastFlowLM on a Windows 11 PC with no NPU; no speed numbers. Before | after: fork phase stack, phase 1 ac3e06e | phase 2 945ba98. Shot at 945ba98; this PR's head has since moved to f888dfe (backend download relay fix, no UI change). Light mode, mint theme, glass on, same on both sides.

Before | after: an NPU download in the Hub gets a Cancel button, and a cancelled row goes back to Download. Not shot: Verifying, Finishing, resume and the free-space refusal need FastFlowLM's file list, which the spoof doesn't have.
17526

Also checked by building Unsloth Studio from this PR's merge base (left) and head (right), on the same spoofed AMD NPU:

Model Hub, format NPU, while a model downloads: the row now has a Cancel button next to its percent.

Before and after

…cked and locked

Check free space before an NPU download and name an out-of-space write as such,
add a cancel route that stops at the next chunk and keeps the .partial files,
show Verifying while kept files are rehashed and Finishing while Lemonade
registers the download, and lock each model's download across processes with a
PID lock file that a dead owner's lock does not block.
A PID lock file had to be cleared when its owner died, and two processes
clearing the same stale lock could delete the one the other had just taken.
An OS lock (fcntl.flock, msvcrt.locking) is released by the OS when its
process dies, so there is no cleanup to race. Also list the cancel route
in the multi-account route inventory.
oobabooga added a commit to LeoBorcherding/unsloth that referenced this pull request Nov 12, 2026
@danielhanchen danielhanchen self-assigned this Nov 13, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 13, 2026
@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-14T13:41:22.404097Z ccc8894 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: 62344949eb

ℹ️ 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 +200 to +201
// 404: the pull already ended.
if (!response.ok && response.status !== 404) {

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 Retain early cancellation until the download POST has started

If Cancel is clicked before the initial download POST reaches _start_download (for example, while authFetch is in its desktop retry delay), followNpuDownload has already marked the row as downloading, but the cancel endpoint returns 404 because no job exists yet. Treating that response as success loses the cancellation: the pending POST can subsequently start the entire multi-GB download, and no further cancel is issued. Retain the cancellation intent until startup is acknowledged, or otherwise prevent cancellation from racing the initial request.

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 this: the window is the start request's auth retry, and the row keeps its Cancel button while the download runs, so a second click cancels it once the job exists.


export async function cancelNpuDownload(id: string): Promise<void> {
try {
await cancelNpuModelDownload(id);

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 Remember cancellation across download-stream reconnects

If the progress stream has dropped and followNpuDownload is waiting to reconnect, a successful Cancel causes the backend to publish cancelled and remove the job before the next follow request. That request then gets 404 rather than the cancellation SSE event, so the reconnected branch shows an error toast saying “The download ended without finishing.” despite the successful cancellation. Record the accepted cancellation locally and use it when reconciling a missing job instead of relying exclusively on receiving NpuDownloadCancelled from the stream.

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 this: it needs the progress stream to drop and the cancel to land inside the reconnect gap, and the cost is the toast text; the partial files are kept either way.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 62344949eb

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

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: 314ec4aa8d

ℹ️ 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 +206 to +207
if (listedDownloaded(id)) return true;
if (error instanceof NpuDownloadCancelled) return 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 Handle cancellation before accepting the downloaded state

If Cancel lands during finishing, Lemonade can register the model before the backend observes the cancellation flag and emits cancelled. The ensuing catalogue refresh then reports downloaded: true, so this success check makes followNpuDownload() resolve true despite receiving NpuDownloadCancelled. The continuations in pickers.tsx and composer-model-picker.tsx use that result to select the model, meaning a picker-initiated download cancelled from the Hub can still switch the selected chat model. Handle explicit cancellation before the downloaded-state reconciliation.

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 ccc8894: an explicit cancel now returns false before the downloaded-state check, so a picker never switches to a model whose download was cancelled while finishing. Test added.

@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: ccc8894cc9

ℹ️ 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 merged commit 14f0b66 into unslothai:main Nov 14, 2026
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.

3 participants