whisper_npu: WhisperEncoder::add_bias_optimized leaks 1,331,200 B on every encode_audio() call

Aperta
#788 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
15/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
cpp

Direzione di ricerca

The leaking operator new[] call is in the prebuilt whisper_npu engine, and no WhisperEncoder source exists in the tree; src/CMakeLists.txt and src/include/whisper/whisper_npu.hpp are the only in-repo touchpoints. Start by reproducing with heaptrack and confirming the nm/objdump evidence against the shipped .so. Done means the per-encode scratch buffer is owned and released, which requires an upstream engine change rather than an edit in this repository.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

Every call to whisper_npu::encode_audio() leaks 1,331,200 bytes. The allocations all come from a single operator new[] inside WhisperEncoder::add_bias_optimized() that has no matching delete[].

The buffer looks like per-call scratch for the bias broadcast. It survives every teardown path — engine destruction included — because nothing holds a pointer to it after the call returns. At ~1.3 MB per 30 s encode window that is roughly 160 MB per hour of continuous speech, growing without bound.

Component: whisper_npu prebuilt engine — affects both src/lib/hrx/libwhisper_npu.so and src/lib/xrt/libwhisper_npu.so (and the matching .dlls).
Model exercised: whisper-v3:turbo (Whisper-V3-Turbo-NPU2, 32 encoder layers).

Measurement

heaptrack, comparing a 10-encode run against a 40-encode run of the same audio. Leaked bytes scale linearly and exactly with encode count. Decode steps and clear_context() leak nothing, which isolates it to the encode path.

Per encode, 33 allocations:

count size each subtotal
32 (one per encoder layer) 40,960 B 1,310,720 B
1 20,480 B 20,480 B
total 1,331,200 B

Static confirmation from the shipped binaries

This is verifiable from the released artifacts alone — no source needed.

The symbol is local, hence absent from the dynamic table:

$ nm -C src/lib/hrx/libwhisper_npu.so | grep add_bias
0000000000028660 t WhisperEncoder::add_bias_optimized(buffer<bfloat16_t>&, buffer<bfloat16_t>&, buffer<bfloat16_t>&) [clone .isra.0]

The function spans 0x28660–0x289d0. Every call instruction in that range:

$ objdump -d -C --start-address=0x28660 --stop-address=0x289d0 \
      src/lib/hrx/libwhisper_npu.so | grep -oE 'call.*<.*>'
      3  __assert_fail@plt
      1  operator new[](unsigned long)@plt

No operator delete[], no free, no destructor call. The allocation at 0x286d0 has no release path at all:

286c5:  lea    0x0(,%r15,4),%rdi        ; byte count = element_count * 4  (float32 scratch)
286cd:  mov    %r8,%r12
286d0:  call   f040 <operator new[](unsigned long)@plt>
286d5:  mov    %rax,%rdi                ; result used directly, never stored for cleanup

Library-wide the imbalance comes down to this one site:

$ objdump -d -C src/lib/hrx/libwhisper_npu.so | grep -c 'operator new\[\](unsigned long)@plt'
14
$ objdump -d -C src/lib/hrx/libwhisper_npu.so | grep -c 'operator delete\[\](void\*)@plt'
13

14 array allocations against 13 array frees. The one unmatched new[] is the one above. The same unmatched allocation is present in the XRT build at 0x57df0.

The lea (,%r15,4) gives a 4-byte element, i.e. a float32 scratch buffer: 40,960 B = 10,240 floats per encoder layer (×32), and 20,480 B = 5,120 floats once per encode.

Suggested fix

Allocate the scratch once and reuse it. Best as a WhisperEncoder member sized at construction — the shapes are fixed per model — which also removes 33 allocator round-trips from the per-encode hot path. Failing that, a std::vector<float> / std::unique_ptr<float[]> local would at least bound the lifetime to the call.

Why this needs an engine rebuild

whisper_npu ships only as a prebuilt binary (src/CMakeLists.txt resolves it out of ${FLM_ENGINE_LIB_DIR}), and no WhisperEncoder source exists in the tree. Unlike gemma4e_npu, which has an opt-in source build under src/detail/ behind FLM_BUILD_GEMMA4E, there is no whisper equivalent. The implementation is fully behind the pimpl in src/include/whisper/whisper_npu.hpp, so there is no caller-side workaround.

Possible relation to #585

#585 reports flm serve --asr 1 with whisper-v3:turbo exiting with code -1 after ~2.5–2.8 h, consistently. An unbounded encode-path leak is an obvious suspect, and a single 100 MB audio file is on the order of a hundred encode windows (~138 MB), so the growth can be fast for large inputs.

That said, the evidence does not cleanly line up and I do not want to overclaim: #585 reports uptimes clustering tightly in a 2h28m–2h48m band regardless of load, including while idle. A per-encode leak is load-dependent, so heavy-file periods should die noticeably sooner than idle ones. That argues for a time- or handle-based cause as the primary driver, with this leak at most a contributor. Worth checking RSS against uptime on an affected host to separate the two.


Found while investigating a separate HRX runtime retention problem (#787).

Lingua principale
C++
Stelle
2.1k
Fork
184
Merge medio
12h 22m
PR unite (30g)
239

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ROCm/FastFlowLM

Tutte le issue di ROCm/FastFlowLM

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.