Skip to content

[diffusion] video: let requests choose the libx264 preset - #41797

Merged
mickqian merged 3 commits into
mainfrom
mick/diffusion-video-encode-preset
Oct 4, 2026
Merged

mickqian merged 3 commits into
mainfrom
mick/diffusion-video-encode-preset

Conversation

@mickqian

@mickqian mickqian commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

The libx264 preset for MP4 output is a module constant (X264_PRESET = "fast", from #38657), so it cannot be changed from a request, a CLI flag or an environment variable. Only CRF can be set, through output_compression / output_quality. Two use cases need the preset:

  • Latency-bound deployments. Encoding sits on every video request's critical path. A deployment may prefer a faster preset and accept a larger file.
  • Cross-engine benchmarks. A comparison should pin the same encoder settings on every system. With the preset hardcoded, SGLang can only run at fast, while other engines are commonly run at ultrafast.

Modifications

  • SamplingParams.x264_preset (None by default, which keeps fast), validated against the libx264 preset list. Unknown values raise, so the video API answers them with HTTP 400. sglang generate takes --x264-preset.
  • /v1/videos accepts x264_preset in JSON bodies and as a multipart form field.
  • It reaches all three libx264 call sites: the direct CUDA pipe, the single-pass video+audio encode, and the imageio fallback. It is passed from every save_outputs caller: the GPU worker (single and grouped outputs), the HTTP server's generate fallback, DiffGenerator and the H3 web UI. Grouped outputs must share it, like output_compression.
  • The HTTP server's generate fallback now also passes output_compression, which it used to drop. With the default quality this changes nothing: both give CRF 25.
  • Docs: a short "MP4 encoding" section in the OpenAI API page.

Accuracy Tests

The default is unchanged, so existing outputs are byte-identical.

  • test_video_encode_preset.py::test_request_preset_reaches_the_encoder: real encodes with ultrafast and slow through both save paths (a host tensor goes through imageio, a CUDA tensor through the direct pipe). libx264 writes the options it resolved into the file, and the test compares the preset-derived ones against a reference encode by ffmpeg.
  • test_output_saving.py::test_x264_preset_reaches_every_video_save_path: the preset reaches the parallel direct save, the serial direct save, the imageio fallback and the single-pass audio encode.
  • test_unknown_preset_is_rejected.
  • Mutation check: dropping the preset in the direct path fails both CUDA cases; dropping it in the imageio path fails both host cases and the plumbing test. 23 passed on H200.
  • End to end on H200 with Wan2.1-T2V-1.3B, reading the x264 options stamped in each returned MP4:
Request subme / ref / me / trellis Same as ffmpeg -preset
no field 6 / 2 / hex / 1 fast
JSON "x264_preset": "ultrafast" 0 / 1 / dia / 0 ultrafast
multipart x264_preset=slow 8 / 5 / hex / 2 slow
sglang generate --x264-preset ultrafast 0 / 1 / dia / 0 ultrafast
JSON "x264_preset": "lightspeed" HTTP 400 –

Speed Tests

Direct save of a 1344×768, 121-frame, 24 fps clip (MiniMax-H3's 5 s output size) on H200 with a 192-core x86 host and 24 x264 threads, all at CRF 25. The content is a Wan output resized to that size. The time covers the whole save step: GPU conversion, pipe and encode. PSNR is measured against the source frames.

Preset Save time Bitrate PSNR
ultrafast 0.426 s 2661 kb/s 44.24 dB
superfast 0.465 s 2615 kb/s 46.21 dB
veryfast 0.489 s 1632 kb/s 44.89 dB
faster 0.549 s 1992 kb/s 45.73 dB
fast (default) 0.651 s 2117 kb/s 45.81 dB
medium 0.730 s 1793 kb/s 45.49 dB

Relative to fast, ultrafast saves 0.23 s per 5 s clip on this host, and at the same CRF gives 1.6 dB lower PSNR at 26% higher bitrate. This is one clip; the default stays fast.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #36704766112
Latest PR Test (Extra): ❌ Run #36704765823
Latest PR Test (AMD ROCm 10): ❌ Run #36704766192

The MP4 encoder preset was a module constant (fast), so neither users nor
benchmarks could change it: a latency-bound deployment could not trade file
size for encode time, and a comparison could not pin the same encoder
settings on every system.

Requests now take x264_preset: in a JSON body, as a multipart form field,
and as --x264-preset for sglang generate. It reaches all three libx264 call
sites (the direct CUDA pipe, the single-pass video and audio encode, and
the imageio fallback). Unset keeps fast, unknown values are rejected with
HTTP 400, and grouped outputs must share it like output_compression. The
HTTP /generate fallback save now also passes output_compression, which it
used to drop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mintlify

mintlify Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
sglang-doc 🟢 Ready View Preview Sep 30, 2026, 10:52 AM

💡 Tip: Enable Automations to automatically generate PRs for you.

@mickqian

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Sep 30, 2026
mickqian and others added 2 commits September 30, 2026 12:56
The WebUI save now forwards request.x264_preset, which the fake request in
the lifecycle test did not have. Give it a non-default preset and check that
it reaches save_outputs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mickqian
mickqian merged commit ba68306 into main Oct 4, 2026
223 of 255 checks passed
@mickqian
mickqian deleted the mick/diffusion-video-encode-preset branch October 4, 2026 08:22

This branch was successfully deployed

1 active deployment
staging - docs — aaa16499 Deployed Sep 30, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diffusion SGLang Diffusion documentation Improvements or additions to documentation run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant