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));