onnx-ir 0.19.1

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
use crate::ir::{
    ArgType, Argument, AttributeValue, Data, ElementType, Node, NodeType, TensorData, TensorType,
};
use std::collections::HashMap;

/// Builder for creating test node instances with convenient defaults and simple API.
pub struct NodeBuilder {
    node_type: NodeType,
    name: String,
    inputs: Vec<Argument>,
    outputs: Vec<Argument>,
    attrs: HashMap<String, AttributeValue>,
}

impl NodeBuilder {
    /// Create a new builder with the specified node type and name
    pub fn new(node_type: NodeType, name: &str) -> Self {
        Self {
            node_type,
            name: name.to_string(),
            inputs: Vec::new(),
            outputs: Vec::new(),
            attrs: HashMap::new(),
        }
    }

    /// Add a generic input with the given name and type
    ///
    /// Note: Prefer using the specialized methods like `input_tensor_f32`,
    /// `input_scalar_f32`, etc. for better readability and type safety.
    #[doc(hidden)]
    pub fn add_input(mut self, name: &str, ty: ArgType) -> Self {
        self.inputs.push(Argument {
            name: name.to_string(),
            ty,
            value: None,
            passed: true,
        });
        self
    }

    /// Add a float32 tensor input with the given name and rank
    pub fn input_tensor_f32(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_input(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Float32,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a float64 tensor input with the given name and rank
    pub fn input_tensor_f64(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_input(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Float64,
                rank,
                static_shape,
            }),
        )
    }

    /// Add an int32 tensor input with the given name and rank
    pub fn input_tensor_i32(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_input(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Int32,
                rank,
                static_shape,
            }),
        )
    }

    /// Add an int64 tensor input with the given name and rank
    pub fn input_tensor_i64(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_input(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Int64,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a bool tensor input with the given name and rank
    pub fn input_tensor_bool(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_input(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Bool,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a float16 tensor input with the given name and rank
    pub fn input_tensor_f16(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_input(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Float16,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a string tensor input with the given name and rank
    pub fn input_tensor_string(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_input(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::String,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a scalar input with the given name and element type
    pub fn input_scalar(self, name: &str, elem_type: ElementType) -> Self {
        self.add_input(name, ArgType::Scalar(elem_type))
    }

    /// Add a float32 scalar input with the given name
    pub fn input_scalar_f32(self, name: &str) -> Self {
        self.input_scalar(name, ElementType::Float32)
    }

    /// Add an int64 scalar input with the given name
    pub fn input_scalar_i64(self, name: &str) -> Self {
        self.input_scalar(name, ElementType::Int64)
    }

    /// Add a shape input with the given name and rank
    pub fn input_shape(self, name: &str, rank: usize) -> Self {
        self.add_input(name, ArgType::Shape(rank))
    }

    /// Add a tensor input with data value
    pub fn input_tensor_with_data(
        mut self,
        name: &str,
        elem_type: ElementType,
        rank: usize,
        data: Data,
        shape: Vec<usize>,
    ) -> Self {
        let arg = Argument {
            name: name.to_string(),
            ty: ArgType::Tensor(TensorType {
                elem_type,
                rank,
                static_shape: None,
            }),
            value: Some(TensorData { data, shape }),
            passed: true,
        };
        self.inputs.push(arg);
        self
    }

    /// Add a float32 tensor input with data values
    pub fn input_tensor_f32_data(self, name: &str, data: Vec<f32>, shape: Vec<usize>) -> Self {
        self.input_tensor_with_data(
            name,
            ElementType::Float32,
            shape.len(),
            Data::Float32s(data),
            shape,
        )
    }

    /// Add an int64 tensor input with data values
    pub fn input_tensor_i64_data(self, name: &str, data: Vec<i64>, shape: Vec<usize>) -> Self {
        self.input_tensor_with_data(
            name,
            ElementType::Int64,
            shape.len(),
            Data::Int64s(data),
            shape,
        )
    }

    /// Add a float32 scalar tensor input (rank 0)
    pub fn input_scalar_tensor_f32(mut self, name: &str, value: Option<f32>) -> Self {
        let arg = Argument {
            name: name.to_string(),
            ty: ArgType::Tensor(TensorType {
                elem_type: ElementType::Float32,
                rank: 0,
                static_shape: None,
            }),
            value: value.map(|val| TensorData {
                data: Data::Float32(val),
                shape: vec![],
            }),
            passed: true,
        };
        self.inputs.push(arg);
        self
    }

    /// Add an int64 scalar tensor input (rank 0)
    pub fn input_scalar_tensor_i64(mut self, name: &str, value: i64) -> Self {
        let arg = Argument {
            name: name.to_string(),
            ty: ArgType::Tensor(TensorType {
                elem_type: ElementType::Int64,
                rank: 0,
                static_shape: None,
            }),
            value: Some(TensorData {
                data: Data::Int64(value),
                shape: vec![],
            }),
            passed: true,
        };
        self.inputs.push(arg);
        self
    }

    /// Add multiple tensor inputs with the same type but different names
    pub fn input_tensors_f32<I>(
        mut self,
        name_prefix: &str,
        count: usize,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        for i in 0..count {
            self = self.input_tensor_f32(&format!("{name_prefix}_{i}"), rank, static_shape.clone());
        }
        self
    }

    /// Add a generic output with the given name and type
    ///
    /// Note: Prefer using the specialized methods like `output_tensor_f32`,
    /// `output_scalar_f32`, etc. for better readability and type safety.
    #[doc(hidden)]
    pub fn add_output(mut self, name: &str, ty: ArgType) -> Self {
        self.outputs.push(Argument {
            name: name.to_string(),
            ty,
            value: None,
            passed: true,
        });
        self
    }

    /// Add a float32 tensor output with the given name and rank
    pub fn output_tensor_f32(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_output(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Float32,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a float64 tensor output with the given name and rank
    pub fn output_tensor_f64(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_output(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Float64,
                rank,
                static_shape,
            }),
        )
    }

    /// Add an int32 tensor output with the given name and rank
    pub fn output_tensor_i32(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_output(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Int32,
                rank,
                static_shape,
            }),
        )
    }

    /// Add an int64 tensor output with the given name and rank
    pub fn output_tensor_i64(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_output(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Int64,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a bool tensor output with the given name and rank
    pub fn output_tensor_bool(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_output(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Bool,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a float16 tensor output with the given name and rank
    pub fn output_tensor_f16(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_output(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::Float16,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a string tensor output with the given name and rank
    pub fn output_tensor_string(
        self,
        name: &str,
        rank: usize,
        static_shape: Option<Vec<usize>>,
    ) -> Self {
        self.add_output(
            name,
            ArgType::Tensor(TensorType {
                elem_type: ElementType::String,
                rank,
                static_shape,
            }),
        )
    }

    /// Add a scalar output with the given name and element type
    pub fn output_scalar(self, name: &str, elem_type: ElementType) -> Self {
        self.add_output(name, ArgType::Scalar(elem_type))
    }

    /// Add a float32 scalar output with the given name
    pub fn output_scalar_f32(self, name: &str) -> Self {
        self.output_scalar(name, ElementType::Float32)
    }

    /// Add an int64 scalar output with the given name
    pub fn output_scalar_i64(self, name: &str) -> Self {
        self.output_scalar(name, ElementType::Int64)
    }

    /// Add a shape output with the given name and rank
    pub fn output_shape(self, name: &str, rank: usize) -> Self {
        self.add_output(name, ArgType::Shape(rank))
    }

    /// Add an integer attribute
    pub fn attr_int(mut self, name: &str, value: i64) -> Self {
        self.attrs
            .insert(name.to_string(), AttributeValue::Int64(value));
        self
    }

    /// Add a float attribute
    pub fn attr_float(mut self, name: &str, value: f32) -> Self {
        self.attrs
            .insert(name.to_string(), AttributeValue::Float32(value));
        self
    }

    /// Add a string attribute
    pub fn attr_string(mut self, name: &str, value: &str) -> Self {
        self.attrs
            .insert(name.to_string(), AttributeValue::String(value.to_string()));
        self
    }

    /// Add an integer array attribute
    pub fn attr_ints(mut self, name: &str, values: Vec<i64>) -> Self {
        self.attrs
            .insert(name.to_string(), AttributeValue::Int64s(values));
        self
    }

    /// Add a float array attribute
    pub fn attr_floats(mut self, name: &str, values: Vec<f32>) -> Self {
        self.attrs
            .insert(name.to_string(), AttributeValue::Float32s(values));
        self
    }

    /// Add a string array attribute
    pub fn attr_strings(mut self, name: &str, values: Vec<String>) -> Self {
        self.attrs
            .insert(name.to_string(), AttributeValue::Strings(values));
        self
    }

    /// Add a tensor attribute
    pub fn attr_tensor(mut self, name: &str, tensor: TensorData) -> Self {
        self.attrs
            .insert(name.to_string(), AttributeValue::Tensor(tensor));
        self
    }

    /// Add a default output with the given name
    pub fn output_default(mut self, name: &str) -> Self {
        self.outputs.push(Argument {
            name: name.to_string(),
            ty: ArgType::default(),
            value: None,
            passed: true,
        });
        self
    }

    /// Build the node
    pub fn build(self) -> Node {
        Node {
            node_type: self.node_type,
            name: self.name,
            inputs: self.inputs,
            outputs: self.outputs,
            attrs: self.attrs,
        }
    }
}