Skip to content

Studio: MiniMax-H3 GGUF video follows the reference recipe and keeps quality through the MP4 export - #17770

Merged
danielhanchen merged 11 commits into
mainfrom
studio-h3-reference-parity-sdcpp
Nov 14, 2026
Merged

danielhanchen merged 11 commits into
mainfrom
studio-h3-reference-parity-sdcpp

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Makes Studio's MiniMax-H3 GGUF (stable-diffusion.cpp) video engine follow the reference workflow's sampling recipe and stop losing quality between sd.cpp and the exported MP4. Builds on #17747 (merged).

Needs a new sd.cpp prebuilt. Every new behaviour is gated on what the installed build offers, so this PR is safe on the current pin (master-982-0518b34-u67cba2c), but the lossless handoff and explicit soundtrack pairing only switch on once DEFAULT_TAG in studio/install_sd_cpp_prebuilt.py points at a prebuilt that contains unslothai/stable-diffusion.cpp#88. Bump the tag in a follow-up after that fork PR merges and its prebuilt is published.

Changes

  1. Sampler and schedule. Regular H3 checkpoints now run res_multistep on the simple schedule, on both the sd-cli argv (--sampling-method, --scheduler) and the sd-server sample_params. Distilled and PDD files keep Euler, on the same simple schedule. Before this, Studio sent no sampler, so sd.cpp ran Euler on its discrete grid (base linspace(1, 0.001, N)), which spends about one evaluation near sigma 0. UNSLOTH_H3_SD_CPP_SAMPLER=legacy restores Euler on the discrete grid.
  2. One lossy encode. When the build lists it, Studio asks sd.cpp for an uncompressed AVI (BGR24 frames and float PCM): --lossless-video with a .avi output on sd-cli, or output_format: "avi_raw" on sd-server. The server result is downloaded as raw bytes from the job's media_url instead of base64 JSON. Studio's x264 / AAC export is then the only lossy step. UNSLOTH_H3_SD_CPP_LOSSLESS=0 keeps MJPG.
  3. Export fixes in transcode_video_to_mp4. These help every build, including the current pin:
    • sd.cpp's MJPG frames decode as full-range yuvj420p. The yuv420p encoder treated them as limited range and stretched them (gain 1.14, offset -16), so they now go through RGB first.
    • Decoded frames are tagged as I frames, and libx264 obeyed that, so every export was all-intra. The tag is now cleared.
    • The export now uses the shared x264 stream: frame threads, and libx264 capped at AVX2 for reproducible output.
    • Float audio is limited to [-1, 1] and NaN is set to 0 before AAC, because the AAC encoder rejects NaN.
  4. Soundtrack pairing. A silent reference video can now come before one with a soundtrack when the build accepts --ref-video-audio-index (on sd-server, the ref_video_audio_indices feature). Older builds keep the refusal. If the soundtracks already pair by position, the argv is unchanged.
  5. The NaN-render probe now also catches float PCM that is all NaN.
  6. Audio shift. A non-default audio_flow_shift is no longer refused on builds that accept --audio-flow-shift. It is sent on the argv and in sample_params, and the saved recipe records the shift that ran. Older builds keep the refusal.

Evidence

  • Unit tests: studio/backend/tests/test_h3_sd_cpp_parity.py (24 tests). Each export test fails on the base branch: MJPG gain 1.099, 0 inter frames, and AAC raises on NaN audio.
  • Related suites pass: test_h3_native_server.py, test_h3_native_nonfinite.py, test_sd_cpp_args.py, test_video_backend.py and test_media_auto_switch.py (875 tests). Two failures exist on the base branch as well: test_h3_reference_image_size.py::test_vae_and_vision_tower_see_the_fitted_images[max] and test_sd_cpp_moss_image.py::test_diffusers_takes_a_moss_safetensors_single_file.
  • Export quality, measured on a real 1344x768, 124-frame H3 clip. All three arms use the same frames written by the fork's own AVI writer, and PSNR is taken of the final MP4 against those frames:
arm sd.cpp container export final MP4 PSNR mean / min
base (current Studio) MJPG q90, 22.0 MB 1.8 s 7.9 MB 27.98 / 27.56 dB
this PR, old build (MJPG) MJPG q90, 22.0 MB 2.0 s 2.9 MB 37.55 / 37.12 dB
this PR, new build (raw) raw AVI, 385 MB 1.6 s 2.9 MB 37.73 / 37.24 dB

Writing the raw container takes 0.7 s, against 2.4 s for MJPG. The cost is temporary disk (about 385 MB per 5 s clip) and a 385 MB loopback download from sd-server. GPU end to end: Colab RTX PRO 6000 Blackwell, Studio defaults (1344x768, 124 frames, 20 steps), runs ordered base, head, head, base. Both runs of each arm were pixel-identical. Base is the current prebuilt plus the base branch; head is the new fork build plus this branch.

task arm s/clip export final MP4 final MP4 vs true frames (Y) identity cos (ref2va)
t2va base 239.3 0.84 s 5.93 MB 28.25 dB
t2va head 236.9 0.54 s 1.78 MB 39.11 dB
fl2va base 278.0 0.97 s 6.83 MB 28.12 dB
fl2va head 276.4 0.75 s 4.47 MB 38.11 dB
ref2va base 447.9 0.87 s 6.05 MB 28.14 dB 0.649
ref2va head 446.5 0.54 s 1.57 MB 38.66 dB 0.657

On head, the true frames are the raw AVI, which matches PNG frames written by sd-cli exactly. On base, they are the PNG frames that the base prebuilt's sd-cli writes at the same seed.

Denoise and VAE decode times are unchanged. The server handoff drops from 2.0 s to 0.8 s, of which the raw media download is 0.21 s. Python peak RSS rises by 0.55 GB because Studio holds the 385 MB AVI. LPIPS against the Diffusers engine was not measured.

fit_h3_reference_image scales each ref2va reference image down (never up)
to the "match" or "max" limit, but Diffusers' Ref2VA setup step then
rescaled every image reference to its 2048 px short edge, upscaling
included. The default "match" therefore ran at the full 2048 token cost
with a blurred reference, and "max" upscaled images under 2048 px.

Switch the pipeline's Ref2VA setup step to a subclass that skips that
rescale for the images the request passed, so the VAE encode and the
vision tower both receive the fitted LANCZOS image. One config value
cannot express this because references in one request can differ in
size. The step is identical in diffusers 0.41.0, 0.42.1 and 0.43.0.

Pass ref_image_size=max to stable-diffusion.cpp with reference images:
its default "match" rule is not idempotent after the 32 px snap and
resized about 8% of fitted images a second time, while "max" leaves an
image with a short edge of at most 2048 untouched.
…licit soundtrack pairing

- Pass res_multistep on the simple schedule (Euler on simple for distilled / PDD files) on
  both the sd-cli argv and the sd-server sample_params. UNSLOTH_H3_SD_CPP_SAMPLER=legacy
  restores Euler on the discrete grid.
- Ask sd.cpp for an uncompressed AVI (sd-cli --lossless-video, sd-server output_format
  avi_raw, fetched from the job's media_url) when the build offers it, so the x264 / AAC
  export is the only lossy step. UNSLOTH_H3_SD_CPP_LOSSLESS=0 keeps MJPG.
- The export uses the shared x264 stream (frame threads, AVX2-capped libx264).
- A silent reference video may precede one with a soundtrack when the build takes
  --ref-video-audio-index (sd-server: ref_video_audio_indices).
- The NaN-render probe also recognises float PCM that is all NaN.
…e types, bound float audio

- sd.cpp's MJPG decodes to full-range yuvj420p, which the yuv420p encoder took as limited
  range and stretched (gain 1.14, offset -16): a real 1344x768 H3 clip came out at 28.0 dB
  against its frames. Such frames now go through RGB first (37.6 dB).
- Decoded frames carry picture type I and libx264 obeyed it, so every export was
  all-intra (7.9 MB for 5 s). The type is cleared and x264 picks I / P / B (2.9 MB).
- Float PCM from the uncompressed AVI is bounded to [-1, 1] with NaN zeroed before AAC,
  which refuses NaN; 16-bit PCM clipped the same range.
…uilds that take it

Builds listing --audio-flow-shift get it on the sd-cli argv and as
sample_params.audio_flow_shift on sd-server, and the request is checked like the Diffusers
engine instead of refused. Older builds keep the refusal. The saved recipe records the
shift that ran.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 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-14T19:47:25.708257Z 42a31e4 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 42a31e49ff

ℹ️ 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 3051c0b into main Nov 14, 2026
@danielhanchen
danielhanchen deleted the studio-h3-reference-parity-sdcpp branch November 14, 2026 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant