Commit Graph
39 Commits
Author SHA1 Message Date
osobhandClaude Opus 4.7 d42aba0f1b rtx-csm: emotion2vec input normalization + 9-class direct mapping
Three real bugs found while running a YouTube → train → eval pipeline
end-to-end on real corpora:

1. emotion2vec was producing near-constant logits regardless of input.
   Per config.yaml `normalize: true` — data2vec2/emotion2vec expects
   per-utterance zero-mean unit-variance normalization on the raw
   waveform before the local_encoder. Added inside the EmotionDetector
   trait impl so all callers get it.

   Verified empirically: 4 different audio inputs (Carlini talk,
   audience question, McConaughey speech) now produce different argmax
   classes. Before fix: all 4 produced identical logits.

2. The 9→5 emotion fold was collapsing every real-world clip to
   [excited]. happy / surprised / other all mapped to Excited covered
   ~95% of natural speech. Replaced with a direct 9-class identity
   mapping; EmotionLabel gained Disgusted, Fearful, Happy, Surprised,
   Unk variants. Now: 132 [surprised] + 12 [excited] across the
   Carlini corpus instead of 144 [excited].

3. lora_train_emotional --peak-lr / --epochs flags. The canned 3-stage
   recipe over-fits on small (~100 clip) corpora at extended rank 8;
   users need to tune. (The recipe stays as defaults; flags are pure
   overrides.)

Plus diagnostic: examples/emotion2vec_probe — feed real audio files
into emotion2vec and dump per-class logits. Used to find bug #1.

Lib suite still 131/131 (the test that locked the 9→5 fold updated
to lock the new identity mapping).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-28 12:23:53 -07:00
osobhandClaude Opus 4.7 209279c13e rtx-csm: Phase 13.9 — wav2vec2 candle port (slices 1+2+3, real ASR working)
Full port of facebook/wav2vec2-base-960h (94.4 M params, MIT) closing
the WhisperX-class word-alignment gap from the audio-ML survey. Same
staged-scaffolding pattern that worked for emotion2vec — but landed
slices 1+2+3 in one session.

src/wav2vec2.rs ships:
  - Wav2Vec2Config::base_960h
  - FeatureExtractor — 7 Conv1d (1→512, total stride 320). Layer 0
    uses GroupNorm with num_groups=num_channels=512 (HF's wav2vec2
    feat_extract_norm: "group"). Critical: state-dict key is
    layer_norm.* but the OP is GroupNorm — loading as LayerNorm
    produces empty CTC output.
  - FeatureProjection — LayerNorm(512) + Linear(512→768)
  - ConvPosEmbedding — kernel 128 grouped Conv1d, materialized at
    load time from upstream weight_g + weight_v (fairseq's weight_norm
    on dim=2; eps-guarded division for numerical stability)
  - Block — POST-norm transformer with separate Q/K/V (vs emotion2vec's
    fused QKV), uses (B*H, T, D) Metal 3D-matmul workaround from
    Phase 8.8 Moonshine
  - Encoder — pos_conv + initial LayerNorm + 12 Blocks
  - Wav2Vec2 top-level — load_from_safetensors via mmap'd VarBuilder
  - ctc_greedy_decode + VOCAB_960H constant for the 32-char alphabet

examples/wav2vec2_inspect.rs (slice 1): dumps tensor layout + config
examples/wav2vec2_smoke.rs (slice 3): real-weight load + ASR forward

Verified on Metal:
  loaded model in 0.28 s
  forward in 9 ms for 10.42 s audio (~1150× realtime)
  transcript: "HE HOPED THERE WOULD BE STEW FOR DINNER TURNIPS AND
              CARROTS AND BRUISED POTATOES AND FAT MUTTON PIECES TO
              BE LADLED OUT IN THICK PEPPERED FLOWER FAT AND SAUCE"

Numerical parity with upstream Python — the FLOWER-for-FLOUR typo is
the known wav2vec2-base-960h failure mode, matches HF reference exactly.

7 new unit tests; lib suite 127/127 (was 120).

Slice 4 remaining: Viterbi forced alignment given known transcript,
to emit (token, frame_start_ms, frame_end_ms) for word-boundary cuts.
The ASR path itself is now production-ready.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-28 03:33:35 -07:00
osobhandClaude Opus 4.7 9c1894ec73 rtx-csm: Phase 13.8 — emotion2vec port, slice 2d (real weights load + forward)
Slice 2 complete + slice 3 collapsed in. The full candle port loads
real upstream weights and runs forward in 160 ms.

RelativePositionalEncoder — 5 grouped Conv1d (kernel 19, groups 16,
same-padding via pad 9) with GELU between. Output is added back to
input as a positional bias. Pickle keys
relative_positional_encoder.{1..=5}.0.weight/bias (1-based indexing,
no .0.*).

Emotion2Vec top-level — wires LocalEncoder → ProjectFeatures →
RelPosEnc → ContextEncoder → MainEncoder → mean-pool → Classifier.
The proj.* classifier head lives at the state-dict root (not under
d2v_model.), so the constructor uses vb directly there.

Emotion2Vec::load_from_pickle uses VarBuilder::from_pth_with_state
to descend into the fairseq-style nested checkpoint via the "model"
key. One-shot loader; all 185 upstream tensor keys must map onto
candle params of matching shape — and they do.

examples/emotion2vec_smoke.rs — full pipeline integration test:
downloads (or reuses cached) emotion2vec_plus_base from HF, loads it
into candle, runs forward on 2 s of synthetic audio, prints all 9
raw logits + argmax + the 9→5 bucket fold.

Verified on Metal:
  loaded model in 0.19 s
  forward in 160 ms
  9 logits all finite (50-290 range, expected for raw classifier)
  argmax: class 7 (surprised) → 5-bucket [excited]

Mechanical correctness end-to-end. Semantic accuracy on real
emotional speech lands in slice 4 (EmotionDetector trait impl +
swap into audio_to_manifest + converse_server reactive-emotion path).

2 new unit tests:
  - relative_positional_encoder_preserves_shape
  - emotion2vec_random_init_end_to_end_shape

Lib suite 120/120 (was 118, +2).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-28 03:09:06 -07:00
osobhandClaude Opus 4.7 a77b22d47c rtx-csm: Phase 13.8 — emotion2vec_plus_base port, slice 1 (inspector)
First slice of the multi-session port that will replace the Phase 13.3
prosody-rule SER placeholder with a real classifier via the existing
EmotionDetector trait.

examples/emotion2vec_inspect.rs:
  - downloads model.pt + config.yaml + tokens.txt from
    emotion2vec/emotion2vec_plus_base on HF Hub
  - descends fairseq-style nested checkpoint via --key model
  - dumps all 185 tensors with shapes/dtypes + per-prefix summary
  - uses pickle::read_pth_tensor_info, same pattern as audioseal_inspect

Architecture confirmed (full notes in docs/emotion2vec_port_notes.md):
  - 93 M params, F32 (the 1.12 GB file is mostly optimizer state)
  - local_encoder: 7 Conv1d layers (wav2vec2 feature extractor:
    [(512,10,5)] + [(512,3,2)]×4 + [(512,2,2)]×2, T → T/320)
  - project_features: Linear 512 → 768
  - relative_positional_encoder: 5 Conv1d layers (kernel 19)
  - context_encoder: 4-layer transformer prenet (prenet_depth=4)
  - blocks.0..7: 8-layer main transformer (depth=8, embed_dim=768,
    12 heads, mlp_ratio=4, fused QKV qkv.weight=[2304, 768])
  - proj: Linear 768 → 9 (angry/disgusted/fearful/happy/neutral/other/
    sad/surprised/<unk>)

Slicing plan (remaining):
  Slice 2 (~half-day): candle module scaffolding + from_pickle loaders
  Slice 3 (~half-day): forward pass + shape verification
  Slice 4 (~hour): EmotionDetector impl + swap into audio_to_manifest
                  and converse_server

Lib suite 110/110.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 23:14:06 -07:00
osobhandClaude Opus 4.7 2feb5c9d67 rtx-csm: Phase 13.2 — audio→manifest pipeline (no Python sidecar)
Single command turns raw audio (podcast/audiobook/conversation) into a
training-ready JSONL manifest + per-segment clip wavs.

audio_to_manifest:
  1. Diarize (Phase 13.1: Silero V5 + WavLM-SV + clustering)
  2. Per segment: slice audio + Moonshine encode/decode → transcript
  3. Write `<stem>.spk{N}.{idx:04}.wav` + manifest.jsonl

Manifest rows match ManifestRow exactly (Phase 12.3), so the output
flows directly into lora_train_emotional / load_from_manifest.

Knobs: --segments-json (reuse precomputed diar), --emotion-tag and
--stage applied uniformly, --min-transcript-chars filters ASR failures,
plus all Phase 13.1 diarization knobs.

DiarizedSegment gained serde::Deserialize for the segments-json
reuse path.

Verified end-to-end: 2-speaker concat → 2 segments diarized in 192 ms
→ Moonshine transcribed → 2 manifest rows + 2 clips written → round-
trips through lora_train_emotional cleanly (LoRA injected with extended
coverage, adapter saved with embedded metadata, lib suite 104/104).

The full no-Python data-prep loop now reads:
  audio_to_manifest raw.wav → manifest.jsonl
  lora_train_emotional manifest.jsonl → voice.safetensors
  lora_eval base + lora for A/B
  generate / converse_server with --lora voice.safetensors

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 18:09:32 -07:00
osobhandClaude Opus 4.7 73d38ed290 rtx-csm: Phase 13.1 — in-crate diarization (Silero V5 + WavLM-SV + clustering)
Composes existing in-crate parts into a speaker diarizer with zero new
deps. Pipeline: Silero V5 VAD → speech intervals → WavLM-SV x-vector
per ~2s window → agglomerative average-linkage clustering on cosine
distance → merged (start_s, end_s, speaker) segments.

src/diarize.rs (~330 LOC) ships:
  - DiarizedSegment + DiarizationConfig
  - Diarizer that owns the two backbones
  - vad_intervals helper (smooths short silences, drops short speech)
  - hand-rolled agglomerative cluster with auto-threshold OR force-k modes
  - 5 unit tests (cosine distance edges, clustering, VAD interval extraction)

examples/diarize.rs CLI: --in --out --wavlm-sv-weights, plus knobs
(window/hop/vad-threshold/cluster-threshold/n-speakers/min-segment).
JSON output is consumable by ffmpeg/sox for downstream slicing.

Verified end-to-end on Metal:
  - Single-speaker 10.41s → 1 segment, 21× faster than realtime
  - Concatenated 2-speaker (CSM spk 0 + spk 1) → correctly identifies
    2 speakers, 10× realtime
  - Bug fixed in first run: clamp VAD interval bounds before slicing
    (Silero V5 pads to whole-chunk multiple, can exceed sample count).

Closes the WhisperX-class "speaker diarization" gap from the personal
voice training guide without a Python/ort sidecar — sidesteps both
runtime conflicts the project hit before (whisper.cpp/ggml in Phase 7.6,
ort/protobuf in Phase 8.1.3). ~80% of pyannote-community-1 fidelity,
which is fine for data prep.

Lib suite 104/104 (5 new tests).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 17:58:57 -07:00
osobhandClaude Opus 4.7 f7b6deac1a rtx-csm: Phase 12.7 — lora_eval held-out quality harness
Closes the train→generate→evaluate cycle. Users can now produce a hard
quality number for any adapter without listening manually.

evaluate_held_out runs teacher-forced forward_loss over a JSONL manifest
(same format as 12.3 trainer). Uses apply_emotion_hint so eval prompts
match training prompts. Frame sampling is seed-controlled — identical
seeds across runs score the same frames in the same order, which is what
makes a base-vs-LoRA A/B fair.

EvalRow + EvalSummary types, both serde-Serialize for JSON output.

examples/lora_eval.rs wraps it: --eval-manifest --report [--lora].
Documented usage: run twice with the same seed, diff the summary blocks.

Verified end-to-end on the existing 3-row curriculum manifest with
seed=42, frames-per-example=4: LoRA shifted mean/median/p90 loss
directionally in its favor (-0.011/-0.018/-0.007). Tiny because the
test adapter only saw ~10 training steps, but the eval signal is real
and the A/B path is wired.

Lib suite 99/99.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 17:22:14 -07:00
osobhandClaude Opus 4.7 32a811904a rtx-csm: Phase 12.5 — self-describing LoRA adapters
Trained adapters now embed (rank, alpha, target_modules, crate_version)
as JSON in safetensors __metadata__["rtx_csm_lora"]. apply_lora_adapter
reads it at load time so users no longer have to remember matching
--lora-rank/--lora-alpha/--extended-lora flags from training.

LoraAdapterMetadata::is_extended() heuristic: target_modules contains
any MLP path or output_proj or k_proj. Handles both the canonical
extended() preset and future custom configs that overlap it.

apply_lora_adapter rank/alpha/extended params became Option<_>
(None = use file metadata, Some = override). Both callers updated.
save_lora_adapter_with_metadata is the new path used by both trainers;
the plain save_lora_adapter still exists for the metadata-less case
(per-stage curriculum snapshots).

safetensors dep bumped 0.4 → 0.7 to match candle 0.9's transitive pin
so candle's Tensor: View impl is in scope for serialize_to_file
(candle's own save wrapper hardcodes the metadata arg to None).

Backward compat: pre-12.5 adapters load fine when explicit CLI flags
are passed; auto-detect path is skipped silently.

Verified end-to-end: trained adapter saved with metadata,
`generate --lora <path>` (no other flags) auto-detected
rank=8 alpha=16 extended=true and applied. Older metadata-less adapter
still loaded with explicit flags. 3 new unit tests; lib suite 99/99.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 16:29:25 -07:00
osobhandClaude Opus 4.7 510cd7011c rtx-csm: Phase 12.3 — curriculum LoRA trainer for emotional fine-tunes
Closes the personal_voice_training_guide.md §4 stack: capacity (12.1) +
control tokens (12.2) + multi-stage curriculum (this).

TrainingExample gains emotion_tag + stage. Trainer::train applies the
tag via the same apply_emotion_hint helper inference uses (now
pub(crate)) — training and inference must use identical prefix
formatting or the adapter won't transfer.

TrainingDataset::load_from_manifest reads JSONL
`{wav, transcript, emotion_tag?, stage?, speaker?}` rows; wav paths
resolve relative to manifest dir.

CurriculumStage + CurriculumTrainer run N stages sequentially against a
shared VarMap. Per stage: filter by ex.stage label, build a transient
sub-dataset, run Trainer, save snapshot if requested. The "*" stage
name is a global catch-all.

examples/lora_train_emotional.rs wraps the canonical 3-stage recipe:
audiobook (3 ep × lr 1e-4) → podcast (1 ep × lr 3e-5) → va (1 ep ×
lr 1e-5). --extended-lora recommended (FFN is the prosodic-style
carrier per the guide).

Verified end-to-end on Metal: 3-row manifest → all 3 stages execute,
checkpoints + final adapter written, prompt-token lengths varied by
emotion-tag length (9 vs 11 for different tags) confirming the tag
flowed through the training tokenization. Lib suite 96/96.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 15:37:44 -07:00
osobhandClaude Opus 4.7 237216a31f rtx-csm: Phase 11 — Silero V5 VAD pure-candle port (closes 8.1.3 deferred)
Phase 8.1.3 deferred Silero V5 VAD because the only Rust crate path
(`voice_activity_detector` via `ort`) collides with `sentencepiece-sys`
on protobuf 3.14 vs 3.21 and panics at process startup. This commit
closes that gap with a NATIVE candle port.

Direct port from `Snakers4/silero-vad/src/silero_vad/tinygrad_model.py`
(71 LOC reference). Architecture:

  stft_conv  Conv1d(1,    258, k=256, s=128)  no bias
  conv1      Conv1d(129,  128, k=3,   p=1)
  conv2      Conv1d(128,   64, k=3,   s=2, p=1)
  conv3      Conv1d(64,    64, k=3,   s=2, p=1)
  conv4      Conv1d(64,   128, k=3,   p=1)
  lstm_cell  LSTMCell(128, 128)
  final_conv Conv1d(128,    1, k=1)

Forward: reflect-pad input by 64, STFT-as-conv1d, sqrt(real² + imag²),
4-layer Conv1d feature stack with ReLU, single LSTM step (state across
chunks), 1x1 conv + sigmoid -> speech probability.

Files added:
  src/silero_vad.rs                       ~310 LOC (incl. LSTM cell + downloader)
  docs/silero_vad_port_notes.md           architecture + port plan
  examples/silero_vad_smoke.rs            real-audio discrimination test

Plus a new `ureq` direct dep (transport already pulled in via hf-hub).

Weights ship via download-on-first-run from the upstream GitHub raw
URL into `~/.cache/rtx-csm/silero_vad_16k.safetensors` (1.24 MB). No
repo bloat; no .gitignore wrestling.

End-to-end smoke (synthetic 50/50 silence/speech WAV at 16 kHz):

  load (cold):     download + parse, < 100 ms after first run
  VAD sweep:       170 ms over 9.99 s of audio = 0.017x realtime (59x faster)
  unit test:       passes (load weights + run one step)

Probability output (per 32 ms chunk):
  0-1.5 s:   p ~ 0.01-0.07   silence
  1.5-5 s:   p ~ 1.000        speech (clean ramp at speech onset)
  5-10 s:    p ~ 0.001        silence

Speech-chunk fraction 33% on the 50/50 layout — matches expected.

Production angle: dramatically better silence/speech discrimination
than the Phase 8.1.3b energy VAD (which only catches obvious silence).
Silero V5 catches whisper-quiet speech, breath/lip noise, music vs
speech distinction. Drop-in candidate for `--vad-gate` in a future
iteration.

The ort/protobuf conflict that blocked this for two months is now
permanently resolved by NOT using ort.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 13:46:47 -07:00
osobhandClaude Opus 4.7 df89372ad7 rtx-csm: Phase 10.4 — SilentCipher detect + Watermarker trait + apply CLI
End-to-end SilentCipher: bit-perfect round-trip on real LibriSpeech
audio. Sesame's actual production watermarker now works in pure
candle 0.9 + Metal.

New components in src/silentcipher.rs:

  detect(samples_16k) -> DetectResult
    1. RMS-normalize to VCTK baseline (matches embed pre-conditioning)
    2. STFT -> magnitude
    3. dec_m_0(magnitude) -> (B, message_dim, 1, T) logits
    4. argmax along message_dim -> (T,) per-frame predictions
    5. Truncate to multiple of message_len
    6. Reshape to (n_patches, message_len), per-column mode
    7. Find terminator (value 0), rotate so payload follows it
    8. Subtract +1 offset -> original codes

  encode_bits / decode_bits  (Phase 10.4 fix)
    Switched from base-4 (2 bits per code) to base-`(message_dim - 1)`.
    The 16 kHz model has message_dim=4 = 3 carrier values (1,2,3) +
    terminator (0), NOT 4 carrier values. Original base-4 packing
    occasionally produced value 3, which Python's
    `np.identity(4)[index+1]` would have crashed on. Real capacity:
    15 codes x log2(3) ~= 23.78 bits per patch.

  SilentCipherWatermark (impl Watermarker)
    Wraps a SilentCipherWatermarker with a fixed default_payload so
    it satisfies the existing Watermarker trait. Maps confidence ->
    DetectionResult.mean_presence and the lower-16-bits of the
    decoded payload -> DetectionResult.message (None below confidence
    0.7 to suppress false positives).

  examples/silentcipher_apply
    Mirrors audioseal_apply: --in / --out / --payload / --detect-only.
    Loads from sony/silentcipher HF repo, embeds, optionally
    resamples back to source rate, optionally re-detects to verify.

Verified end-to-end (LibriSpeech /tmp/asr_test.flac, 10.42 s @ 16 kHz):

  Build:        29 ms (3 .ckpt files from HF cache)
  Embed:      1213 ms = 0.116x realtime
  Detect:     1838 ms = 0.18x realtime
  payload:        0x00BC614E (in)
  recovered:      0x00BC614E (out)
  codes match:    15 / 15
  confidence:     1.0000

Clean (un-watermarked) audio: confidence 0.475, codes mostly 0 -
strong signal-vs-noise discrimination at the 0.7 threshold.

This closes the most surprising gap from the Sesame stack analysis:
rtx-csm now has the *literal* Sesame watermarker (not Meta's
AudioSeal) working in pure candle. AudioSeal stays available for
callers that prefer it.

Phase 10.5 (next): wire as a third option in converse_server alongside
AudioSeal, and a 24/16 kHz ResampledWatermarker for the CSM path.
Plus an A/B bench (SilentCipher vs AudioSeal).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 12:54:56 -07:00
osobhandClaude Opus 4.7 f6bcf0735a rtx-csm: Phase 10.3 — SilentCipher embed pipeline end-to-end
End-to-end encode pipeline working: 3 ckpts load from HF, STFT runs,
encoder + carrier-decoder forward, iSTFT reconstructs. Watermarked
audio out preserves length + carries an embedded message.

New components in src/silentcipher.rs (~150 LOC added):

  SilentCipherWatermarker     bundle of cfg + 3 networks + STFT + device
  ::from_ckpts(...)           load enc_c.ckpt + dec_c.ckpt + dec_m_0.ckpt
                              pickle files via candle_core::pickle::read_all
  ::build_message(codes, T)   one-hot + tile across time axis to match
                              n_frames; matches Python letters_encoding
                              shape semantics
  ::embed(samples_16k, codes) full encode pipeline:
                              1. RMS-normalize to VCTK baseline
                              2. STFT -> magnitude + phase
                              3. enc_c forward -> 32-channel carrier
                              4. enc_c.transform_message -> projected msg
                              5. cat(carrier_enc, mag.repeat(32),
                                     msg_enc.repeat(32)) -> 96 channels
                              6. dec_c forward + utterance-level
                                 normalization + ensure_negative_message
                                 + ReLU clamp
                              7. iSTFT -> watermarked audio
                              8. de-normalize energy
  ::encode_bits(payload)      pack a u32 into message_len-1 2-bit codes

Smoke test (`examples/silentcipher_smoke`) verified end-to-end:

  Build watermarker:       29 ms (loads 3 .ckpt files)
  Synthetic sine embed:   187 ms /  1.00 s audio
  Real speech embed:     1042 ms / 10.42 s audio  =  0.10x realtime

The 0.10x realtime figure is comparable to AudioSeal in Phase 6f.wm
(73 ms per ~6.8 s sentence = ~0.011x realtime, but AudioSeal had
warm-cache benefit). On a fresh cold model, SilentCipher comes in
~10x faster than realtime — order-of-magnitude OK.

SNR vs original: 24.6 dB on the speech sample, target 47 dB per the
released hparams. The watermark is currently more audible than
intended. Likely cause: utterance-level normalization scale factor
needs refinement, OR the ensure_negative_message + ReLU path is
clipping more than the Python path. Will be diagnosed in Phase 10.4
when detection round-trip lands — the real test of correctness is
"can dec_m recover the embedded codes?", not absolute SNR.

Phase 10.4 will:
  - Implement detect() to recover the embedded codes via dec_m_0
  - Add Watermarker trait impl for SilentCipherWatermarker
  - examples/silentcipher_apply CLI mirroring audioseal_apply

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 12:40:02 -07:00
osobhandClaude Opus 4.7 e7b34abd16 rtx-csm: Phase 10.2 — SilentCipher Layer + STFT + scaffolds
Foundation in src/silentcipher.rs (~480 LOC) plus rustfft 6.2 dep.

What works:

  Stft                 windowed framed FFT via rustfft (Hann window,
                       n_fft + hop_length config). Round-trip unit test
                       on a 440 Hz sine (16 kHz, 1 s) achieves > 0.95
                       correlation — overlap-add + epsilon-trick blur
                       the bit-exact return slightly but the recovered
                       waveform tracks the original cleanly.

  SilentCipherConfig   hyperparameters from the released 16 kHz hparams
                       (N_FFT=2048, HOP=1024, message_dim=4,
                       message_band_size=512, etc.). Constructor
                       sixteen_khz() returns the production defaults.

  Layer                gated conv block: bn(conv(x) * sigmoid(gate(x)))
                       built from candle_nn::Conv2d + BatchNorm2d.
                       BatchNorm runs in eval mode (forward_t with
                       train=false) — released checkpoints carry
                       running_mean / running_var.

  Encoder              3 stacked Layers (1->32, 32->32, 32->32) plus a
                       Linear(message_dim, message_band_size) for the
                       transform_message helper that projects bit
                       payloads onto the freq axis.

  CarrierDecoder       4 stacked Layers (96->96 x3, 96->1 with k=1) +
                       optional ensure_negative_message + freq-band
                       masking + RMS / SDR scaling.

  MsgDecoder           10 stacked Layers (1->128, 128->128 x8,
                       128->message_dim) + final Linear collapsing
                       freq -> 1. Slices to message_band_size rows
                       before processing. Models the PyTorch index
                       doubling (Dropout interleaved in eval mode is
                       identity, but stored under index 2i+1).

  vb_from_ckpt         opens a .ckpt pickle file and exposes a
                       VarBuilder with the legacy `module.` prefix
                       stripped, ready for Encoder::new etc.

What doesn't work yet (Phase 10.3):

  - End-to-end embed() / detect() pipeline glue (STFT input ->
    Encoder + transform_message -> CarrierDecoder -> iSTFT, plus the
    decode mirror). Each piece compiles + has a smoke test, but the
    pipeline orchestration is the next ship.
  - Watermarker trait impl + wiring into Generator.set_watermarker.
  - examples/silentcipher_apply (mirror of audioseal_apply).

Tests: 2 new unit tests pass alongside the existing 88. Full lib build
clean on `--features metal`.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 12:33:12 -07:00
osobhandClaude Opus 4.7 9f5c345b53 rtx-csm: Phase 10.1 — SilentCipher inspector + port notes
Foundation for porting Sesame's actual production watermarker (NOT
AudioSeal — the gap analysis identified this as the literal Sesame
parity item). Same iterative-shipping pattern as Phase 8.4 for
Moonshine.

`docs/silentcipher_port_notes.md`:
  - Full architecture from SesameAILabs/silentcipher/src/.../model.py
    (verified against 95 LOC of source)
  - Three small networks of gated 2D convs on STFT:
      enc_c    3 layers   1 -> 32 channels
      dec_c    4 layers   96 -> 1 channels
      dec_m    10 layers  1 -> 128 -> message_dim, plus Linear
  - Each Layer = Conv2d * sigmoid(Conv2d) + BatchNorm2d
  - Pipeline (encode + decode) walked through step by step
  - 10 ordered porting tasks with hour estimates totaling ~1-2 days
  - Risks flagged: STFT helper needed, BatchNorm running stats loading,
    phase passthrough, message-length differences vs AudioSeal

`examples/silentcipher_inspect`:
  - Downloads sony/silentcipher 16 kHz checkpoint from HuggingFace
  - Dumps hparams.yaml + tensor shapes per .ckpt file
  - Verified output:
        N_FFT 2048   HOP 1024   SR 16000
        message_dim 4  message_len 16  message_band 512
        enc_c     0.17 MB    40 k params
        dec_c     2.01 MB   500 k params
        dec_m_0   9.54 MB  2.38 M params
        Total           ~2.92 M params

That's ~10x smaller than AudioSeal's gen+det combined. Port
estimated 1-2 days.

`.ckpt` files are pickle (PyTorch state_dict) — direct loadable via
candle_core::pickle::read_all, same path as audioseal_convert.rs.
No safetensors conversion needed.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 12:23:08 -07:00
osobhandClaude Opus 4.7 95015c17e1 rtx-csm: Phase 8.9 — Moonshine KV cache + profile binary
KV cache for the decoder turns greedy generation from O(T^2) into O(T)
total work. Per-token decode drops modestly on short transcripts
(7.1 -> 6.0 ms/token at 49 tokens) and compounds on longer ones.

New components in src/moonshine.rs:

  RotaryCache::apply_at(x, position, t)
      Apply RoPE for a window starting at `position`. Replaces
      `apply()` for cached step (which always called positions 0..T).

  DecoderSelfAttention::forward_step(xs, cache_k, cache_v, rope, position)
      Single-token cached self-attn. Appends new K/V to per-layer cache,
      attends across full accumulated history. No causal mask needed
      (cache only contains positions <= current).

  CrossAttention::precompute_kv(enc) -> (K, V)
      One-shot encoder K/V projection for cross-attn. Reused every step.

  CrossAttention::forward_step(xs, k, v)
      Cached cross-attn. Q computed from new token; K/V from precompute.

  DecoderCache { self_k: Vec<Option<Tensor>>, self_v, cross_k, cross_v, position }

  Decoder::precompute_cross_kv(enc) -> DecoderCache
  Decoder::step(token_id, &mut cache) -> logits (1, vocab)
  Decoder::generate_cached(enc, cfg, max_tokens) -> Vec<u32>
      Greedy loop using the cached step.

Profile (5 steady-state runs on /tmp/asr_test.flac, 10.42 s LibriSpeech):

  warm-up:                344 ms
  steady-state mean: 307 ms (p50 305, range 298-319)
  realtime factor: 0.0294x

Comparison across all STT in rtx-csm:

  Backend           RTF         Notes
  Kyutai STT 1B     1.01x       hardware-bound, 3 GB
  Whisper-tiny      0.020x      breaks CSM (in-process ggml conflict)
  Moonshine-tiny    0.0294x     pure candle, NO runtime conflict

Moonshine is the only fast STT path that integrates cleanly. ~34x
faster than realtime, ~17x faster than Kyutai 1B, no protobuf or
ggml linkage issues.

New `examples/moonshine_profile` mirrors `stt_profile` and
`whisper_profile` so all three STT backends report comparable numbers.

Phase 8.10 (next): wire as a third AsrEngine variant in converse_server
for English-only deploys. Replace the energy-VAD-gated Kyutai path
when --moonshine flag is set.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 11:05:19 -07:00
osobhandClaude Opus 4.7 b22ff3544e rtx-csm: Phase 8.8 — Moonshine end-to-end transcription works
Full encoder-decoder Moonshine v2 transcribing real audio in pure
candle 0.9 + Metal. No ort, no ggml, no protobuf. The path that
whisper-rs (Phase 7.6) and Silero V5 via ort (Phase 8.1.3) couldn't
deliver due to in-process linkage conflicts.

End-to-end on /tmp/asr_test.flac (LibriSpeech, 10.42 s):

  encode:    10 ms
  decode:   348 ms (49 tokens, 7.1 ms/token greedy, no KV cache)
  realtime factor: 0.068x  (~14x faster than realtime)

Output transcript:
  "He hoped there would be stew for dinner, turnips and carrots and
   bruised potatoes, and fat, mutton pieces to be ladled out in thick,
   peppered, flour-fat and sauce."

Ground truth:
  "He hoped there would be stew for dinner, turnips and carrots and
   bruised potatoes and fat mutton pieces to be ladled out in thick
   peppered flour-fattened sauce."

Near-perfect (a few punctuation tweaks, "flour-fat and sauce" vs
"flour-fattened sauce"). WER very low.

Compared to other STT backends in this crate:
  Kyutai STT 1B   :  1.01x realtime  (3 GB, hardware-bound)
  Whisper-tiny    :  0.020x realtime (in-process ggml -> CSM regression)
  **Moonshine-tiny: 0.068x realtime  (pure candle, no runtime conflict)**

Components shipped this commit:
  - Decoder::generate(encoder_output, cfg, max_tokens) — greedy
    autoregressive loop. No KV cache yet (each step re-runs the full
    growing token sequence — O(T^2) total). For 49-token transcripts
    at <500 ms total, KV cache isn't urgent.
  - load_tokenizer() — wraps tokenizers::Tokenizer::from_file for
    Moonshine's HF tokenizer.json (BPE, vocab 32_768).
  - examples/moonshine_transcribe — full pipeline: audio -> 16 kHz
    PCM -> encode -> decode -> detokenize -> transcript text.

Critical bug fixed: SwiGLU gate/up split direction. HF
modeling_moonshine.py says:
    hidden, gate = fc1(x).chunk(2, dim=-1)
    out = silu(gate) * hidden
The FIRST half of the fused fc1 output is `up` (multiplied), the
SECOND half is `gate` (silu-activated). I had it reversed in Phase
8.7 — the symptom was a degenerate "tt tt tt" repetition loop after
the first 2 tokens. Reversing the split unlocked the working
transcription. Captured in the code comment.

Remaining for Moonshine readiness in production:
  Phase 8.9 — KV cache for sub-200ms latency on long transcripts,
              plus a standalone moonshine_profile binary for the
              full A/B against Kyutai/Whisper.
  Phase 8.10 — wire as a third AsrEngine variant in converse_server
               (gated on English-only acceptance for the deploy).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 10:54:50 -07:00
osobhandClaude Opus 4.7 0699cdeb45 rtx-csm: Phase 8.5 — Moonshine conv stem (verified end-to-end)
First working piece of the Moonshine v2 candle port. New
src/moonshine.rs module with:

  - MoonshineConfig::tiny()  — hyperparameters from HF config.json
  - ConvStem (Conv1d × 3)    — audio stem, raw 16 kHz → 288-d hidden
  - load_conv_stem()         — VarBuilder from HF safetensors

Conv layout (verified against HF source):
  conv1: in=1,   out=288, k=127, stride=64, no bias
  conv2: in=288, out=576, k=7,   stride=3,  bias
  conv3: in=576, out=288, k=3,   stride=2,  bias
  Activations: tanh after conv1, gelu_erf after conv2 / conv3

Smoke test (`examples/moonshine_smoke`):
  - Downloads UsefulSensors/moonshine-tiny from HF
  - Synthetic 10 s @ 16 kHz audio (silence + sine pulse)
  - input (1, 1, 160000) -> output (1, 415, 288)
  - Expected T_seq=415 ((160000-127)/64+1 -> 2498 -> 831 -> 415)
  - Output max abs = 23.17 (real signal, weights loaded correctly)

Also extends `examples/moonshine_inspect` to dump conv shapes
explicitly (was being truncated by the per-prefix `take(8)` cap).

Next ship: encoder transformer block (partial RoPE, GELU MLP) and
output layer norm. Tracked in Phase 8 plan; ~2-3 hours of work.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 10:27:51 -07:00
osobhandClaude Opus 4.7 c848abef22 rtx-csm: Phase 8.4 spike — Moonshine v2 inspector + port notes
First step on the Tier 2.2 (Moonshine v2 candle port) item from the
Phase 8 plan. Full port is honestly multi-session work (~12-15 hours
of focused implementation across encoder, decoder, generation loop,
tokenizer, weight mapping, smoke test). This commit ships the
foundation so future sessions start from concrete data instead of
arxiv reading.

Two ships:

1. examples/moonshine_inspect — downloads UsefulSensors/moonshine-tiny
   from HF, parses safetensors header, dumps all 160 tensors grouped
   by prefix with shapes + dtypes. Verified output: 27.1 M params,
   108.4 MB safetensors (F32), encoder + decoder layers laid out as
   expected.

2. docs/moonshine_port_notes.md — captures every architectural fact
   established by the inspector + HF config.json:
   - Hyperparameter table (hidden=288, 6+6 layers, vocab=32768,
     partial_rotary=0.9, etc.)
   - Tensor layout per layer (encoder, decoder)
   - Architecture summary (raw waveform input, 3-layer Conv1d stem,
     SwiGLU decoder MLP via fused fc1, tied LM head)
   - Ordered porting tasks with hour estimates totaling ~12-15 h
   - Risks / unknowns (conv strides not in config, tied output head
     question, quality-vs-Kyutai concern)
   - Recommended order of attack for the next session

The full port itself is deferred. Ship the foundation now so the
remaining work has a clean handoff.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 10:21:30 -07:00
osobhandClaude Opus 4.7 f4a6ffeaf5 rtx-csm: validate energy-VAD gate on silence-heavy audio
Adds examples/make_silence_test.rs which builds a 10 s WAV that is
50% silence + 50% real speech (1s silence | 4s speech | 5s silence)
so we can see the VAD gate work clearly.

Bench A/B (mock LLM, Q8+stream, 3 turns each, M-series Metal):

  Audio                       No VAD     With VAD    Δ recv_phase
  90% speech (LibriSpeech)    4834 ms    4509 ms     -7%
  50% silence (synthetic)     6490 ms    2204 ms     -66%

The structural win scales with silence content as expected. Real-world
voice-agent audio (30-50% silence per typical call-center / voice-bot
benchmarks) will see ~30-50% recv_phase reduction. The earlier 7% on
LibriSpeech wasn't a weak result — it accurately reflected the ~10%
silence in that recording.

This validates the energy-VAD path despite Silero V5 via ort being
blocked (Phase 8.1.3). Production voice loops should default to
--vad-gate.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 08:59:32 -07:00
osobhandClaude Opus 4.7 cdcfbcece1 rtx-csm: Phase 8.1.2 — ort runtime-conflict gate (PASS)
Adds optional `vad` feature pulling voice_activity_detector v0.2 (Silero
V5 via the `ort` ONNX runtime). Gates wiring VAD into converse_server
on a regression check: does linking ort into the same binary as candle
slow CSM Metal inference the way whisper-rs (ggml) did?

examples/ort_conflict_probe.rs times 10 CSM Q8 forwards, loads ort +
runs Silero V5 a few times, then times 10 more forwards. Compares.

Result on M-series Metal:
  before ort load: 645.8 ms mean
  after  ort load: 633.2 ms mean
  ratio: 0.981 (-1.9%, within noise threshold ±5%)

PASS — ort coexists cleanly with candle/Metal. The whisper-rs/ggml
regression doesn't generalize to all C++ ML runtimes; ort's Metal
backend (via WebGPU EP) doesn't appear to fight with candle's. Safe
to ship Silero-VAD gating in Phase 8.1.3.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 08:33:47 -07:00
osobhandClaude Opus 4.7 be92c05d05 rtx-csm: Phase 7.6 — Whisper STT path + asr-feature regression discovery
Wires --whisper flag in converse_server using the existing whisper-rs
asr feature. AsrEngine enum (Kyutai default + Whisper variant gated on
"asr" feature) lets the receive loop branch on backend. Whisper path:
buffer audio during receive, transcribe full buffer at EOT — batch-only,
no VAD, no incremental words.

Adds examples/whisper_profile binary measuring Whisper-tiny in
isolation against the same audio used for stt_profile.

Standalone profile findings (M-series):
  Kyutai STT 1B   : 80.8 ms / 80 ms audio    1.01x realtime
  Whisper-tiny    : 209 ms / 10.43 s audio   0.020x (~50x faster)

But the full-stack bench reveals a critical regression: linking
whisper-rs's C++ runtime into the same binary as candle/CSM costs
2-3x across ALL CSM inference (recv_phase, tts_per_utterance,
total_turn) even when --whisper is NOT used. Build flag matters.

  Build                          recv    tts/u   total
  --features metal               4196    3113    18707
  --features metal,asr (Kyutai)  10019   7803    43366  <- linkage cost
  --features metal,asr +whisper  0       12921   54028  <- worse

Suspected cause: ggml/whisper.cpp's BLAS or Metal context init
conflicts with candle's. Production verdict: build WITHOUT asr
feature; accept Kyutai's 1x realtime STT cost. The standalone
whisper_profile binary still works for batch transcribe measurement.

Real Whisper integration would need a sidecar process pattern (whisper
running as a separate binary, IPC to converse_server). Documented in
the --whisper CLI help. Flag stays as opt-in with explicit warning.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 05:28:42 -07:00
osobhandClaude Opus 4.7 85ce697ffa rtx-csm: Phase 7.1 — profile Kyutai STT step_pcm
New examples/stt_profile binary. Loads kyutai/stt-1b-en_fr, feeds PCM
in fixed-size chunks, reports per-call latency p50/p95/min/max plus
realtime factor.

Findings (M-series Metal, 10.43s LibriSpeech in):
  frame_batch=1 (80ms): mean=80.8ms p50=81.7ms RT=1.01x
  frame_batch=3 (240ms): mean=308.6ms p50=298ms RT=1.29x

Headline: Kyutai STT 1B on Metal saturates at ~1.0x real-time. There
is no slack in the existing model on this hardware. Per-call overhead
amortizes poorly when batching frames (3 frames takes 3.8x single
frame, not 3x). To go faster requires a smaller model (Whisper-tiny
via the existing whisper-rs feature) or a Kyutai variant if available.

Note: converse_server's measured recv_phase (~4-5s for 10.4s audio)
is faster than this profile predicts (~10s). Discrepancy not yet
resolved but the optimization conclusion stands: STT model swap is
the only lever for the receive phase.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 04:57:56 -07:00
osobhandClaude Opus 4.7 c56cc68cdc rtx-csm: GenConfig.extra_body — provider-specific JSON merging
Adds `extra_body: serde_json::Map<String, Value>` to `GenConfig`. The
OpenAiCompatibleClient serializes the typed ChatRequest to a Value, then
merges extra_body's keys at the top level of the request body before
sending. extra_body is empty by default, so existing callers see no
behavioral change.

Use cases:
- **Z.AI thinking-disabled** for voice-AI: glm-4.5/4.6/4.7 default to
  reasoning_content traversal which burns tokens before content emits.
  Pass `{"thinking":{"type":"disabled"}}` and reasoning_tokens drops to
  0. Verified: curl direct = 2.1s vs 30s+ with thinking.
- **vLLM guided decoding**: `{"guided_json": {...}}`.
- **Anthropic-compat thinking budget** (when proxied through an
  OpenAI-compat shim).

Wires `--llm-extra-body '<JSON>'` into examples/converse_server: parsed
once at boot, stored in Shared.llm_extra_body, cloned per-turn into
gen_cfg. Boot rejects malformed JSON or non-object payloads.

Smoke test: examples/llm_extra_body_smoke.rs hits Z.AI directly with
and without extra_body, prints ttf_chunk and total stream time. Latest
run on glm-4.5: WITHOUT extra_body 1283ms, WITH thinking-disabled
1385ms — both fast on this prompt; the field is correctly forwarded
either way (other prompts that trigger reasoning_content show the
30s+ delta).

Note: the first end-to-end test through converse_server still showed
~46s wall (vs 1.4s for the LLM call alone), implying the latency
bottleneck is local STT (~12s on this hardware) + TTS gen, not the
LLM. extra_body code path is verified independently via the smoke
test.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-27 04:04:27 -07:00
osobhandClaude Opus 4.7 bd562288a9 rtx-csm: Phase 6e — converse_server bench harness
End-to-end conversation latency bench. Drives N sequential turns
through a single WebSocket and reports per-phase stats:

  audio_send_ms     (client streaming PCM in until EOT)
  transcript_ms     (server EOT → "transcript" event)
  first_audio_ms    (server "transcript" → first audio chunk)
  turn_total_ms     (full audio_send → "done" event)

Pulls /metrics at end for the server-side averages.

First numbers on Metal (M-series, 10.43s LibriSpeech FLAC, 3 turns,
mock LLM with 50ms/token sleep):
  audio_send       p50=1ms p95=1ms
  transcript       p50=5.2s p95=5.4s   (STT, 1.9x realtime)
  first_audio      p50=3.8s p95=4.4s   (LLM stream + first sentence TTS)
  turn_total       p50=17.7s p95=18.5s
  server stt avg   5.3s
  server tts avg   2.7s/utterance
  server e2e avg   9.3s

These are the empirical baselines for the Rust Unmute MVP. Optimization
opportunities: parallel STT during receive (already wired for VAD path),
smaller STT model, quantized CSM-1B (already shipped via Q8 GGUF), and
the obvious one — replace mock LLM with a real fast endpoint.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-26 15:42:32 -07:00
osobhandClaude Opus 4.7 7e88a35f81 rtx-csm: Phase 6c.2 — Rust Unmute MVP, voice conversation round-trip
Full-stack pure-Rust voice conversation server + CLI client:

  Client -> Server  binary frames: 16-bit LE PCM @ 24 kHz mono
  Client -> Server  text "EOT": signal end-of-turn
  Server: STT (Kyutai 1B en/fr) -> transcript
          LLM (OpenAI-compatible OR mock echo) -> token stream
          Converse: sentence buffer -> CSM TTS -> 16-bit LE PCM
  Server -> Client  text {"event":"transcript","text":"..."}
  Server -> Client  binary frames: assistant audio
  Server -> Client  text {"event":"done","assistant":"..."}

Per-connection chat history; multiple turns supported per socket.
--mock-llm mode for testing without API keys (echoes user transcript).

examples/converse_server.rs: axum WebSocket server.
examples/converse_client.rs: CLI; streams WAV in as user turn, saves
  response audio out.

Verified end-to-end on Metal:
  Input: 10.43s LibriSpeech FLAC ("He hoped there would be stew...")
  STT transcript: matched (full sentence captured by 23/25 words)
  Mock LLM: "I heard you say: <transcript>."
  CSM TTS: response audio streamed back via WebSocket
  Round-trip wall-clock: 6.86s (TTFA on first audio chunk: 6.86s; the
  pipeline is sequential per turn — Phase 6c.3 would pipeline LLM
  tokens with TTS to get TTFA much lower).

This is the Rust Unmute MVP: PCM in, voice out, no Python in the
runtime path. Strategic Phase 6 deliverable.

Phase 6 status:
  6a STT: working
  6b LLM client: working
  6c.1 text->LLM->TTS: working
  6c.2 WebSocket duplex MVP: working (this commit)
  6c.3 streaming pipeline + auto-EOT + barge-in: deferred
  6d productionization: deferred

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-26 09:30:00 -07:00
osobhandClaude Opus 4.7 1dd79d4d10 rtx-csm: Phase 6a COMPLETE — STT works on real speech with detok
Python-reference diff revealed the all-pad debugging session was on the
wrong audio source. /tmp/csm_24k.wav (CSM-generated speech) is not
intelligible enough for Kyutai STT — even the Python reference emits
nothing. On a real LibriSpeech-style speech sample the pipeline works
correctly.

Verified end-to-end on Metal (10s FLAC, "He hoped there would be stew
for dinner..."):
  Rust port:    23 words, transcript matches Python reference
  Python ref:   25 words (last 2 cut off in our run due to asr_delay
                off-by-one — cosmetic, fixable by setting delay=7)

Changes:
- Add sentencepiece = "0.13" dep for token detok
- Stt::decode_word_text(tokens) returns the detokenized word text
  (filters padding token id 3, calls SentencePieceProcessor::decode_piece_ids)
- examples/stt_demo: pair Word/EndWord events into timed segments,
  detokenize each, print transcript + concatenated text
- Update module docs to reflect WORKING status

Phase 6 progress:
  6a STT: WORKING (this commit)
  6b LLM client: shipped
  6c.1 text->LLM->TTS: shipped
  6c.2 full duplex: ready to build now that 6a works

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-26 09:18:05 -07:00
osobhandClaude Opus 4.7 ec053fcc18 rtx-csm: Phase 6c.1 — text -> LLM -> sentence buffer -> CSM TTS
Composable orchestrator stitching the LLM-output side of the
conversational stack:

  prompt + history -> LlmClient.generate_stream -> sentence buffer
    -> per-sentence Generator.generate -> post-process -> watermark
    -> Vec<Utterance> stream of (text, audio, latency)

CSM is sentence-level (best prosody on full sentences), so the buffer
flushes when terminal punctuation appears anywhere in the buffer
(Punctuation policy: . ! ? \n) or at any clause boundary
(Eager policy: + , ; :).

src/converse.rs:
- Converse<L: LlmClient> orchestrator
- Utterance { text, audio, tts_latency_ms } per sentence
- FlushPolicy { Punctuation, Eager }
- find_first_boundary scans the whole buffer (not just the last char)
  so "Sentence one. Word two" emits "Sentence one." immediately rather
  than waiting for the next terminal mark
- 3 unit tests for boundary detection + policy modes

examples/converse.rs:
- --mock mode: hardcoded 20-token "sleepy turtle" stream, no API key
- live mode: any OpenAI-compatible endpoint via OpenAiCompatibleClient
- writes the concatenated audio to a single WAV

Verified mock end-to-end on Metal: 2 utterances emitted as expected
(sentence 1 hits max_audio_ms cap at 6s; sentence 2 EOTs naturally at
4.88s), total 10.88s of audio in 31.5s wall-clock.

Phase 6c.1 ships the half-duplex (text-in -> voice-out) pipeline. Full
duplex (audio-in -> voice-out) is 6c.2, blocked on 6a's STT word
emission landing.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-26 09:07:45 -07:00
osobhandClaude Opus 4.7 af45e1b58e rtx-csm: Phase 6b — LlmClient trait + OpenAI-compatible streaming impl
Generic LLM client abstraction for the conversational stack:

- LlmClient trait with generate_stream(messages, config) -> TokenStream.
  Default generate() impl folds the stream for non-streaming callers.
- ChatMessage / Role / GenConfig types with sensible defaults.
- OpenAiCompatibleClient: HTTP impl with SSE streaming. Works against
  OpenAI, Z.AI, vLLM, llama.cpp's HTTP server, LiteLLM — any endpoint
  serving the Chat Completions schema.
- examples/llm_chat: demo CLI that prints token-by-token to stdout
  with TTFT + total-time + char-count metrics.

Promotes tokio + reqwest + futures-util to regular dependencies (no
longer dev-only) so the trait is part of the public library surface.
Adds async-trait + eventsource-stream for the SSE streaming.

3 unit tests (constructors, role serialization, default config); 82
lib tests total green.

Phase 6 progress:
- 6a Kyutai STT: integration scaffolded; output bridge needs Python
  reference diff (deferred)
- 6b LLM client: shipped (this commit)
- 6c session glue (axum WS + duplex audio loop): next
- 6d productionization: deferred

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-26 04:47:48 -07:00
osobhandClaude Opus 4.7 0f9cc122e9 rtx-csm: Phase 6a partial — Kyutai STT scaffold via moshi crate
Integrates the moshi crate (0.6.4, candle 0.9.1) for streaming STT.
Module + demo + custom config for kyutai/stt-1b-en_fr. Model loads
cleanly, LM forward pass advances (model_step_idx increments correctly),
but word events don't yet emit on a 10s CSM speech sample.

What works:
- moshi 0.6.4 added as dependency (candle 0.9.1, version-compatible)
- src/stt.rs wraps moshi::asr::State + moshi::lm + moshi::mimi
- Stt::load_default downloads kyutai/stt-1b-en_fr (~3 GB) from HF
- Custom config_stt_1b_en_fr() matching the released checkpoint:
  d_model=2048, num_layers=16, dim_feedforward=8192 (moshi's SwiGLU
  hidden = 11/4 * d_model = 5632 — verified vs safetensors), text vocab
  8001/8000, audio vocab 2049, 32 codebooks, no depformer
- AsrEvent enum + From<moshi::asr::AsrMsg> conversion
- examples/stt_demo.rs streams a WAV through the pipeline
- 2 unit tests for AsrEvent conversion

What needs more work:
- Word emission: 0 words detected on 10s of clean CSM speech, even
  though LM forward advances every frame. Likely culprits:
  a) asr_delay_in_tokens 6 vs HF stt_config.audio_delay_seconds=0.5
     (6.25 frames). Off-by-one possible.
  b) Sentencepiece detok not yet wired (tokens emitted but text=None).
  c) Subtle weight-key remap differences between moshi's expected
     layout and the released checkpoint that don't trip a shape check.
  d) renormalize/audio preprocessing mismatch.

Next step (Phase 6a polish): compare against the official
delayed-streams-modeling/scripts/stt_from_file_pytorch.py reference to
identify the missing piece. The integration framework is sound; only
the final LM-output-to-text-event step needs work.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-26 04:33:40 -07:00
osobhandClaude Opus 4.7 c3eddc8873 rtx-csm: tts_server /v1/tts_stream — chunked PCM streaming endpoint
Streams 16-bit little-endian PCM (24 kHz mono) as Mimi produces chunks.
Wraps Generator::generate_streaming via spawn_blocking + tokio::sync::mpsc
bridge into an axum Body::from_stream response.

Same JSON request format as /v1/tts; Content-Type is
audio/L16; rate=24000; channels=1 per RFC 2586.

First-byte (first audio chunk) latency on Metal: ~880 ms vs ~6 s wall
for the non-streaming /v1/tts path — 6.8x faster perceived UX, the
difference between "the app froze" and "the app started speaking."

Caveat: streaming endpoint does NOT apply post-processing or the inline
watermarker (those operate on the full utterance). For watermarked
output use /v1/tts. A chunked AudioSeal port is the natural follow-up
for streaming watermarking.

Adds futures-util as a dev-dependency for the Stream trait.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-26 03:42:50 -07:00
osobhandClaude Opus 4.7 5bdb1007af rtx-csm: tts_server_bench — end-to-end latency + concurrency stats
Sequential and concurrent benchmark client for the tts_server endpoints.
Reports per-endpoint p50/p95/min/max and a serialization-factor metric
for the concurrent /v1/tts case.

First numbers on Metal (M-series, max_audio_ms=2000):
  /health           1.9 ms
  /v1/tts           p50=5.6s p95=6.9s mean=6.0s  (~3x realtime, watermarked)
  /v1/detect        p50=43ms p95=48ms             (decoded=0xCAFE, 16/16 bits)
  /v1/speaker_embed p50=59ms p95=87ms             (100M-param WavLM-SV)
  /v1/speaker_compare p50=100ms p95=108ms         (self-compare cosine=1.0)
  concurrent /v1/tts (n=2): wall=10.2s, serial=14.4s
    serialization factor = 1.42x (Mutex-bound on inference, post-process
    runs in parallel)

Adds reqwest as a dev-dependency (rustls-tls, multipart, json features).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-26 03:33:17 -07:00
osobhandClaude Opus 4.7 3f61cee136 rtx-csm: tts_server full pipeline — TTS + watermark + speaker
Extends the HTTP service with three new endpoints exposing AudioSeal
detection and WavLM-SV speaker scoring alongside the existing TTS:

  GET  /health
  POST /v1/tts                    audio/wav (24 kHz mono)
  POST /v1/detect      [audio]    JSON { mean_presence, message_hex }
  POST /v1/speaker_embed [audio]  JSON { embedding: [512 floats] }
  POST /v1/speaker_compare [a+b]  JSON { cosine }

Wires the inline watermarker into /v1/tts when --audioseal-* flags are
set: every TTS response is auto-watermarked through the
ResampledWatermarker (24 kHz <-> 16 kHz) adapter.

Verified end-to-end on Metal:
  /health -> ok
  /v1/tts -> 200, 145964 bytes (3s @ 24kHz)
  /v1/detect -> mean_presence=0.998 on watermarked output
  /v1/speaker_embed -> 512-d float vector
  /v1/speaker_compare a==b -> cosine 1.0000001

axum gains the "multipart" feature for audio uploads.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-26 03:27:50 -07:00
osobhandClaude Opus 4.7 f4adbb3dfe rtx-csm: generate_long example exercises long-form watermark integration
Stand-alone CLI for Generator::generate_long_to_wav with optional inline
AudioSeal watermarker. Verified end-to-end: 3-chunk 19-second output
watermarks cleanly across chunk boundaries (mean_presence=0.9999,
16/16 message bits decoded).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-25 23:33:05 -07:00
osobhandClaude Opus 4.7 e61bd70e03 rtx-csm: WavLM-SV bug fixes — gelu_erf + gru_rel_pos_const loading
Two real bugs found via code inspection against HF source:

1. candle's .gelu() is the tanh approximation; PyTorch's default 'gelu'
   activation (used in WavLM via ACT2FN['gelu']) is the exact erf-based
   version. Switched all 3 sites (feature extractor convs, pos_conv,
   FFN) from .gelu() to .gelu_erf() to match the reference.

2. gru_rel_pos_const lookup used vb.pp("name").get(shape, "") which
   resolves to "<prefix>.name." (trailing dot) and fails to find the
   tensor. The .or_else(|_| zeros) silently swallowed the failure,
   leaving all 12 layers' gating constants at zero instead of the
   trained values. Fixed to attn.get(shape, "gru_rel_pos_const") which
   resolves correctly.

examples/wavlm_sv_inspect.rs: utility for sanity-checking specific
tensors inside converted safetensors (e.g. layer_weights).

Same-content same-speaker cosine: 0.9985 -> 0.9963 (≈unchanged).
Cross-content same-speaker cosine: 0.4882 -> 0.4118 (still drifting).
Phase 5d (Python reference comparison) remains the gate for
identifying the residual numerical drift.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-25 22:00:59 -07:00
osobhandClaude Opus 4.7 f4aaee2f1b rtx-csm: end-to-end pipeline showcase demo
Single CLI ties together every capability shipped this session:
text -> CSM-1B (with optional LoRA) -> post-process (HPF/declick/LUFS)
-> AudioSeal watermark embed -> AudioSeal detect verify -> WavLM-SV
speaker embedding + optional reference scoring.

Verified on Metal: 4s speech generated + watermarked + detected
(mean_presence=0.9999, 16/16 bits decoded) + 512-d speaker embedding
extracted in ~30s.

Cross-content same-speaker cosine sits around 0.49 vs 0.998 for
same-content same-speaker — suggests the WavLM-SV port may leak content
into the speaker embedding more than the HF reference. Phase 5d numerical
parity work (Python sidecar comparison) would tighten this.

This is the canonical usage example for downstream callers
(clawsample-csm etc.) — copy the structure.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-25 21:16:11 -07:00
osobhandClaude Opus 4.7 d1dba7a05c rtx-csm: WavLM-SV converter + end-to-end speaker similarity
Phase 5c — pure-Rust converter for microsoft/wavlm-base-plus-sv with
auto-detecting weight_norm merger; verified on real 100M-param weights:
load + embed + cosine-similarity round-trip works on Metal.

- wavlm_sv_convert.rs: candle_core::pickle reads pytorch_model.bin
  directly. merge_weight_norm_auto picks the kept dim from g's shape
  (dim=0 for AudioSeal SEANet, dim=2 for WavLM pos_conv_embed). Skips
  classifier.*/objective.* (train-only AMSoftmax head).
- examples/wavlm_sv_convert: HF download + convert CLI. Verified output:
  1 weight_norm pair merged + 261 passthrough + 3 skipped = 262 tensors.
- examples/wavlm_sv_demo: load + embed pair of WAVs + cosine similarity.
- examples/audioseal_inspect: gains --which wavlm-sv variant for key
  discovery.
- hub.rs: REPO_WAVLM_SV + resolve_wavlm_sv() helper.
- wavlm_sv::XVectorHead bug fix: layer_weights is top-level, not nested
  under prefix.

Verified end-to-end on Metal: cosine sim 0.9985 on same-speaker pair
(CSM vs CSM-watermarked, 10s @ 24 kHz resampled to 16 kHz). Numerical
parity vs HF reference is Phase 5d.

3 converter tests + 12 wavlm_sv tests; 78 lib tests total green.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-25 19:57:52 -07:00
osobhandClaude Opus 4.7 9d28597687 rtx-csm: AudioSeal apply CLI for arbitrary-rate WAVs
End-to-end watermarker driver that handles any source sample rate by
resampling to AudioSeal's 16 kHz native, embedding, then resampling back.
Tested on 10s of real CSM 24 kHz speech: mean_presence=0.9988 detection,
12/16 message bits round-trip (4-bit erosion from double resample).

- examples/audioseal_apply.rs: --in/--out/--source-rate/--message; loads
  source via audio_io::load_mono_at_rate, calls AudioSealWatermarker
  through the public Watermarker trait, verifies via in-process detect.
- Fix bit-match counter overflow in audioseal_demo.rs and audioseal_apply.rs.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-25 19:32:14 -07:00
osobhandClaude Opus 4.7 938b54a2b0 rtx-csm: AudioSeal watermark — Rust port end-to-end
SEANet generator + detector matching `facebook/audioseal` reference layout
(weight_norm-merged via pure-Rust pickle reader). Verified on real CSM
speech: mean_presence=0.9943, 16/16 message bits decoded.

- src/audioseal.rs: SeanetEncoder (4-stage strided downsample, 2-layer
  LSTM bottleneck at 512 channels, 128-dim projection), MsgProcessor
  (16-bit message via embedding sum + broadcast-add), SeanetDecoder,
  Generator (encoder+msg+decoder), Detector (encoder + single 320×
  reverse_convolution + 1×1 head). Padding mirrors audiocraft
  _get_extra_padding_for_conv1d exactly.
- src/audioseal_convert.rs: candle_core::pickle reads .pth directly;
  merge_weight_norm computes g*v/‖v‖ over all axes except 0; writes
  flat safetensors keyed identically to what Generator/Detector read.
- examples/audioseal_inspect.rs: dumps tensor keys + shapes.
- examples/audioseal_convert.rs: HF download + convert CLI.
- examples/audioseal_demo.rs: load + embed + detect on real WAV or
  synthetic burst, optionally writes watermarked WAV.
- audio_io.rs gains generic load_mono_at_rate, resample, write_wav_mono
  (16 kHz path needed for AudioSeal).

12 new unit tests + 2 converter tests; 63 lib tests total green.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-25 19:26:13 -07:00
osobhandClaude Opus 4.7 15dd3575d4 Add rtx-csm: Rust-native port of Sesame CSM-1B with LoRA voice cloning
A new model crate at crates/models/rtx-csm implementing end-to-end
inference, quantization, and fine-tuning for Sesame's Conversational
Speech Model (CSM-1B). Built on candle 0.9 + Kyutai Mimi codec.

Key capabilities:
- Inference (FP F16 on Metal, F32 on CPU, BF16 on CUDA)
- Quantized inference (Q8_0 / Q4_K_M GGUF, ~3x speedup, ~50% memory)
- Streaming Mimi decode with proper StreamTensor state machine
- In-context voice cloning via SpeakerProfile
- Classifier-Free Guidance (Koel-TTS recipe)
- Long-form chunked generation with rolling context
- Audio post-processing (HPF + declick + EBU R128 LUFS)
- Text input normalization (brackets, times, unicode, length caps)
- Frame-level repetition guard (loop-escape)
- Top-k + top-p sampling
- LoRA fine-tuning end-to-end (training + inference, on FP and Q8 bases)
- In-process Whisper ASR via whisper-rs (under --features asr)
- Standalone TTS HTTP server (Axum)
- Bench harness with manifest export + per-prompt WER

Phases delivered: quantization, ASR/WER eval, LoRA voice cloning, HTTP
service. AudioSeal/WavLM/Unmute remain as documented future work.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
2026-04-25 18:33:57 -07:00