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
//! # Loop
//!
//! Generic looping construct - executes loop body graph for a specified number of iterations.
//!
//! **ONNX Spec**: <https://onnx.ai/onnx/operators/onnx__Loop.html>
//!
//! ## Opset Versions
//! - **Opset 1**: Initial version
//! - **Opset 11**: Adds support for sequence types
//! - **Opset 13**: Clarified scoping rules
//! - **Opset 16**: Further refinements
use derive_new::new;
use onnx_ir_derive::NodeBuilder;
use crate::ir::{ArgType, Argument, DType, Node, OnnxGraph, RawNode};
use crate::processor::{
NodeProcessor, OutputPreferences, ProcessError, build_outer_scope_from_inputs,
};
/// Helper function to transform type for scan output concatenation
/// Per ONNX Loop spec, scan outputs are concatenated along axis 0:
/// - Scalars (rank 0): unsqueezed to [1], then concatenated → [N, 1] (rank 2)
/// - Tensors (rank K): concatenated along axis 0 → [N*D0, D1, ...] (rank K, same rank)
fn add_concat_dimension(ty: ArgType) -> ArgType {
use crate::ir::TensorType;
match ty {
// Scalar (rank 0) → unsqueeze to [1] → concat → [N, 1] (rank 2)
ArgType::ScalarTensor(dtype) | ArgType::ScalarNative(dtype) => {
ArgType::Tensor(TensorType {
dtype,
rank: 2, // ONNX unsqueezes scalars before concat, resulting in rank 2
static_shape: None,
})
}
// Tensors: concatenated along axis 0 (same rank, first dim changes)
ArgType::Tensor(mut tensor_type) => {
// Clear static shape since num_iterations affects first dimension
tensor_type.static_shape = None;
ArgType::Tensor(tensor_type)
}
ArgType::Shape(_) => {
// Shapes become rank-1 tensors when concatenated
ArgType::Tensor(TensorType {
dtype: DType::I64,
rank: 1,
static_shape: None,
})
}
}
}
/// Configuration for Loop operation
#[derive(Debug, Clone, new)]
pub struct LoopConfig {
pub body: OnnxGraph,
/// Names of outer-scope references (in order corresponding to inputs after ONNX inputs)
/// These are the original sanitized ONNX names that subgraphs reference
#[new(default)]
pub scope_ref_names: Vec<String>,
}
/// Node representation for Loop operation
#[derive(Debug, Clone, NodeBuilder)]
pub struct LoopNode {
pub name: String,
pub inputs: Vec<Argument>,
pub outputs: Vec<Argument>,
pub config: LoopConfig,
}
/// Loop node processor
pub(crate) struct LoopProcessor;
impl NodeProcessor for LoopProcessor {
type Config = LoopConfig;
fn input_preferences(
&self,
node: &RawNode,
_opset: usize,
) -> Result<Option<crate::processor::InputPreferences>, ProcessError> {
use crate::processor::{ArgPreference, InputPreferences};
// Loop's max_trip_count (input 0) and condition (input 1) must be native
let mut prefs = InputPreferences::new();
if !node.inputs.is_empty() {
prefs = prefs.add(&node.inputs[0].name, ArgPreference::ScalarNative);
}
if node.inputs.len() > 1 {
prefs = prefs.add(&node.inputs[1].name, ArgPreference::ScalarNative);
}
Ok(Some(prefs))
}
fn infer_types(
&self,
node: &mut RawNode,
opset: usize,
_output_preferences: &OutputPreferences,
) -> Result<(), ProcessError> {
crate::processor::validate_opset(opset, 1)?;
// Loop has at least 2 inputs: M (optional), cond (optional), v_initial... (variadic)
// But M and cond can be empty strings in ONNX, so we need at least 2 inputs
if node.inputs.len() < 2 {
return Err(ProcessError::Custom(format!(
"Loop node requires at least 2 inputs (M, cond), got {}",
node.inputs.len()
)));
}
// Validate M input (max trip count) - should be scalar int64 or optional
if !node.inputs[0].is_optional() {
let m_type = &node.inputs[0].ty;
if !m_type.is_scalar() {
return Err(ProcessError::TypeMismatch {
expected: "Scalar I64 (rank-0 tensor or Scalar type) or empty".to_string(),
actual: format!("{:?}", m_type),
});
}
match m_type {
ArgType::ScalarNative(dtype) | ArgType::ScalarTensor(dtype)
if *dtype == DType::I64 =>
{
// Valid scalar int64
}
_ => {
return Err(ProcessError::TypeMismatch {
expected: "Scalar I64 or empty".to_string(),
actual: format!("{:?}", m_type),
});
}
}
}
// Validate cond input - should be scalar bool or optional
if !node.inputs[1].is_optional() {
let cond_type = &node.inputs[1].ty;
if !cond_type.is_scalar() {
return Err(ProcessError::TypeMismatch {
expected: "Scalar Bool (rank-0 tensor or Scalar type) or empty".to_string(),
actual: format!("{:?}", cond_type),
});
}
match cond_type {
ArgType::ScalarNative(dtype) | ArgType::ScalarTensor(dtype) if dtype.is_bool() => {
// Valid scalar bool
}
_ => {
return Err(ProcessError::TypeMismatch {
expected: "Scalar Bool or empty".to_string(),
actual: format!("{:?}", cond_type),
});
}
}
}
// Get body graph from config
let config = self
.extract_config(node, opset)
.expect("Config extraction failed");
let body_inputs = config.body.inputs.clone();
let body_outputs = config.body.outputs.clone();
// Body must have at least 2 inputs (iter_num, cond_in)
if body_inputs.len() < 2 {
return Err(ProcessError::Custom(format!(
"Loop body must have at least 2 inputs (iter_num, cond_in), got {}",
body_inputs.len()
)));
}
// Second body input must be scalar bool (cond_in)
let cond_in_type = &body_inputs[1].ty;
if !cond_in_type.is_scalar() {
return Err(ProcessError::TypeMismatch {
expected: "Loop body second input (cond_in) must be Scalar Bool (rank-0 tensor or Scalar type)".to_string(),
actual: format!("{:?}", cond_in_type),
});
}
match cond_in_type {
ArgType::ScalarNative(dtype) | ArgType::ScalarTensor(dtype) if dtype.is_bool() => {
// Valid
}
_ => {
return Err(ProcessError::TypeMismatch {
expected: "Loop body second input (cond_in) must be Scalar Bool".to_string(),
actual: format!("{:?}", cond_in_type),
});
}
}
// Body must have at least 1 output (cond_out)
if body_outputs.is_empty() {
return Err(ProcessError::Custom(
"Loop body must have at least 1 output (cond_out)".to_string(),
));
}
// First body output must be scalar bool (cond_out)
let cond_out_type = &body_outputs[0].ty;
if !cond_out_type.is_scalar() {
return Err(ProcessError::TypeMismatch {
expected: "Loop body first output (cond_out) must be Scalar Bool (rank-0 tensor or Scalar type)".to_string(),
actual: format!("{:?}", cond_out_type),
});
}
match cond_out_type {
ArgType::ScalarNative(dtype) | ArgType::ScalarTensor(dtype) if dtype.is_bool() => {
// Valid
}
_ => {
return Err(ProcessError::TypeMismatch {
expected: "Loop body first output (cond_out) must be Scalar Bool".to_string(),
actual: format!("{:?}", cond_out_type),
});
}
}
// Number of loop-carried dependencies in the body (excluding iter_num and cond_in)
let num_body_loop_inputs = body_inputs.len() - 2;
// Number of loop-carried outputs from body (excluding cond_out)
// Body outputs = [cond_out, v_out..., scan_outputs...]
let num_body_loop_outputs = body_outputs.len() - 1;
// The body must output at least as many values as it takes as loop-carried inputs
// (it can output more due to scan outputs)
if num_body_loop_outputs < num_body_loop_inputs {
return Err(ProcessError::Custom(format!(
"Loop body must have at least {} loop-carried outputs to match {} loop-carried inputs (got {})",
num_body_loop_inputs, num_body_loop_inputs, num_body_loop_outputs
)));
}
// Create outputs based on body outputs (excluding cond_out)
// Per ONNX spec:
// - Loop-carried dependencies: final value matches body output type
// - Scan outputs: concatenated along axis 0, adding a new dimension
//
// Body outputs: [cond_out, v_out_1, ..., v_out_N, scan_out_1, ..., scan_out_K]
// Loop outputs: [v_final_1, ..., v_final_N, scan_1, ..., scan_K]
let num_loop_carried_outputs = num_body_loop_inputs;
if node.outputs.is_empty() {
for (i, body_output) in body_outputs.iter().skip(1).enumerate() {
let mut output = body_output.clone();
// Scan outputs get concatenated, adding a dimension at axis 0
if i >= num_loop_carried_outputs {
output.ty = add_concat_dimension(output.ty);
}
node.outputs.push(output);
}
} else {
// Update types for existing outputs
for (i, body_output) in body_outputs.iter().skip(1).enumerate() {
if i < node.outputs.len() {
let mut ty = body_output.ty.clone();
// Scan outputs get concatenated, adding a dimension at axis 0
if i >= num_loop_carried_outputs {
ty = add_concat_dimension(ty);
}
node.outputs[i].ty = ty;
}
}
}
Ok(())
}
fn extract_config(&self, node: &RawNode, _opset: usize) -> Result<Self::Config, ProcessError> {
// Extract body graph from attributes
let body_attr = node
.attrs
.get("body")
.ok_or_else(|| ProcessError::MissingAttribute("body".to_string()))?
.clone();
// Build outer scope types map from additional inputs (beyond ONNX inputs)
let outer_scope = build_outer_scope_from_inputs(node);
// Handle DeferredGraph and Graph
let body = match body_attr {
crate::ir::AttributeValue::DeferredGraph(deferred) => {
// Build the subgraph now with outer-scope types
log::debug!(
"Building deferred Loop body subgraph with {} outer-scope types",
outer_scope.len()
);
deferred
.build_graph_with_outer_scope(outer_scope)
.map_err(|e| {
ProcessError::Custom(format!("Failed to build Loop body: {:?}", e))
})?
}
crate::ir::AttributeValue::Graph(g) => g,
_ => {
return Err(ProcessError::Custom(
"Expected DeferredGraph or Graph for body".to_string(),
));
}
};
// Get the scope ref names for use in code generation
let scope_ref_names: Vec<String> = node
.attrs
.get("__scope_ref_names")
.and_then(|v| match v {
crate::ir::AttributeValue::Strings(names) => Some(names.clone()),
_ => None,
})
.unwrap_or_default();
Ok(LoopConfig {
body,
scope_ref_names,
})
}
fn build_node(&self, builder: RawNode, opset: usize) -> Node {
let config = self
.extract_config(&builder, opset)
.expect("Config extraction failed");
Node::Loop(LoopNode {
name: builder.name,
inputs: builder.inputs,
outputs: builder.outputs,
config,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::AttributeValue;
use crate::ir::{Argument, BoolStore, NodeType, OnnxGraph, TensorType};
use crate::node::test_utils::TestNodeBuilder;
use std::collections::HashMap;
fn create_test_body(_num_loop_vars: usize) -> OnnxGraph {
OnnxGraph {
nodes: vec![],
inputs: vec![
// iter_num
Argument {
name: "iter".to_string(),
ty: ArgType::ScalarNative(DType::I64),
value_source: crate::ir::ValueSource::Dynamic,
value_store: None,
},
// cond_in
Argument {
name: "cond_in".to_string(),
ty: ArgType::ScalarNative(DType::Bool(BoolStore::Native)),
value_source: crate::ir::ValueSource::Dynamic,
value_store: None,
},
// v_in (loop-carried variable)
Argument {
name: "v_in".to_string(),
ty: ArgType::Tensor(TensorType {
dtype: DType::F32,
rank: 2,
static_shape: None,
}),
value_source: crate::ir::ValueSource::Dynamic,
value_store: None,
},
],
outputs: vec![
// cond_out
Argument {
name: "cond_out".to_string(),
ty: ArgType::ScalarNative(DType::Bool(BoolStore::Native)),
value_source: crate::ir::ValueSource::Dynamic,
value_store: None,
},
// v_out (loop-carried variable output)
Argument {
name: "v_out".to_string(),
ty: ArgType::Tensor(TensorType {
dtype: DType::F32,
rank: 2,
static_shape: None,
}),
value_source: crate::ir::ValueSource::Dynamic,
value_store: None,
},
],
value_store: None,
}
}
#[test]
fn test_loop_basic() {
let mut attrs = HashMap::new();
attrs.insert(
"body".to_string(),
AttributeValue::Graph(create_test_body(1)),
);
let mut node = TestNodeBuilder::new(NodeType::Loop, "test_loop")
.input_scalar("M", DType::I64)
.input_scalar("cond", DType::Bool(BoolStore::Native))
.input_tensor_f32("v_initial", 2, None)
.build();
node.attrs = attrs;
let processor = LoopProcessor;
// Extract config first
let _config = processor.extract_config(&node, 16).unwrap();
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 16, &prefs).unwrap();
// Loop should have 1 output (v_final)
assert_eq!(node.outputs.len(), 1);
}
#[test]
fn test_loop_invalid_trip_count() {
let mut attrs = HashMap::new();
attrs.insert(
"body".to_string(),
AttributeValue::Graph(create_test_body(1)),
);
let mut node = TestNodeBuilder::new(NodeType::Loop, "test_loop")
.input_tensor_f32("M", 1, None) // Should be scalar, not tensor
.input_scalar("cond", DType::Bool(BoolStore::Native))
.input_tensor_f32("v_initial", 2, None)
.build();
node.attrs = attrs;
let processor = LoopProcessor;
let _config = processor.extract_config(&node, 16).unwrap();
let prefs = OutputPreferences::new();
let result = processor.infer_types(&mut node, 16, &prefs);
assert!(matches!(result, Err(ProcessError::TypeMismatch { .. })));
}
}