onnx-ir 0.21.0

ONNX-IR is a pure Rust library for parsing ONNX models into an intermediate representation that can be used to generate code for various ML/DL frameworks
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
647
648
//! # DFT (Discrete Fourier Transform)
//!
//! Computes the discrete Fourier Transform (or its inverse) of the input.
//!
//! **ONNX Spec**: <https://onnx.ai/onnx/operators/onnx__DFT.html>
//!
//! ## Opset Versions
//! - **Opset 17**: Initial version
//! - **Opset 20**: Updated type constraints
//!
//! ## Supported Configurations
//!
//! Only forward real-input DFT maps to Burn's signal API:
//! - Forward real DFT with `onesided=1`: maps to `burn::tensor::signal::rfft`
//! - Forward real DFT with `onesided=0`: rfft + conjugate symmetry reconstruction
//!
//! Not supported (will produce a codegen error):
//! - Inverse DFT (`inverse=1`): ONNX uses complex-to-complex IDFT, but Burn only
//!   provides `irfft` (inverse of real FFT), which is a different operation
//! - Complex-to-complex forward DFT (complex input with trailing dim = 2)
//!
//! ## Type Constraints
//! - **T1**: tensor(bfloat16), tensor(double), tensor(float), tensor(float16)
//! - **T2**: tensor(int32), tensor(int64)

use onnx_ir_derive::NodeBuilder;

use crate::ir::{ArgType, Argument, Node, RawNode, TensorData, TensorType};
use crate::processor::{
    InputSpec, NodeProcessor, NodeSpec, OutputPreferences, OutputSpec, ProcessError, validate_opset,
};

/// Extract a scalar integer from tensor data, supporting both i32 and i64 dtypes.
fn extract_scalar_int(data: TensorData, name: &str) -> Result<i64, ProcessError> {
    if let Ok(slice) = data.as_slice::<i64>() {
        slice.first().copied().ok_or_else(|| {
            ProcessError::Custom(format!(
                "DFT: {name} constant must contain at least one element"
            ))
        })
    } else if let Ok(slice) = data.as_slice::<i32>() {
        slice.first().copied().map(i64::from).ok_or_else(|| {
            ProcessError::Custom(format!(
                "DFT: {name} constant must contain at least one element"
            ))
        })
    } else {
        Err(ProcessError::Custom(format!(
            "DFT: {name} constant must have type int32 or int64"
        )))
    }
}

/// Configuration for the DFT operation
#[derive(Debug, Clone, Default)]
pub struct DftConfig {
    /// Whether to compute the inverse DFT (default: false)
    pub inverse: bool,
    /// Whether to produce onesided output for real input (default: false)
    pub onesided: bool,
    /// The axis along which to perform the DFT (resolved to positive index)
    pub axis: usize,
    /// Optional DFT length (None means use the signal dimension size)
    pub dft_length: Option<usize>,
    /// Whether the input is real (trailing dim = 1) or complex (trailing dim = 2)
    pub is_real_input: bool,
}

/// Node representation for DFT operation
#[derive(Debug, Clone, NodeBuilder)]
pub struct DftNode {
    pub name: String,
    pub inputs: Vec<Argument>,
    pub outputs: Vec<Argument>,
    pub config: DftConfig,
}

pub(crate) struct DftProcessor;

impl NodeProcessor for DftProcessor {
    type Config = DftConfig;

    fn spec(&self) -> NodeSpec {
        NodeSpec {
            min_opset: 17,
            max_opset: None,
            inputs: InputSpec::Range(1, 3),
            outputs: OutputSpec::Exact(1),
        }
    }

    fn lift_constants(&self, node: &mut RawNode, opset: usize) -> Result<(), ProcessError> {
        // Lift dft_length (input[1]) if present and constant
        if let Some(input) = node.inputs.get(1)
            && !input.is_optional()
            && input.is_constant()
        {
            node.inputs[1].to_static()?;
        }

        // Lift axis (input[2]) if present and constant (opset 20+ only; opset 17-19 uses attribute)
        if opset >= 20
            && let Some(input) = node.inputs.get(2)
            && !input.is_optional()
            && input.is_constant()
        {
            node.inputs[2].to_static()?;
        }

        Ok(())
    }

    fn infer_types(
        &self,
        node: &mut RawNode,
        opset: usize,
        _output_preferences: &OutputPreferences,
    ) -> Result<(), ProcessError> {
        validate_opset(opset, 17)?;

        let input_tensor = match &node.inputs[0].ty {
            ArgType::Tensor(tensor) => tensor.clone(),
            _ => {
                return Err(ProcessError::TypeMismatch {
                    expected: "Tensor".to_string(),
                    actual: format!("{:?}", node.inputs[0].ty),
                });
            }
        };

        if input_tensor.rank < 2 {
            return Err(ProcessError::Custom(format!(
                "DFT: input must have rank >= 2 (got rank {}). \
                 The last dimension represents real/complex components.",
                input_tensor.rank
            )));
        }

        // Determine if input is real or complex from the trailing dimension.
        // The trailing dim must be statically known (1 = real, 2 = complex) because
        // the codegen strategy is fundamentally different for each case.
        let is_real_input = match &input_tensor.static_shape {
            Some(shape) => match shape.last() {
                Some(Some(1)) => true,
                Some(Some(2)) => false,
                Some(Some(d)) => {
                    return Err(ProcessError::Custom(format!(
                        "DFT: last dimension must be 1 (real) or 2 (complex), got {d}"
                    )));
                }
                _ => {
                    return Err(ProcessError::Custom(
                        "DFT: last dimension must be statically known as 1 (real) or 2 (complex). \
                         Ensure the ONNX model has static shapes on the DFT input."
                            .to_string(),
                    ));
                }
            },
            None => {
                return Err(ProcessError::Custom(
                    "DFT: input shape must be statically known. \
                     The last dimension determines real (1) vs complex (2) input."
                        .to_string(),
                ));
            }
        };

        // Extract attributes
        let inverse = node
            .attrs
            .get("inverse")
            .map(|v| v.clone().into_i64() != 0)
            .unwrap_or(false);

        let onesided = node
            .attrs
            .get("onesided")
            .map(|v| v.clone().into_i64() != 0)
            .unwrap_or(false);

        // Reject unsupported configurations early with clear errors
        if inverse {
            return Err(ProcessError::Custom(
                "DFT: inverse DFT (inverse=1) is not supported. \
                 Burn's irfft is the inverse of rfft (onesided real FFT), \
                 which differs from ONNX's complex-to-complex inverse DFT. \
                 A full ifft implementation in Burn is needed to support this."
                    .to_string(),
            ));
        }

        if !is_real_input {
            return Err(ProcessError::Custom(
                "DFT: complex-to-complex DFT is not supported by Burn's current signal API. \
                 Only real-input forward DFT (onesided or full) is supported."
                    .to_string(),
            ));
        }

        // Validate: complex input cannot be onesided (unreachable now, but kept for spec completeness)
        if !is_real_input && onesided {
            return Err(ProcessError::Custom(
                "DFT: onesided output is not possible with complex input".to_string(),
            ));
        }

        // Resolve axis (default is -2, i.e. last signal dimension)
        let axis = self.resolve_axis(node, &input_tensor, opset)?;

        // Try to extract dft_length for shape inference (if constant)
        let static_dft_length = match node.inputs.get(1) {
            Some(input) if !input.is_optional() => match input.value() {
                Some(data) => Some(extract_scalar_int(data, "dft_length")? as usize),
                None => None,
            },
            _ => None,
        };

        // Output is always complex: same shape but last dim = 2
        // The signal axis uses dft_length if provided, otherwise the input dimension.
        // If onesided, the signal axis dimension becomes floor(N/2) + 1.
        let out_rank = input_tensor.rank;
        let out_static_shape = if let Some(shape) = &input_tensor.static_shape {
            let mut out_shape = shape.clone();
            // Last dim is always 2 (complex output)
            *out_shape.last_mut().unwrap() = Some(2);

            // Effective DFT length: dft_length if provided, otherwise the input dim
            let effective_n = static_dft_length.or_else(|| out_shape.get(axis).copied().flatten());

            if let Some(n) = effective_n {
                out_shape[axis] = Some(if onesided { n / 2 + 1 } else { n });
            }

            Some(out_shape)
        } else {
            None
        };

        node.outputs[0].ty = ArgType::Tensor(TensorType {
            dtype: input_tensor.dtype,
            rank: out_rank,
            static_shape: out_static_shape,
        });

        Ok(())
    }

    fn extract_config(&self, node: &RawNode, opset: usize) -> Result<Self::Config, ProcessError> {
        let input_tensor = match &node.inputs[0].ty {
            ArgType::Tensor(tensor) => tensor.clone(),
            _ => {
                return Err(ProcessError::TypeMismatch {
                    expected: "Tensor".to_string(),
                    actual: format!("{:?}", node.inputs[0].ty),
                });
            }
        };

        let inverse = node
            .attrs
            .get("inverse")
            .map(|v| v.clone().into_i64() != 0)
            .unwrap_or(false);

        let onesided = node
            .attrs
            .get("onesided")
            .map(|v| v.clone().into_i64() != 0)
            .unwrap_or(false);

        let axis = self.resolve_axis(node, &input_tensor, opset)?;

        // Extract dft_length from input[1] if present
        let dft_length = match node.inputs.get(1) {
            Some(input) if !input.is_optional() => match input.value() {
                Some(data) => {
                    let val = extract_scalar_int(data, "dft_length")?;
                    if val <= 0 {
                        return Err(ProcessError::Custom(
                            "DFT: dft_length must be a positive integer".to_string(),
                        ));
                    }
                    Some(val as usize)
                }
                None => {
                    return Err(ProcessError::Custom(
                        "DFT: dft_length must be a compile-time constant".to_string(),
                    ));
                }
            },
            _ => None,
        };

        // Determine is_real_input from trailing dim (validated in infer_types)
        let is_real_input = match &input_tensor.static_shape {
            Some(shape) => matches!(shape.last(), Some(Some(1))),
            _ => true, // infer_types rejects unknown shapes, so this is a safe fallback
        };

        Ok(DftConfig {
            inverse,
            onesided,
            axis,
            dft_length,
            is_real_input,
        })
    }

    fn build_node(&self, builder: RawNode, opset: usize) -> Node {
        let config = self
            .extract_config(&builder, opset)
            .expect("Config extraction failed");

        Node::Dft(DftNode {
            name: builder.name,
            inputs: builder.inputs,
            outputs: builder.outputs,
            config,
        })
    }
}

impl DftProcessor {
    /// Resolve the axis parameter.
    ///
    /// In opset 17-19, axis is an attribute. In opset 20+, it moved to input[2].
    fn resolve_axis(
        &self,
        node: &RawNode,
        input_tensor: &TensorType,
        opset: usize,
    ) -> Result<usize, ProcessError> {
        let rank = input_tensor.rank as i64;

        let raw_axis: i64 = if opset < 20 {
            // Opset 17-19: axis is an attribute
            node.attrs
                .get("axis")
                .map(|v| v.clone().into_i64())
                .unwrap_or(-2)
        } else {
            // Opset 20+: axis is an optional input
            match node.inputs.get(2) {
                Some(input) if !input.is_optional() => match input.value() {
                    Some(data) => extract_scalar_int(data, "axis")?,
                    None => {
                        return Err(ProcessError::Custom(
                            "DFT: axis must be a compile-time constant".to_string(),
                        ));
                    }
                },
                _ => -2,
            }
        };

        // Validate axis range: [-r, -2] union [0, r-2]
        // The last dimension is reserved for real/complex encoding, so axis = -1 is invalid.
        let valid =
            (raw_axis >= -rank && raw_axis <= -2) || (raw_axis >= 0 && raw_axis <= rank - 2);
        if !valid {
            return Err(ProcessError::Custom(format!(
                "DFT: axis {raw_axis} out of valid range [-{rank}, -2] or [0, {}] for input rank {rank}",
                rank - 2
            )));
        }

        // Resolve negative axis
        let axis = if raw_axis < 0 {
            (rank + raw_axis) as usize
        } else {
            raw_axis as usize
        };

        Ok(axis)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::{DType, NodeType};
    use crate::node::test_utils::TestNodeBuilder;
    use crate::processor::OutputPreferences;

    #[test]
    fn test_dft_forward_real_onesided() {
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 3, Some(vec![1, 16, 1]))
            .add_input(
                "",
                ArgType::Tensor(TensorType {
                    dtype: DType::I64,
                    rank: 0,
                    static_shape: None,
                }),
            ) // optional dft_length
            .add_input(
                "",
                ArgType::Tensor(TensorType {
                    dtype: DType::I64,
                    rank: 0,
                    static_shape: None,
                }),
            ) // optional axis
            .output_tensor_f32("output", 0, None)
            .attr_int("onesided", 1)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        processor.infer_types(&mut node, 17, &prefs).unwrap();

        match &node.outputs[0].ty {
            ArgType::Tensor(t) => {
                assert_eq!(t.rank, 3);
                assert_eq!(t.dtype, DType::F32);
                // onesided: signal dim 16 -> 16/2+1 = 9, last dim = 2
                let shape = t.static_shape.as_ref().unwrap();
                assert_eq!(shape, &vec![Some(1), Some(9), Some(2)]);
            }
            _ => panic!("Expected Tensor output"),
        }
    }

    #[test]
    fn test_dft_forward_real_twosided() {
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 3, Some(vec![1, 16, 1]))
            .output_tensor_f32("output", 0, None)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        processor.infer_types(&mut node, 17, &prefs).unwrap();

        match &node.outputs[0].ty {
            ArgType::Tensor(t) => {
                assert_eq!(t.rank, 3);
                // Non-onesided: signal dim stays 16, last dim = 2
                let shape = t.static_shape.as_ref().unwrap();
                assert_eq!(shape, &vec![Some(1), Some(16), Some(2)]);
            }
            _ => panic!("Expected Tensor output"),
        }
    }

    #[test]
    fn test_dft_inverse_rejected() {
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 3, Some(vec![1, 16, 2]))
            .output_tensor_f32("output", 0, None)
            .attr_int("inverse", 1)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        let result = processor.infer_types(&mut node, 17, &prefs);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("inverse"));
    }

    #[test]
    fn test_dft_complex_input_rejected() {
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 3, Some(vec![1, 16, 2]))
            .output_tensor_f32("output", 0, None)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        let result = processor.infer_types(&mut node, 17, &prefs);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("complex-to-complex")
        );
    }

    #[test]
    fn test_dft_unknown_shape_rejected() {
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 3, None)
            .output_tensor_f32("output", 0, None)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        let result = processor.infer_types(&mut node, 17, &prefs);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("statically known"));
    }

    #[test]
    fn test_dft_rank_too_low() {
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 1, Some(vec![8]))
            .output_tensor_f32("output", 0, None)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        let result = processor.infer_types(&mut node, 17, &prefs);
        assert!(result.is_err());
    }

    #[test]
    fn test_dft_opset_too_low() {
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 3, Some(vec![1, 16, 1]))
            .output_tensor_f32("output", 0, None)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        let result = processor.infer_types(&mut node, 16, &prefs);
        assert!(result.is_err());
    }

    #[test]
    fn test_dft_preserves_dtype() {
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f64("input", 3, Some(vec![1, 16, 1]))
            .output_tensor_f32("output", 0, None)
            .attr_int("onesided", 1)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        processor.infer_types(&mut node, 17, &prefs).unwrap();

        match &node.outputs[0].ty {
            ArgType::Tensor(t) => assert_eq!(t.dtype, DType::F64),
            _ => panic!("Expected Tensor output"),
        }
    }

    #[test]
    fn test_dft_config_extraction() {
        let node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 3, Some(vec![1, 16, 1]))
            .output_tensor_f32("output", 0, None)
            .attr_int("inverse", 1)
            .attr_int("onesided", 1)
            .build();

        let processor = DftProcessor;
        let config = processor.extract_config(&node, 17).unwrap();

        assert!(config.inverse);
        assert!(config.onesided);
        assert_eq!(config.axis, 1); // default -2 on rank 3 = 1
        assert_eq!(config.dft_length, None);
        assert!(config.is_real_input);
    }

    #[test]
    fn test_dft_axis_out_of_range() {
        // Opset 17: axis is an attribute
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 3, Some(vec![1, 16, 1]))
            .output_tensor_f32("output", 0, None)
            .attr_int("axis", 2) // axis=2 is last dim (invalid for rank 3)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        let result = processor.infer_types(&mut node, 17, &prefs);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("axis"));
    }

    #[test]
    fn test_dft_axis_out_of_range_opset20() {
        // Opset 20: axis is an input
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 3, Some(vec![1, 16, 1]))
            .input_tensor_i64_data("dft_length", vec![16], vec![])
            .input_tensor_i64_data("axis", vec![2], vec![])
            .output_tensor_f32("output", 0, None)
            .build_with_graph_data(20);

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        let result = processor.infer_types(&mut node, 20, &prefs);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("axis"));
    }

    #[test]
    fn test_dft_with_axis_attribute_opset17() {
        // Opset 17: axis is an attribute
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 4, Some(vec![2, 8, 16, 1]))
            .output_tensor_f32("output", 0, None)
            .attr_int("onesided", 1)
            .attr_int("axis", 1)
            .build();

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        processor.infer_types(&mut node, 17, &prefs).unwrap();

        match &node.outputs[0].ty {
            ArgType::Tensor(t) => {
                assert_eq!(t.rank, 4);
                let shape = t.static_shape.as_ref().unwrap();
                assert_eq!(shape, &vec![Some(2), Some(5), Some(16), Some(2)]);
            }
            _ => panic!("Expected Tensor output"),
        }
    }

    #[test]
    fn test_dft_with_axis_input_opset20() {
        // Opset 20: axis is an input
        let mut node = TestNodeBuilder::new(NodeType::Dft, "test_dft")
            .input_tensor_f32("input", 4, Some(vec![2, 8, 16, 1]))
            .add_input(
                "",
                ArgType::Tensor(TensorType {
                    dtype: DType::I64,
                    rank: 0,
                    static_shape: None,
                }),
            ) // optional dft_length
            .input_tensor_i64_data("axis", vec![1], vec![])
            .output_tensor_f32("output", 0, None)
            .attr_int("onesided", 1)
            .build_with_graph_data(20);

        let processor = DftProcessor;
        let prefs = OutputPreferences::new();
        processor.infer_types(&mut node, 20, &prefs).unwrap();

        match &node.outputs[0].ty {
            ArgType::Tensor(t) => {
                assert_eq!(t.rank, 4);
                // axis=1, dim=8, onesided -> 8/2+1=5
                let shape = t.static_shape.as_ref().unwrap();
                assert_eq!(shape, &vec![Some(2), Some(5), Some(16), Some(2)]);
            }
            _ => panic!("Expected Tensor output"),
        }
    }
}