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
//! 2D Fast Fourier Transform operations
//!
//! This module provides 2D FFT implementations including forward FFT and inverse FFT
//! operations with both CPU and GPU acceleration support.
use crate::tensor::TensorStorage;
use crate::{Result, Tensor, TensorError};
use num_complex::Complex;
use oxifft::{Direction, Flags, Plan};
use scirs2_core::ndarray::{ArrayD, IxDyn};
use scirs2_core::numeric::{Float, FromPrimitive, Signed, Zero};
use std::fmt::Debug;
use super::fft1d::fft;
// GPU FFT kernels are not yet implemented, using CPU fallbacks
/// Convert num_complex slice to oxifft Complex slice
/// Both types have identical #[repr(C)] memory layout, making this conversion safe
#[inline]
fn to_oxifft_complex<T: oxifft::Float>(data: &[Complex<T>]) -> &[oxifft::kernel::Complex<T>] {
// Safety: Both num_complex::Complex and oxifft::Complex have #[repr(C)] layout
// with identical memory representation (re: T, im: T)
unsafe {
std::slice::from_raw_parts(
data.as_ptr() as *const oxifft::kernel::Complex<T>,
data.len(),
)
}
}
/// Convert num_complex mutable slice to oxifft Complex mutable slice
/// Both types have identical #[repr(C)] memory layout, making this conversion safe
#[inline]
fn to_oxifft_complex_mut<T: oxifft::Float>(
data: &mut [Complex<T>],
) -> &mut [oxifft::kernel::Complex<T>] {
// Safety: Both num_complex::Complex and oxifft::Complex have #[repr(C)] layout
// with identical memory representation (re: T, im: T)
unsafe {
std::slice::from_raw_parts_mut(
data.as_mut_ptr() as *mut oxifft::kernel::Complex<T>,
data.len(),
)
}
}
/// 2D FFT along the last two axes
pub fn fft2<T>(input: &Tensor<T>) -> Result<Tensor<Complex<T>>>
where
T: Float
+ Send
+ Sync
+ 'static
+ FromPrimitive
+ Signed
+ Debug
+ Default
+ bytemuck::Pod
+ bytemuck::Zeroable
+ oxifft::Float,
Complex<T>: Default,
{
match &input.storage {
TensorStorage::Cpu(arr) => {
let shape = arr.shape();
let ndim = shape.len();
if ndim < 2 {
return Err(TensorError::InvalidShape {
operation: "fft2".to_string(),
reason: "FFT2 requires at least 2D input".to_string(),
shape: Some(shape.to_vec()),
context: None,
});
}
let height = shape[ndim - 2];
let width = shape[ndim - 1];
// First, apply FFT along the last axis (width)
let _fft_last = fft(input)?;
// Now we need to apply FFT along the second-to-last axis (height)
// This requires transposing the last two dimensions, applying FFT, and transposing back
// For now, implement a simpler version that processes each row and column
let fft_width =
Plan::dft_1d(width, Direction::Forward, Flags::ESTIMATE).ok_or_else(|| {
TensorError::InvalidShape {
operation: "fft2".to_string(),
reason: "Failed to create width FFT plan".to_string(),
shape: Some(shape.to_vec()),
context: None,
}
})?;
let fft_height =
Plan::dft_1d(height, Direction::Forward, Flags::ESTIMATE).ok_or_else(|| {
TensorError::InvalidShape {
operation: "fft2".to_string(),
reason: "Failed to create height FFT plan".to_string(),
shape: Some(shape.to_vec()),
context: None,
}
})?;
// Calculate the number of 2D slices to process
let total_elements: usize = shape.iter().product();
let elements_per_slice = height * width;
let num_slices = total_elements / elements_per_slice;
// Convert input to complex and prepare output
let mut output_data = vec![Complex::zero(); total_elements];
if let Some(input_slice) = arr.as_slice() {
for slice_idx in 0..num_slices {
let slice_start = slice_idx * elements_per_slice;
// Create a temporary buffer for this 2D slice
let mut slice_data: Vec<Complex<T>> = input_slice
[slice_start..slice_start + elements_per_slice]
.iter()
.map(|&x| Complex::new(x, T::zero()))
.collect();
// Apply FFT along rows (width dimension)
for row in 0..height {
let row_start = row * width;
let row_end = row_start + width;
let mut row_input = slice_data[row_start..row_end].to_vec();
let mut row_output = vec![Complex::zero(); width];
fft_width.execute(
to_oxifft_complex(&row_input),
to_oxifft_complex_mut(&mut row_output),
);
slice_data[row_start..row_end].copy_from_slice(&row_output);
}
// Apply FFT along columns (height dimension)
for col in 0..width {
let mut col_input = Vec::with_capacity(height);
for row in 0..height {
col_input.push(slice_data[row * width + col]);
}
let mut col_output = vec![Complex::zero(); height];
fft_height.execute(
to_oxifft_complex(&col_input),
to_oxifft_complex_mut(&mut col_output),
);
for (row, &val) in col_output.iter().enumerate() {
slice_data[row * width + col] = val;
}
}
// Copy result back to output
output_data[slice_start..slice_start + elements_per_slice]
.copy_from_slice(&slice_data);
}
// Create output tensor
let output_array =
ArrayD::from_shape_vec(IxDyn(shape), output_data).map_err(|e| {
TensorError::InvalidShape {
operation: "fft".to_string(),
reason: e.to_string(),
shape: None,
context: None,
}
})?;
Ok(Tensor::from_array(output_array))
} else {
Err(TensorError::unsupported_operation_simple(
"Cannot get slice from input array".to_string(),
))
}
}
#[cfg(feature = "gpu")]
TensorStorage::Gpu(_gpu_buffer) => {
// GPU FFT2 not yet implemented, fallback to CPU
let cpu_tensor = input.to_cpu()?;
fft2(&cpu_tensor)
}
}
}
/// 2D inverse FFT along the last two axes
pub fn ifft2<T>(input: &Tensor<Complex<T>>) -> Result<Tensor<Complex<T>>>
where
T: Float
+ Send
+ Sync
+ 'static
+ FromPrimitive
+ Signed
+ Debug
+ Default
+ bytemuck::Pod
+ bytemuck::Zeroable
+ oxifft::Float,
Complex<T>: Default,
{
match &input.storage {
TensorStorage::Cpu(arr) => {
let shape = arr.shape();
let ndim = shape.len();
if ndim < 2 {
return Err(TensorError::InvalidShape {
operation: "ifft2".to_string(),
reason: "IFFT2 requires at least 2D input".to_string(),
shape: Some(shape.to_vec()),
context: None,
});
}
let height = shape[ndim - 2];
let width = shape[ndim - 1];
let ifft_width =
Plan::dft_1d(width, Direction::Backward, Flags::ESTIMATE).ok_or_else(|| {
TensorError::InvalidShape {
operation: "ifft2".to_string(),
reason: "Failed to create width IFFT plan".to_string(),
shape: Some(shape.to_vec()),
context: None,
}
})?;
let ifft_height = Plan::dft_1d(height, Direction::Backward, Flags::ESTIMATE)
.ok_or_else(|| TensorError::InvalidShape {
operation: "ifft2".to_string(),
reason: "Failed to create height IFFT plan".to_string(),
shape: Some(shape.to_vec()),
context: None,
})?;
// Calculate the number of 2D slices to process
let total_elements: usize = shape.iter().product();
let elements_per_slice = height * width;
let num_slices = total_elements / elements_per_slice;
// Prepare output
let mut output_data = vec![Complex::zero(); total_elements];
if let Some(input_slice) = arr.as_slice() {
for slice_idx in 0..num_slices {
let slice_start = slice_idx * elements_per_slice;
// Create a temporary buffer for this 2D slice
let mut slice_data =
input_slice[slice_start..slice_start + elements_per_slice].to_vec();
// Apply IFFT along rows (width dimension)
for row in 0..height {
let row_start = row * width;
let row_end = row_start + width;
let mut row_input = slice_data[row_start..row_end].to_vec();
let mut row_output = vec![Complex::zero(); width];
ifft_width.execute(
to_oxifft_complex(&row_input),
to_oxifft_complex_mut(&mut row_output),
);
// Normalize by width
let width_t = T::from(width).expect("width should convert to float type");
for val in &mut row_output {
*val /= width_t;
}
slice_data[row_start..row_end].copy_from_slice(&row_output);
}
// Apply IFFT along columns (height dimension)
for col in 0..width {
let mut col_input = Vec::with_capacity(height);
for row in 0..height {
col_input.push(slice_data[row * width + col]);
}
let mut col_output = vec![Complex::zero(); height];
ifft_height.execute(
to_oxifft_complex(&col_input),
to_oxifft_complex_mut(&mut col_output),
);
// Normalize by height
let height_t =
T::from(height).expect("height should convert to float type");
for val in &mut col_output {
*val /= height_t;
}
for (row, &val) in col_output.iter().enumerate() {
slice_data[row * width + col] = val;
}
}
// Copy result back to output
output_data[slice_start..slice_start + elements_per_slice]
.copy_from_slice(&slice_data);
}
// Create output tensor
let output_array =
ArrayD::from_shape_vec(IxDyn(shape), output_data).map_err(|e| {
TensorError::InvalidShape {
operation: "fft".to_string(),
reason: e.to_string(),
shape: None,
context: None,
}
})?;
Ok(Tensor::from_array(output_array))
} else {
Err(TensorError::unsupported_operation_simple(
"Cannot get slice from input array".to_string(),
))
}
}
#[cfg(feature = "gpu")]
TensorStorage::Gpu(_gpu_buffer) => {
// GPU IFFT2 not yet implemented
Err(TensorError::unsupported_operation_simple(
"GPU IFFT2 not yet implemented".to_string(),
))
}
}
}