Repository navigation
Studio MiniMax-H3: bound reference audio resampling memory for rates coprime with 32 kHz - #18569
Merged
Merged
Conversation
…coprime with 32 kHz A source rate sharing almost no factor with 32 kHz (44,099 Hz) made the dense sinc table [32000, 1, 44117], 5.3 GiB per intermediate and over 22 GiB peak for a 23 ms clip. Past 4M table elements, evaluate the same filter only on the 2 * width + 1 taps around each output sample, in chunks. Common rates keep the dense path and are unchanged bit for bit.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#17764 added
resample_h3_audio, which converts MiniMax-H3 reference audio to 32 kHz before either engine (Diffusers or sd.cpp) sees it. It builds the sinc filter as one dense table of shape[new, 1, 2 * width + orig], whereorigandneware the two rates divided by their gcd. For common rates the table is tiny (44.1 kHz is[320, 1, 459]), but a rate that shares almost no factor with 32 kHz makes it enormous regardless of clip length. A valid WAV at 44,099 Hz gives[32000, 1, 44117], 5.3 GiB per float32 intermediate, and the filter construction holds several of them at once. Resampling 1,024 samples (23 ms) of such a clip peaked at 22.2 GiB of host RAM and took 8.6 s on CPU. The rate comes straight from the uploaded container (stream.codec_context.sample_rate), so any user upload can hit it.Change
When the dense table would exceed 4M elements (16 MiB in float32),
resample_h3_audionow evaluates the same filter only on the2 * width + 1taps around each output sample, in chunks of 65,536 output samples. Every tap outside that window is clamped to the edge of the filter window, where the dense kernel is below 1e-20, so dropping them changes nothing beyond float rounding. The kernel values are computed with the same float32 expressions as the dense path. Memory now scales with the output length instead of the rate ratio.Rates whose table fits under the threshold (44.1, 48, 22.05, 11.025, 16, 8, 96, 192 kHz and every other common rate) still take the existing dense path, so their output is bit for bit unchanged and still equals torchaudio's default kernel.
Tests
test_resampler_memory_does_not_scale_with_the_rate_ratio(44,099, 44,101, 8,001 and 32,001 Hz) runs the resampler on meta tensors and records the largest tensor any op produces, requiring it to stay under 64 MiB. It fails on main (largest intermediate 5.3 GiB at 44,099 Hz) and passes here.test_windowed_resampler_matches_the_dense_kernelforces the windowed path on five rates and compares it with the reference dense implementation: within 1e-12 in float64 and within 1e-6 in float32.tests/test_h3_reference_recipe.pyplus the H3 and video test files that importvideo_minimax_h3: same failing test IDs on main and on this branch, no new failures.Not proven
torchaudio.functional.resampleat a coprime rate (torchaudio itself builds the same dense table, so it needs the same tens of GiB); the windowed path is checked against the dense formula instead.