pingo-ptbr / docs /HOW-IT-WORKS.md
itxtoledo's picture
Add pingo-ptbr: 0.87 MB pt-BR TTS (MLX + ONNX + web demo)
27d8618 verified
|
Raw History Blame Contribute Delete
5.05 kB

How pingo-ptbr works and why it fits in under 1 MB

From text to audio

text --(English respelling)--> text --(dizione-ptbr)--> phonemes + stress
  phonemes --> encoder (6 blocks) --> per-phoneme encoding --> duration predictor
  encoding --(repeat each phoneme by its duration + 4 timing signals)--> sequence of 10 ms frames
  frames --> causal decoder (2 blocks, 64-frame window) --> 20 features per frame
  features --> FARGAN vocoder (frozen) --> 16 kHz audio
  1. Phonemes. dizione-ptbr converts the text (numbers, dates and currency already written out) to 55 phonetic symbols and marks the stressed syllable. English words are read with Portuguese rules, so pingo/pronunciation.py rewrites some terms first ("Hugging Face" becomes "Ráguin Feice").
  2. Encoder. Phoneme, voice and stress embeddings go through 6 blocks that alternate attention (GQA, RoPE, RMSNorm, gated-SiLU FFN) and dilated convolution.
  3. Durations. A small predictor says how many 10 ms frames each phoneme lasts (in log1p). Each phoneme is repeated for that long and gets 4 timing signals [2f−1, sin(πf), cos(πf), log1p(n)/log1p(200)], where f is how much of the phoneme has elapsed.
  4. Decoder. Two causal attention blocks, with a 64-frame window, produce 20 numbers per frame: 18 cepstra, pitch and pitch correlation (the Opus features).
  5. Vocoder. FARGAN, from Opus, turns the features into 16 kHz audio, 40 samples per step and 4 steps per frame. It is language-independent and we did not train it.

The acoustic model (steps 2 to 4) is ours: 270 thousand parameters in pingo-ptbr, 585 thousand in pivo-ptbr. The vocoder has 817 thousand and is the same in both.

Why 0.87 MB

Two independent moves that add up:

1. A narrower acoustic model. Going from width 96 to 64 cuts the acoustic parameters from 585k to 270k (about half) and the validation loss only worsens from 0.1682 to 0.1699. A model this size is easy to train: 37 minutes on a rented GPU.

2. Fewer bits per weight. A float32 weight takes 4 bytes. We store each matrix with 5 to 8 bits and an fp16 scale per output channel: weight ≈ integer × scale. On load the integers are multiplied by the scale and the model runs in float32 as before (there is no "5-bit arithmetic"; only the file shrinks). The 5-, 6- and 8-bit values are packed bit by bit, with no waste.

The trick is not using the same number of bits everywhere. We measured each layer's sensitivity (how much the result worsens when only that layer loses bits) and gave more bits to the layers that feel it most:

  • In the acoustic model, the embeddings (only 12k weights) and the decoder are the most sensitive; the encoder takes 5 bits.
  • In the vocoder, almost every layer takes 6 bits; the exceptions are cond.dense1, skip_dense, frame_conv and gru2.input. The glu*, gru3.*, gru2.hidden, frame_glu and skip_glu layers take as little as 5.

Quantizing everything the same works worse: 4 bits everywhere (0.75 MB) makes the vocoder noisy, and 6 bits everywhere (0.62 MB for the vocoder) has almost twice the spectral distortion of a mixed scheme of similar size.

How we measured "noise" without listening every time

  • Feature error of the acoustic model with float32's durations forced (separates the effect of the weights from the effect of rhythm).
  • Log-mel spectral distance (LSD) between the float32 vocoder's audio and the quantized one, running both on the same features. Sample-by-sample signal-to-noise ratio is useless here: FARGAN is autoregressive and a tiny phase difference already drops the SNR (8 bits gives only ~3.5 dB) even though it sounds the same.
  • WER (Qwen3-ASR, MiniMax pt set, 100 sentences per voice): confirms the words come through. It stays near 1% and barely moves with quantization, so it does not detect timbre noise: only listening does.
  • Listening. Listening is what showed that 4 bits everywhere sounds noisy while the mixed scheme sounds like 8 bits.

What the 0.87 MB figure means (and does not)

It is the size of the weights file. In memory, speak.py dequantizes once to float32, so RAM does not shrink, run time is that of the normal model (about 24× real time on an Apple M4, MLX, CPU), and the browser ONNX graphs (onnx/, 1.3 MB, int8 weights with the same values) do not read this file. For a runtime that computes in low precision (or a smaller browser download), that runtime would need a reader for the .bin: the format is simple and described at the top of pingo/qpack.py.

Limits of what we did

  • The vocoder is still the quality bottleneck: 16 kHz, "phone call" sound and a metallic texture, and it accounts for 0.66 of the 0.87 MB. Reducing it further needs fine-tuning or pruning FARGAN (a possible next step, not done).
  • Quantization is post-training, with no calibration and no quantization-aware training (QAT). With QAT the acoustic model could probably take 4 bits.
  • There was no listening test with several people.