torsh-backend 0.1.2

Backend abstraction layer for ToRSh
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Fast Fourier Transform operations for all backends
//!
//! This module provides a unified interface for FFT operations across all backends,
//! with optimized implementations for each platform.

use crate::{BackendResult, Buffer, Device};
use torsh_core::dtype::DType;

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, string::String, vec::Vec};

/// FFT direction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FftDirection {
    /// Forward FFT
    Forward,
    /// Inverse FFT
    Inverse,
}

/// FFT normalization mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FftNormalization {
    /// No normalization
    None,
    /// Normalize by 1/N
    Backward,
    /// Normalize by 1/sqrt(N)
    Ortho,
}

/// FFT operation type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FftType {
    /// 1D Complex-to-Complex FFT
    C2C,
    /// 1D Real-to-Complex FFT
    R2C,
    /// 1D Complex-to-Real FFT
    C2R,
    /// 2D Complex-to-Complex FFT
    C2C2D,
    /// 2D Real-to-Complex FFT
    R2C2D,
    /// 2D Complex-to-Real FFT
    C2R2D,
    /// 3D Complex-to-Complex FFT
    C2C3D,
    /// 3D Real-to-Complex FFT
    R2C3D,
    /// 3D Complex-to-Real FFT
    C2R3D,
}

/// FFT execution plan
#[derive(Debug, Clone)]
pub struct FftPlan {
    /// Plan ID for caching
    pub id: String,
    /// FFT type
    pub fft_type: FftType,
    /// Transform dimensions
    pub dimensions: Vec<usize>,
    /// Batch size
    pub batch_size: usize,
    /// Input data type
    pub input_dtype: DType,
    /// Output data type
    pub output_dtype: DType,
    /// Direction
    pub direction: FftDirection,
    /// Normalization mode
    pub normalization: FftNormalization,
    /// Backend-specific plan data
    pub backend_data: Vec<u8>,
}

impl FftPlan {
    /// Create a new FFT plan
    pub fn new(
        fft_type: FftType,
        dimensions: Vec<usize>,
        batch_size: usize,
        input_dtype: DType,
        output_dtype: DType,
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> Self {
        let id = format!(
            "{:?}_{:?}_{}_{}_{:?}_{:?}_{:?}",
            fft_type, dimensions, batch_size, input_dtype, output_dtype, direction, normalization
        );

        Self {
            id,
            fft_type,
            dimensions,
            batch_size,
            input_dtype,
            output_dtype,
            direction,
            normalization,
            backend_data: Vec::new(),
        }
    }

    /// Create a new 1D FFT plan with default parameters
    ///
    /// This is a convenience function for creating 1D FFT plans commonly used in benchmarks.
    ///
    /// # Arguments
    ///
    /// * `size` - Size of the 1D FFT
    /// * `direction` - Forward or inverse transform
    ///
    /// # Returns
    ///
    /// A new FftPlan configured for 1D transforms
    pub fn new_1d(size: usize, direction: FftDirection) -> Self {
        Self::new(
            FftType::C2C,
            vec![size],
            1,          // Single batch
            DType::C64, // Complex64 input
            DType::C64, // Complex64 output
            direction,
            FftNormalization::None,
        )
    }

    /// Get the total number of elements in the transform
    pub fn total_elements(&self) -> usize {
        self.dimensions.iter().product::<usize>() * self.batch_size
    }

    /// Get input buffer size in bytes
    pub fn input_buffer_size(&self) -> usize {
        let element_size = match self.input_dtype {
            DType::F32 => 4,
            DType::F64 => 8,
            DType::C64 => 8,
            DType::C128 => 16,
            _ => 4, // Default to f32
        };

        self.total_elements() * element_size
    }

    /// Get output buffer size in bytes
    pub fn output_buffer_size(&self) -> usize {
        let element_size = match self.output_dtype {
            DType::F32 => 4,
            DType::F64 => 8,
            DType::C64 => 8,
            DType::C128 => 16,
            _ => 8, // Default to c32
        };

        match self.fft_type {
            FftType::R2C | FftType::R2C2D | FftType::R2C3D => {
                // Real-to-complex transforms have reduced output size
                let mut output_elements = self.batch_size;
                for (i, &dim) in self.dimensions.iter().enumerate() {
                    if i == self.dimensions.len() - 1 {
                        // Last dimension is halved + 1 for R2C
                        output_elements *= (dim / 2) + 1;
                    } else {
                        output_elements *= dim;
                    }
                }
                output_elements * element_size
            }
            _ => self.total_elements() * element_size,
        }
    }

    /// Check if the plan is valid
    pub fn is_valid(&self) -> bool {
        !self.dimensions.is_empty() && self.batch_size > 0 && self.dimensions.iter().all(|&d| d > 0)
    }
}

/// FFT operations trait
#[async_trait::async_trait]
pub trait FftOps: Send + Sync {
    /// Create an FFT plan
    async fn create_fft_plan(
        &self,
        device: &Device,
        plan: &FftPlan,
    ) -> BackendResult<Box<dyn FftExecutor>>;

    /// Execute a 1D FFT
    async fn fft_1d(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: usize,
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()>;

    /// Execute a 2D FFT
    async fn fft_2d(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: (usize, usize),
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()>;

    /// Execute a 3D FFT
    async fn fft_3d(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: (usize, usize, usize),
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()>;

    /// Execute a batched FFT
    async fn fft_batch(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: &[usize],
        batch_size: usize,
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()>;

    /// Execute a real-to-complex FFT
    async fn rfft(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: &[usize],
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()>;

    /// Execute a complex-to-real FFT
    async fn irfft(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: &[usize],
        normalization: FftNormalization,
    ) -> BackendResult<()>;

    /// Check if FFT operations are supported
    fn supports_fft(&self) -> bool;

    /// Get optimal FFT sizes for performance
    fn get_optimal_fft_sizes(&self, min_size: usize, max_size: usize) -> Vec<usize>;
}

/// FFT executor for cached plans
#[async_trait::async_trait]
pub trait FftExecutor: Send + Sync {
    /// Execute the FFT plan
    async fn execute(&self, device: &Device, input: &Buffer, output: &Buffer) -> BackendResult<()>;

    /// Get the plan this executor was created for
    fn plan(&self) -> &FftPlan;

    /// Get memory requirements for execution
    fn memory_requirements(&self) -> usize;

    /// Check if the executor is valid
    fn is_valid(&self) -> bool;
}

/// Default FFT operations implementation
pub struct DefaultFftOps;

impl DefaultFftOps {
    /// Create a new DefaultFftOps instance
    pub fn new() -> Self {
        Self
    }
}

impl Default for DefaultFftOps {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl FftOps for DefaultFftOps {
    async fn create_fft_plan(
        &self,
        _device: &Device,
        plan: &FftPlan,
    ) -> BackendResult<Box<dyn FftExecutor>> {
        Ok(Box::new(DefaultFftExecutor { plan: plan.clone() }))
    }

    async fn fft_1d(
        &self,
        _device: &Device,
        _input: &Buffer,
        _output: &Buffer,
        _size: usize,
        _direction: FftDirection,
        _normalization: FftNormalization,
    ) -> BackendResult<()> {
        Err(torsh_core::error::TorshError::BackendError(
            "FFT operations not implemented for this backend".to_string(),
        ))
    }

    async fn fft_2d(
        &self,
        _device: &Device,
        _input: &Buffer,
        _output: &Buffer,
        _size: (usize, usize),
        _direction: FftDirection,
        _normalization: FftNormalization,
    ) -> BackendResult<()> {
        Err(torsh_core::error::TorshError::BackendError(
            "FFT operations not implemented for this backend".to_string(),
        ))
    }

    async fn fft_3d(
        &self,
        _device: &Device,
        _input: &Buffer,
        _output: &Buffer,
        _size: (usize, usize, usize),
        _direction: FftDirection,
        _normalization: FftNormalization,
    ) -> BackendResult<()> {
        Err(torsh_core::error::TorshError::BackendError(
            "FFT operations not implemented for this backend".to_string(),
        ))
    }

    async fn fft_batch(
        &self,
        _device: &Device,
        _input: &Buffer,
        _output: &Buffer,
        _size: &[usize],
        _batch_size: usize,
        _direction: FftDirection,
        _normalization: FftNormalization,
    ) -> BackendResult<()> {
        Err(torsh_core::error::TorshError::BackendError(
            "FFT operations not implemented for this backend".to_string(),
        ))
    }

    async fn rfft(
        &self,
        _device: &Device,
        _input: &Buffer,
        _output: &Buffer,
        _size: &[usize],
        _direction: FftDirection,
        _normalization: FftNormalization,
    ) -> BackendResult<()> {
        Err(torsh_core::error::TorshError::BackendError(
            "FFT operations not implemented for this backend".to_string(),
        ))
    }

    async fn irfft(
        &self,
        _device: &Device,
        _input: &Buffer,
        _output: &Buffer,
        _size: &[usize],
        _normalization: FftNormalization,
    ) -> BackendResult<()> {
        Err(torsh_core::error::TorshError::BackendError(
            "FFT operations not implemented for this backend".to_string(),
        ))
    }

    fn supports_fft(&self) -> bool {
        false
    }

    fn get_optimal_fft_sizes(&self, min_size: usize, max_size: usize) -> Vec<usize> {
        // Return power-of-2 sizes as default
        let mut sizes = Vec::new();
        let mut size = 1;
        while size < min_size {
            size *= 2;
        }
        while size <= max_size {
            sizes.push(size);
            size *= 2;
        }
        sizes
    }
}

/// Default FFT executor implementation
pub struct DefaultFftExecutor {
    plan: FftPlan,
}

#[async_trait::async_trait]
impl FftExecutor for DefaultFftExecutor {
    async fn execute(
        &self,
        _device: &Device,
        _input: &Buffer,
        _output: &Buffer,
    ) -> BackendResult<()> {
        Err(torsh_core::error::TorshError::BackendError(
            "FFT execution not implemented for this backend".to_string(),
        ))
    }

    fn plan(&self) -> &FftPlan {
        &self.plan
    }

    fn memory_requirements(&self) -> usize {
        self.plan.input_buffer_size() + self.plan.output_buffer_size()
    }

    fn is_valid(&self) -> bool {
        self.plan.is_valid()
    }
}

/// Convenience functions for common FFT operations
pub mod convenience {
    use super::*;

    /// Create a 1D complex-to-complex FFT plan
    pub fn create_c2c_1d_plan(
        size: usize,
        batch_size: usize,
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> FftPlan {
        FftPlan::new(
            FftType::C2C,
            vec![size],
            batch_size,
            DType::C64,
            DType::C64,
            direction,
            normalization,
        )
    }

    /// Create a 1D real-to-complex FFT plan
    pub fn create_r2c_1d_plan(
        size: usize,
        batch_size: usize,
        normalization: FftNormalization,
    ) -> FftPlan {
        FftPlan::new(
            FftType::R2C,
            vec![size],
            batch_size,
            DType::F32,
            DType::C64,
            FftDirection::Forward,
            normalization,
        )
    }

    /// Create a 2D complex-to-complex FFT plan
    pub fn create_c2c_2d_plan(
        size: (usize, usize),
        batch_size: usize,
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> FftPlan {
        FftPlan::new(
            FftType::C2C2D,
            vec![size.0, size.1],
            batch_size,
            DType::C64,
            DType::C64,
            direction,
            normalization,
        )
    }

    /// Create a 3D complex-to-complex FFT plan
    pub fn create_c2c_3d_plan(
        size: (usize, usize, usize),
        batch_size: usize,
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> FftPlan {
        FftPlan::new(
            FftType::C2C3D,
            vec![size.0, size.1, size.2],
            batch_size,
            DType::C64,
            DType::C64,
            direction,
            normalization,
        )
    }

    /// Get the next power of 2 greater than or equal to n
    pub fn next_power_of_2(n: usize) -> usize {
        if n == 0 {
            return 1;
        }
        let mut power = 1;
        while power < n {
            power *= 2;
        }
        power
    }

    /// Check if a size is optimal for FFT (power of 2, 3, 5, 7)
    pub fn is_optimal_fft_size(size: usize) -> bool {
        if size == 0 {
            return false;
        }

        let mut n = size;
        for prime in &[2, 3, 5, 7] {
            while n % prime == 0 {
                n /= prime;
            }
        }

        n == 1
    }

    /// Find the next optimal FFT size
    pub fn next_optimal_fft_size(size: usize) -> usize {
        let mut candidate = size;
        while !is_optimal_fft_size(candidate) {
            candidate += 1;
        }
        candidate
    }
}

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

    #[test]
    fn test_fft_plan_creation() {
        let plan = FftPlan::new(
            FftType::C2C,
            vec![1024],
            1,
            DType::C64,
            DType::C64,
            FftDirection::Forward,
            FftNormalization::None,
        );

        assert_eq!(plan.fft_type, FftType::C2C);
        assert_eq!(plan.dimensions, vec![1024]);
        assert_eq!(plan.batch_size, 1);
        assert_eq!(plan.input_dtype, DType::C64);
        assert_eq!(plan.output_dtype, DType::C64);
        assert_eq!(plan.direction, FftDirection::Forward);
        assert_eq!(plan.normalization, FftNormalization::None);
        assert!(plan.is_valid());
    }

    #[test]
    fn test_fft_plan_buffer_sizes() {
        let plan = FftPlan::new(
            FftType::C2C,
            vec![1024],
            1,
            DType::C64,
            DType::C64,
            FftDirection::Forward,
            FftNormalization::None,
        );

        assert_eq!(plan.input_buffer_size(), 1024 * 8); // C32 is 8 bytes
        assert_eq!(plan.output_buffer_size(), 1024 * 8);
    }

    #[test]
    fn test_r2c_plan_buffer_sizes() {
        let plan = FftPlan::new(
            FftType::R2C,
            vec![1024],
            1,
            DType::F32,
            DType::C64,
            FftDirection::Forward,
            FftNormalization::None,
        );

        assert_eq!(plan.input_buffer_size(), 1024 * 4); // F32 is 4 bytes
        assert_eq!(plan.output_buffer_size(), (1024 / 2 + 1) * 8); // C32 is 8 bytes, output is N/2+1
    }

    #[test]
    fn test_convenience_functions() {
        let plan =
            convenience::create_c2c_1d_plan(1024, 1, FftDirection::Forward, FftNormalization::None);

        assert_eq!(plan.fft_type, FftType::C2C);
        assert_eq!(plan.dimensions, vec![1024]);
        assert!(plan.is_valid());
    }

    #[test]
    fn test_optimal_fft_sizes() {
        assert!(convenience::is_optimal_fft_size(1024)); // 2^10
        assert!(convenience::is_optimal_fft_size(1080)); // 2^3 * 3^3 * 5
        assert!(!convenience::is_optimal_fft_size(1023)); // Prime

        assert_eq!(convenience::next_power_of_2(1000), 1024);
        assert_eq!(convenience::next_power_of_2(1024), 1024);

        assert_eq!(convenience::next_optimal_fft_size(1023), 1024);
        assert_eq!(convenience::next_optimal_fft_size(1024), 1024);
    }

    #[test]
    fn test_default_fft_ops() {
        let ops = DefaultFftOps;
        assert!(!ops.supports_fft());

        let sizes = ops.get_optimal_fft_sizes(100, 2000);
        assert!(!sizes.is_empty());
        assert!(sizes.iter().all(|&size| size >= 100 && size <= 2000));
        assert!(sizes.iter().all(|&size| size.is_power_of_two()));
    }
}