Skip to content

feat(sanotts): opt-in ARM NEON CPU decoder for nano voices (sanotts.cpu_decoder=neon) - #860

Merged
0xShug0 merged 5 commits into
0xShug0:mainfrom
tarelerulz:sanotts-neon-decoder
Oct 10, 2026
Merged

0xShug0 merged 5 commits into
0xShug0:mainfrom
tarelerulz:sanotts-neon-decoder

Conversation

@tarelerulz

Copy link
Copy Markdown
Contributor

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: ggml
  • Model-local: src/community_models/sanotts/neon_decoder.{h,cpp}. No framework or GGML changes.
  • Only the decoder graph is replaced. Durations, the token stage, 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.
  • Sizes come from SanoTtsConfig, so it works for both nano voices (heart-nano and heart, both tested). Piperlite voices ignore the option.
  • Weights are copied out of the CPU backend once (ggml_backend_tensor_get) and repacked; acoustic.output is folded into decoder.embed (an extra row of ones carries its bias, so edge padding matches). Setup time is logged as sanotts.neon_decoder_setup_ms: ~3 ms heart-nano, ~27 ms heart on the Pi.
  • Portability: the file is guarded by __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 for neon there, or on a non-CPU backend, fails with a clear error.

Where the speed comes from

  1. 1x1 convolutions (pwconv1/2, head, input proj): weights repacked as [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).
  2. k>1 convolutions: zero-padded input rows, weights as [cin*K][cout], same register block.
  3. All threads work on every layer (split by output channels or frames), with a spin barrier between layers (a mutex/condvar barrier was measurably worse, see the Pixel numbers).
  4. The mel projection is folded into the decoder embedding; NEON GELU (exact-erf form, erf by Abramowitz-Stegun 7.1.26, |error| < 1.5e-7).

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 with taskset -c 1-3. Same binary for both columns; only sanotts.cpu_decoder differs. Whole audiocpp_cli process, best of 3 after a warm-up run.

Voice Text (audio) ggml neon Speed-up
heart-nano greeting (2.8 s) 77 ms 61 ms 1.26x
heart-nano quotes (6.6 s) 137 ms 97 ms 1.41x
heart-nano one sentence (6.3 s) 129 ms 91 ms 1.42x
heart-nano numbers/dates (13.5 s) 245 ms 162 ms 1.51x
heart-nano email, 6 sentences (13.8 s) 250 ms 162 ms 1.54x
heart-nano run-on, no punctuation (13.4 s) 247 ms 156 ms 1.58x
heart-nano 80-word paragraph (28.5 s) 476 ms 298 ms 1.60x
heart-nano 4.9 kB long-form, framework chunker (337 s) 4944 ms 2673 ms 1.85x
heart greeting (2.8 s) 177 ms 147 ms 1.20x
heart quotes (6.8 s) 365 ms 255 ms 1.43x
heart one sentence (6.2 s) 348 ms 243 ms 1.43x
heart numbers/dates (14.2 s) 726 ms 497 ms 1.46x
heart email, 6 sentences (14.3 s) 714 ms 450 ms 1.59x
heart run-on, no punctuation (13.1 s) 679 ms 427 ms 1.59x
heart 80-word paragraph (28.8 s) 1440 ms 887 ms 1.62x
heart 4.9 kB long-form, framework chunker (342 s) 15888 ms 9053 ms 1.75x

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_ms summed per request, heart, email text, median of 8:

threads ggml neon
1 288 ms 116 ms (2.5x)
2 275 ms 170 ms (1.6x)

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

  • vs ggml (all 32 runs, both machines): same sample count; 99.56-99.84% of 16-bit samples identical, and all remaining samples differ by exactly 1 LSB; correlation 1.000000. The differences come from float summation order, the folded projection and the GELU approximation.
  • Long-lived session: --batch-text-file with 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.
  • Tests: model_spec_system_test and sanotts_frontend_test pass. neon_decoder.cpp compiles clean with -Wall -Wextra; the non-NEON stub also compiles.

Things you may want to look at

  • Existing GGUFs embed the older model spec, so on those files the new session option is rejected as unknown until the package is re-converted (or with --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.
  • Memory: in neon mode 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.
  • Default: I left ggml as the default everywhere. Making neon the default on AArch64 CPU sessions is your call.
  • Threads: the NEON decoder uses the session's thread count. In the Pixel VM one thread was fastest; on the Pi three were.

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.

…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]>
@tarelerulz

Copy link
Copy Markdown
Contributor Author

More phone numbers, same PR head (fcac13fa), same Pixel 9 Linux VM build as in the description (Cortex-X4/A720/A520, armv8.2-a+dotprod+i8mm, ENGINE_ENABLE_NATIVE_CPU=OFF). All heart-nano unless noted.

Per stage, like prompt vs generation speed

Medians of 5 runs, stage times from --log summed over chunks. Writing = decoder_ms + istft_ms, in seconds of audio per second; overall = session.wall_ms.

Pixel 9 VM text (audio) writing ggml -> neon overall ggml -> neon
1 thread, heart-nano email (13.4 s) 116x -> 257x 108x -> 230x
1 thread, heart-nano paragraph (28.1 s) 155x -> 342x 148x -> 315x
1 thread, heart-nano 4.9 kB long-form (330 s) 190x -> 577x 181x -> 515x
1 thread, heart 4.9 kB long-form (336 s) 50x -> 139x 48x -> 126x
2 threads, heart-nano 4.9 kB long-form 120x -> 117x 114x -> 109x
2 threads, heart 4.9 kB long-form 43x -> 72x 40x -> 71x

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 run

The Wikipedia "Apollo 11" article as plain text (13,185 words, 79,427 characters, references cut), heart-nano, 1 thread. The whole audiocpp_cli process, including start-up, eSpeak on the full text, the framework chunker and writing the 265 MB WAV:

wall audio real time peak RSS
cpu_decoder=ggml 34.1 s 92 min 14 s 162x 1159 MB
cpu_decoder=neon 14.2 s 92 min 14 s 390x 1146 MB

(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]>
@tarelerulz

Copy link
Copy Markdown
Contributor Author

Two things about how people will find and use the option, plus a small follow-up commit (4356400c):

1. Existing packages reject the option with a misleading error. GGUF packages converted before this PR embed a model spec without cpu_decoder, so someone who reads the docs and adds --session-option sanotts.cpu_decoder=neon got unknown sanoTTS session option: sanotts.cpu_decoder, which reads as if the docs were wrong. The new commit keeps the same check but explains the cause and the fixes for that one key (other unknown keys keep the old message):

sanotts.cpu_decoder is not in this model package's embedded model spec (the package predates the option).
Re-download or re-convert the package, or pass --model-spec-override <audio.cpp>/model_specs/sanotts.json

The docs' option entry now says the same. Tested: old package without override -> new message; sanotts.bogus -> unchanged message; with the override -> synthesises, audio byte-identical to before. Re-publishing the sanoTTS packages with the updated spec would remove the problem entirely. That's on your side, if you want it.

2. Discoverability. Right now the option is only visible on the sanoTTS docs page; --help and --inspect don't list per-family session options. The simplest way for Pi and phone users to benefit would be to make neon the default for nano voices on AArch64 CPU sessions, with cpu_decoder=ggml to opt out. I left ggml as the default because that's your call, and because the newer-core and Apple Silicon numbers are still untested. Happy to switch the default in this PR if you'd like that.

@0xShug0

0xShug0 commented Oct 10, 2026

Copy link
Copy Markdown
Owner

@tarelerulz Thanks! I did a quick test on M4.

Voice Threads GGML → NEON Speedup
heart-nano 1 25.58 → 4.93 ms 5.18×
heart-nano 4 8.79 → 1.76 ms 5.01×
heart 1 122.73 → 38.66 ms 3.17×
heart 4 39.10 → 11.25 ms 3.47×

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.

@0xShug0

0xShug0 commented Oct 10, 2026

Copy link
Copy Markdown
Owner

@tarelerulz I'm going to make some changes to address the issues directly.

@0xShug0

0xShug0 commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Two changes:

  • Adds auto and makes it the default: NEON for supported ARM64 CPU nano voices, GGML otherwise.
  • Adds model-local compatibility so legacy GGUFs accept the option without a spec override.

tarelerulz and others added 2 commits October 10, 2026 12:19
…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]>
@tarelerulz

Copy link
Copy Markdown
Contributor Author

@0xShug0 Thanks for testing on the M4 and for the auto change. Making it the default is the best outcome. Two more commits are on top of yours (0622da6a):

1. 5a716e0b, a faster NEON decoder (output byte-identical). Profiling heart showed the time going to memory traffic rather than arithmetic. Each thread took a block of output channels and re-read the whole packed input, which is 3.4 MB for long-form heart and doesn't fit the Pi's 1 MB L2. The K>1 weights were also laid out so that every load started a new cache line. Now each thread takes a range of frames instead, and K>1 weights are stored per 4-output block ([cout/4][cin*K][4]). Audio is byte-identical to the previous NEON version. On the Pi 400 (median of 5, alternating A/B), heart is 1.13–1.26x faster and heart-nano 1.02–1.08x. Against ggml, that's heart 1.27–2.12x and heart-nano 1.28–1.96x.

2. d4df60d8, a self-check and a unit test. The NEON decoder re-implements build_decoder_graph() by hand, so these keep a later change on either side from silently producing wrong audio.

  • Startup check: when the NEON decoder is selected, it decodes 24 fixed frames through both paths and compares them. heart and heart-nano differ by about 3e-5 and 7e-5; the limit is 1e-3 relative. The difference is logged as sanotts.neon_self_check_max_diff and the check costs about 20 ms once per session. Because neon is now the default, a failed check under auto falls back to the GGML graph (logged as sanotts.neon_self_check_fallback). An explicit cpu_decoder=neon still refuses to run.
  • Unit test: tests/unittests/test_sanotts_neon_decoder.cpp checks the NEON decoder against a double-precision reference on small random weights, with no model files needed. It covers frame counts 1–100, identical output for 1–7 threads and on instance reuse, and size validation. On non-NEON builds it checks that the decoder refuses to construct.

Tested on the Pi 400:

  • The unit test passes.
  • The default output equals neon output and is byte-identical to before, for heart and heart-nano.
  • With a forced check failure, auto produced the same audio as ggml, and neon stopped with the error.

Happy to drop or split either commit if you'd rather keep the PR as it is.

@0xShug0
0xShug0 merged commit 23047b9 into 0xShug0:main Oct 10, 2026
6 checks passed
@0xShug0

0xShug0 commented Oct 10, 2026

Copy link
Copy Markdown
Owner

@tarelerulz Merged! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants