Skip to content

Studio video: compare streamed frames without chunk-sized temporaries (H3 export stalls) - #16024

Merged
danielhanchen merged 2 commits into
mainfrom
studio-video-export-stall
Oct 29, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-video-export-stall

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

On MiniMax-H3 1344x768x124 (A100-40), the export of a finished clip sometimes took 115-184 s instead of about 1-2 s. The time goes into the frame compare that decides whether the streamed mp4 can be handed out (StreamEncode._matches, np.array_equal per chunk).

Cause: np.array_equal allocates a bool array the size of the chunk (about 55 MB per H3 chunk). numpy marks allocations of 4 MiB and up for transparent huge pages, so every fault into that array tries to build a 2 MiB page on the spot. After an H3 load the host holds about 45 GB of small pinned pages spread through memory plus the page cache of the weights, so almost no 2 MiB block can be freed: each fault runs direct reclaim and a failed compaction, then falls back to a small page.

Measured on the live H3 A100 host with the real _matches code:

compare s small-page faults failed compactions
base 115.7 93,751 967
base, numpy huge-page marking off 0.30
head (30 s later, same host) 0.084 34

Background pinning and the GIL are not the cause: pin_plain had finished minutes before the exports started (it only left memory fragmented), and a 4 Hz Python sampler thread kept its full rate through a 183 s export.

Change

  • _frames_equal compares each chunk in 1 MiB slices (as uint64 words) into one reused 128 KiB bool buffer, so the compare allocates nothing per slice and never reaches the huge-page fault path. It decides exactly as np.array_equal: shape, dtype, the trailing bytes, and non-contiguous inputs (those fall back to np.array_equal).
  • The mp4 bytes are untouched.
  • Kill switch: UNSLOTH_STUDIO_VIDEO_STREAM_COMPARE_SLICED=0 brings back the whole-chunk np.array_equal.

Results

H3 1344x768x124 on an A100-40, base and head alternating (S F S F), a fresh Studio home per arm, page cache prewarmed before each arm, a cold render plus 2 more per arm (8 steps; the export does not depend on the step count):

arm export s (cold / 2nd / 3rd) compare s
base 1 1.56 / 2.61 / 0.76 0.18-1.06
head 1 1.32 / 1.72 / 0.66 0.07-0.13
base 2 152.96 / 1.82 / 125.34 152.2 / 0.22 / 124.3
head 2 1.93 / 1.76 / 1.17 0.13-0.14
  • Base: median 2.2 s, range 0.76-153 s, stalled in 2 of 6 exports.
  • Head: median 1.54 s, range 0.66-1.93 s.
  • Clean path on L4 (3 runs each): compare 0.18-0.19 s on base, 0.09-0.12 s on head.

Frames: the final frames are identical in all four arms, and the cold render's streamed mp4 is byte-identical across arms and to the stock (non-streamed) export of the same frames. In the CPU microbench the mp4 and decoded-frame md5 match across base, head, the kill switch and the no-huge-page control on L4 and A100.

Tests

  • New studio/backend/tests/test_video_stream_encode_compare.py (9 CPU tests): equality and every mismatch case against np.array_equal, the kill switch, and a tracemalloc check that the compare allocates under 512 KiB (a whole chunk with the kill switch). All pass on head and fail on base.
  • test_video_stream_encode.py and test_video_decode_oom_retry.py: 29 passed, 1 skipped. test_a_compiled_decode_runs_the_chunk_hook_as_one_eager_island[cpu] and one test in test_integrated_gpu_no_host_pin.py fail the same way on base.

Not proven

  • 20-step renders and the returning-home case were not re-run.
  • libx264 also marks its own buffers for huge pages, so the encoder itself can still be slowed on such a host (once 10.4 s in the microbench, 0.4-1.3 s in the real runs); not changed here.
  • Separate, pre-existing: for the 2nd and later render in one process the streamed mp4 is not byte-identical to the stock export (base and head alike, even though the frame compare passes). Tracked separately.

np.array_equal allocated a bool array the size of each streamed chunk.
numpy madvises allocations over 4 MiB for transparent huge pages, and on a
host whose memory is fragmented by pinned weights every such fault ran
direct reclaim plus compaction that failed. On MiniMax-H3 1344x768x124
(A100-40) this made the streamed export compare take 115-152 s.

The compare now walks the chunk in 1 MiB uint64 slices into a reused
128 KiB scratch, so it allocates nothing per slice and decides exactly
like np.array_equal. UNSLOTH_STUDIO_VIDEO_STREAM_COMPARE_SLICED=0 restores
the old compare.
@danielhanchen danielhanchen self-assigned this Oct 29, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T18:31:26.306028Z 89eac22 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. Breezy!

Reviewed commit: 89eac2206e

ℹ️ 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 added a commit that referenced this pull request Oct 29, 2026
`Repo tests (CPU, rest-2)` is red on main since #15996 (Use via API
button on the Train page). It now fails on unrelated PRs, for example
#16024:

```
test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding[5]
AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding ...
  'studio/backend/tests/test_training_api_snippet_contract.py:19: read_text()'
```

The new snippet contract test reads its JSON fixture
`fixtures/training_api_job_bodies.json` with `read_text()`. On Windows
that decodes as cp1252, so the test breaks as soon as the fixture gains
a non-ASCII byte. The guard is right and the test was wrong. The fix is
`read_text(encoding = "utf-8")`.

## Verification

- `tests/test_source_read_encoding.py`: fails on main (the one site
above), 16 passed here.
- `studio/backend/tests/test_training_api_snippet_contract.py`: 12
passed.
@danielhanchen
danielhanchen merged commit d84e074 into main Oct 29, 2026
1 of 38 checks passed
@danielhanchen
danielhanchen deleted the studio-video-export-stall branch October 29, 2026 19:53
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