whisper-apr 0.3.1

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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#![allow(clippy::expect_used, clippy::float_cmp)]
//! GPU GELU activation (WAPR-133)
//!
//! Provides Gaussian Error Linear Unit activation using compute shaders.
//! Used in transformer feed-forward networks.

use crate::gpu::error::{GpuError, GpuResult};

/// GELU approximation method
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GeluApproximation {
    /// Exact GELU: x * 0.5 * (1 + erf(x / sqrt(2)))
    Exact,
    /// Tanh approximation: x * 0.5 * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3)))
    #[default]
    Tanh,
    /// Sigmoid approximation: x * sigmoid(1.702 * x) (fastest)
    Sigmoid,
}

impl GeluApproximation {
    /// Get description of the approximation
    #[must_use]
    pub fn description(&self) -> &str {
        match self {
            Self::Exact => "exact GELU",
            Self::Tanh => "tanh approximation",
            Self::Sigmoid => "sigmoid approximation",
        }
    }

    /// Get relative accuracy (1.0 = exact)
    #[must_use]
    pub fn accuracy(&self) -> f32 {
        match self {
            Self::Exact => 1.0,
            Self::Tanh => 0.999,
            Self::Sigmoid => 0.995,
        }
    }

    /// Get relative speed (1.0 = baseline)
    #[must_use]
    pub fn relative_speed(&self) -> f32 {
        match self {
            Self::Exact => 1.0,
            Self::Tanh => 1.5,
            Self::Sigmoid => 2.0,
        }
    }
}

/// GELU configuration
#[derive(Debug, Clone)]
pub struct GeluConfig {
    /// Approximation method
    pub approximation: GeluApproximation,
    /// Total number of elements
    pub num_elements: u32,
    /// Whether to apply in-place (input = output buffer)
    pub inplace: bool,
    /// Workgroup size
    pub workgroup_size: u32,
    /// Label for debugging
    pub label: Option<String>,
}

impl Default for GeluConfig {
    fn default() -> Self {
        Self {
            approximation: GeluApproximation::default(),
            num_elements: 1,
            inplace: false,
            workgroup_size: 256,
            label: None,
        }
    }
}

impl GeluConfig {
    /// Create config for given number of elements
    #[must_use]
    pub fn new(num_elements: u32) -> Self {
        Self {
            num_elements,
            ..Default::default()
        }
    }

    /// Create config for transformer FFN
    #[must_use]
    pub fn for_ffn(batch_size: u32, hidden_size: u32) -> Self {
        Self {
            num_elements: batch_size * hidden_size,
            approximation: GeluApproximation::Tanh,
            ..Default::default()
        }
    }

    /// Set approximation method
    #[must_use]
    pub fn with_approximation(mut self, approx: GeluApproximation) -> Self {
        self.approximation = approx;
        self
    }

    /// Use exact GELU
    #[must_use]
    pub fn exact(mut self) -> Self {
        self.approximation = GeluApproximation::Exact;
        self
    }

    /// Use tanh approximation
    #[must_use]
    pub fn tanh(mut self) -> Self {
        self.approximation = GeluApproximation::Tanh;
        self
    }

    /// Use sigmoid approximation (fastest)
    #[must_use]
    pub fn sigmoid(mut self) -> Self {
        self.approximation = GeluApproximation::Sigmoid;
        self
    }

    /// Enable in-place operation
    #[must_use]
    pub fn inplace(mut self) -> Self {
        self.inplace = true;
        self
    }

    /// Set workgroup size
    #[must_use]
    pub fn with_workgroup_size(mut self, size: u32) -> Self {
        self.workgroup_size = size;
        self
    }

    /// Set label
    #[must_use]
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Validate configuration
    pub fn validate(&self) -> GpuResult<()> {
        if self.num_elements == 0 {
            return Err(GpuError::compute("Number of elements cannot be zero"));
        }
        if !self.workgroup_size.is_power_of_two() {
            return Err(GpuError::compute("Workgroup size must be power of two"));
        }
        Ok(())
    }

    /// Calculate number of workgroups needed
    #[must_use]
    pub fn num_workgroups(&self) -> u32 {
        self.num_elements.div_ceil(self.workgroup_size)
    }
}

/// GPU GELU operation
#[derive(Debug)]
pub struct GpuGelu {
    /// Operation ID
    id: u64,
    /// Configuration
    config: GeluConfig,
    /// Whether executed
    executed: bool,
}

impl GpuGelu {
    /// Create a new GELU operation
    #[allow(clippy::items_after_statements)]
    pub fn new(config: GeluConfig) -> GpuResult<Self> {
        config.validate()?;

        static OP_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);

        Ok(Self {
            id: OP_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
            config,
            executed: false,
        })
    }

    /// Create simple GELU for given elements
    pub fn simple(num_elements: u32) -> GpuResult<Self> {
        Self::new(GeluConfig::new(num_elements))
    }

    /// Create for transformer FFN
    pub fn for_ffn(batch_size: u32, hidden_size: u32) -> GpuResult<Self> {
        Self::new(GeluConfig::for_ffn(batch_size, hidden_size))
    }

    /// Get operation ID
    #[must_use]
    pub fn id(&self) -> u64 {
        self.id
    }

    /// Get configuration
    #[must_use]
    pub fn config(&self) -> &GeluConfig {
        &self.config
    }

    /// Check if executed
    #[must_use]
    pub fn is_executed(&self) -> bool {
        self.executed
    }

    /// Get memory requirement in bytes
    #[must_use]
    pub fn memory_requirement(&self) -> usize {
        let input = self.config.num_elements as usize * 4;
        if self.config.inplace {
            input // Same buffer for input/output
        } else {
            input * 2 // Separate input and output
        }
    }

    /// Calculate workgroups for dispatch
    #[must_use]
    pub fn workgroups(&self) -> (u32, u32, u32) {
        (self.config.num_workgroups(), 1, 1)
    }

    /// Generate WGSL shader for this operation
    #[must_use]
    pub fn generate_shader(&self) -> String {
        let workgroup_size = self.config.workgroup_size;

        let gelu_function = match self.config.approximation {
            GeluApproximation::Exact => {
                r"
// Exact GELU: x * 0.5 * (1 + erf(x / sqrt(2)))
// We approximate erf using a polynomial
fn gelu(x: f32) -> f32 {
    let sqrt2_inv = 0.7071067811865475;
    let a = x * sqrt2_inv;

    // Polynomial approximation of erf
    let a2 = a * a;
    let a3 = a2 * a;
    let erf_approx = sign(a) * (1.0 - 1.0 / (1.0 + 0.278393 * abs(a) + 0.230389 * a2 + 0.000972 * a3 + 0.078108 * a2 * a2));

    return x * 0.5 * (1.0 + erf_approx);
}"
            }
            GeluApproximation::Tanh => {
                r"
// Tanh approximation (most common in transformers)
// x * 0.5 * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3)))
fn gelu(x: f32) -> f32 {
    let sqrt_2_over_pi = 0.7978845608028654;
    let coeff = 0.044715;
    let inner = sqrt_2_over_pi * (x + coeff * x * x * x);
    return x * 0.5 * (1.0 + tanh(inner));
}"
            }
            GeluApproximation::Sigmoid => {
                r"
// Sigmoid approximation (fastest)
// x * sigmoid(1.702 * x)
fn gelu(x: f32) -> f32 {
    let sigmoid_input = 1.702 * x;
    let sigmoid_val = 1.0 / (1.0 + exp(-sigmoid_input));
    return x * sigmoid_val;
}"
            }
        };

        let output_binding = if self.config.inplace {
            "@group(0) @binding(1) var<storage, read_write> data: array<f32>;"
        } else {
            "@group(0) @binding(1) var<storage, read> input: array<f32>;\n@group(0) @binding(2) var<storage, read_write> output: array<f32>;"
        };

        let compute_body = if self.config.inplace {
            r"
    let idx = global_id.x;
    if (idx >= params.num_elements) {
        return;
    }
    data[idx] = gelu(data[idx]);"
        } else {
            r"
    let idx = global_id.x;
    if (idx >= params.num_elements) {
        return;
    }
    output[idx] = gelu(input[idx]);"
        };

        format!(
            r"// GELU activation shader ({approx})
// Elements: {num_elements}
// Workgroup size: {wg_size}
{gelu_fn}

struct Params {{
    num_elements: u32,
}}

@group(0) @binding(0) var<uniform> params: Params;
{output_binding}

@compute @workgroup_size({wg_size}, 1, 1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {{{compute_body}
}}
",
            approx = self.config.approximation.description(),
            num_elements = self.config.num_elements,
            wg_size = workgroup_size,
            gelu_fn = gelu_function,
            output_binding = output_binding,
            compute_body = compute_body,
        )
    }
}

/// WGSL shader source for GELU (tanh approximation)
#[allow(dead_code)]
pub const GELU_SHADER_TANH: &str = r"
struct Params {
    num_elements: u32,
    _padding1: u32,
    _padding2: u32,
    _padding3: u32,
}

@group(0) @binding(0) var<uniform> params: Params;
@group(0) @binding(1) var<storage, read> input: array<f32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;

// Tanh approximation GELU
fn gelu(x: f32) -> f32 {
    let sqrt_2_over_pi = 0.7978845608028654;
    let coeff = 0.044715;
    let inner = sqrt_2_over_pi * (x + coeff * x * x * x);
    return x * 0.5 * (1.0 + tanh(inner));
}

@compute @workgroup_size(256, 1, 1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let idx = global_id.x;
    if (idx >= params.num_elements) {
        return;
    }
    output[idx] = gelu(input[idx]);
}
";

/// WGSL shader source for GELU (sigmoid approximation - fastest)
#[allow(dead_code)]
pub const GELU_SHADER_SIGMOID: &str = r"
struct Params {
    num_elements: u32,
    _padding1: u32,
    _padding2: u32,
    _padding3: u32,
}

@group(0) @binding(0) var<uniform> params: Params;
@group(0) @binding(1) var<storage, read> input: array<f32>;
@group(0) @binding(2) var<storage, read_write> output: array<f32>;

// Sigmoid approximation GELU (fastest)
fn gelu(x: f32) -> f32 {
    let sigmoid_val = 1.0 / (1.0 + exp(-1.702 * x));
    return x * sigmoid_val;
}

@compute @workgroup_size(256, 1, 1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    let idx = global_id.x;
    if (idx >= params.num_elements) {
        return;
    }
    output[idx] = gelu(input[idx]);
}
";

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gelu_approximation_default() {
        assert_eq!(GeluApproximation::default(), GeluApproximation::Tanh);
    }

    #[test]
    fn test_gelu_approximation_description() {
        assert!(GeluApproximation::Exact.description().contains("exact"));
        assert!(GeluApproximation::Tanh.description().contains("tanh"));
        assert!(GeluApproximation::Sigmoid.description().contains("sigmoid"));
    }

    #[test]
    fn test_gelu_approximation_accuracy() {
        assert_eq!(GeluApproximation::Exact.accuracy(), 1.0);
        assert!(GeluApproximation::Tanh.accuracy() > 0.99);
        assert!(GeluApproximation::Sigmoid.accuracy() > 0.99);
    }

    #[test]
    fn test_gelu_approximation_speed() {
        // Sigmoid should be fastest
        assert!(
            GeluApproximation::Sigmoid.relative_speed() > GeluApproximation::Tanh.relative_speed()
        );
        assert!(
            GeluApproximation::Tanh.relative_speed() > GeluApproximation::Exact.relative_speed()
        );
    }

    #[test]
    fn test_gelu_config_default() {
        let config = GeluConfig::default();
        assert_eq!(config.approximation, GeluApproximation::Tanh);
        assert!(!config.inplace);
        assert_eq!(config.workgroup_size, 256);
    }

    #[test]
    fn test_gelu_config_new() {
        let config = GeluConfig::new(1024);
        assert_eq!(config.num_elements, 1024);
    }

    #[test]
    fn test_gelu_config_for_ffn() {
        let config = GeluConfig::for_ffn(32, 3072);
        assert_eq!(config.num_elements, 32 * 3072);
        assert_eq!(config.approximation, GeluApproximation::Tanh);
    }

    #[test]
    fn test_gelu_config_builders() {
        let config = GeluConfig::new(1024)
            .exact()
            .inplace()
            .with_workgroup_size(128)
            .with_label("test_gelu");

        assert_eq!(config.approximation, GeluApproximation::Exact);
        assert!(config.inplace);
        assert_eq!(config.workgroup_size, 128);
        assert_eq!(config.label, Some("test_gelu".to_string()));
    }

    #[test]
    fn test_gelu_config_approximation_builders() {
        assert_eq!(
            GeluConfig::new(1024).exact().approximation,
            GeluApproximation::Exact
        );
        assert_eq!(
            GeluConfig::new(1024).tanh().approximation,
            GeluApproximation::Tanh
        );
        assert_eq!(
            GeluConfig::new(1024).sigmoid().approximation,
            GeluApproximation::Sigmoid
        );
    }

    #[test]
    fn test_gelu_config_validate() {
        assert!(GeluConfig::new(1024).validate().is_ok());
        assert!(GeluConfig::new(0).validate().is_err());
        assert!(GeluConfig::new(1024)
            .with_workgroup_size(100)
            .validate()
            .is_err());
    }

    #[test]
    fn test_gelu_config_num_workgroups() {
        let config = GeluConfig::new(1000).with_workgroup_size(256);
        assert_eq!(config.num_workgroups(), 4); // ceil(1000/256)

        let config2 = GeluConfig::new(256).with_workgroup_size(256);
        assert_eq!(config2.num_workgroups(), 1);
    }

    #[test]
    fn test_gpu_gelu_new() {
        let gelu = GpuGelu::new(GeluConfig::new(1024)).expect("Should create GELU");
        assert!(gelu.id() > 0);
        assert!(!gelu.is_executed());
    }

    #[test]
    fn test_gpu_gelu_simple() {
        let gelu = GpuGelu::simple(1024).expect("Should create");
        assert_eq!(gelu.config().num_elements, 1024);
    }

    #[test]
    fn test_gpu_gelu_for_ffn() {
        let gelu = GpuGelu::for_ffn(32, 3072).expect("Should create");
        assert_eq!(gelu.config().num_elements, 32 * 3072);
    }

    #[test]
    fn test_gpu_gelu_memory_requirement() {
        let gelu = GpuGelu::new(GeluConfig::new(1024)).expect("Should create");
        assert_eq!(gelu.memory_requirement(), 1024 * 4 * 2); // input + output

        let inplace = GpuGelu::new(GeluConfig::new(1024).inplace()).expect("Should create");
        assert_eq!(inplace.memory_requirement(), 1024 * 4); // same buffer
    }

    #[test]
    fn test_gpu_gelu_workgroups() {
        let gelu =
            GpuGelu::new(GeluConfig::new(1000).with_workgroup_size(256)).expect("Should create");
        let (x, y, z) = gelu.workgroups();
        assert_eq!(x, 4);
        assert_eq!(y, 1);
        assert_eq!(z, 1);
    }

    #[test]
    fn test_gpu_gelu_generate_shader_tanh() {
        let gelu = GpuGelu::new(GeluConfig::new(1024).tanh()).expect("Should create");
        let shader = gelu.generate_shader();

        assert!(shader.contains("@compute"));
        assert!(shader.contains("tanh"));
        assert!(shader.contains("0.044715"));
    }

    #[test]
    fn test_gpu_gelu_generate_shader_sigmoid() {
        let gelu = GpuGelu::new(GeluConfig::new(1024).sigmoid()).expect("Should create");
        let shader = gelu.generate_shader();

        assert!(shader.contains("sigmoid"));
        assert!(shader.contains("1.702"));
    }

    #[test]
    fn test_gpu_gelu_generate_shader_exact() {
        let gelu = GpuGelu::new(GeluConfig::new(1024).exact()).expect("Should create");
        let shader = gelu.generate_shader();

        assert!(shader.contains("exact"));
        assert!(shader.contains("erf"));
    }

    #[test]
    fn test_gpu_gelu_generate_shader_inplace() {
        let gelu = GpuGelu::new(GeluConfig::new(1024).inplace()).expect("Should create");
        let shader = gelu.generate_shader();

        assert!(shader.contains("data[idx]"));
        assert!(!shader.contains("input[idx]"));
    }

    #[test]
    fn test_gelu_shader_tanh() {
        assert!(GELU_SHADER_TANH.contains("@compute"));
        assert!(GELU_SHADER_TANH.contains("tanh"));
        assert!(GELU_SHADER_TANH.contains("0.044715"));
    }

    #[test]
    fn test_gelu_shader_sigmoid() {
        assert!(GELU_SHADER_SIGMOID.contains("@compute"));
        assert!(GELU_SHADER_SIGMOID.contains("1.702"));
    }

    #[test]
    fn test_gpu_gelu_unique_ids() {
        let g1 = GpuGelu::new(GeluConfig::new(1024)).expect("g1");
        let g2 = GpuGelu::new(GeluConfig::new(1024)).expect("g2");
        assert_ne!(g1.id(), g2.id());
    }
}