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
//! # Conv (2D)
//!
//! 2D convolution operation.
//!
//! **ONNX Spec**: <https://onnx.ai/onnx/operators/onnx__Conv.html>
//!
//! ## Opset Versions
//! - **Opset 1**: Initial version with basic convolution support
//! - **Opset 11**: No changes to Conv operator itself (broader ONNX updates)

use derive_new::new;
use onnx_ir_derive::NodeBuilder;

use crate::ir::{ArgType, Argument, Node, RawNode, TensorType};
use crate::node::padding::{AutoPad, PaddingConfig2d, padding_config_2d};
use crate::processor::{
    InputSpec, NodeProcessor, NodeSpec, OutputPreferences, OutputSpec, ProcessError,
};

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

/// Configuration for Conv2d operations
#[derive(Debug, Clone, new)]
#[allow(clippy::too_many_arguments)]
pub struct Conv2dConfig {
    /// Kernel size [height, width]
    pub kernel_size: [usize; 2],
    /// Stride [height, width]
    pub stride: [usize; 2],
    /// Padding configuration
    pub padding: PaddingConfig2d,
    /// Dilation [height, width]
    pub dilation: [usize; 2],
    /// Number of groups
    pub groups: usize,
    /// Auto padding mode
    pub auto_pad: AutoPad,
}

/// Node processor for Conv2d operation
pub(crate) struct Conv2dProcessor;

impl NodeProcessor for Conv2dProcessor {
    type Config = Conv2dConfig;

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

    fn lift_constants(&self, node: &mut RawNode, _opset: usize) -> Result<(), ProcessError> {
        // Lift weight (input[1]) and optional bias (input[2])
        if node.inputs.len() > 1 && node.inputs[1].is_constant() {
            node.inputs[1].to_static()?;
        }
        if node.inputs.len() > 2 && node.inputs[2].is_constant() {
            node.inputs[2].to_static()?;
        }

        Ok(())
    }

    fn infer_types(
        &self,
        node: &mut RawNode,
        _opset: usize,
        _output_preferences: &OutputPreferences,
    ) -> Result<(), ProcessError> {
        // TODO: Add test for zero or negative stride values - spec requires positive strides
        // TODO: Add test for zero or negative dilation values - spec requires positive dilations
        // TODO: Add test for zero or negative group values - spec requires positive groups
        // TODO: Validate channels_in divisible by groups - required by spec but not validated
        // TODO: Validate channels_out divisible by groups - required by spec but not validated
        // TODO: Add test for very large kernel_shape/stride/dilation - potential overflow/memory issues
        // TODO: Add test coverage for auto_pad values: SAME_UPPER, SAME_LOWER, VALID - currently unsupported
        // TODO: Add test for asymmetric kernel shapes (e.g., [3, 5]) - valid but may not be tested

        // Validate attributes before extracting config
        for (key, value) in node.attrs.iter() {
            match key.as_str() {
                "kernel_shape" | "strides" | "pads" | "dilations" | "group" => {}
                "auto_pad" => {
                    AutoPad::parse(&value.clone().into_string())?;
                }
                _ => {
                    return Err(ProcessError::InvalidAttribute {
                        name: key.clone(),
                        reason: format!("Unexpected attribute for Conv2d: {key}"),
                    });
                }
            }
        }

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

        // Validate input tensor rank - Conv2d expects rank 4 (N x C x H x W)
        if tensor.rank != 4 {
            return Err(ProcessError::Custom(format!(
                "Conv2d expects input tensor of rank 4 (N x C x H x W), got rank {}",
                tensor.rank
            )));
        }

        // Validate weight tensor type and rank
        let weight_tensor = match &node.inputs[1].ty {
            ArgType::Tensor(tensor) => tensor,
            _ => {
                return Err(ProcessError::TypeMismatch {
                    expected: "Tensor (weight)".to_string(),
                    actual: format!("{:?}", node.inputs[1].ty),
                });
            }
        };

        // Weight should be rank 4 (M x C/group x kH x kW)
        if weight_tensor.rank != 4 {
            return Err(ProcessError::Custom(format!(
                "Conv2d expects weight tensor of rank 4 (M x C/group x kH x kW), got rank {}",
                weight_tensor.rank
            )));
        }

        // Validate dtypes match
        if tensor.dtype != weight_tensor.dtype {
            return Err(ProcessError::TypeMismatch {
                expected: format!("Weight tensor with dtype {:?}", tensor.dtype),
                actual: format!("Weight tensor with dtype {:?}", weight_tensor.dtype),
            });
        }

        // Validate bias if present
        if node.inputs.len() > 2 {
            let bias_tensor = match &node.inputs[2].ty {
                ArgType::Tensor(tensor) => tensor,
                _ => {
                    return Err(ProcessError::TypeMismatch {
                        expected: "Tensor (bias)".to_string(),
                        actual: format!("{:?}", node.inputs[2].ty),
                    });
                }
            };

            // Bias should be rank 1 (M)
            if bias_tensor.rank != 1 {
                return Err(ProcessError::Custom(format!(
                    "Conv2d expects bias tensor of rank 1 (M), got rank {}",
                    bias_tensor.rank
                )));
            }

            // Validate bias dtype matches
            if tensor.dtype != bias_tensor.dtype {
                return Err(ProcessError::TypeMismatch {
                    expected: format!("Bias tensor with dtype {:?}", tensor.dtype),
                    actual: format!("Bias tensor with dtype {:?}", bias_tensor.dtype),
                });
            }
        }

        // Compute output static_shape: [batch, out_channels, H_out, W_out]
        let static_shape = {
            let batch = tensor
                .static_shape
                .as_ref()
                .and_then(|s| s.first().copied().flatten());
            let out_channels = node.inputs[1]
                .value()
                .and_then(|data| data.shape.first().copied())
                .or_else(|| {
                    weight_tensor
                        .static_shape
                        .as_ref()
                        .and_then(|s| s.first().copied().flatten())
                });

            let compute_spatial = |dim_idx: usize,
                                   kernel: usize,
                                   stride: usize,
                                   dilation: usize,
                                   pad_begin: usize,
                                   pad_end: usize|
             -> Option<usize> {
                let input_dim = tensor
                    .static_shape
                    .as_ref()
                    .and_then(|s| s.get(dim_idx).copied().flatten())?;
                let padding = pad_begin + pad_end;
                let numerator = input_dim as isize + padding as isize
                    - dilation as isize * (kernel as isize - 1)
                    - 1;
                if numerator < 0 || stride == 0 {
                    return None;
                }
                Some(numerator as usize / stride + 1)
            };

            let spatial = self.extract_config(node, _opset).ok().map(|config| {
                let (pad_top, pad_left, pad_bottom, pad_right) = config.padding.as_tuple();
                let h_out = compute_spatial(
                    2,
                    config.kernel_size[0],
                    config.stride[0],
                    config.dilation[0],
                    pad_top,
                    pad_bottom,
                );
                let w_out = compute_spatial(
                    3,
                    config.kernel_size[1],
                    config.stride[1],
                    config.dilation[1],
                    pad_left,
                    pad_right,
                );
                (h_out, w_out)
            });
            let (h_out, w_out) = spatial.unwrap_or((None, None));
            Some(vec![batch, out_channels, h_out, w_out])
        };

        // Conv2d preserves rank (same as input)
        node.outputs[0].ty = ArgType::Tensor(TensorType {
            dtype: tensor.dtype,
            rank: tensor.rank,
            static_shape,
        });

        Ok(())
    }

    fn extract_config(&self, node: &RawNode, _opset: usize) -> Result<Self::Config, ProcessError> {
        let mut kernel_shape = Vec::new();
        let mut strides = vec![1, 1];
        let mut pads = vec![0, 0, 0, 0];
        let mut dilations = vec![1, 1];
        let mut group: usize = 1;
        let mut auto_pad = AutoPad::NotSet;

        let weight_shape = node.inputs[1]
            .value()
            .ok_or_else(|| {
                ProcessError::Custom("Conv2d: weight tensor must be present".to_string())
            })?
            .shape
            .to_vec();

        for (key, value) in node.attrs.iter() {
            match key.as_str() {
                "kernel_shape" => kernel_shape = value.clone().into_i64s(),
                "strides" => strides = value.clone().into_i64s(),
                "pads" => pads = value.clone().into_i64s(),
                "dilations" => dilations = value.clone().into_i64s(),
                "group" => group = value.clone().into_i64() as usize,
                "auto_pad" => auto_pad = AutoPad::parse(&value.clone().into_string())?,
                _ => {}
            }
        }

        let padding = padding_config_2d(&pads);

        let kernel_size = if kernel_shape.is_empty() {
            if weight_shape.len() != 4 {
                return Err(ProcessError::Custom(format!(
                    "Conv2d: expected to infer kernel shape from a weight tensor of rank 4 but got shape {:?}",
                    weight_shape
                )));
            }
            [weight_shape[2], weight_shape[3]]
        } else {
            [kernel_shape[0] as _, kernel_shape[1] as _]
        };

        let config = Conv2dConfig::new(
            kernel_size,
            [strides[0] as usize, strides[1] as usize],
            padding,
            [dilations[0] as usize, dilations[1] as usize],
            group,
            auto_pad,
        );

        Ok(config)
    }

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

        Node::Conv2d(Conv2dNode {
            name: builder.name,
            inputs: builder.inputs,
            outputs: builder.outputs,
            config,
        })
    }
}

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

    fn create_test_node(
        kernel_shape: Vec<i64>,
        strides: Vec<i64>,
        pads: Vec<i64>,
        dilations: Vec<i64>,
        group: i64,
        has_bias: bool,
        auto_pad: Option<&str>,
    ) -> TestNodeBuilder {
        // Weight tensor data - not important for the test
        // [output_channels, input_channels/groups, k_h, k_w]
        let weight_shape = vec![4, 2, 2, 2];
        let weight_data = vec![0.0; 32]; // 4*2*2*2 = 32

        let has_kernel_shape = !kernel_shape.is_empty();

        let mut builder = TestNodeBuilder::new(NodeType::Conv2d, "test_conv2d")
            .input_tensor_f32("data", 4, None)
            .input_tensor_f32_data("weight", weight_data.clone(), weight_shape)
            .output_tensor_f32("output", 4, None)
            .attr_ints("strides", strides)
            .attr_ints("pads", pads)
            .attr_ints("dilations", dilations)
            .attr_int("group", group);

        if has_kernel_shape {
            builder = builder.attr_ints("kernel_shape", kernel_shape);
        }

        if has_bias {
            builder = builder.input_tensor_f32("bias", 1, None);
        }

        if let Some(auto_pad) = auto_pad {
            builder = builder.attr_string("auto_pad", auto_pad);
        }

        builder
    }

    #[test]
    fn test_conv2d_config_basic() {
        let node = create_test_node(
            vec![2, 2],
            vec![1, 1],
            vec![0, 0, 0, 0],
            vec![1, 1],
            1,
            false,
            None,
        )
        .build_with_graph_data(16);
        let mut node = node;
        let processor = Conv2dProcessor;
        let prefs = OutputPreferences::new();
        let config = processor.extract_config(&node, 16).unwrap();
        processor.infer_types(&mut node, 16, &prefs).unwrap();

        assert_eq!(config.kernel_size, [2, 2]);
        assert_eq!(config.stride, [1, 1]);
        assert_eq!(config.dilation, [1, 1]);
        assert_eq!(config.groups, 1);
        assert!(matches!(config.padding, PaddingConfig2d::Valid));
    }

    #[test]
    fn test_conv2d_config_with_padding() {
        let node = create_test_node(
            vec![3, 3],
            vec![1, 1],
            vec![1, 1, 1, 1],
            vec![1, 1],
            1,
            false,
            None,
        )
        .build_with_graph_data(16);
        let mut node = node;
        let processor = Conv2dProcessor;
        let prefs = OutputPreferences::new();
        let config = processor.extract_config(&node, 16).unwrap();
        processor.infer_types(&mut node, 16, &prefs).unwrap();

        assert_eq!(config.kernel_size, [3, 3]);
        assert!(matches!(
            config.padding,
            PaddingConfig2d::Explicit(1, 1, 1, 1)
        ));
    }

    #[test]
    fn test_conv2d_config_with_groups() {
        let node = create_test_node(
            vec![2, 2],
            vec![1, 1],
            vec![0, 0, 0, 0],
            vec![1, 1],
            2,
            false,
            None,
        )
        .build_with_graph_data(16);
        let mut node = node;
        let processor = Conv2dProcessor;
        let prefs = OutputPreferences::new();
        let config = processor.extract_config(&node, 16).unwrap();
        processor.infer_types(&mut node, 16, &prefs).unwrap();

        assert_eq!(config.groups, 2);
    }

    #[test]
    fn test_conv2d_config_autopad_not_set() {
        let node = create_test_node(
            vec![3, 3],
            vec![1, 1],
            vec![1, 1, 1, 1],
            vec![1, 1],
            1,
            false,
            Some("NOTSET"),
        )
        .build_with_graph_data(16);
        let mut node = node;
        let processor = Conv2dProcessor;
        let prefs = OutputPreferences::new();
        let config = processor.extract_config(&node, 16).unwrap();
        processor.infer_types(&mut node, 16, &prefs).unwrap();

        assert_eq!(config.kernel_size, [3, 3]);
        assert!(matches!(
            config.padding,
            PaddingConfig2d::Explicit(1, 1, 1, 1)
        ));
    }

    #[test]
    fn test_conv2d_config_autopad_same_upper() {
        let node = create_test_node(
            vec![3, 3],
            vec![1, 1],
            vec![1, 1, 1, 1],
            vec![1, 1],
            1,
            false,
            Some("SAME_UPPER"),
        )
        .build_with_graph_data(16);
        let mut node = node;
        let processor = Conv2dProcessor;
        let prefs = OutputPreferences::new();
        processor.infer_types(&mut node, 16, &prefs).unwrap();
        let config = processor.extract_config(&node, 16).unwrap();
        assert_eq!(config.auto_pad, AutoPad::SameUpper);
    }

    #[test]
    fn test_conv2d_config_kernel_shape_not_set() {
        let node = create_test_node(
            vec![],
            vec![1, 1],
            vec![0, 0, 0, 0],
            vec![1, 1],
            1,
            false,
            None,
        )
        .build_with_graph_data(16);
        let mut node = node;
        let processor = Conv2dProcessor;
        let prefs = OutputPreferences::new();
        let config = processor.extract_config(&node, 16).unwrap();
        processor.infer_types(&mut node, 16, &prefs).unwrap();

        assert_eq!(config.kernel_size, [2, 2]); // Inferred via weight tensor shape
    }

    #[test]
    fn test_conv2d_static_shape_known() {
        // Input [1, 2, 8, 8], weight [4, 2, 2, 2], stride=[1,1], pad=0, dilation=[1,1]
        // H_out = (8 + 0 - 1*(2-1) - 1) / 1 + 1 = (8 - 1 - 1) / 1 + 1 = 7
        // W_out = same = 7
        let mut node = TestNodeBuilder::new(NodeType::Conv2d, "test")
            .input_tensor_f32("data", 4, Some(vec![1, 2, 8, 8]))
            .input_tensor_f32_data("weight", vec![0.0; 32], vec![4, 2, 2, 2])
            .output_tensor_f32("output", 4, None)
            .attr_ints("kernel_shape", vec![2, 2])
            .attr_ints("strides", vec![1, 1])
            .attr_ints("pads", vec![0, 0, 0, 0])
            .attr_ints("dilations", vec![1, 1])
            .attr_int("group", 1)
            .build_with_graph_data(16);

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

        match &node.outputs[0].ty {
            ArgType::Tensor(t) => {
                assert_eq!(t.rank, 4);
                assert_eq!(
                    t.static_shape,
                    Some(vec![Some(1), Some(4), Some(7), Some(7)])
                );
            }
            _ => panic!("Expected tensor output"),
        }
    }

    #[test]
    fn test_conv2d_static_shape_with_padding() {
        // Input [1, 2, 8, 8], weight [4, 2, 3, 3], stride=[1,1], pad=[1,1,1,1], dilation=[1,1]
        // H_out = (8 + 2 - 1*(3-1) - 1) / 1 + 1 = (8 + 2 - 2 - 1) / 1 + 1 = 8
        let mut node = TestNodeBuilder::new(NodeType::Conv2d, "test")
            .input_tensor_f32("data", 4, Some(vec![1, 2, 8, 8]))
            .input_tensor_f32_data("weight", vec![0.0; 72], vec![4, 2, 3, 3])
            .output_tensor_f32("output", 4, None)
            .attr_ints("kernel_shape", vec![3, 3])
            .attr_ints("strides", vec![1, 1])
            .attr_ints("pads", vec![1, 1, 1, 1])
            .attr_ints("dilations", vec![1, 1])
            .attr_int("group", 1)
            .build_with_graph_data(16);

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

        match &node.outputs[0].ty {
            ArgType::Tensor(t) => {
                assert_eq!(t.rank, 4);
                assert_eq!(
                    t.static_shape,
                    Some(vec![Some(1), Some(4), Some(8), Some(8)])
                );
            }
            _ => panic!("Expected tensor output"),
        }
    }

    #[test]
    fn test_conv2d_static_shape_no_input_shape() {
        // No input static_shape -> batch and spatial are None, out_channels is known
        let node = create_test_node(
            vec![2, 2],
            vec![1, 1],
            vec![0, 0, 0, 0],
            vec![1, 1],
            1,
            false,
            None,
        )
        .build_with_graph_data(16);
        let mut node = node;
        let processor = Conv2dProcessor;
        let prefs = OutputPreferences::new();
        processor.infer_types(&mut node, 16, &prefs).unwrap();

        match &node.outputs[0].ty {
            ArgType::Tensor(t) => {
                assert_eq!(t.rank, 4);
                assert_eq!(t.static_shape, Some(vec![None, Some(4), None, None]));
            }
            _ => panic!("Expected tensor output"),
        }
    }
}