Repository navigation
[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
Conversation
…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.
[skip ci]
|
@codex review |
1 similar comment
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| // 404: the pull already ended. | ||
| if (!response.ok && response.status !== 404) { |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…now shared with the detail card) [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 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".
| if (listedDownloaded(id)) return true; | ||
| if (error instanceof NpuDownloadCancelled) return false; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
…l registered while finishing [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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/pullregistered the files, because Unsloth Studio drops Lemonade's own events there.shutil.disk_usageof 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/cancelstops the pull at its next chunk (including during a rehash or a retry wait) and keeps the.partialfiles, so Download resumes. The Hub NPU catalog row gets a Cancel button. A cancel ends quietly, with no error toast.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.verifyingevent is emitted before rehashing a kept file, and afinishingevent 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 innpu-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. Thetest_npu_backend.pyadditions 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 2945ba98. Shot at945ba98; this PR's head has since moved tof888dfe(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.

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.