Skip to content

Unsloth Studio: restyle a video with Cosmos3-Nano-Sim-Transfer on sd-server - #16975

Merged
danielhanchen merged 49 commits into
unslothai:mainfrom
LeoBorcherding:feat/restyle-video
Nov 9, 2026
Merged

danielhanchen merged 49 commits into
unslothai:mainfrom
LeoBorcherding:feat/restyle-video

Conversation

@LeoBorcherding

@LeoBorcherding LeoBorcherding commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Adds a Restyle mode to the Unsloth Studio video page: pick a short clip (or a folder of frames), check the edge preview, type a prompt, click Restyle. Unsloth Studio traces Canny edges, sends them to sd-server running NVIDIA's Cosmos3-Nano-Sim-Transfer, shows progress per output frame, and plays input, edges and result side by side. The result also lands in the video gallery.

Why: Cosmos3 restyles a clip on one GPU in under a minute, through the sd-server Unsloth Studio already runs, with no Python model stack.

Draft: needs the Cosmos3 port of stable-diffusion.cpp (unslothai/stable-diffusion.cpp#53) in the Unsloth sd.cpp prebuilt; until that ships, this stays a draft.

Input | edges | restyled, from Unsloth Studio:

car
sea

light dark
Whole flow, cancel, narrow window, bad file, not set up
light dark
768 px
unreadable file
weights or sd-server missing

What's in it

It reuses SdCppServer as the MiniMax-H3 path does (sd-server, /sdcpp/v1/vid_gen, job polling, cancel) instead of adding a family to VideoBackend. The server is started for each restyle and stopped when it ends, so its roughly 30 GB never sits idle outside the GPU arbiter; it takes the GPU as a video load would and refuses other loads while it runs.

  • core/inference/video_restyle.py: frames at 30 fps (PyAV), cover-crop to 832x480, a numpy Canny that matches cv2.Canny(rgb, 100, 200) pixel for pixel, the edge preview, a setup check (weights set, sd-server built with Cosmos3), and a one-at-a-time runner.
  • routes/video_restyle.py: GET status, POST preview (runs no model), POST restyle, progress, cancel, unload, and input|control|output.mp4. Jobs are scoped to the account that started them.
  • Frontend: the Restyle pill (restyle-panel.tsx), API calls in features/video/api.ts.
  • Weights and binary come from env for now: UNSLOTH_COSMOS3_TRANSFER_MODEL, UNSLOTH_COSMOS3_TRANSFER_VAE, SD_SERVER_PATH.

Speed

Colab G4 (RTX PRO 6000 Blackwell), Linux, 121-frame car clip, submit to done in the UI, first run after the server loaded unless noted.

sd.cpp e6a98b1 sd.cpp a8345e5
Unsloth Studio at bdab3a0 47.4 s
Unsloth Studio at d2fe290 47.5 s 47.5 s (44.5 s on a second run)
transformer only, output fps 7.6 fps 9.4 fps

The faster port saves about 3 s in the transformer, but end to end in Unsloth Studio the first run is the same 47.5 s at both sd.cpp commits (wip-car-light.log, final-fast.log); the 44.5 s is a second run with warm caches.

About 4 s edges, 12 s warm-up and control encode, 14 s for the frames, 15 s decode and export with the old port. Restyle against a direct sd-cli run with the same edges and seed: 35.3 dB PSNR mean (measured at 823eb1d, MJPG plus H.264 on our side).

Memory and speed

Restyle in Unsloth Studio: peak VRAM, resident VRAM after the run, and time

Measured at d2fe290 with sd-server built from stable-diffusion.cpp a8345e5, on a Colab G4 (RTX PRO 6000, 96 GB). A Restyle of the car clip peaks at 39.9 GiB (nvidia-smi card total, set in the VAE decode), the same as sd-cli alone, and sd-server keeps 29.3 GiB loaded after the run until it's unloaded. Not run: any other GPU, q8_0 weights.

Tests

  • tests/test_video_restyle.py: 91 passed at 7fcc306 (GPU handoff, reservations, account retirement, frame prep, Canny, preview, setup check, request body, runner with a fake sd-server, cancel, account scoping, routes, the upload middleware).
  • npm run typecheck passes, frontend tests 14952/14952, eslint clean on restyle-panel.tsx.
  • Whole flow through the UI in headless Chromium on the G4, light and dark, car and sea clips, plus cancel, a 768 px window, an unreadable file, a 120 fps clip and a host with no weights.

Not run or not done

  • No download or install path: weights and binary are env vars until there's a hosted merged checkpoint and a prebuilt with the port.
  • No GPU arbitration: the bf16 transformer is about 32 GB and doesn't ask gpu_arbiter for room.
  • Cancel while frames are generating: sd-server answers 409, so Unsloth Studio stops the server after a grace period and the next run reloads the weights. Fixing that is an sd-server change.
  • Edge control only, 832x480 at 30 fps, up to 4 s; no per-frame preview.
  • Only NVIDIA Blackwell on Linux. Not run on ROCm, Vulkan, Metal, CPU or Windows; other hosts see the "not set up" notice.

Before / after on a host without the Cosmos3 weights

Merge base 4accec06a vs head c58b2b5d2, two isolated installs (CPU host, video page enabled with UNSLOTH_VIDEO_ALLOW_CPU=1). Before: the mode pills are Create / Train and GET /api/inference/video/restyle/status is 404. After: Create / Restyle / Train. The Restyle panel shows the "Restyle is not set up here" notice and the Restyle button is disabled; the status route returns available: false.

Video page mode pills and Restyle panel, before and after

…ges clip in the result

Picking a clip now shows one frame next to the edges the model follows and how
much of the clip is used (the first 121 frames), before anything heavy runs. A
host without the weights or a Cosmos3 sd-server sees why up front instead of an
env var error after uploading. The result plays input, edges and output, shows
the seed, and the Restyle button can't be double-submitted. Undecodable uploads
get a plain message instead of the PyAV exception.
… rail width on the UI scale

The progress line counted latent frames (0 to 31), which only means something
to someone who knows the VAE; it now counts output frames (1 to 121) and says
"Preparing the edges for the model" while the control video is encoded. The
header showed the Create tab's model picker, which does nothing for a restyle;
it now names the one model Restyle runs. Restyle uses the Generate button's
fill, Cancel gets the spinner Create's Cancel has, and the rail width follows
the UI scale like the Create rail (two frontend tests that #769 left red).
…n it is stopping, keeps tall results in their pane

Picking a new clip left the previous run's three players on screen, even beside
an error for the new file. Cancel gave no feedback for the seconds sd-server
needs to stop; the button now reads Stopping. On a narrow window the stacked
players were centred taller than their pane and drew over the form; they use
safe centring and scroll instead. Long high-frame-rate clips are fitted only for
the frames kept (a 120 fps source dropped three in four after fitting them).
@danielhanchen

Copy link
Copy Markdown
Member

The two Backend CI reds here, (Python 3.13, a-f) and (Python 3.13, rest), come from the new routes.video_restyle module meeting two backend guards:

  1. studio/backend/tests/test_desktop_auth.py::test_the_router_stub_covers_every_router_main_imports

    AssertionError: main.py imports from routes.{video_restyle}, which this file never registers in sys.modules, so the real package would be imported instead
    

    main.py now has from routes.video_restyle import ..., and that test file stubs each routes.<submodule> that main.py imports with monkeypatch.setitem(sys.modules, "routes.<name>", ...). Adding a routes.video_restyle stub next to the existing ones should clear it.

  2. studio/backend/tests/multi_account/test_alice_bob_matrix.py::test_object_route_factory_completeness

    AssertionError: ['routes.video_restyle:GET:/video/restyle/{job_id}/{kind}.mp4']
    

    Every route that serves a per-user object needs an entry in the multi-account isolation matrix: a Factory in studio/backend/tests/multi_account/factories_media.py (the routes.video:GET:/video/gallery/... entries there are the closest model), or a SKIPPED entry with a reason if it cannot be exercised. That is the check that one account cannot fetch another account's restyle output, so a factory is preferable to a skip.

@danielhanchen danielhanchen self-assigned this Nov 7, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 7, 2026
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

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

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-09T22:26:13.426224Z
We couldn’t complete this review. Comment @codex review to try again.
7fcc306 Draft marked ready
ℹ️ 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.

…ancel off the event loop, fence stale clips

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 9, 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 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: c42ee378b6

ℹ️ 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 +572 to +574
if self.holds_gpu() or video.loading_repo_ids() or video.status().get("loaded"):
return
gpu_arbiter.release(gpu_arbiter.VIDEO)

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 Release the VIDEO claim atomically with the idle checks

When a Create load overlaps a restyle's cleanup, the load can already hold the arbiter lock without having published its VideoBackend._loading state. These checks then see an idle backend, release() waits for the load's registration to finish, and subsequently drops the new VIDEO residency. A later chat/image acquire therefore skips evicting the still-loading or loaded video pipeline and can allocate alongside it, risking GPU OOM. Use the existing gpu_arbiter.release_if() to evaluate the idle predicate under the same lock as the release, or verify the acquisition epoch before releasing.

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 dd739b0: the release goes through gpu_arbiter.release_if(VIDEO, idle), so the idle predicate (runner, Create loading, Create loaded) is evaluated under the same lock as the drop; test_the_video_claim_idle_check_runs_under_the_arbiter_lock.

@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. Chef's kiss.

Reviewed commit: dd739b0fcf

ℹ️ 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 ready for review November 9, 2026 22:26
@danielhanchen
danielhanchen merged commit 945d2b6 into unslothai:main Nov 9, 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.

2 participants