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
use crate::ir::{
ArgType, Argument, AttributeValue, BoolStore, DType, NodeType, RawNode, TensorData, TensorType,
};
use std::collections::HashMap;
/// Builder for creating test node instances with convenient defaults and simple API.
pub struct TestNodeBuilder {
pub(crate) node_type: NodeType,
pub(crate) name: String,
pub(crate) inputs: Vec<Argument>,
pub(crate) outputs: Vec<Argument>,
pub(crate) attrs: HashMap<String, AttributeValue>,
/// Stores constant data for inputs that should be constants (input_name -> (data, shape))
pub(crate) constant_data: HashMap<String, TensorData>,
}
impl TestNodeBuilder {
/// 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(),
constant_data: 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 {
// In ONNX protobuf, optional inputs are represented by empty names
let value_source = if name.is_empty() {
crate::ir::ValueSource::Optional
} else {
crate::ir::ValueSource::Dynamic
};
self.inputs.push(Argument {
name: name.to_string(),
ty,
value_source,
value_store: None,
});
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_input(
name,
ArgType::Tensor(TensorType {
dtype: DType::F32,
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_input(
name,
ArgType::Tensor(TensorType {
dtype: DType::F64,
rank,
static_shape,
}),
)
}
/// Add an int8 tensor input with the given name and rank
pub fn input_tensor_i8(
self,
name: &str,
rank: usize,
static_shape: Option<Vec<usize>>,
) -> Self {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_input(
name,
ArgType::Tensor(TensorType {
dtype: DType::I8,
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_input(
name,
ArgType::Tensor(TensorType {
dtype: DType::I32,
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_input(
name,
ArgType::Tensor(TensorType {
dtype: DType::I64,
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_input(
name,
ArgType::Tensor(TensorType {
dtype: DType::Bool(BoolStore::Native),
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_input(
name,
ArgType::Tensor(TensorType {
dtype: DType::F16,
rank,
static_shape,
}),
)
}
/// Add a bfloat16 tensor input with the given name and rank
pub fn input_tensor_bf16(
self,
name: &str,
rank: usize,
static_shape: Option<Vec<usize>>,
) -> Self {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_input(
name,
ArgType::Tensor(TensorType {
dtype: DType::BF16,
rank,
static_shape,
}),
)
}
/// Add a scalar input with the given name and data type
pub fn input_scalar(self, name: &str, dtype: DType) -> Self {
self.add_input(name, ArgType::ScalarNative(dtype))
}
/// Add a float32 scalar input with the given name
pub fn input_scalar_f32(self, name: &str) -> Self {
self.input_scalar(name, DType::F32)
}
/// Add an int64 scalar input with the given name
pub fn input_scalar_i64(self, name: &str) -> Self {
self.input_scalar(name, DType::I64)
}
/// 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 shape input that carries a static value (i64 vector). Mirrors
/// `input_tensor_with_data` for the ArgType::Shape case: the argument is
/// flagged as a constant and the data is registered in GraphState so
/// `arg.value()` resolves once `build_with_graph_data` runs.
pub fn input_shape_with_data(mut self, name: &str, data: Vec<i64>) -> Self {
let len = data.len();
let arg = Argument {
name: name.to_string(),
ty: ArgType::Shape(len),
value_source: crate::ir::ValueSource::Constant,
value_store: None,
};
self.inputs.push(arg);
self.constant_data
.insert(name.to_string(), TensorData::new(data, vec![len]));
self
}
/// Add a tensor input with data value
///
/// Note: In the new design, constant values are stored in GraphState, not in Arguments.
/// This method creates the argument without the value field. If you need the value
/// in GraphState for testing, you'll need to add it separately.
pub fn input_tensor_with_data(
mut self,
name: &str,
dtype: DType,
rank: usize,
tensor_data: TensorData,
) -> Self {
let arg = Argument {
name: name.to_string(),
ty: ArgType::Tensor(TensorType {
dtype,
rank,
static_shape: None,
}),
value_source: crate::ir::ValueSource::Constant,
value_store: None,
};
self.inputs.push(arg);
// Store the constant data for later registration in GraphState
self.constant_data.insert(name.to_string(), tensor_data);
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 {
let tensor_data = TensorData::new(data, shape.clone());
self.input_tensor_with_data(name, DType::F32, shape.len(), tensor_data)
}
/// Add an int64 tensor input with data values
pub fn input_tensor_i64_data(self, name: &str, data: Vec<i64>, shape: Vec<usize>) -> Self {
let tensor_data = TensorData::new(data, shape.clone());
self.input_tensor_with_data(name, DType::I64, shape.len(), tensor_data)
}
/// Add a float32 scalar tensor input (rank 0)
///
/// Note: In the new design, constant values are stored in GraphState, not in Arguments.
/// This method creates the argument without the value field.
pub fn input_scalar_tensor_f32(mut self, name: &str, value: Option<f32>) -> Self {
let value_source = if value.is_some() {
crate::ir::ValueSource::Constant
} else {
crate::ir::ValueSource::Dynamic
};
let arg = Argument {
name: name.to_string(),
ty: ArgType::Tensor(TensorType {
dtype: DType::F32,
rank: 0,
static_shape: None,
}),
value_source,
value_store: None,
};
self.inputs.push(arg);
// If value is provided, store it as constant data
if let Some(v) = value {
self.constant_data
.insert(name.to_string(), TensorData::new(vec![v], [0usize; 0]));
}
self
}
/// Add an int64 scalar tensor input (rank 0)
///
/// Note: In the new design, constant values are stored in GraphState, not in Arguments.
/// This method creates the argument without the value field.
pub fn input_scalar_tensor_i64(mut self, name: &str, value: Option<i64>) -> Self {
let value_source = if value.is_some() {
crate::ir::ValueSource::Constant
} else {
crate::ir::ValueSource::Dynamic
};
let arg = Argument {
name: name.to_string(),
ty: ArgType::Tensor(TensorType {
dtype: DType::I64,
rank: 0,
static_shape: None,
}),
value_source,
value_store: None,
};
self.inputs.push(arg);
// If value is provided, store it as constant data
if let Some(v) = value {
self.constant_data
.insert(name.to_string(), TensorData::new(vec![v], [0usize; 0]));
}
self
}
/// Add multiple tensor inputs with the same type but different names
pub fn input_tensors_f32(
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_source: crate::ir::ValueSource::Dynamic,
value_store: None,
});
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_output(
name,
ArgType::Tensor(TensorType {
dtype: DType::F32,
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_output(
name,
ArgType::Tensor(TensorType {
dtype: DType::F64,
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_output(
name,
ArgType::Tensor(TensorType {
dtype: DType::I32,
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_output(
name,
ArgType::Tensor(TensorType {
dtype: DType::I64,
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_output(
name,
ArgType::Tensor(TensorType {
dtype: DType::Bool(BoolStore::Native),
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 {
let static_shape = static_shape.map(|s| s.into_iter().map(Some).collect());
self.add_output(
name,
ArgType::Tensor(TensorType {
dtype: DType::F16,
rank,
static_shape,
}),
)
}
/// Add a scalar output with the given name and data type
pub fn output_scalar(self, name: &str, dtype: DType) -> Self {
self.add_output(name, ArgType::ScalarNative(dtype))
}
/// Add a float32 scalar output with the given name
pub fn output_scalar_f32(self, name: &str) -> Self {
self.output_scalar(name, DType::F32)
}
/// Add an int64 scalar output with the given name
pub fn output_scalar_i64(self, name: &str) -> Self {
self.output_scalar(name, DType::I64)
}
/// 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_source: crate::ir::ValueSource::Dynamic,
value_store: None,
});
self
}
/// Build the node and process it with the given processor.
pub(crate) fn process<P: crate::processor::NodeProcessor>(
self,
processor: P,
opset: usize,
) -> RawNode {
use crate::processor::OutputPreferences;
let mut node = self.build_with_graph_data(opset);
let prefs = OutputPreferences::new();
// Run type inference
let _ = processor.infer_types(&mut node, opset, &prefs);
node
}
/// Build the node
pub(crate) fn build(self) -> RawNode {
RawNode {
node_type: self.node_type,
name: self.name,
inputs: self.inputs,
outputs: self.outputs,
attrs: self.attrs,
}
}
/// Build the node and register any constant inputs in GraphState.
/// This is useful for tests that need constant values accessible via GraphState.
///
/// Note: After calling this method, a ValueStore is built from the GraphState
/// and attached to the node's arguments.
pub(crate) fn build_with_graph_data(self, _opset: usize) -> RawNode {
// Create a new GraphState for this test
let mut graph_state = crate::graph_state::GraphState::new(&[], &[], &[], &[]);
// Register constants in GraphState before building the node
for (input_name, tensor_data) in &self.constant_data {
graph_state.register_test_constant(input_name.clone(), tensor_data.clone());
}
// Build the node first
let node = self.build();
// Build ValueStore and attach to all arguments
let value_store = graph_state.build_value_store();
let mut node = node;
for arg in &mut node.inputs {
arg.set_value_store(value_store.clone());
}
for arg in &mut node.outputs {
arg.set_value_store(value_store.clone());
}
node
}
}