Refactor TDT decoder: Extract reusable components#474
Conversation
Fixes actor isolation violations that appeared with stricter Swift 6 concurrency checking in newer Xcode versions. The issue was caused by extracting actor references from properties into local variables using if-let/guard-let, which changes isolation context and risks data races. Solution uses optional chaining with proper scoping: - Avoids force unwrapping (repository rule) - Prevents actor isolation violations (Swift 6 requirement) - Handles actor reentrancy safely (asrManager can become nil after await) - Uses if-let for conditional blocks to avoid skipping critical state updates Changes: - reset(): Optional chaining for resetDecoderState - finish(): Guard-let on processTranscriptionResult return value - processWindow(): Guard-let for required results, if-let for optional rescoring - All early-return guards use guard-let at function level - Conditional block uses if-let to avoid premature function exit Fixes prevent partial state mutations and ensure subscriber notifications always occur even if optional vocabulary rescoring fails.
Moves state mutations to occur AFTER all required async calls complete, preventing inconsistent state if asrManager becomes nil during suspension. Previously, if the second guard-let failed (line 408), the function would return after having already mutated: - accumulatedTokens - lastProcessedFrame - segmentIndex - processedChunks This created inconsistency where tokens were accumulated but transcript state and subscriber notifications were skipped. Solution: Delay all state mutations until after both required async calls (transcribeChunk and processTranscriptionResult) complete successfully.
## Bug Fix Fixed critical bug in decoder projection normalization that caused 82-113% WER (complete model failure). The issue was in TdtModelInference.swift where the destination stride was hardcoded to 1 instead of using the actual MLMultiArray stride, causing incorrect BLAS copy operations. **Impact**: All TDT models (v2, v3, tdt-ctc-110m) were producing garbage output **Root cause**: Hardcoded stride in normalizeDecoderProjection() **Fix**: Use actual destination array stride from MLMultiArray ## Refactoring Extracted reusable decoder components into separate files for better maintainability and code organization: - TdtModelInference.swift: Centralized model inference operations - runDecoder(): LSTM decoder execution - runJointPrepared(): Joint network execution with zero-copy optimization - normalizeDecoderProjection(): BLAS-based projection normalization (BUG FIX HERE) - TdtJointDecision.swift: Joint network decision data structure - TdtJointInputProvider.swift: Reusable feature provider for joint network - TdtDurationMapping.swift: Duration bin mapping utilities - TdtFrameNavigation.swift: Frame position calculation for streaming Simplified TdtDecoderV3.swift from 700+ lines to ~500 lines by extracting common operations. ## Validation Full test-clean benchmark (2,620 files): - Parakeet v3: WER 2.64% (baseline: 2.6%) ✓ - Parakeet v2: WER 3.79% (baseline: 3.8%) ✓ - TDT-CTC-110M: WER 3.56% (baseline: 3.6%) ✓ - All models: No regressions, performance matches baselines Perfect transcriptions: 74.3% (1,947/2,620 files) Processing speed: 45x real-time (5.4 hours audio in 7.2 minutes)
Sortformer High-Latency Benchmark ResultsES2004a Performance (30.4s latency config)
Sortformer High-Latency • ES2004a • Runtime: 2m 44s • 2026-04-02T04:55:37.794Z |
VAD Benchmark ResultsPerformance Comparison
Dataset Details
✅: Average F1-Score above 70% |
Parakeet EOU Benchmark Results ✅Status: Benchmark passed Performance Metrics
Streaming Metrics
Test runtime: 1m13s • 04/02/2026, 12:59 AM EST RTFx = Real-Time Factor (higher is better) • Processing includes: Model inference, audio preprocessing, state management, and file I/O |
PocketTTS Smoke Test ✅
Runtime: 0m36s Note: PocketTTS uses CoreML MLState (macOS 15) KV cache + Mimi streaming state. CI VM lacks physical GPU — audio quality may differ from Apple Silicon. |
Kokoro TTS Smoke Test ✅
Runtime: 0m39s Note: Kokoro TTS uses CoreML flow matching + Vocos vocoder. CI VM lacks physical ANE — performance may differ from Apple Silicon. |
Qwen3-ASR int8 Smoke Test ✅
Performance Metrics
Runtime: 3m29s Note: CI VM lacks physical GPU — CoreML MLState (macOS 15) KV cache produces degraded results on virtualized runners. On Apple Silicon: ~1.3% WER / 2.5x RTFx. |
Added documentation for the new refactored decoder components: - TdtModelInference.swift - TdtJointDecision.swift - TdtJointInputProvider.swift - TdtDurationMapping.swift - TdtFrameNavigation.swift These files were extracted from TdtDecoderV3.swift as part of the decoder refactoring to improve code organization and maintainability.
ASR Benchmark Results ✅Status: All benchmarks passed Parakeet v3 (multilingual)
Parakeet v2 (English-optimized)
Streaming (v3)
Streaming (v2)
Streaming tests use 5 files with 0.5s chunks to simulate real-time audio streaming 25 files per dataset • Test runtime: 6m52s • 04/02/2026, 12:57 AM EST RTFx = Real-Time Factor (higher is better) • Calculated as: Total audio duration ÷ Total processing time Expected RTFx Performance on Physical M1 Hardware:• M1 Mac: ~28x (clean), ~25x (other) Testing methodology follows HuggingFace Open ASR Leaderboard |
Speaker Diarization Benchmark ResultsSpeaker Diarization PerformanceEvaluating "who spoke when" detection accuracy
Diarization Pipeline Timing BreakdownTime spent in each stage of speaker diarization
Speaker Diarization Research ComparisonResearch baselines typically achieve 18-30% DER on standard datasets
Note: RTFx shown above is from GitHub Actions runner. On Apple Silicon with ANE:
🎯 Speaker Diarization Test • AMI Corpus ES2004a • 1049.0s meeting audio • 49.9s diarization time • Test runtime: 2m 49s • 04/02/2026, 12:54 AM EST |
Offline VBx Pipeline ResultsSpeaker Diarization Performance (VBx Batch Mode)Optimal clustering with Hungarian algorithm for maximum accuracy
Offline VBx Pipeline Timing BreakdownTime spent in each stage of batch diarization
Speaker Diarization Research ComparisonOffline VBx achieves competitive accuracy with batch processing
Pipeline Details:
🎯 Offline VBx Test • AMI Corpus ES2004a • 1049.0s meeting audio • 248.3s processing • Test runtime: 4m 10s • 04/02/2026, 12:55 AM EST |
- Remove private makeBlasIndex that shadowed global version - Flatten nested conditionals in TdtFrameNavigation and TdtDecoderV3 - Add comprehensive unit tests for refactored TDT components (30 tests) All tests pass (30/30). Global makeBlasIndex supports negative strides for reverse traversal, which the private version blocked. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
## Summary This PR adds **experimental** Mandarin Chinese ASR support via the CTC zh-CN model and includes critical Swift 6 concurrency fixes for `SlidingWindowAsrManager`. > **⚠️ Experimental Feature**: CTC zh-CN Mandarin ASR is an early preview. The API and performance characteristics may change in future releases. ## Swift 6 Concurrency Fixes ### Fixed Issues - **Removed premature state mutations** in `processWindow()` that violated Swift 6 actor isolation - State updates (`accumulatedTokens`, `lastProcessedFrame`, `segmentIndex`, `processedChunks`) now occur **after** all async calls complete successfully - Prevents data races when async calls fail mid-execution ### Changes - `SlidingWindowAsrManager.processWindow()`: Moved state mutation to after async guard statements - Ensures atomic state updates only when processing succeeds ## CTC zh-CN Mandarin ASR Integration (Experimental) ### New Features #### Models - **CtcZhCnManager**: High-level API for Mandarin Chinese ASR using CTC decoder - **CtcZhCnModels**: Model management with int8/fp32 encoder variants - Int8: 571 MB (default) - FP32: 1.1 GB - Auto-downloads from HuggingFace: `FluidInference/parakeet-ctc-0.6b-zh-cn-coreml` #### CLI Commands ```bash # Transcribe Mandarin audio swift run fluidaudiocli ctc-zh-cn-transcribe audio.wav # Benchmark on THCHS-30 dataset (full 2,495 samples) swift run fluidaudiocli ctc-zh-cn-benchmark --auto-download # Benchmark subset (100 samples for faster testing) swift run fluidaudiocli ctc-zh-cn-benchmark --auto-download --samples 100 ``` #### Benchmark Results (THCHS-30 Full Test Set) **Full dataset** (2,495 samples): - **Mean CER**: 8.23% - **Median CER**: 6.45% - **CER = 0% (perfect)**: 435 samples (17.4%) - **Distribution**: 67.1% of samples <10% CER, 93.2% <20% CER - **Mean Latency**: 614 ms - **Mean RTFx**: 14.83x ### Dataset **THCHS-30** - Mandarin Chinese speech corpus from Tsinghua University - 30 hours of clean speech - 50 speakers - 2,495 test utterances (10 speakers, 250 unique sentences) - Content domain: News (not classical literature) - Source: http://www.openslr.org/18/ - HuggingFace: `FluidInference/THCHS-30-tests` ### Text Normalization CER calculation includes: - Chinese punctuation removal (,。!?、;:\u{201C}\u{201D}\u{2018}\u{2019}) - English punctuation removal (,.!?;:()[]{}\\<>"'-) - Arabic digit → Chinese character conversion (0→零, 1→一, etc.) - Whitespace normalization - Levenshtein distance calculation ## Devin Review Fixes ✅ Addressed all issues from [Devin code review](https://app.devin.ai/review/fluidinference/fluidaudio/pull/476): ### Review #1 (4 issues) 1. **✅ Fixed digit-to-Chinese conversion** - Added missing normalization (0→零, 1→一, etc.) that was inflating CER by ~1.66% 2. **✅ Added unit tests** - Created 13 comprehensive test cases for text normalization, CER calculation, and Levenshtein distance 3. **✅ Fixed CI dataset cache path** - Not applicable after CI workflow removal 4. **✅ Fixed CI model cache path** - Not applicable after CI workflow removal ### Review #2 (2 issues) 5. **✅ Fixed CER threshold mismatch** - Not applicable after CI workflow removal 6. **✅ Fixed saveResults NaN crash** - Added guard for empty results array to prevent division by zero ### Review #3 (2 issues) 7. **✅ Fixed FP32 encoder download** - Include both int8 and fp32 encoders in `requiredModels` set 8. **✅ Fixed AsrManager CTC-only handling** - Throw explicit error instead of routing to incompatible TDT decoder ### Additional Fixes - **✅ Fixed Unicode curly quotes** - Used escape sequences (`\u{201C}` etc.) in both source and tests - Added missing English punctuation removal - Added missing Chinese quotation mark handling ## Files Changed ### Swift 6 Concurrency - `Sources/FluidAudio/ASR/Parakeet/SlidingWindow/SlidingWindowAsrManager.swift` - `Sources/FluidAudio/ASR/Parakeet/AsrManager.swift` (added .ctcZhCn case + error handling) ### CTC zh-CN Integration - `Sources/FluidAudio/ASR/Parakeet/CtcZhCnManager.swift` (new) - `Sources/FluidAudio/ASR/Parakeet/CtcZhCnModels.swift` (new) - `Sources/FluidAudioCLI/Commands/ASR/CtcZhCnTranscribeCommand.swift` (new) - `Sources/FluidAudioCLI/Commands/ASR/CtcZhCnBenchmark.swift` (new) - `Sources/FluidAudio/ModelNames.swift` (updated - both encoder variants) - `Documentation/Benchmarks.md` (updated - marked experimental) ### Tests - `Tests/FluidAudioTests/ASR/Parakeet/CtcZhCnTests.swift` (new - 13 test cases) ## Testing - [x] Swift 6 concurrency fixes pass existing tests - [x] CTC zh-CN transcription tested manually - [x] THCHS-30 full benchmark: 8.23% mean CER (2,495 samples) - [x] Unit tests: 13 test cases for normalization and CER (100% passing) - [x] Text normalization matches baseline exactly - [x] FP32 encoder download verified ## Notes - This PR is a clean rebase of #475 off main - Skipped conflicting decoder refactoring commit (superseded by #474) - **Experimental feature**: CTC zh-CN API may change in future releases - **No CI workflow**: Benchmarks are run manually for experimental features
Summary
This PR refactors the TDT decoder code by extracting reusable components into separate files for better maintainability.
Code Refactoring 🔨
Extracted reusable decoder components into separate files:
New Files
TdtModelInference.swift - Centralized model inference operations
runDecoder()- LSTM decoder executionrunJointPrepared()- Joint network with zero-copy optimizationnormalizeDecoderProjection()- BLAS-based projection normalization with correct stride handlingTdtJointDecision.swift - Joint network decision structure
TdtJointInputProvider.swift - Reusable feature provider
TdtDurationMapping.swift - Duration bin mapping utilities
TdtFrameNavigation.swift - Frame position calculations for streaming
Modified Files
standardOverlapFramesconstantKey Implementation Detail
The
normalizeDecoderProjection()function correctly uses the actual MLMultiArray stride from the destination buffer rather than assuming a contiguous layout:This ensures correct BLAS copy operations regardless of the MLMultiArray memory layout.
Validation ✅
Full Test-Clean Benchmark (2,620 files)
Results:
Subset Benchmarks (100 files each)
All 6 model variants tested and validated:
Changes
Testing
Benefits
Code Quality:
Maintainability:
Performance: