Skip to content

minimax_h3: area + long-side limit, so portrait (704x1248, 768x1344) fits - #161

Merged
jayleaton merged 5 commits into
mainfrom
feat/h3-portrait-area-limits
Nov 20, 2026
Merged

jayleaton merged 5 commits into
mainfrom
feat/h3-portrait-area-limits

Conversation

@jayleaton

@jayleaton jayleaton commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

What

MiniMax H3's size check limited width and height separately (max_width/max_height), so Jarvis (1280x704) capped portrait at 704x704. Every pipeline buffer (DiT tokens, latents, frames) scales with a frame's area, so the limit is now:

  • max_pixels: a frame's area at max_seconds. A frame larger than that is accepted for a clip short enough to fit the same buffers, and the refusal names the longest clip it takes.
  • max_side: the long side (default 1344, the model's 768x1344 canvas).
  • Sides must still be multiples of 32.

Backward compatible: a config with only max_width/max_height gets their product as max_pixels, and either axis may take the long side.

Jarvis: max_pixels: 901120 (same buffers and resident_mb as 1280x704). That allows 1280x704 or 704x1248 for 8 s, and 768x1344 for up to 6 s.

Bit-exactness is unaffected: no numerical change. For a given area the buffer sizes are identical (ceiling is a 32-px strip of the same area).

Tests

minimax_h3_tool tests now cover:

  • accepted: 1280x704 and 704x1248 (new and old-style config); 768x1344 at 6 s on Jarvis; 768x1344 and 1344x768 with a 768x1344 budget
  • refused: 768x1344 at 7 s on Jarvis, with its message; long side past 1344 (area within budget); oversize; non-multiples of 32
  • default size

Ran CI's script-syntax/JSON checks, zig build and zig build test on a Davit Ubuntu 24.04 box: all pass.

Review fixes (Randy, head a17c235)

  • Booking follows the buffers. The scheduler books the weights plus the buffers the limits allocate (P.scratchBytes): the DiT's, the text encoder's, the audio VAE's and the sampler's. These come from the same size tables their init allocates from. A request adds its own scratch (P.requestBytes): the video VAE's decode and, with a first frame, the video and vision encoders, its latent and its condition. resident_mb is now a floor. The default limits still book the measured 40,000 MB peak. Jarvis (901120 px, 8 s) books 53,567 MiB loaded and at most 57,149 MiB for its largest clip.
  • Limits checked at start. A new engine.Entry.validate hook runs from serve against budget_bytes, or MemTotal less reserve_bytes. H3 refuses options outside the API's range (max_pixels up to 2048x2048, max_seconds 1 to 20, sides 256 to 2048), and limits whose largest clip does not fit. max_side=2048, max_pixels=4194304, max_seconds=20 needs about 198 GiB, so the daemon stops with that reason.
  • No overflowing narrowing. Options are read with saturating casts within those ranges. max_pixels=4294967296 no longer crashes a 768x448 check.
  • Default size within the API. max_side is capped at the API's 2048 everywhere it is used. With max_side=4096 and default_size="2080x256", a request without a size now gets 960x544, which the API accepts.
  • Tests. Each fix has a test that failed on a17c235; the overflow one panicked.

Checks (0f9b20f)

zig build and zig build test pass (598 tests, 3 skipped). zig fmt --check on the changed files and git diff --check are clean.

Portrait parity on the target GPU

DGX Spark GB10 (sm_121), NGC PyTorch 26.07, ReleaseSafe build of 0f9b20f. Prompt: the twin's default. Seed 7, 8 steps, h3-turbo-nvfp4. For each shape, python -m stk_twin.h3.generate --size WxH --frames N was compared with localrouter check h3-e2e, which replays the capture from Zig's own outputs.

Size Frames (aligned) Token ids Light ops Frames Waveform Warm repeat Zig sample / video decode
704x1248 56 equal 38 equal equal equal equal 103 s / 60 s
768x1344 56 equal 38 equal equal equal equal 129 s / 59 s
704x1248 192 (8 s) equal 38 equal equal equal equal 497 s / 99 s
768x1344 158 (6 s asked, 144) equal 38 equal equal equal equal 275 s / 80 s

Every row is bit-exact: the uint8 frames' sha256 equals the twin's (e.g. 704x1248x192 3d320dae..., 768x1344x158 5e30550b...), and so do the fp32 waveform's. The last shape ran under node02's GPU lock.

Review fixes, round 3 (Randy, head 0f9b20f)

  • Decode booked with the host latents. The decode phase now counts generateImage's fp32 and fp16 latents on the host (83.25 MiB at 2048x2048 for 5 s). P.requestBytes returns the largest phase: decode; with a first frame, also its encode (device pixels, video and vision encoders, latent) and its sampling (latent and condition). workingBytes adds that to the frames, the waveform, a first frame's host pixels and ffmpeg's buffers.
  • First-frame condition freed. The first frame's latent is freed once the condition is built, and the condition (21 MiB at 768x1344) once the steps are done. The next first frame frees any condition before it allocates, so nothing stays resident or overlaps. check h3-e2e reports condition_resident and fails unless it is 0.
  • Tests. The booking test in minimax_h3_tool.zig failed on 0f9b20f. The release is checked on the GPU (below).

Checks (22f7dd9)

zig build test passes (33/33 steps, 378/379 tests, 1 skipped). zig fmt --check on the changed files and git diff --check are clean.

First-frame condition release on the target GPU

DGX Spark GB10 (sm_121), NGC PyTorch 26.07, ReleaseSafe, under node02's GPU lock (18 min). Image to video at 704x1248, 56 frames, seed 7, 8 steps, with a generated 704x1248 PNG as the first frame. One twin capture (stk_twin.h3.generate --image) was checked with check h3-e2e against two builds of 22f7dd9: as pushed, and with the pipeline's releaseCondition call removed.

Build Token ids Light ops Frames Waveform Warm repeat condition_resident Result
release removed equal 41 equal equal equal equal 18,779,904 B fail
22f7dd9 equal 41 equal equal equal equal 0 pass

Frames sha256 afd72c74... matches the twin's in both. Freeing the condition changes no bits.

Review fixes, round 4 (Randy, head 22f7dd9)

  • Condition released on error. The steps that read a first frame's condition run inside conditioned, which releases it when they return or fail. An allocation or CUDA error after firstFrame used to skip the release and leave 22,063,104 B resident at 768x1344. The release still comes before the decode, and firstFrame already frees its own buffers when it fails.
  • Test. A host test drives conditioned with a fake DiT whose steps fail, and checks the condition is released, the first frame's latent is freed once, and a failed firstFrame leaves the latent to its caller. It failed with the release placed only after successful steps (the old code).

Checks (23a315d)

zig build test passes (33/33 steps, 379/380 tests, 1 skipped). zig fmt --check and git diff --check are clean. No GPU run: the replay order and the kernels are unchanged.

Not done

Jarvis is not redeployed: the box is busy, so that waits on an OK.

🤖 Generated with Claude Code

… 768x1344) fits

The pipeline's buffers all scale with a frame's area, so the limit is now max_pixels
(x max_seconds) plus the long side (max_side, the model's 1344) instead of a separate
width and height. A frame past max_pixels is taken for a clip short enough to fit the
same buffers. max_width / max_height configs get their product. Jarvis: 901120 pixels,
the same memory as 1280x704: 704x1248 for 8 s, 768x1344 for up to 6 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…preflight, defaults

- Dit.init takes the audio latent frames and sizes a_in from them; a small budget's
  max_s could be below the carried audio (512x512, 5 s: 23,356 bytes for 26,496).
- fits caps a frame at the larger of max_pixels and the canvas's 768x1344 area, which
  bounds the video VAE's per-decode scratch (Jarvis: at most 486 MiB vs 445 MiB).
- A first frame whose canvas leaves the prompt no text tokens is refused before loading.
- defaultSize: default_size, then a legacy max_width/max_height size, then widest, each
  only if the tool takes it; widest stays within the API's 256 minimum side.
- Tests: buffer and DiT capacities over every admitted shape, first-frame admission,
  and the default regressions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jayleaton

Copy link
Copy Markdown
Owner Author

Addressed Randy's review in a17c235:

  • P1: Dit.init now takes the number of audio latent frames and sizes a_in from it. A new test checks, for every shape fits admits under four budgets, that the shape fits the buffers, the DiT rows and the audio scratch.
  • P2 defaults: the default size is default_size, else the old max_width/max_height size, else widest. Each is used only if the tool accepts it. widest never goes below the API's 256-pixel minimum side.
  • P2 first frame: a canvas whose image tokens leave the prompt no room is refused before loading.
  • Memory claim: a frame is now capped at the larger of max_pixels and the canvas's 768x1344 area. The video VAE's scratch on Jarvis is therefore at most 486 MiB (1344x768) against 445 MiB today, and the docs say so.

zig build and zig build test pass on the Davit Ubuntu box.

@jayleaton

Copy link
Copy Markdown
Owner Author

Randy reviewed head a17c235. The a_in P1 is fixed: the 512×512, 5 s case now allocates the 26,496 bytes it needs, and the four-budget regression test passes. Legacy configs are still accepted, and the 6 s limit message for a 768×1344 request at 7 s is correct. zig build test passes (582 tests, 3 skipped). Still needed:

  1. Blocking: src/engines/minimax_h3_tool.zig:155. With max_side=2048, max_pixels=4194304 and max_seconds=20, the API accepts a 2048×2048 20 s clip. The DiT and pipeline buffers alone need over 135 GiB before the weights are loaded, but the scheduler only books about 39 GiB. Fix: compute the booking from the configured buffers plus per-request scratch, and refuse configs that exceed the memory budget.
  2. Blocking: minimax_h3_tool.zig:50. With max_pixels=4294967296, an ordinary 768×448 request crashes the daemon at the narrowing cast. Fix: validate the limits when the config loads, and use checked arithmetic before narrowing.
  3. Blocking: minimax_h3_tool.zig:67. With max_side=4096, max_pixels=532480 and default_size="2080x256", a request that omits size gets a 400, while an explicit 2048×256 passes. Fix: apply the API's 2048-pixel ceiling when choosing the default size, the same as when checking sizes.
  4. Blocking: CONTRIBUTING.md:46. The PR only reports host checks. Fix: run the target-GPU comparison against the Python reference and record decoded pixel and audio parity for the newly admitted portrait shapes.

…ep the default size within the API

- The booking is the weights plus the buffers the limits allocate (P.scratchBytes:
  the DiT's, the text encoder's, the audio VAE's and the sampler's, from the same
  size tables their init allocates from), and a request adds its own scratch
  (P.requestBytes: the video VAE's decode; with a first frame the video and vision
  encoders, its latent and condition). resident_mb is a floor. The default limits
  still book the measured 40,000 MB peak.
- engine.Entry.validate, called by serve at start against budget_bytes (else
  MemTotal less reserve_bytes): H3 refuses options outside the API's (max_pixels to
  2048x2048, max_seconds 1 to 20, sides 256 to 2048) and limits whose largest clip
  does not fit. 2048x2048 for 20 s needs 198 GiB and stops the daemon; Jarvis
  (901120 px, 8 s) needs at most 57,149 MiB.
- Options are read with saturating casts within those ranges, so no request can
  reach an overflowing narrowing (max_pixels 2^32 crashed a 768x448 check).
- max_side is capped at the API's 2048 everywhere it is used, so the default size
  is one the API takes (max_side 4096 with default_size 2080x256 gave a 400).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jayleaton

Copy link
Copy Markdown
Owner Author

Randy round 2 at 0f9b20f. The original over-budget and narrowing cases now fail cleanly at startup, and the default size follows the 2048 ceiling. The shared size tables match the device allocations, and 84,004 admitted shapes across 5 configs pass the capacity checks. GPU parity is 4 of 4 bit-exact. Still needed:

  1. Blocking: src/engines/minimax_h3_tool.zig:178. At 2048×2048 for 5 s under a 113 GiB budget, the fp32 and fp16 host latent buffers exist alongside the booked device scratch during decode. That's an extra 83.25 MiB the estimate leaves out. Fix: count the host buffers in the decode-phase peak and book the largest phase.
  2. Blocking: src/engines/minimax_h3/dit.zig:450. A 768×1344 image-to-video job leaves 21.0 MiB of conditioning buffers resident. A following text-to-video job carries that memory without booking it, and a following image-to-video job allocates new buffers before freeing the old ones. Fix: free the conditioning buffers once their GPU work finishes, or book both their residency and the replacement overlap.

…ter sampling

- The decode phase now counts the latents on the host in fp32 and fp16 (83.25 MiB
  at 2048x2048 for 5 s), and a request books the largest of its phases (decode;
  with a first frame, its encode or its sampling with the condition) on top of
  the frames, waveform and first-frame pixels on the host.
- A first frame's latent is freed once the condition is built, and the
  condition (21 MiB at 768x1344) once the steps are done; the next first frame
  frees any condition before it allocates. check h3-e2e reports
  condition_resident and fails unless it is 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jayleaton

Copy link
Copy Markdown
Owner Author

Randy round 3 at 22f7dd9. What passes:

  • The decode booking test, which covers the extra 83.25 MiB of host latents at 2048×2048 for 5 s.
  • The archived GB10 run at 704×1248 with 56 frames: the control kept 18.8 MB resident and the fix kept 0, with bit-exact frames and audio.

One blocker:

Blocking: src/engines/minimax_h3/pipeline.zig:568. An allocation or CUDA error after firstFrame can skip releaseCondition. The worker stays loaded after the error, so a failed 768×1344 image-to-video job can leave 22,063,104 bytes resident after its booking ends. Fix: register an errdefer as soon as conditioning is installed, keep the early release before decode, and add a test that injects a failure after firstFrame.

The steps that read the condition run inside conditioned(), which releases it
when they return or fail (an allocation or CUDA error after firstFrame used to
skip the release and leave 22,063,104 B resident at 768x1344). The release still
comes before the decode. A host test drives conditioned() with a fake DiT whose
steps fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jayleaton

Copy link
Copy Markdown
Owner Author

Randy round 4: safe to merge. The condition buffers are now released on every error path after firstFrame, and the host test covers the step failure, the step success and a firstFrame failure. zig build and zig build test pass, CI is 8/8 green, and it meets CONTRIBUTING and AGENTS.md.

@jayleaton
jayleaton merged commit c6b4b7d into main Nov 20, 2026
8 checks passed
jayleaton added a commit that referenced this pull request Nov 20, 2026
…s send the filled fps

After the rebase onto #161: a cmd video tool's default_width and default_height
(the ComfyUI worker's) are its declared default size when default_size is absent,
and are checked at startup like it. #161's H3 tests call check without fill, so
their requests carry the 24 fps fill would give.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jayleaton added a commit that referenced this pull request Nov 20, 2026
…2) guide (#162)

* video: Wan 2.2 TI2V-5B as a cmd worker, generic video limits, and a desktop guide

A `cmd` video tool can now declare its limits (`max_pixels`, `max_side`, `size_multiple`, `default_size`,
`max_seconds`, `fps`), checked before it loads (`engine.checkVideo`) and listed by `/v1/models` and
`list_models`. Any video tool may declare `"audio": false`; a request's `audio` is optional and means the
model's own when omitted, so a silent model is not refused by callers that leave it out.

tools/video/wan runs Wan 2.2 TI2V-5B (Apache-2.0) through Diffusers on CUDA, text and image to video at
24 fps, sized for a 16 GB consumer card: the transformer is loaded onto the GPU and stored in fp8, and one
large module holds the GPU at a time. docs/dev/DESKTOP.md sets up a Windows 11 desktop through WSL2
(systemd, a keepalive task, tailscale serve, pairing).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* video: one set of effective limits for cmd workers; H3 keeps its audio; reserve_for refuses first; Wan worker frees the GPU

- engine.videoLimits: a cmd video tool's options within the API's (sides 256 to
  2048, the grid made a multiple of the API's 16, max_seconds at most 20, fps at
  most 30). fill, checkVideo and the listings all use it, so the defaults are
  always accepted: max_side 320 gets 320x256; max_seconds 3 or fps [16] gives a
  request without them 3 s or 16 fps; size_multiple 24 means a 48 grid.
- request.Video seconds and fps are 0 when omitted (HTTP, MCP, voice,
  reserve_for); engine.fill gives 5 s (or a shorter max_seconds) and 24 fps (or
  the first listed).
- The generic audio option and its refusal apply to cmd workers only: an engine
  with its own check (H3, Kandinsky 6) keeps its audio.
- reserve_for validates a video job's size and length (request.validate, then
  engine.check) before planning, unloading or loading anything.
- Wan worker: after every job a fresh scheduler (UniPC holds the job's tensors),
  gc and empty_cache even when the modules are already on the CPU, the
  exception's traceback dropped before the release; idle GPU memory is logged
  after each job and at exit.
- fetch.sh stages into WEIGHTS.staging and publishes only after every digest
  matches; a failed check keeps the previous snapshot. test_fetch.py runs in CI.
- UPSTREAM.md entries 13 and 14 (the offload hooks, the VAE wrappers); NOTICE: the
  Wan weights, the copied negative prompt and Diffusers; DESKTOP.md restarts WSL
  and checks systemd, and calls localrouter from zig-out/bin; pins.json is LF.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* wan: record the BF16 reference and acceptance for FP8 transformer storage

Measured on the RTX 5070 Ti at 704x1280, 2 s, seed 7: text to video PSNR 22.17 dB /
SSIM 0.904 mean against bf16, image to video 28.57 dB / 0.945 with its first frame at
48.03 dB; the criteria (20 dB, 0.70, first frame 30 dB) were set before the run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* UPSTREAM.md: the Wan offload and VAE patches stay local, and why

Diffusers' chained offload is right for in-order pipelines, and changing what
enable_model_cpu_offload evicts would change every pipeline; the cost shows only
on small cards where WDDM pages. It goes upstream as an opt-in option if the
maintainers want one, or gives way to #14304's offload strategies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* wan fetch.sh: a retry publishes only the current manifest's files

The published directory is built from the manifest's paths in the staging
directory, so a file left there by an earlier attempt is never published.
test_fetch covers a retry with an obsolete staged file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Video requests: an explicit 0 for seconds or fps is refused

Left out is the model's own; 0 sent is refused before anything is submitted or
reserved (POST /v1/videos, MCP generate_video and reserve_for, the voice loop's
generate_video), where it used to pick the default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* cmd video tools: limits that leave nothing to serve stop the daemon

checkDefaults refuses at startup, naming the option: max_side below the
smallest side on the size_multiple grid, max_pixels below the smallest frame,
an empty or invalid fps list, a default_size the tool does not take.
MODEL-DROPIN lists audio with the cmd worker's options (H3 and Kandinsky 6
keep their own).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* wan fetch.sh: reuse a file only at its pinned digest; publish through a directory of its own

- A staged or published file is kept only when its size and sha256 are the
  pinned ones; a same-size repin is fetched, not reused. Each file is hashed
  once (when kept or downloaded), so verify_assets.py is no longer run.
- Each run publishes through its own WEIGHTS.publish.XXXXXX and removes only
  that: a WEIGHTS.new or WEIGHTS.old that is not fetch.sh's is left alone (the
  old script deleted the first and restored the second as WEIGHTS).
- A file that fails its digest stays in staging to inspect and is fetched anew
  on the next run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* cmd video tools: an explicit max_seconds 0 stops the daemon

It used to start and advertise a 1 s limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* UPSTREAM.md: the Diffusers opt-in for 13 and 14 is prepared, awaiting filing

enable_model_cpu_offload(exclusive=True), drafted against Diffusers main as an
issue, a patch with a shared MemoryTesterMixin test and a docs note, and the PR
text; filing is the maintainer's decision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* cmd video limits: default_width and default_height from main; H3 tests send the filled fps

After the rebase onto #161: a cmd video tool's default_width and default_height
(the ComfyUI worker's) are its declared default size when default_size is absent,
and are checked at startup like it. #161's H3 tests call check without fill, so
their requests carry the 24 fps fill would give.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* UPSTREAM.md: the Wan offload hooks stay local, and why; CHANGELOG says cmd video tool

Exclusive offload, tried on Diffusers main, breaks pipelines that call submodules
outside the top-level hooks (Motif's vision_tower; overrides without the option),
and Diffusers is moving offload strategies into modular pipelines (#14304). For Wan
TI2V-5B it is verified on GB10 (the exclusive test and the 20 existing Wan offload
tests). Nothing is filed upstream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: JJ Eaton <58495321+oldbettie@users.noreply.github.com>
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