whisper-apr 0.3.0

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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Backend trait abstractions (WAPR-140)
//!
//! Provides traits for backend-agnostic compute operations,
//! allowing seamless switching between SIMD and GPU implementations.

#[cfg(test)]
mod tests;

use crate::error::WhisperResult;

/// Compute backend type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum BackendType {
    /// CPU with SIMD acceleration
    Simd,
    /// GPU via WebGPU
    Gpu,
    /// CPU fallback (no SIMD)
    Cpu,
    /// Automatic selection based on workload
    #[default]
    Auto,
}

impl BackendType {
    /// Check if this is a GPU backend
    #[must_use]
    pub fn is_gpu(&self) -> bool {
        matches!(self, Self::Gpu)
    }

    /// Check if this is a CPU backend
    #[must_use]
    pub fn is_cpu(&self) -> bool {
        matches!(self, Self::Simd | Self::Cpu)
    }

    /// Check if this is auto-selection
    #[must_use]
    pub fn is_auto(&self) -> bool {
        matches!(self, Self::Auto)
    }

    /// Get human-readable name
    #[must_use]
    pub fn name(&self) -> &str {
        match self {
            Self::Simd => "SIMD",
            Self::Gpu => "GPU",
            Self::Cpu => "CPU",
            Self::Auto => "Auto",
        }
    }
}

impl std::fmt::Display for BackendType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
}

/// Backend capabilities
#[derive(Debug, Clone)]
pub struct BackendCapabilities {
    /// Backend type
    pub backend_type: BackendType,
    /// Whether this backend is available
    pub available: bool,
    /// Maximum parallel elements (threads for CPU, SMs for GPU)
    pub max_parallelism: u32,
    /// Maximum buffer size in bytes
    pub max_buffer_size: u64,
    /// Whether f16 is supported
    pub supports_f16: bool,
    /// Relative performance score (higher is better)
    pub performance_score: f32,
}

impl Default for BackendCapabilities {
    fn default() -> Self {
        Self {
            backend_type: BackendType::Cpu,
            available: true,
            max_parallelism: 1,
            max_buffer_size: usize::MAX as u64,
            supports_f16: false,
            performance_score: 1.0,
        }
    }
}

impl BackendCapabilities {
    /// Create SIMD backend capabilities
    #[must_use]
    pub fn simd() -> Self {
        // Detect number of cores and SIMD width
        let num_cpus = std::thread::available_parallelism()
            .map(|n| n.get() as u32)
            .unwrap_or(1);

        Self {
            backend_type: BackendType::Simd,
            available: true,
            max_parallelism: num_cpus * 4, // 4 SIMD lanes per core
            max_buffer_size: usize::MAX as u64,
            supports_f16: false, // Most CPUs don't have native f16
            performance_score: 10.0 * num_cpus as f32,
        }
    }

    /// Create GPU backend capabilities
    #[must_use]
    pub fn gpu(available: bool, max_buffer: u64, parallelism: u32, supports_f16: bool) -> Self {
        Self {
            backend_type: BackendType::Gpu,
            available,
            max_parallelism: parallelism,
            max_buffer_size: max_buffer,
            supports_f16,
            performance_score: if available {
                100.0 * parallelism as f32 / 1024.0
            } else {
                0.0
            },
        }
    }

    /// Create CPU fallback capabilities
    #[must_use]
    pub fn cpu_fallback() -> Self {
        let num_cpus = std::thread::available_parallelism()
            .map(|n| n.get() as u32)
            .unwrap_or(1);

        Self {
            backend_type: BackendType::Cpu,
            available: true,
            max_parallelism: num_cpus,
            max_buffer_size: usize::MAX as u64,
            supports_f16: false,
            performance_score: 1.0 * num_cpus as f32,
        }
    }

    /// Check if this backend can handle given workload size
    #[must_use]
    pub fn can_handle(&self, size_bytes: u64) -> bool {
        self.available && size_bytes <= self.max_buffer_size
    }

    /// Estimate throughput for given operation size
    #[must_use]
    pub fn estimated_throughput(&self, elements: usize) -> f32 {
        if !self.available {
            return 0.0;
        }
        self.performance_score * (elements as f32).sqrt()
    }
}

/// Trait for compute operations that can run on different backends
pub trait ComputeOp {
    /// Output type
    type Output;

    /// Execute on SIMD backend
    fn execute_simd(&self) -> WhisperResult<Self::Output>;

    /// Execute on GPU backend (if available)
    fn execute_gpu(&self) -> WhisperResult<Self::Output>;

    /// Execute on the best available backend
    fn execute(&self, backend: BackendType) -> WhisperResult<Self::Output> {
        match backend {
            BackendType::Gpu => self.execute_gpu(),
            BackendType::Simd | BackendType::Cpu => self.execute_simd(),
            BackendType::Auto => {
                // Try GPU first, fall back to SIMD
                self.execute_gpu().or_else(|_| self.execute_simd())
            }
        }
    }

    /// Get estimated FLOPs for this operation
    fn estimated_flops(&self) -> u64;

    /// Get estimated memory requirement in bytes
    fn memory_requirement(&self) -> usize;
}

/// Matrix multiplication operation
#[derive(Debug, Clone)]
pub struct MatMulOp {
    /// M dimension (rows of A)
    pub m: usize,
    /// K dimension (cols of A / rows of B)
    pub k: usize,
    /// N dimension (cols of B)
    pub n: usize,
    /// Transpose A
    pub trans_a: bool,
    /// Transpose B
    pub trans_b: bool,
    /// Input matrix A data (optional, needed for actual GPU execution)
    a_data: Option<Vec<f32>>,
    /// Input matrix B data (optional, needed for actual GPU execution)
    b_data: Option<Vec<f32>>,
}

impl MatMulOp {
    /// Create new matmul operation
    #[must_use]
    #[provable_contracts_macros::contract("whisper-matmul-v1", equation = "new")]
    pub fn new(m: usize, k: usize, n: usize) -> Self {
        Self {
            m,
            k,
            n,
            trans_a: false,
            trans_b: false,
            a_data: None,
            b_data: None,
        }
    }

    /// Attach input data for GPU execution
    #[must_use]
    pub fn with_data(mut self, a: Vec<f32>, b: Vec<f32>) -> Self {
        self.a_data = Some(a);
        self.b_data = Some(b);
        self
    }

    /// Transpose A
    #[must_use]
    pub fn transpose_a(mut self) -> Self {
        self.trans_a = true;
        self
    }

    /// Transpose B
    #[must_use]
    pub fn transpose_b(mut self) -> Self {
        self.trans_b = true;
        self
    }

    /// Get output dimensions
    #[must_use]
    pub fn output_shape(&self) -> (usize, usize) {
        (self.m, self.n)
    }
}

impl ComputeOp for MatMulOp {
    type Output = Vec<f32>;

    fn execute_simd(&self) -> WhisperResult<Self::Output> {
        // Placeholder - would use trueno SIMD matmul
        Ok(vec![0.0; self.m * self.n])
    }

    fn execute_gpu(&self) -> WhisperResult<Self::Output> {
        #[cfg(feature = "webgpu")]
        {
            // Only dispatch to GPU if input data is attached
            if let (Some(a), Some(b)) = (&self.a_data, &self.b_data) {
                use crate::gpu::ops::matmul::GpuMatMul;
                use crate::gpu::{ExecutorConfig, GpuExecutorSync};
                use std::sync::OnceLock;

                let gpu_op = GpuMatMul::simple(self.m as u32, self.k as u32, self.n as u32)
                    .map_err(|e| {
                        crate::error::WhisperError::Inference(format!(
                            "GPU matmul setup failed: {e}"
                        ))
                    })?;

                static EXECUTOR: OnceLock<Option<GpuExecutorSync>> = OnceLock::new();
                let executor_opt = EXECUTOR.get_or_init(|| {
                    GpuExecutorSync::new(&ExecutorConfig::for_inference()).ok()
                });

                if let Some(executor) = executor_opt {
                    let result = executor.execute_matmul(&gpu_op, a, b).map_err(|e| {
                        crate::error::WhisperError::Inference(format!("GPU matmul failed: {e}"))
                    })?;
                    return Ok(result);
                }
            }
        }
        // Fall back to SIMD when webgpu feature is disabled or no data attached
        self.execute_simd()
    }

    fn estimated_flops(&self) -> u64 {
        2 * (self.m as u64) * (self.k as u64) * (self.n as u64)
    }

    fn memory_requirement(&self) -> usize {
        (self.m * self.k + self.k * self.n + self.m * self.n) * 4
    }
}

/// Softmax operation
#[derive(Debug, Clone)]
pub struct SoftmaxOp {
    /// Number of rows
    pub rows: usize,
    /// Number of columns (softmax dimension)
    pub cols: usize,
    /// Temperature
    pub temperature: f32,
}

impl SoftmaxOp {
    /// Create new softmax operation
    #[must_use]
    pub fn new(rows: usize, cols: usize) -> Self {
        Self {
            rows,
            cols,
            temperature: 1.0,
        }
    }

    /// Set temperature
    #[must_use]
    pub fn with_temperature(mut self, temp: f32) -> Self {
        self.temperature = temp;
        self
    }
}

impl ComputeOp for SoftmaxOp {
    type Output = Vec<f32>;

    fn execute_simd(&self) -> WhisperResult<Self::Output> {
        Ok(vec![0.0; self.rows * self.cols])
    }

    fn execute_gpu(&self) -> WhisperResult<Self::Output> {
        self.execute_simd()
    }

    fn estimated_flops(&self) -> u64 {
        // exp + sum + div for each element, plus max finding
        (self.rows as u64) * (self.cols as u64) * 5
    }

    fn memory_requirement(&self) -> usize {
        self.rows * self.cols * 4 * 2 // input + output
    }
}

/// Layer normalization operation
#[derive(Debug, Clone)]
pub struct LayerNormOp {
    /// Batch size
    pub batch_size: usize,
    /// Hidden dimension
    pub hidden_size: usize,
    /// Epsilon
    pub epsilon: f32,
}

impl LayerNormOp {
    /// Create new layer norm operation
    #[must_use]
    pub fn new(batch_size: usize, hidden_size: usize) -> Self {
        Self {
            batch_size,
            hidden_size,
            epsilon: 1e-5,
        }
    }

    /// Set epsilon
    #[must_use]
    pub fn with_epsilon(mut self, eps: f32) -> Self {
        self.epsilon = eps;
        self
    }
}

impl ComputeOp for LayerNormOp {
    type Output = Vec<f32>;

    fn execute_simd(&self) -> WhisperResult<Self::Output> {
        Ok(vec![0.0; self.batch_size * self.hidden_size])
    }

    fn execute_gpu(&self) -> WhisperResult<Self::Output> {
        self.execute_simd()
    }

    fn estimated_flops(&self) -> u64 {
        // mean + variance + normalize
        (self.batch_size as u64) * (self.hidden_size as u64) * 6
    }

    fn memory_requirement(&self) -> usize {
        let data = self.batch_size * self.hidden_size * 4 * 2;
        let params = self.hidden_size * 4 * 2; // gamma + beta
        data + params
    }
}

/// GELU activation operation
#[derive(Debug, Clone)]
pub struct GeluOp {
    /// Number of elements
    pub num_elements: usize,
    /// Use fast approximation
    pub fast_approx: bool,
}

impl GeluOp {
    /// Create new GELU operation
    #[must_use]
    pub fn new(num_elements: usize) -> Self {
        Self {
            num_elements,
            fast_approx: true,
        }
    }

    /// Use exact GELU
    #[must_use]
    pub fn exact(mut self) -> Self {
        self.fast_approx = false;
        self
    }
}

impl ComputeOp for GeluOp {
    type Output = Vec<f32>;

    fn execute_simd(&self) -> WhisperResult<Self::Output> {
        Ok(vec![0.0; self.num_elements])
    }

    fn execute_gpu(&self) -> WhisperResult<Self::Output> {
        self.execute_simd()
    }

    fn estimated_flops(&self) -> u64 {
        // tanh approximation: ~10 FLOPs per element
        (self.num_elements as u64) * 10
    }

    fn memory_requirement(&self) -> usize {
        self.num_elements * 4 * 2 // input + output
    }
}