whisper-apr 0.3.3

WASM-first automatic speech recognition engine implementing OpenAI Whisper
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Tests for WASM capability detection (native Rust only - WASM tests would need wasm-bindgen-test)

use super::*;

// =========================================================================
// Capabilities Tests
// =========================================================================

#[test]
fn test_capabilities_default() {
    let caps = Capabilities::default();
    assert!(!caps.simd);
    assert!(!caps.threads);
    assert!(!caps.cross_origin_isolated);
    assert!(!caps.webgpu);
}

#[test]
fn test_capabilities_with_values() {
    let caps = Capabilities::with_values(true, true, true, 8);
    assert!(caps.simd);
    assert!(caps.threads);
    assert!(caps.cross_origin_isolated);
    assert_eq!(caps.hardware_concurrency, 8);
}

#[test]
fn test_capabilities_setters() {
    let mut caps = Capabilities::new();
    caps.set_simd(true);
    caps.set_threads(true);
    caps.set_cross_origin_isolated(true);
    caps.set_memory_mb(1024);
    caps.set_hardware_concurrency(4);

    assert!(caps.simd());
    assert!(caps.threads());
    assert!(caps.cross_origin_isolated());
    assert_eq!(caps.memory_mb(), 1024);
    assert_eq!(caps.hardware_concurrency(), 4);
}

// =========================================================================
// Binary Name Tests
// =========================================================================

#[test]
fn test_binary_name_simd_threaded() {
    let caps = Capabilities::with_values(true, true, true, 8);
    assert_eq!(caps.get_binary_name(), "whisper-apr-simd-threaded.wasm");
}

#[test]
fn test_binary_name_simd_sequential() {
    let caps = Capabilities::with_values(true, false, false, 4);
    assert_eq!(caps.get_binary_name(), "whisper-apr-simd-sequential.wasm");
}

#[test]
fn test_binary_name_scalar() {
    let caps = Capabilities::with_values(false, false, false, 2);
    assert_eq!(caps.get_binary_name(), "whisper-apr-scalar.wasm");
}

#[test]
fn test_binary_name_threads_without_isolation() {
    // Has threads but not cross-origin isolated - falls back to SIMD sequential
    let mut caps = Capabilities::with_values(true, true, false, 4);
    assert_eq!(caps.get_binary_name(), "whisper-apr-simd-sequential.wasm");

    // Without SIMD, just scalar
    caps.set_simd(false);
    assert_eq!(caps.get_binary_name(), "whisper-apr-scalar.wasm");
}

// =========================================================================
// Thread Count Tests
// =========================================================================

#[test]
fn test_optimal_thread_count_no_threads() {
    let caps = Capabilities::with_values(true, false, false, 8);
    assert_eq!(caps.optimal_thread_count(), 1);
}

#[test]
fn test_optimal_thread_count_single_core() {
    let caps = Capabilities::with_values(true, true, true, 1);
    assert_eq!(caps.optimal_thread_count(), 1);
}

#[test]
fn test_optimal_thread_count_dual_core() {
    let caps = Capabilities::with_values(true, true, true, 2);
    assert_eq!(caps.optimal_thread_count(), 1);
}

#[test]
fn test_optimal_thread_count_quad_core() {
    let caps = Capabilities::with_values(true, true, true, 4);
    assert_eq!(caps.optimal_thread_count(), 3);
}

#[test]
fn test_optimal_thread_count_many_cores() {
    let caps = Capabilities::with_values(true, true, true, 16);
    assert_eq!(caps.optimal_thread_count(), 8); // Capped at 8
}

// =========================================================================
// Model Compatibility Tests
// =========================================================================

#[test]
fn test_can_run_model_unknown_memory() {
    let caps = Capabilities::default();
    assert!(caps.can_run_model("tiny"));
    assert!(caps.can_run_model("large"));
}

#[test]
fn test_can_run_model_limited_memory() {
    let mut caps = Capabilities::default();
    caps.set_memory_mb(500);

    assert!(caps.can_run_model("tiny"));
    assert!(caps.can_run_model("base"));
    assert!(!caps.can_run_model("small"));
    assert!(!caps.can_run_model("large"));
}

#[test]
fn test_can_run_model_ample_memory() {
    let mut caps = Capabilities::default();
    caps.set_memory_mb(5000);

    assert!(caps.can_run_model("tiny"));
    assert!(caps.can_run_model("base"));
    assert!(caps.can_run_model("small"));
    assert!(caps.can_run_model("large"));
}

// =========================================================================
// Performance Tier Tests
// =========================================================================

#[test]
fn test_performance_tier() {
    // Scalar
    let caps = Capabilities::with_values(false, false, false, 1);
    assert_eq!(caps.performance_tier(), 0);

    // Threads only
    let caps = Capabilities::with_values(false, true, true, 4);
    assert_eq!(caps.performance_tier(), 1);

    // SIMD only
    let caps = Capabilities::with_values(true, false, false, 4);
    assert_eq!(caps.performance_tier(), 2);

    // SIMD + Threads
    let caps = Capabilities::with_values(true, true, true, 8);
    assert_eq!(caps.performance_tier(), 3);
}

// =========================================================================
// Description Tests
// =========================================================================

#[test]
fn test_description_scalar() {
    let caps = Capabilities::default();
    assert_eq!(caps.description(), "Scalar (no acceleration)");
}

#[test]
fn test_description_simd_only() {
    let caps = Capabilities::with_values(true, false, false, 4);
    assert_eq!(caps.description(), "SIMD");
}

#[test]
fn test_description_full() {
    let mut caps = Capabilities::with_values(true, true, true, 8);
    caps.set_webgpu(true);
    assert!(caps.description().contains("SIMD"));
    assert!(caps.description().contains("Threads"));
    assert!(caps.description().contains("CrossOriginIsolated"));
    assert!(caps.description().contains("WebGPU"));
}

// =========================================================================
// Execution Mode Tests
// =========================================================================

#[test]
fn test_execution_mode_from_capabilities() {
    let caps = Capabilities::with_values(true, true, true, 8);
    assert_eq!(ExecutionMode::from(&caps), ExecutionMode::SimdThreaded);

    let caps = Capabilities::with_values(true, false, false, 4);
    assert_eq!(ExecutionMode::from(&caps), ExecutionMode::SimdSequential);

    let caps = Capabilities::with_values(false, false, false, 2);
    assert_eq!(ExecutionMode::from(&caps), ExecutionMode::Scalar);
}

#[test]
fn test_execution_mode_rtf_multiplier() {
    assert!((ExecutionMode::SimdThreaded.rtf_multiplier() - 1.0).abs() < f32::EPSILON);
    assert!((ExecutionMode::SimdSequential.rtf_multiplier() - 1.5).abs() < f32::EPSILON);
    assert!((ExecutionMode::Scalar.rtf_multiplier() - 4.0).abs() < f32::EPSILON);
}

#[test]
fn test_execution_mode_name() {
    assert!(ExecutionMode::SimdThreaded
        .name()
        .contains("High Performance"));
    assert!(ExecutionMode::SimdSequential
        .name()
        .contains("Compatibility"));
    assert!(ExecutionMode::Scalar.name().contains("Fallback"));
}

// =========================================================================
// Cross-Browser Profile Tests (Spec WAPR-QA-002)
// =========================================================================

/// Chrome Desktop (2024+): Full support
#[test]
fn test_browser_profile_chrome_desktop() {
    let mut caps = Capabilities::new();
    caps.set_simd(true);
    caps.set_threads(true);
    caps.set_cross_origin_isolated(true);
    caps.set_hardware_concurrency(8);
    caps.set_memory_mb(4096);

    assert_eq!(caps.performance_tier(), 3);
    assert_eq!(caps.get_binary_name(), "whisper-apr-simd-threaded.wasm");
    assert!(caps.can_run_model("large"));
    assert_eq!(caps.optimal_thread_count(), 7);
}

/// Firefox Desktop (2024+): Full support with COOP/COEP headers
#[test]
fn test_browser_profile_firefox_desktop() {
    let mut caps = Capabilities::new();
    caps.set_simd(true);
    caps.set_threads(true);
    caps.set_cross_origin_isolated(true);
    caps.set_hardware_concurrency(8);
    caps.set_memory_mb(4096);

    assert_eq!(caps.performance_tier(), 3);
    assert_eq!(caps.get_binary_name(), "whisper-apr-simd-threaded.wasm");
    assert_eq!(ExecutionMode::from(&caps), ExecutionMode::SimdThreaded);
}

/// Safari Desktop (macOS 14+): SIMD support, limited threading
#[test]
fn test_browser_profile_safari_desktop() {
    let mut caps = Capabilities::new();
    caps.set_simd(true);
    caps.set_threads(false); // Safari historically more restrictive
    caps.set_cross_origin_isolated(false);
    caps.set_hardware_concurrency(8);
    caps.set_memory_mb(1024); // Safari stricter memory (spec 3.1)

    assert_eq!(caps.performance_tier(), 2);
    assert_eq!(caps.get_binary_name(), "whisper-apr-simd-sequential.wasm");
    assert!(caps.can_run_model("small"));
    assert!(!caps.can_run_model("large")); // Safari memory limit
    assert_eq!(ExecutionMode::from(&caps), ExecutionMode::SimdSequential);
}

/// Chrome Mobile (Android): SIMD but limited threads/memory
#[test]
fn test_browser_profile_chrome_mobile() {
    let mut caps = Capabilities::new();
    caps.set_simd(true);
    caps.set_threads(true);
    caps.set_cross_origin_isolated(true);
    caps.set_hardware_concurrency(4); // Mobile typically 4-8 cores
    caps.set_memory_mb(512); // Limited mobile memory

    assert!(caps.performance_tier() >= 2);
    assert!(caps.can_run_model("tiny"));
    assert!(caps.can_run_model("base"));
    assert!(!caps.can_run_model("small"));
    assert_eq!(caps.optimal_thread_count(), 3);
}

/// Safari Mobile (iOS): More restrictive environment
#[test]
fn test_browser_profile_safari_mobile() {
    let mut caps = Capabilities::new();
    caps.set_simd(true);
    caps.set_threads(false);
    caps.set_cross_origin_isolated(false);
    caps.set_hardware_concurrency(6);
    caps.set_memory_mb(256); // iOS very restricted

    assert_eq!(caps.performance_tier(), 2);
    assert_eq!(caps.get_binary_name(), "whisper-apr-simd-sequential.wasm");
    assert!(caps.can_run_model("tiny"));
    assert!(!caps.can_run_model("base")); // Too little memory
    assert_eq!(caps.optimal_thread_count(), 1);
}

/// Legacy browser (pre-SIMD): Scalar fallback
#[test]
fn test_browser_profile_legacy() {
    let caps = Capabilities::with_values(false, false, false, 2);

    assert_eq!(caps.performance_tier(), 0);
    assert_eq!(caps.get_binary_name(), "whisper-apr-scalar.wasm");
    assert_eq!(ExecutionMode::from(&caps), ExecutionMode::Scalar);
    assert!((caps.rtf_multiplier() - 4.0).abs() < f32::EPSILON);
}

/// Embedded WebView (restrictive): Often no SIMD
#[test]
fn test_browser_profile_webview() {
    let mut caps = Capabilities::new();
    caps.set_simd(false);
    caps.set_threads(false);
    caps.set_hardware_concurrency(4);
    caps.set_memory_mb(128);

    assert_eq!(caps.performance_tier(), 0);
    assert_eq!(caps.get_binary_name(), "whisper-apr-scalar.wasm");
    assert!(!caps.can_run_model("tiny")); // Insufficient memory
}

// =========================================================================
// Edge Case Tests
// =========================================================================

/// Threading without COOP/COEP should not enable threaded mode
#[test]
fn test_threads_without_isolation() {
    let mut caps = Capabilities::new();
    caps.set_simd(true);
    caps.set_threads(true);
    caps.set_cross_origin_isolated(false);

    // Should fall back to SIMD sequential
    assert_eq!(caps.get_binary_name(), "whisper-apr-simd-sequential.wasm");
    assert_eq!(ExecutionMode::from(&caps), ExecutionMode::SimdSequential);
}

/// Memory boundary tests
#[test]
fn test_memory_boundary_tiny() {
    let mut caps = Capabilities::new();
    caps.set_memory_mb(199); // Just below tiny requirement
    assert!(!caps.can_run_model("tiny"));

    caps.set_memory_mb(200); // Exactly at tiny requirement
    assert!(caps.can_run_model("tiny"));
}

/// Memory boundary tests for larger models
#[test]
fn test_memory_boundary_large() {
    let mut caps = Capabilities::new();
    caps.set_memory_mb(3999); // Just below large requirement
    assert!(!caps.can_run_model("large"));

    caps.set_memory_mb(4000); // Exactly at large requirement
    assert!(caps.can_run_model("large"));
}

/// Hardware concurrency edge cases
#[test]
fn test_hardware_concurrency_zero() {
    let caps = Capabilities::with_values(true, true, true, 0);
    assert_eq!(caps.optimal_thread_count(), 1);
}

/// Model variants should have same memory requirements
#[test]
fn test_model_variant_equivalence() {
    let mut caps = Capabilities::new();
    caps.set_memory_mb(500);

    // Standard and English-only variants should have same requirements
    assert_eq!(caps.can_run_model("tiny"), caps.can_run_model("tiny.en"));
    assert_eq!(caps.can_run_model("base"), caps.can_run_model("base.en"));
    assert_eq!(caps.can_run_model("small"), caps.can_run_model("small.en"));
}

/// RTF scaling across execution modes
#[test]
fn test_rtf_scaling() {
    let simd_threaded = Capabilities::with_values(true, true, true, 8);
    let simd_seq = Capabilities::with_values(true, false, false, 4);
    let scalar = Capabilities::with_values(false, false, false, 2);

    // SIMD threaded should be fastest
    assert!(simd_threaded.rtf_multiplier() < simd_seq.rtf_multiplier());
    assert!(simd_seq.rtf_multiplier() < scalar.rtf_multiplier());

    // Scalar should be 4x slower than SIMD threaded
    assert!((scalar.rtf_multiplier() / simd_threaded.rtf_multiplier() - 4.0).abs() < f32::EPSILON);
}

// =========================================================================
// WebGPU Future Capability Tests
// =========================================================================

#[test]
fn test_webgpu_capability() {
    let mut caps = Capabilities::new();
    caps.set_webgpu(true);

    assert!(caps.webgpu());
    assert!(caps.description().contains("WebGPU"));
}

#[test]
fn test_full_capability_description() {
    let mut caps = Capabilities::with_values(true, true, true, 8);
    caps.set_webgpu(true);

    let desc = caps.description();
    assert!(desc.contains("SIMD"));
    assert!(desc.contains("Threads"));
    assert!(desc.contains("CrossOriginIsolated"));
    assert!(desc.contains("WebGPU"));
}