From a7da5b2dbd0115b6924d74876ba866295dffe20e Mon Sep 17 00:00:00 2001 From: hrx-compat lane Date: Sun, 15 Nov 2026 00:56:48 -0400 Subject: [PATCH] audio: the mel STFT computes its twiddles once, not 482 million times (engine #1783) The log-mel front-end was a naive DFT with std::cos/std::sin evaluated per term: frames(3000) x nfreqs(201) x n_fft(400) = 241 M iterations and 482 M transcendental calls for one 30 s window. Measured on the real NPU path, whisper-small, jfk.wav: before: load 0.041 s, mel 3.491 s, encoder 2.499 s, ttft 6.030 s, wall 25.670 s after: load 0.001 s, mel 0.115 s, encoder 1.687 s, ttft 1.803 s, wall 14.279 s mel is 30.4x faster and TTFT drops from 6.03 s to 1.80 s -- which matters because this family was LARGER THAN THE ENCODER it feeds and sat entirely inside the TTFT budget. The fix is exact, not an approximation: the angle depends only on (k, n), so the twiddles are a table built once per call, and each entry is the same std::cos/std::sin of the same double argument that the loop used to recompute. The sums are therefore bit-identical by construction. audio_mel_test, which compares the front-end against the reference extractor, still passes ('matches the reference extractor on tones, silences on the clamp'), and the transcribe run still prints 'transcript ids match the reference decode'. An FFT (or a real-to-complex transform, or zero-padding 400 to 512) would go further; this does not need to decide that question, and a twiddle table is the change that cannot move a single bit. Also splits one number the tool was reporting as one number: 'mel+load' is now 'load s, mel s, encoder s'. They are 41 ms and 3.49 s apart with completely different fixes, and summing them is why nobody could tell which half to look at. --- npu/forward/include/audio_mel.h | 20 +++++++++++++++++--- tools/whisper_transcribe.cpp | 4 ++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/npu/forward/include/audio_mel.h b/npu/forward/include/audio_mel.h index bb0b2d8f8..7b29375fd 100644 --- a/npu/forward/include/audio_mel.h +++ b/npu/forward/include/audio_mel.h @@ -100,15 +100,29 @@ inline bool log_mel_spectrogram(const float* audio, size_t samples, const MelCon const std::vector fb = mel_filterbank(cfg.bins, cfg.n_fft, cfg.sample_rate); out->assign(size_t(cfg.bins) * size_t(cfg.frames), 0.0f); std::vector re(nfreqs), im(nfreqs), power(nfreqs); + // engine#1783: this was a naive DFT with std::cos/std::sin evaluated PER TERM -- frames(3000) x nfreqs(201) x + // n_fft(400) = 241 M iterations and 482 M transcendental calls for one 30 s window, which is why the front-end + // measured 3.49 s. The angle depends only on (k, n), so the twiddles are a table built ONCE. The entries are the + // same std::cos/std::sin of the same double argument, so the sums below are bit-identical -- this is not an + // approximation, and audio_mel_test (which compares against the reference extractor) proves it. + std::vector tw_c(size_t(nfreqs) * size_t(cfg.n_fft)), tw_s(size_t(nfreqs) * size_t(cfg.n_fft)); + for (int k = 0; k < nfreqs; ++k) { + for (int n = 0; n < cfg.n_fft; ++n) { + const double ang = -2.0 * M_PI * double(k) * double(n) / double(cfg.n_fft); + tw_c[size_t(k) * size_t(cfg.n_fft) + size_t(n)] = std::cos(ang); + tw_s[size_t(k) * size_t(cfg.n_fft) + size_t(n)] = std::sin(ang); + } + } for (int t = 0; t < cfg.frames; ++t) { const size_t base = size_t(t) * size_t(cfg.hop); for (int k = 0; k < nfreqs; ++k) { double sr = 0.0, si = 0.0; + const double* tc = &tw_c[size_t(k) * size_t(cfg.n_fft)]; + const double* ts = &tw_s[size_t(k) * size_t(cfg.n_fft)]; for (int n = 0; n < cfg.n_fft; ++n) { const double v = x[base + size_t(n)] * win[size_t(n)]; - const double ang = -2.0 * M_PI * double(k) * double(n) / double(cfg.n_fft); - sr += v * std::cos(ang); - si += v * std::sin(ang); + sr += v * tc[size_t(n)]; + si += v * ts[size_t(n)]; } re[size_t(k)] = sr; im[size_t(k)] = si; diff --git a/tools/whisper_transcribe.cpp b/tools/whisper_transcribe.cpp index 60c33557b..b09730d7e 100644 --- a/tools/whisper_transcribe.cpp +++ b/tools/whisper_transcribe.cpp @@ -428,6 +428,10 @@ int main(int argc, char** argv) { } const auto t_dec = std::chrono::steady_clock::now(); const double load_s = secs(t_start, t_load); + // engine#1783: `mel+load` was reported as one number, and at a 30 s window it is 2.37 s of a 4.47 s TTFT -- + // larger than the encoder it feeds. Split them: they have completely different fixes (graph parse + mmap vs + // the mel front-end), and summing them is why nobody could tell which half to look at. + std::printf("time: load %.3f s, mel %.3f s, encoder %.3f s\n", load_s, mel_s, enc_s); std::printf("time: mel+load %.3f s, encoder %.3f s, decode %.3f s for %zu token(s) -> %.3f s/token, wall %.3f s\n", mel_s + load_s, enc_s, dec_s, ids.size(), ids.empty() ? 0.0 : dec_s / double(ids.size()), secs(t_start, t_dec));