Skip to content

fix: bound the resample kernel cache by memory with LRU eviction - #1124

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
Liquid4All:resample-kernel-cache-cap
Oct 29, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
Liquid4All:resample-kernel-cache-cap

Conversation

@ykhrustalev

@ykhrustalev ykhrustalev commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem
resample_mono_torchaudio_sinc_hann keeps every kernel it builds, one per rate pair and option set, so a server fed audio at many sample rates grows without bound: 74-75 MiB after 1 s from each of 401 rates. Clearing the whole cache at a fixed count bounds memory, but hot rates then rebuild their kernels after every clear, 11-21 ms per call on x86-64 for 44.1 or 22.05 kHz to 16 kHz with default options.

Solution

  • Bounds the kernel cache by bytes, 32 MiB by default, evicting the least recently used kernels; a kernel larger than the budget is built per call and not cached
  • Hands out kernels as std::shared_ptr, so a resample still using an evicted kernel keeps it alive
  • Adds set_resample_kernel_cache_budget_bytes(size_t), which evicts at once (0 disables the cache), and resample_kernel_cache_stats()
  • Adds the top-level server config key resample_kernel_cache_mb, and --resample-kernel-cache-mb <mb> on the server (overriding the key) and on audiocpp_cli

Testing

  • audio_dsp_test, server_config_test, mel_spectrogram_frontend_test and earshot_vad_test pass on M3 Ultra (Metal) and H100 (CUDA, x86-64 Linux); the new audio_dsp_test cases (byte budget, LRU order, budget 0, shrinking, 4 threads) are clean under TSan and ASan/UBSan, and cli_request_options_test covers the flag's parser.
  • Resampler output is bit-identical to main over 8x8 rate pairs, all kernel modes, both accumulations and 3 input lengths, at budgets of 32 MiB, 64 KiB and 0.
  • Under churn, 20 calls at new rates (8-48 kHz) precede each hot call, which resamples 1 s with default options (single-threaded medians). The server row is lfm3_audio ASR without libsoxr: a 7.5 s 44.1 kHz request after each burst of 20 requests at other rates, with the same transcript on every build and budget.
M3 Ultra / H100 main 16-entry limit this PR
Kernel builds per hot call under churn 0 / 0 1 / 1 0 / 0
Hot call, 44.1 kHz to 16 kHz 0.16 / 0.29 ms 0.73 / 10.7 ms 0.16 / 0.30 ms
Hot call, 22.05 kHz to 16 kHz 0.11 / 0.24 ms 1.21 / 20.6 ms 0.11 / 0.26 ms
Peak RSS, 1 s from each of 401 rates to 16 kHz 74.8 / 73.9 MiB 10.9 / 8.8 MiB 38.5 / 38.1 MiB
Server hot request under churn 255.6 / 121.0 ms 255.5 / 120.2 ms 255.5 / 118.7 ms
  • A kernel above the budget is rebuilt on every call. 44056 Hz to 16 kHz needs 84.5 MiB, so a 1 s call takes 1.59 s with default options on the H100 host's CPU (174 ms with float32 options), where main builds it once. A 100 MiB limit, by flag or config key, restores caching (M3 Ultra server: 232.5 to 184 ms per request).

@0xShug0

0xShug0 commented Oct 28, 2026

Copy link
Copy Markdown
Owner

@ykhrustalev In your scenario where many resampling configurations accumulate, one concern is the impact on TTFA/RTF. In my tests, cache eviction increased Higgs Audio streaming TTS TTFA from about 85 ms to 104 ms (avg +23%) when processing an uncached reference. Prepared-reference cache hits were unaffected. But if cache churn is frequent, this rebuilding cost will recur, creating a sustained performance penalty rather than a one-time dela.

I think the 16-entry limit should be configurable. A better approach could be a memory-bounded cache with LRU eviction, exposed through a framework API, server config, and CLI flag. This would avoid clearing the entire cache when it reaches the limit.

E.g., set_resample_kernel_cache_budget_bytes(size_t bytes) in resampling.h; cache policy and accounting stay in resampling.cpp. Then

  • Server config: top-level resample_kernel_cache_mb: 32.
  • CLI/server flag: --resample-kernel-cache-mb 32.

The torchaudio sinc kernel cache kept one kernel per rate pair forever.
It now evicts least recently used kernels to stay within 32 MiB, set by
resample_kernel_cache_mb or --resample-kernel-cache-mb (0 disables it).
A kernel larger than the budget is rebuilt on every call, not cached.
@ykhrustalev
ykhrustalev force-pushed the resample-kernel-cache-cap branch from b6d873c to 648b34e Compare October 29, 2026 19:15
@ykhrustalev ykhrustalev changed the title fix: cap the torchaudio sinc resample kernel cache fix: bound the resample kernel cache by memory with LRU eviction Oct 29, 2026
@ykhrustalev

Copy link
Copy Markdown
Contributor Author

Reworked as you suggested: an LRU cache bounded by bytes, 32 MiB by default, set with set_resample_kernel_cache_budget_bytes(), the top-level resample_kernel_cache_mb server key, or --resample-kernel-cache-mb on the server and CLI, where 0 disables it. With 20 calls at new rates before each call at a hot rate, the hot kernels stay cached: 44.1 and 22.05 kHz to 16 kHz calls take 0.30 and 0.26 ms on x86-64 Linux, against 10.7 and 20.6 ms with the 16-entry limit (table in the description). The trade-off is that a kernel larger than the budget is never cached: 44056 Hz to 16 kHz needs 84.5 MiB and is rebuilt on every call, 1.6 s with default options on x86-64, until the limit is raised.

@0xShug0 0xShug0 added the ready label Oct 29, 2026
@0xShug0
0xShug0 merged commit 8703f9e into 0xShug0:main Oct 29, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants