Metadata-Version: 2.4
Name: roccoro
Version: 0.1.4
Summary: Native ROCm/HIP Kokoro-82M TTS engine (~245x real-time on AMD Strix Halo / RDNA3+ with zero PyTorch or ONNX Runtime at inference).
Author: Sergey Plis
License-Expression: MIT
Project-URL: Homepage, https://github.com/sergeyplis/roccoro
Project-URL: Repository, https://github.com/sergeyplis/roccoro
Project-URL: Issues, https://github.com/sergeyplis/roccoro/issues
Keywords: tts,kokoro,rocm,hip,amd,strix-halo,gfx1151,speech-synthesis
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: tqdm>=4.66
Requires-Dist: misaki>=0.9.4
Requires-Dist: spacy>=3.7
Requires-Dist: num2words>=0.5.13
Requires-Dist: espeakng-loader>=0.2.4
Provides-Extra: fixture
Requires-Dist: kokoro>=0.9.4; extra == "fixture"
Requires-Dist: torch>=2.4; extra == "fixture"
Provides-Extra: export
Requires-Dist: kokoro>=0.9.4; extra == "export"
Requires-Dist: torch>=2.4; extra == "export"
Dynamic: license-file

# Roccoro

**Native ROCm/HIP Kokoro-82M TTS engine — ~245× real-time on AMD Strix Halo (`gfx1151`), ~830 MB VRAM/GTT, zero PyTorch or ONNX Runtime at inference.**

`roccoro` implements the complete [Kokoro-82M](https://huggingface.co/hexgrad/Kokoro-82M) acoustic model (ALBERT/PL-BERT, 6 BiLSTMs, AdaINResBlk1d, ConvTranspose1d + fused Snake, 9-harmonic NSF sine source, 20-pt STFT/iSTFT) directly in standalone C++/HIP + rocBLAS — bypassing MIOpen completely and running at roofline on AMD RDNA3 / RDNA3.5 GPUs.

---

## Why Roccoro?

On AMD Strix Halo (`gfx1151` / Radeon 8060S), PyTorch ROCm's MIOpen backend stalls on Kokoro-82M at **< 1× real-time** due to workspace fallback issues, while CPU ONNX Runtime caps out around **~33× RT**.

`roccoro` replaces the ~4 GB `torch` + `onnxruntime` + `transformers` stack with a single ~500 KB HIP binary (`roccoro-engine`) plus lightweight `misaki` G2P:

| Backend (AMD Ryzen AI MAX+ 395 / Radeon 8060S `gfx1151`) | Speed (`× RT`) | GPU / GTT Memory | 9-Hour Audiobook | Inference Deps |
|---|---:|---:|---:|---|
| PyTorch ROCm (`cudnn=True` / MIOpen) | `< 1× RT` | ~3+ GB | > 9 hours | `torch`, `transformers`, `kokoro` |
| ONNX Runtime CPU FP32 (16 Zen 5 threads) | `33.4× RT` | 0 MB | ~16 min | `onnxruntime`, `kokoro` |
| **`roccoro` (native HIP, wave32 WMMA + persistent FP32 BiLSTM)** | **`245× RT` synth** (`222× RT` e2e w/ G2P+Opus) | **~830 MB** | **~2 min 14 sec** | **`numpy`, `misaki` (zero `torch`/`ort`)** |

### Quality & Hardware Parity (verified vs PyTorch FP32 reference)
- **Exact FP32 durations**: `0/43` phoneme duration mismatches on reference fixture (`verify_fixture` — 19/19 layer checks pass).
- **Audio fidelity**: `cos = 0.99936` waveform correlation on deterministic fixture, `env_mel_cos = 0.9999986` / `env_mel_lsd_db = 0.0395 dB` vs ONNX CPU FP32 on an 82-minute chapter.
- **Roofline utilization**: Custom `gfx1151` wave32 FP16-WMMA implicit-GEMM Conv1d kernel (`__builtin_amdgcn_wmma_f32_16x16x16_f16_w32`, `BM=64, BN=128, BK=64`) hits **27.61 TFLOP/s** (**107.7%** of measured 25.64 TFLOP/s roof); single-launch persistent FP32 BiLSTM (`k_bilstm_persistent_h256`) slashes BiLSTM time 12–16× while preserving exact FP32 prosody.
- **Coexists safely with large LLMs**: Uses only **~830 MB** total GTT (199 MiB weights + 448 MiB arena + runtime), leaving room to run alongside an 85 GB resident `llama-server`.

---

## Quickstart (AMD Strix Halo / RDNA3 / RDNA3.5 + ROCm)

### Instant 1-command `uvx` demo (~84 MB download, zero PyTorch or CUDA!)
```bash
uvx roccoro "Hello from Strix Halo at two hundred forty five times real time!" -o hello.wav
```
On first run, `roccoro` automatically compiles `roccoro-engine` for your GPU arch (~6 s), downloads & folds `Kokoro-82M` weights in pure NumPy (~199 MiB, zero PyTorch), and caches everything under `~/.cache/roccoro/`.

### Or install permanently with `pip`
```bash
pip install roccoro
roccoro build            # optional: auto-detects GPU arch via rocminfo (or pass --arch gfx1151)
roccoro export-weights   # optional: pure-NumPy weight export -> ~/.cache/roccoro/kokoro_v1_fp32.khip

# Synthesize speech (24 kHz WAV)
roccoro "Hello from Strix Halo!" -o hello.wav
roccoro --text-file chapter.txt -o chapter.wav --voice af_heart,af_bella --speed 1.0

# Check device, binary, and weights status
roccoro info
```

---

## Python API

```python
import roccoro

with roccoro.KokoroEngine(voice="af_heart") as engine:
    # Single call -> 24 kHz float32 NumPy waveform
    audio = engine.synthesize("Hello from Roccoro on Strix Halo!", speed=1.0)
    roccoro.write_wav("hello.wav", audio)

    # Or write directly to WAV
    out_path, duration_s = engine.synthesize_to_file("Fast local TTS.", "fast.wav")

    # Low-level pipelined API (used by mumblebee for 222x RT full-book synthesis)
    phonemes = engine.phonemize_batch("Paragraph one.\n\nParagraph two.")
    chunks = engine.synthesize_phonemes(phonemes, model_speed=1.0, show_progress=True)
```

---

## Standalone C++/HIP Binary & Benchmarks (Zero Python Required)

If you clone the repo directly:

```bash
git clone https://github.com/sergeyplis/roccoro.git
cd roccoro
make install               # builds roccoro-engine + installs to ~/.cache/roccoro/roccoro-engine
make verify bench          # builds verify_fixture and kokoro_bench_hip
```

### Layer-by-layer Numerical Verification
```bash
roccoro export-weights --out kokoro_v1_fp32.khip --dump-fixture fixture_short.khip
./src/roccoro/csrc/verify_fixture kokoro_v1_fp32.khip fixture_short.khip
```

### Binary Pipe Protocol (`roccoro-engine`)
`roccoro-engine` can be driven from any language (C++, Rust, Go, Node, Python) over stdin/stdout without Python:
1. Launch: `roccoro-engine --weights kokoro_v1_fp32.khip --arena-mb 448 --max-batch 32`
2. Engine writes 4-byte ready magic `0x4B484F4B` (`"KHOK"`).
3. Client sends request frames: `<magic=0x4B485251, num_chunks, speed_f32, max_batch>` followed by `num_chunks` records of `<token_len_u32, seed_u64, style_256xf32, tokens_lenxi32>`.
4. Engine replies: `<magic=0x4B485250, num_chunks>` followed by `num_chunks` records of `<n_samples_u32, pcm_f32[n_samples]>`.
5. Send `num_chunks = 0` (or close stdin) to exit cleanly.

---

## Configuration & Environment Variables

| Variable | Purpose |
|---|---|
| `ROCCORO_BIN` | Explicit path to `roccoro-engine` binary |
| `ROCCORO_WEIGHTS` | Explicit path to `kokoro_v1_fp32.khip` weights |
| `ROCCORO_CACHE_DIR` | Override cache dir (default: `$XDG_CACHE_HOME/roccoro` or `~/.cache/roccoro`) |
| `ROCM_PATH` | ROCm root used by `roccoro build` (default: `/opt/rocm`) |

## GPU Compatibility
- **Tested & tuned**: AMD Radeon 8060S / Strix Halo (`gfx1151`, ROCm 7.2.2).
- **Compatible ISA**: RDNA3 / RDNA3.5 GPUs supporting wave32 FP16 WMMA (`gfx1100`, `gfx1101`, `gfx1102`, `gfx1103`, `gfx1150`, `gfx1151`). Build with `roccoro build --arch <gfxXXXX>`.

## Credits & License
- Model architecture & weights: [Kokoro-82M](https://huggingface.co/hexgrad/Kokoro-82M) (Apache 2.0) and [Misaki](https://github.com/hexgrad/misaki) G2P.
- Extracted from [mumblebee](https://github.com/sergeyplis/mumblebee) and inspired by custom HIP roofline kernel engineering in [brainchop-core](https://github.com/neuroneural/brainchop-core).
- Licensed under the [MIT License](LICENSE).
