Repository navigation
feat(sanotts): opt-in ARM NEON CPU decoder for nano voices (sanotts.cpu_decoder=neon) - #860
Conversation
…pu_decoder=neon)
Adds a hand-written AArch64 NEON implementation of the nano-lineage frame
stage + decoder (frame blocks -> mel projection -> noise-fed ConvNeXt-1D ->
spectrum head), selected per session with sanotts.cpu_decoder=neon on the
CPU backend. The default stays ggml; durations, the token stage, the host
iSTFT and the DC block are unchanged.
- model-local: src/community_models/sanotts/neon_decoder.{h,cpp}; sizes from
SanoTtsConfig (works for heart-nano and heart), weights copied out of the
CPU backend once and repacked; whole file guarded by __aarch64__ &&
__ARM_NEON, other builds reject the option with a clear error
- all threads on every layer, spin-then-yield barrier between layers
- runtime: SanoTtsCpuDecoder {Ggml, Neon}; Neon replaces only the decoder
graph; setup time logged as sanotts.neon_decoder_setup_ms
- session option + model spec entry + docs (options, Pi 400 and Pixel 9 numbers)
Raspberry Pi 400, 3 threads, whole process: heart-nano 1.26-1.85x, heart
1.20-1.75x faster; output 99.6-99.8% sample-identical, max 1 LSB.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
More phone numbers, same PR head ( Per stage, like prompt vs generation speedMedians of 5 runs, stage times from
The token stage (durations + token context) is unchanged by this PR. Inside this VM both paths are slower with 2 threads than with 1, so the 1-thread rows are the meaningful ones here. For comparison, the Pi 400 at 3 threads: writing 79x -> 190x (heart-nano) and 22x -> 43x (heart) on the same long-form text. A long real-world runThe Wikipedia "Apollo 11" article as plain text (13,185 words, 79,427 characters, references cut), heart-nano, 1 thread. The whole
(A first run of the same text gave 33.5 s and 13.8 s.) Memory, unrelated to this PR: peak RSS grows with the length of the output: 17 MB for one sentence, 147 MB for 5.6 min of audio, about 1.15 GB for 92 min, roughly 12 MB per minute of audio. That's about twice the 24 kHz float audio itself, and it's the same with either decoder, so it looks like the long-form path keeps the whole result (in more than one form) until the WAV is written. Fine on this phone, but on a 4 GB Pi it limits a single run to about 3 hours of speech. Mentioning it in case streaming the output to disk per chunk is on your list. |
GGUF packages converted before sanotts.cpu_decoder existed embed a model spec without it, so the documented option failed as "unknown sanoTTS session option". Name the cause and the two fixes (re-download/re-convert, or --model-spec-override) in the error and in the docs. Other unknown options keep the existing message. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Two things about how people will find and use the option, plus a small follow-up commit ( 1. Existing packages reject the option with a misleading error. GGUF packages converted before this PR embed a model spec without The docs' option entry now says the same. Tested: old package without override -> new message; 2. Discoverability. Right now the option is only visible on the sanoTTS docs page; |
|
@tarelerulz Thanks! I did a quick test on M4.
The GGUFs are hosted by a third party. So for the new option, please follow Irodori-TTS's model-local compatibility approach so existing GGUFs can use it without a spec override. Then the PR is ready to merge. This PR is super useful. I've been too focused on GGML graphs, even for CPU execution. The same approach could benefit other models too, like #843 PS I was thinking about extracting the NEON convolution, depthwise convolution, LayerNorm, and GELU kernels into a reusable framework CPU library, but that feels like too much work given that only one model currently needs them. |
|
@tarelerulz I'm going to make some changes to address the issues directly. |
|
Two changes:
|
…per output block Profiling heart's long-form decode on a Pi 400 showed the convolutions were memory-bound, not compute-bound: with threads splitting OUTPUT channels, every thread re-read the whole packed input once per 4-channel block (3.4 MB for heart's pwconv2 on a 1500-frame chunk, more than the A72's 1 MB L2), and the K>1 weights were laid out [cin*K][cout], so each FMA's weight load hit a new cache line. - conv_part: threads now split the 16-frame blocks and compute every output block for a block while it is in L1 (K == 1 packs its own blocks, one barrier less per layer) - K > 1 weights repacked as [cout/4][cin*K][4] (one sequential stream) - store_block() shared by both paths Same arithmetic in the same order: output is byte-identical to the previous commit (16 runs, plus the 7-request batch session). Pi 400, 3 threads, whole process best of 3: heart 1.04-1.20x faster than before (long-form 9.05 -> 7.68 s), heart-nano 1.00-1.09x; vs cpu_decoder=ggml now heart 1.27-2.12x, heart-nano 1.28-1.96x. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…it test The NEON decoder re-implements build_decoder_graph() by hand and folds acoustic.output into decoder.embed, which is only valid while nothing sits between them. Two safeguards so a later change to either side cannot silently produce wrong audio: - runtime: when the NEON decoder is selected, decode 24 fixed frames through both paths; if they differ by more than 1e-3 * max(1, max |ref|), cpu_decoder=auto falls back to the GGML graph (logged as sanotts.neon_self_check_fallback) and explicit cpu_decoder=neon refuses to run (heart / heart-nano measure 3e-5 / 7e-5; logged as sanotts.neon_self_check_max_diff; ~20 ms once per session). Verified it fires with a deliberately broken decoder (max difference 15.9), and with a forced failure: auto produced audio identical to cpu_decoder=ggml, neon refused. - tests/unittests/test_sanotts_neon_decoder.cpp: the NEON decoder against an unfused double-precision reference of the same graph on small random weights (no model files): frame counts 1..100 around the 16-frame block, byte-identical output for 1/2/3/4/7 threads and after instance reuse, size validation. On non-NEON builds it checks the decoder refuses to construct. Fails on the same deliberate bug (max difference 1.27). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@0xShug0 Thanks for testing on the M4 and for the 1. 2.
Tested on the Pi 400:
Happy to drop or split either commit if you'd rather keep the PR as it is. |
|
@tarelerulz Merged! Thanks! |
Follow-up to #850. This is the heart-nano PR; there are no GGML changes, so no second PR for now.
The code was written with Claude (Anthropic's AI) at my direction and tested on my Raspberry Pi 400 and Pixel 9. Happy to answer questions or make changes.
What it does
Adds an opt-in ARM NEON implementation of the sanoTTS nano-lineage decoder (frame-stage acoustic blocks -> mel projection -> noise-fed ConvNeXt-1D -> spectrum head), selected per session:
--session-option sanotts.cpu_decoder=neon # default: ggmlsrc/community_models/sanotts/neon_decoder.{h,cpp}. No framework or GGML changes.expand_context/frame_features, the seeded noise, the host iSTFT and the DC block are the existing code. The NEON path returns the same[log-magnitude | phase]rows as the GGML graph.SanoTtsConfig, so it works for both nano voices (heart-nano and heart, both tested). Piperlite voices ignore the option.ggml_backend_tensor_get) and repacked;acoustic.outputis folded intodecoder.embed(an extra row of ones carries its bias, so edge padding matches). Setup time is logged assanotts.neon_decoder_setup_ms: ~3 ms heart-nano, ~27 ms heart on the Pi.__aarch64__ && __ARM_NEON(C++17,std::thread+ a small atomic spin-then-yield barrier, so it should also build on Apple Silicon — not tested there). Other builds compile a stub; asking forneonthere, or on a non-CPU backend, fails with a clear error.Where the speed comes from
[4-channel block][cin][4], inputs as[16-frame block][cin][16], so the inner loop is one sequential stream into a 4x16 NEON register block (vfmaq_laneq_f32).[cin*K][cout], same register block.Measurements: Raspberry Pi 400
Cortex-A72, 4 GB, Gentoo, gcc 15, Release,
-DAUDIOCPP_MODEL_SET=custom -DAUDIOCPP_MODELS=sanotts, default native CPU flags. CPU backend,--threads 3, pinned withtaskset -c 1-3. Same binary for both columns; onlysanotts.cpu_decoderdiffers. Wholeaudiocpp_cliprocess, best of 3 after a warm-up run.Measurements: Pixel 9 (Android Linux Terminal VM)
Cortex-X4/A720/A520, Debian VM, built with
-DENGINE_ENABLE_NATIVE_CPU=OFF -DGGML_CPU_ARM_ARCH=armv8.2-a+dotprod+i8mm(a native build on this SVE chip gave wrong sanoTTS audio in an earlier test, unrelated to this PR). The GGML output of this build matches the Pi's (99.9% samples identical, max 1 LSB), so the comparison is like for like.Decoder step only,
sanotts.decoder_mssummed per request, heart, email text, median of 8:Whole-process times inside that VM are too noisy to compare (one run took 107 s), and the NEON path does not gain from a second thread there, most likely vCPU preemption. With the first (mutex/condvar) barrier, 2 threads were slower than 1, which is why the barrier now spins. Bare-metal Android and other cores (A76/Pi 5, Apple M, Graviton) are untested.
Output parity and sessions
--batch-text-filewith 7 requests of different lengths in one session (decoder buffers reused across frame counts), both voices, both modes, on both machines: every request's WAV is byte-identical to the same text run on its own.model_spec_system_testandsanotts_frontend_testpass.neon_decoder.cppcompiles clean with-Wall -Wextra; the non-NEON stub also compiles.Things you may want to look at
--model-spec-override model_specs/sanotts.json, which is how I tested). Not sure how you prefer to handle spec additions for already-published packages.neonmode the GGML copy of the weights stays loaded next to the repacked copy (~1 MB extra for heart-nano, ~9 MB for heart). The GGML decoder graph is never built in that mode.ggmlas the default everywhere. Makingneonthe default on AArch64 CPU sessions is your call.Not tested: GPU backends (unchanged path), x86 build of the full tree, macOS.
Maintainer edits are enabled on this PR, as you asked. Interested in the reusable ARM library idea.