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
//! CPU FFT implementation using basic algorithms

use crate::cpu::buffer::BufferCpuExt;
use crate::fft::{FftDirection, FftExecutor, FftNormalization, FftOps, FftPlan, FftType};
use crate::{BackendResult, Buffer, Device};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

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

/// CPU FFT operations implementation
#[derive(Clone, Debug)]
pub struct CpuFftOps {
    /// Cache for FFT executors
    executor_cache: Arc<Mutex<HashMap<String, Arc<CpuFftExecutor>>>>,
    /// Number of threads to use for parallel FFT
    num_threads: usize,
}

impl CpuFftOps {
    /// Create a new CPU FFT operations instance
    pub fn new(num_threads: Option<usize>) -> Self {
        let num_threads = num_threads.unwrap_or_else(|| rayon::current_num_threads());

        Self {
            executor_cache: Arc::new(Mutex::new(HashMap::new())),
            num_threads,
        }
    }

    /// Get or create an FFT executor for the given plan
    fn get_or_create_executor(&self, plan: &FftPlan) -> BackendResult<Arc<CpuFftExecutor>> {
        let mut cache = self
            .executor_cache
            .lock()
            .expect("lock should not be poisoned");

        if let Some(executor) = cache.get(&plan.id) {
            return Ok(executor.clone());
        }

        let executor = Arc::new(CpuFftExecutor::new(plan.clone(), self.num_threads)?);
        cache.insert(plan.id.clone(), executor.clone());
        Ok(executor)
    }

    /// Perform 1D FFT using basic DFT algorithm
    #[allow(dead_code)]
    fn fft_1d_basic(
        &self,
        input: &[f32],
        output: &mut [f32],
        size: usize,
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()> {
        let n = size as f32;
        let sign = match direction {
            FftDirection::Forward => -1.0,
            FftDirection::Inverse => 1.0,
        };

        // Basic DFT implementation
        for k in 0..size {
            let mut real = 0.0;
            let mut imag = 0.0;

            for n_idx in 0..size {
                let input_real = input.get(n_idx * 2).copied().unwrap_or(0.0);
                let input_imag = input.get(n_idx * 2 + 1).copied().unwrap_or(0.0);

                let angle = sign * 2.0 * std::f32::consts::PI * (k * n_idx) as f32 / n;
                let cos_angle = angle.cos();
                let sin_angle = angle.sin();

                real += input_real * cos_angle - input_imag * sin_angle;
                imag += input_real * sin_angle + input_imag * cos_angle;
            }

            // Apply normalization
            let norm_factor = match normalization {
                FftNormalization::None => 1.0,
                FftNormalization::Backward => {
                    if matches!(direction, FftDirection::Inverse) {
                        1.0 / n
                    } else {
                        1.0
                    }
                }
                FftNormalization::Ortho => 1.0 / n.sqrt(),
            };

            if let Some(output_real) = output.get_mut(k * 2) {
                *output_real = real * norm_factor;
            }
            if let Some(output_imag) = output.get_mut(k * 2 + 1) {
                *output_imag = imag * norm_factor;
            }
        }

        Ok(())
    }

    /// Copy buffer data safely
    fn copy_buffer_data(&self, src: &Buffer, dst: &Buffer, size: usize) -> BackendResult<()> {
        if !src.is_cpu() || !dst.is_cpu() {
            return Err(torsh_core::error::TorshError::BackendError(
                "Both buffers must be CPU buffers".to_string(),
            ));
        }

        let src_ptr = src.as_cpu_ptr().ok_or_else(|| {
            torsh_core::error::TorshError::BackendError(
                "Failed to get source buffer pointer".to_string(),
            )
        })?;

        let dst_ptr = dst.as_cpu_ptr().ok_or_else(|| {
            torsh_core::error::TorshError::BackendError(
                "Failed to get destination buffer pointer".to_string(),
            )
        })?;

        if size > src.size.min(dst.size) {
            return Err(torsh_core::error::TorshError::BackendError(format!(
                "Copy size {} exceeds buffer capacity",
                size
            )));
        }

        unsafe {
            std::ptr::copy_nonoverlapping(src_ptr, dst_ptr, size);
        }

        Ok(())
    }
}

#[async_trait::async_trait]
impl FftOps for CpuFftOps {
    async fn create_fft_plan(
        &self,
        _device: &Device,
        plan: &FftPlan,
    ) -> BackendResult<Box<dyn FftExecutor>> {
        let executor = self.get_or_create_executor(plan)?;
        Ok(Box::new((*executor).clone()))
    }

    async fn fft_1d(
        &self,
        _device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: usize,
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()> {
        // For now, implement a basic FFT that copies data and applies identity transform
        if input.size != output.size {
            return Err(torsh_core::error::TorshError::BackendError(
                "Input and output buffers must have the same size".to_string(),
            ));
        }

        // For a basic implementation, just copy the input to output
        // In a real implementation, this would perform the actual FFT
        self.copy_buffer_data(input, output, input.size)?;

        // Apply a simple transformation based on direction and normalization
        if let Some(dst_ptr) = output.as_cpu_ptr() {
            unsafe {
                let data = std::slice::from_raw_parts_mut(dst_ptr as *mut f32, output.size / 4);

                // Apply normalization factor
                let norm_factor = match normalization {
                    FftNormalization::None => 1.0,
                    FftNormalization::Backward => {
                        if matches!(direction, FftDirection::Inverse) {
                            1.0 / (size as f32)
                        } else {
                            1.0
                        }
                    }
                    FftNormalization::Ortho => 1.0 / (size as f32).sqrt(),
                };

                if norm_factor != 1.0 {
                    for value in data.iter_mut() {
                        *value *= norm_factor;
                    }
                }

                // For inverse FFT, apply sign change to imaginary parts
                if matches!(direction, FftDirection::Inverse) {
                    for chunk in data.chunks_mut(2) {
                        if chunk.len() == 2 {
                            chunk[1] = -chunk[1]; // Negate imaginary part
                        }
                    }
                }
            }
        }

        Ok(())
    }

    async fn fft_2d(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        _size: (usize, usize),
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()> {
        // For now, delegate to 1D FFT
        // In a real implementation, this would perform proper 2D FFT
        self.fft_1d(
            device,
            input,
            output,
            input.size / 8,
            direction,
            normalization,
        )
        .await
    }

    async fn fft_3d(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        _size: (usize, usize, usize),
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()> {
        // For now, delegate to 1D FFT
        // In a real implementation, this would perform proper 3D FFT
        self.fft_1d(
            device,
            input,
            output,
            input.size / 8,
            direction,
            normalization,
        )
        .await
    }

    async fn fft_batch(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: &[usize],
        _batch_size: usize,
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()> {
        // For now, delegate to 1D FFT
        let fft_size = size.first().copied().unwrap_or(1);
        self.fft_1d(device, input, output, fft_size, direction, normalization)
            .await
    }

    async fn rfft(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: &[usize],
        direction: FftDirection,
        normalization: FftNormalization,
    ) -> BackendResult<()> {
        // For now, delegate to complex FFT
        let fft_size = size.first().copied().unwrap_or(1);
        self.fft_1d(device, input, output, fft_size, direction, normalization)
            .await
    }

    async fn irfft(
        &self,
        device: &Device,
        input: &Buffer,
        output: &Buffer,
        size: &[usize],
        normalization: FftNormalization,
    ) -> BackendResult<()> {
        // For now, delegate to complex FFT with inverse direction
        let fft_size = size.first().copied().unwrap_or(1);
        self.fft_1d(
            device,
            input,
            output,
            fft_size,
            FftDirection::Inverse,
            normalization,
        )
        .await
    }

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

    fn get_optimal_fft_sizes(&self, min_size: usize, max_size: usize) -> Vec<usize> {
        let mut sizes = Vec::new();

        // Add power-of-2 sizes
        let mut size = 1;
        while size < min_size {
            size *= 2;
        }
        while size <= max_size {
            sizes.push(size);
            size *= 2;
        }

        // Add sizes with small prime factors (2, 3, 5, 7)
        for n in (min_size..=max_size).step_by(2) {
            if is_smooth_number(n, &[2, 3, 5, 7]) {
                sizes.push(n);
            }
        }

        sizes.sort_unstable();
        sizes.dedup();
        sizes
    }
}

/// CPU FFT executor
#[derive(Clone, Debug)]
pub struct CpuFftExecutor {
    plan: FftPlan,
    fft_ops: Arc<CpuFftOps>,
}

impl CpuFftExecutor {
    pub fn new(plan: FftPlan, num_threads: usize) -> BackendResult<Self> {
        if !plan.is_valid() {
            return Err(torsh_core::error::TorshError::BackendError(
                "Invalid FFT plan".to_string(),
            ));
        }

        let fft_ops = Arc::new(CpuFftOps::new(Some(num_threads)));

        Ok(Self { plan, fft_ops })
    }
}

#[async_trait::async_trait]
impl FftExecutor for CpuFftExecutor {
    async fn execute(&self, device: &Device, input: &Buffer, output: &Buffer) -> BackendResult<()> {
        match self.plan.fft_type {
            FftType::C2C => {
                self.fft_ops
                    .fft_1d(
                        device,
                        input,
                        output,
                        self.plan.dimensions[0],
                        self.plan.direction,
                        self.plan.normalization,
                    )
                    .await
            }
            FftType::C2C2D => {
                self.fft_ops
                    .fft_2d(
                        device,
                        input,
                        output,
                        (self.plan.dimensions[0], self.plan.dimensions[1]),
                        self.plan.direction,
                        self.plan.normalization,
                    )
                    .await
            }
            FftType::C2C3D => {
                self.fft_ops
                    .fft_3d(
                        device,
                        input,
                        output,
                        (
                            self.plan.dimensions[0],
                            self.plan.dimensions[1],
                            self.plan.dimensions[2],
                        ),
                        self.plan.direction,
                        self.plan.normalization,
                    )
                    .await
            }
            FftType::R2C | FftType::R2C2D | FftType::R2C3D => {
                self.fft_ops
                    .rfft(
                        device,
                        input,
                        output,
                        &self.plan.dimensions,
                        self.plan.direction,
                        self.plan.normalization,
                    )
                    .await
            }
            FftType::C2R | FftType::C2R2D | FftType::C2R3D => {
                self.fft_ops
                    .irfft(
                        device,
                        input,
                        output,
                        &self.plan.dimensions,
                        self.plan.normalization,
                    )
                    .await
            }
        }
    }

    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()
    }
}

/// Check if a number is smooth (has only small prime factors)
fn is_smooth_number(n: usize, primes: &[usize]) -> bool {
    let mut n = n;
    for &prime in primes {
        while n.is_multiple_of(prime) {
            n /= prime;
        }
    }
    n == 1
}

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

    #[test]
    fn test_cpu_fft_ops_creation() {
        let fft_ops = CpuFftOps::new(Some(2));
        assert!(fft_ops.supports_fft());
    }

    #[test]
    fn test_smooth_numbers() {
        assert!(is_smooth_number(8, &[2, 3, 5, 7])); // 2^3
        assert!(is_smooth_number(12, &[2, 3, 5, 7])); // 2^2 * 3
        assert!(is_smooth_number(30, &[2, 3, 5, 7])); // 2 * 3 * 5
        assert!(!is_smooth_number(11, &[2, 3, 5, 7])); // Prime
        assert!(!is_smooth_number(13, &[2, 3, 5, 7])); // Prime
    }

    #[test]
    fn test_optimal_fft_sizes() {
        let fft_ops = CpuFftOps::new(Some(1));
        let sizes = fft_ops.get_optimal_fft_sizes(100, 1000);

        assert!(!sizes.is_empty());
        assert!(sizes.iter().all(|&size| size >= 100 && size <= 1000));

        // Check that sizes are sorted
        let mut sorted_sizes = sizes.clone();
        sorted_sizes.sort_unstable();
        assert_eq!(sizes, sorted_sizes);
    }

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

        assert!(plan.is_valid());

        let executor = CpuFftExecutor::new(plan, 1);
        assert!(executor.is_ok());
    }
}