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
//! GPU buffer management (WAPR-121)
//!
//! Provides abstractions for GPU memory buffers used in compute operations.

use super::error::{GpuError, GpuResult};
use super::{BUFFER_ALIGNMENT, MAX_BUFFER_SIZE};

/// Buffer usage flags
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::struct_excessive_bools)]
pub struct GpuBufferUsage {
    /// Can be used as source for copy operations
    pub copy_src: bool,
    /// Can be used as destination for copy operations
    pub copy_dst: bool,
    /// Can be bound as storage buffer in compute shader
    pub storage: bool,
    /// Can be bound as uniform buffer
    pub uniform: bool,
    /// Can be mapped for CPU read
    pub map_read: bool,
    /// Can be mapped for CPU write
    pub map_write: bool,
}

impl Default for GpuBufferUsage {
    fn default() -> Self {
        Self {
            copy_src: false,
            copy_dst: false,
            storage: true,
            uniform: false,
            map_read: false,
            map_write: false,
        }
    }
}

impl GpuBufferUsage {
    /// Create storage buffer usage (for compute shaders)
    #[must_use]
    pub fn storage() -> Self {
        Self {
            storage: true,
            copy_src: true,
            copy_dst: true,
            ..Default::default()
        }
    }

    /// Create uniform buffer usage
    #[must_use]
    pub fn uniform() -> Self {
        Self {
            uniform: true,
            copy_dst: true,
            storage: false,
            ..Default::default()
        }
    }

    /// Create staging buffer for upload (CPU -> GPU)
    #[must_use]
    pub fn staging_upload() -> Self {
        Self {
            copy_src: true,
            map_write: true,
            storage: false,
            ..Default::default()
        }
    }

    /// Create staging buffer for download (GPU -> CPU)
    #[must_use]
    pub fn staging_download() -> Self {
        Self {
            copy_dst: true,
            map_read: true,
            storage: false,
            ..Default::default()
        }
    }

    /// Create read-write storage buffer
    #[must_use]
    pub fn storage_read_write() -> Self {
        Self {
            storage: true,
            copy_src: true,
            copy_dst: true,
            ..Default::default()
        }
    }

    /// Add copy source capability
    #[must_use]
    pub fn with_copy_src(mut self) -> Self {
        self.copy_src = true;
        self
    }

    /// Add copy destination capability
    #[must_use]
    pub fn with_copy_dst(mut self) -> Self {
        self.copy_dst = true;
        self
    }

    /// Add map read capability
    #[must_use]
    pub fn with_map_read(mut self) -> Self {
        self.map_read = true;
        self
    }

    /// Add map write capability
    #[must_use]
    pub fn with_map_write(mut self) -> Self {
        self.map_write = true;
        self
    }
}

/// GPU buffer descriptor
#[derive(Debug, Clone)]
pub struct GpuBufferDescriptor {
    /// Buffer size in bytes
    pub size: u64,
    /// Buffer usage flags
    pub usage: GpuBufferUsage,
    /// Label for debugging
    pub label: Option<String>,
    /// Whether to map buffer on creation
    pub mapped_at_creation: bool,
}

impl GpuBufferDescriptor {
    /// Create a new buffer descriptor
    #[must_use]
    pub fn new(size: u64, usage: GpuBufferUsage) -> Self {
        Self {
            size,
            usage,
            label: None,
            mapped_at_creation: false,
        }
    }

    /// Create storage buffer descriptor
    #[must_use]
    pub fn storage(size: u64) -> Self {
        Self::new(size, GpuBufferUsage::storage())
    }

    /// Create uniform buffer descriptor
    #[must_use]
    pub fn uniform(size: u64) -> Self {
        Self::new(size, GpuBufferUsage::uniform())
    }

    /// Create staging upload buffer descriptor
    #[must_use]
    pub fn staging_upload(size: u64) -> Self {
        Self::new(size, GpuBufferUsage::staging_upload()).with_mapped(true)
    }

    /// Create staging download buffer descriptor
    #[must_use]
    pub fn staging_download(size: u64) -> Self {
        Self::new(size, GpuBufferUsage::staging_download())
    }

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

    /// Set mapped at creation
    #[must_use]
    pub fn with_mapped(mut self, mapped: bool) -> Self {
        self.mapped_at_creation = mapped;
        self
    }

    /// Validate the descriptor
    pub fn validate(&self) -> GpuResult<()> {
        if self.size == 0 {
            return Err(GpuError::buffer("Buffer size cannot be zero"));
        }

        if self.size > MAX_BUFFER_SIZE {
            return Err(GpuError::InvalidBufferSize {
                requested: self.size,
                max: MAX_BUFFER_SIZE,
            });
        }

        Ok(())
    }

    /// Get aligned size (rounded up to BUFFER_ALIGNMENT)
    #[must_use]
    pub fn aligned_size(&self) -> u64 {
        align_size(self.size, BUFFER_ALIGNMENT)
    }
}

/// GPU buffer handle
///
/// Represents a buffer allocated on the GPU. The actual buffer
/// data is managed by the GPU device.
#[derive(Debug)]
pub struct GpuBuffer {
    /// Buffer ID (for tracking)
    id: u64,
    /// Buffer size in bytes
    size: u64,
    /// Buffer usage
    usage: GpuBufferUsage,
    /// Buffer label
    label: Option<String>,
    /// Whether buffer is currently mapped
    is_mapped: bool,
}

impl GpuBuffer {
    /// Create a new buffer handle
    ///
    /// Note: This doesn't actually allocate GPU memory without the webgpu feature.
    /// Use `GpuDevice::create_buffer` for actual allocation.
    #[allow(clippy::items_after_statements)]
    pub fn new(descriptor: GpuBufferDescriptor) -> GpuResult<Self> {
        descriptor.validate()?;

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

        Ok(Self {
            id: BUFFER_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
            size: descriptor.aligned_size(),
            usage: descriptor.usage,
            label: descriptor.label,
            is_mapped: descriptor.mapped_at_creation,
        })
    }

    /// Create a storage buffer
    pub fn storage(size: u64) -> GpuResult<Self> {
        Self::new(GpuBufferDescriptor::storage(size))
    }

    /// Create a uniform buffer
    pub fn uniform(size: u64) -> GpuResult<Self> {
        Self::new(GpuBufferDescriptor::uniform(size))
    }

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

    /// Get buffer size in bytes
    #[must_use]
    pub fn size(&self) -> u64 {
        self.size
    }

    /// Get buffer usage
    #[must_use]
    pub fn usage(&self) -> &GpuBufferUsage {
        &self.usage
    }

    /// Get buffer label
    #[must_use]
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }

    /// Check if buffer is currently mapped
    #[must_use]
    pub fn is_mapped(&self) -> bool {
        self.is_mapped
    }

    /// Check if buffer can be used as storage
    #[must_use]
    pub fn is_storage(&self) -> bool {
        self.usage.storage
    }

    /// Check if buffer can be used as uniform
    #[must_use]
    pub fn is_uniform(&self) -> bool {
        self.usage.uniform
    }

    /// Check if buffer can be copied from
    #[must_use]
    pub fn can_copy_src(&self) -> bool {
        self.usage.copy_src
    }

    /// Check if buffer can be copied to
    #[must_use]
    pub fn can_copy_dst(&self) -> bool {
        self.usage.copy_dst
    }

    /// Get size in KB
    #[must_use]
    pub fn size_kb(&self) -> f32 {
        self.size as f32 / 1024.0
    }

    /// Get size in MB
    #[must_use]
    pub fn size_mb(&self) -> f32 {
        self.size as f32 / (1024.0 * 1024.0)
    }
}

/// Align a size to the given alignment
#[inline]
#[must_use]
pub fn align_size(size: u64, alignment: u64) -> u64 {
    size.div_ceil(alignment) * alignment
}

/// Check if an offset is properly aligned
#[inline]
#[must_use]
#[allow(dead_code)]
pub fn is_aligned(offset: u64, alignment: u64) -> bool {
    offset % alignment == 0
}

/// Calculate the number of elements that fit in a buffer
#[inline]
#[must_use]
#[allow(dead_code)]
pub fn elements_in_buffer<T>(buffer_size: u64) -> usize {
    (buffer_size as usize) / std::mem::size_of::<T>()
}

/// Calculate buffer size for a number of elements
#[inline]
#[must_use]
#[allow(dead_code)]
pub fn buffer_size_for<T>(count: usize) -> u64 {
    (count * std::mem::size_of::<T>()) as u64
}

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

    #[test]
    fn test_buffer_usage_default() {
        let usage = GpuBufferUsage::default();
        assert!(usage.storage);
        assert!(!usage.copy_src);
        assert!(!usage.uniform);
    }

    #[test]
    fn test_buffer_usage_storage() {
        let usage = GpuBufferUsage::storage();
        assert!(usage.storage);
        assert!(usage.copy_src);
        assert!(usage.copy_dst);
    }

    #[test]
    fn test_buffer_usage_uniform() {
        let usage = GpuBufferUsage::uniform();
        assert!(usage.uniform);
        assert!(usage.copy_dst);
        assert!(!usage.storage);
    }

    #[test]
    fn test_buffer_usage_staging_upload() {
        let usage = GpuBufferUsage::staging_upload();
        assert!(usage.copy_src);
        assert!(usage.map_write);
        assert!(!usage.storage);
    }

    #[test]
    fn test_buffer_usage_staging_download() {
        let usage = GpuBufferUsage::staging_download();
        assert!(usage.copy_dst);
        assert!(usage.map_read);
    }

    #[test]
    fn test_buffer_usage_builders() {
        let usage = GpuBufferUsage::default()
            .with_copy_src()
            .with_copy_dst()
            .with_map_read();

        assert!(usage.copy_src);
        assert!(usage.copy_dst);
        assert!(usage.map_read);
    }

    #[test]
    fn test_buffer_descriptor_new() {
        let desc = GpuBufferDescriptor::new(1024, GpuBufferUsage::storage());
        assert_eq!(desc.size, 1024);
        assert!(desc.usage.storage);
        assert!(desc.label.is_none());
    }

    #[test]
    fn test_buffer_descriptor_storage() {
        let desc = GpuBufferDescriptor::storage(2048);
        assert_eq!(desc.size, 2048);
        assert!(desc.usage.storage);
    }

    #[test]
    fn test_buffer_descriptor_with_label() {
        let desc = GpuBufferDescriptor::storage(1024).with_label("weights");
        assert_eq!(desc.label, Some("weights".to_string()));
    }

    #[test]
    fn test_buffer_descriptor_validate_zero_size() {
        let desc = GpuBufferDescriptor::new(0, GpuBufferUsage::storage());
        assert!(desc.validate().is_err());
    }

    #[test]
    fn test_buffer_descriptor_validate_too_large() {
        let desc = GpuBufferDescriptor::new(MAX_BUFFER_SIZE + 1, GpuBufferUsage::storage());
        assert!(desc.validate().is_err());
    }

    #[test]
    fn test_buffer_descriptor_aligned_size() {
        let desc = GpuBufferDescriptor::new(100, GpuBufferUsage::storage());
        assert_eq!(desc.aligned_size(), 256); // Aligned to 256
    }

    #[test]
    fn test_gpu_buffer_new() {
        let buffer = GpuBuffer::storage(1024).unwrap();
        assert!(buffer.id() > 0);
        assert_eq!(buffer.size(), 1024); // 1024 aligns to 1024
        assert!(buffer.is_storage());
    }

    #[test]
    fn test_gpu_buffer_uniform() {
        let buffer = GpuBuffer::uniform(512).unwrap();
        assert!(buffer.is_uniform());
        assert!(!buffer.is_storage());
    }

    #[test]
    fn test_gpu_buffer_unique_ids() {
        let b1 = GpuBuffer::storage(1024).unwrap();
        let b2 = GpuBuffer::storage(1024).unwrap();
        assert_ne!(b1.id(), b2.id());
    }

    #[test]
    fn test_gpu_buffer_size_conversions() {
        let buffer = GpuBuffer::storage(1024 * 1024).unwrap(); // 1 MB
        assert!((buffer.size_kb() - 1024.0).abs() < 1.0);
        assert!((buffer.size_mb() - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_align_size() {
        assert_eq!(align_size(100, 256), 256);
        assert_eq!(align_size(256, 256), 256);
        assert_eq!(align_size(257, 256), 512);
        assert_eq!(align_size(1, 256), 256);
    }

    #[test]
    fn test_is_aligned() {
        assert!(is_aligned(0, 256));
        assert!(is_aligned(256, 256));
        assert!(is_aligned(512, 256));
        assert!(!is_aligned(100, 256));
        assert!(!is_aligned(257, 256));
    }

    #[test]
    fn test_elements_in_buffer() {
        // f32 is 4 bytes
        assert_eq!(elements_in_buffer::<f32>(1024), 256);
        assert_eq!(elements_in_buffer::<f32>(4096), 1024);
    }

    #[test]
    fn test_buffer_size_for() {
        assert_eq!(buffer_size_for::<f32>(256), 1024);
        assert_eq!(buffer_size_for::<f32>(1024), 4096);
    }
}