Skip to content

mel: precompute FFT twiddle factors and reuse scratch - #65

Merged
mudler merged 2 commits into
mudler:masterfrom
haydonryan:opt/mel-fft-precompute
Sep 26, 2026
Merged

mudler merged 2 commits into
mudler:masterfrom
haydonryan:opt/mel-fft-precompute

Conversation

@haydonryan

Copy link
Copy Markdown
Contributor

Replace the per-frame iterative radix-2 FFT's per-butterfly twiddle recomputation with a cached FftPlan (bit-reversal + per-stage twiddle table built once per FFT length, thread_local scratch, no per-frame allocation). The transform stays in double and the table is generated bit-identically to the old iterative complex-multiply, so the output is bit-identical.

Measured: ~8.5% faster mel frontend on the 110m (bit-identical).

Assisted-by: AI:DeepSeek-V4-Flash

Replace the per-frame iterative radix-2 FFT's per-butterfly twiddle
recomputation with a cached FftPlan (bit-reversal + per-stage twiddle
table built once per FFT length, thread_local scratch, no per-frame
allocation). The transform stays in double and the table is generated
bit-identically to the old iterative complex-multiply, so the output is
bit-identical.

Measured: ~8.5% faster mel frontend on the 110m (bit-identical).

Assisted-by: AI:DeepSeek-V4-Flash
@haydonryan
haydonryan force-pushed the opt/mel-fft-precompute branch from 50cae1a to 5366723 Compare August 23, 2026 17:59
The FFT plan cache was a single thread_local FftPlan built for the FIRST
length seen, so a later rfft() call with a different power-of-two length
reused the wrong plan and transformed the wrong number of samples. Rebuild
the cached plan whenever n changes (zero steady-state cost: one int
compare). Add tests/test_fft.cpp which calls rfft with several lengths in
both orders on one thread to guard this.

Assisted-by: AI:DeepSeek-V4-Flash
@haydonryan
haydonryan force-pushed the opt/mel-fft-precompute branch from 5366723 to a3d1e4c Compare August 23, 2026 19:04
@mudler
mudler merged commit 16fda17 into mudler:master Sep 26, 2026
7 checks passed
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.

2 participants