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
//! # Squeeze
//!
//! Removes single-dimensional entries from the shape of a tensor.
//!
//! **ONNX Spec**: <https://onnx.ai/onnx/operators/onnx__Squeeze.html>
//!
//! ## Opset Versions
//! - **Opset 1**: Initial version with optional 'axes' attribute.
//! - **Opset 11**: Clarified semantics and behavior for negative axis values.
//! - **Opset 13**: Changed 'axes' from attribute to optional input, enabling dynamic axes specification at runtime.
//!
//! This implementation supports all opset versions. For opset < 13, axes are read from the
//! `axes` attribute. For opset 13+, axes are read from the second input.
use derive_new::new;
use onnx_ir_derive::NodeBuilder;
use crate::processor::{
InputSpec, NodeProcessor, NodeSpec, OutputPreferences, OutputSpec, ProcessError,
};
use crate::ir::{ArgType, Argument, Node, RawNode, RuntimeInputRef, TensorDataExt, TensorType};
/// Represents either a static value or a runtime argument for squeeze axes.
#[derive(Debug, Clone)]
pub enum SqueezeInput {
/// Static axes known at compile time.
Static(Vec<i64>),
/// Runtime axes determined during execution.
Runtime(RuntimeInputRef),
}
impl Default for SqueezeInput {
fn default() -> Self {
SqueezeInput::Static(vec![])
}
}
/// Configuration for Squeeze operation
#[derive(Debug, Clone, new)]
pub struct SqueezeConfig {
pub axes: Option<SqueezeInput>,
}
/// Node representation for Squeeze operation
#[derive(Debug, Clone, NodeBuilder)]
pub struct SqueezeNode {
pub name: String,
pub inputs: Vec<Argument>,
pub outputs: Vec<Argument>,
pub config: SqueezeConfig,
}
pub(crate) struct SqueezeProcessor;
impl NodeProcessor for SqueezeProcessor {
type Config = SqueezeConfig;
fn spec(&self) -> NodeSpec {
NodeSpec {
min_opset: 1,
max_opset: None,
inputs: InputSpec::AtLeast(1),
outputs: OutputSpec::Exact(1),
}
}
fn lift_constants(&self, node: &mut RawNode, opset: usize) -> Result<(), ProcessError> {
// Lift axes input (input[1]) if present (opset 13+ only; opset < 13 uses attribute)
if opset >= 13
&& node.inputs.len() > 1
&& !node.inputs[1].is_optional()
&& node.inputs[1].is_constant()
{
node.inputs[1].to_static()?;
}
Ok(())
}
fn infer_types(
&self,
node: &mut RawNode,
opset: usize,
_output_preferences: &OutputPreferences,
) -> Result<(), ProcessError> {
// Get reference to config for type inference
let config = self
.extract_config(node, opset)
.expect("Config extraction failed");
let axes = config.axes.clone();
// Extract axes for type inference
let axes_vec = match &axes {
Some(SqueezeInput::Static(axes_vec)) => Some(axes_vec.clone()),
Some(SqueezeInput::Runtime(_)) => None,
None => None,
};
// TODO: Missing validation that axes values are in valid range [-rank, rank-1].
// Out-of-bounds axes should be rejected but aren't validated here.
// TODO: Missing validation that axes doesn't contain duplicates.
// Duplicate axes should be rejected per ONNX spec but not validated.
match &node.inputs[0].ty {
ArgType::Tensor(tensor) => {
let output_rank = match axes_vec {
None => {
// When axes is None, ONNX spec squeezes all dimensions of size 1
if let Some(ref static_shape) = tensor.static_shape {
static_shape.iter().filter(|dim| **dim != Some(1)).count()
} else {
return Err(ProcessError::Custom(
"Squeeze: Cannot infer output rank when axes is None and input tensor static shape is unknown".to_string()
));
}
}
Some(ref axes_vec) => {
// Validate that we're not trying to squeeze more axes than the tensor has
if axes_vec.len() > tensor.rank {
return Err(ProcessError::Custom(format!(
"Squeeze: Cannot squeeze {} axes from a rank {} tensor",
axes_vec.len(),
tensor.rank
)));
}
// TODO: Missing validation that squeezed dimensions actually have size 1.
// ONNX spec requires dimensions to be size 1 to be squeezed, but implementation
// doesn't validate this when static_shape is available. Should check:
// for &axis in axes_vec { assert static_shape[axis] == 1 }
tensor.rank - axes_vec.len()
}
};
// Compute output static_shape by removing squeezed dimensions
let static_shape = {
let input_shape = tensor
.static_shape
.clone()
.unwrap_or_else(|| vec![None; tensor.rank]);
match axes_vec {
None => {
// Squeeze all dims of size 1
Some(
input_shape
.iter()
.filter(|dim| **dim != Some(1))
.copied()
.collect(),
)
}
Some(ref axes_vec) => {
// Normalize axes and remove those positions
let rank = tensor.rank as i64;
let remove: Vec<usize> = axes_vec
.iter()
.map(|&a| {
if a < 0 {
(a + rank) as usize
} else {
a as usize
}
})
.collect();
Some(
input_shape
.iter()
.enumerate()
.filter(|(i, _)| !remove.contains(i))
.map(|(_, dim)| *dim)
.collect(),
)
}
}
};
// When all dimensions are squeezed (rank=0), keep as ScalarTensor (on device)
// Downstream consumers that need native will request ScalarNative via preferences
node.outputs[0].ty = if output_rank == 0 {
ArgType::ScalarTensor(tensor.dtype)
} else {
ArgType::Tensor(TensorType {
dtype: tensor.dtype,
rank: output_rank,
static_shape,
})
};
}
ArgType::Shape(shape_rank) => {
if let Some(ref axes_vec) = axes_vec
&& !axes_vec.is_empty()
&& (axes_vec.len() != 1 || axes_vec[0] != 0)
{
return Err(ProcessError::Custom(format!(
"Squeeze on Shape input only supports squeezing axis 0, got axes: {:?}",
axes_vec
)));
}
if *shape_rank == 1 {
node.outputs[0].ty = ArgType::ScalarNative(crate::ir::DType::I64);
} else {
node.outputs[0].ty = ArgType::Shape(*shape_rank);
}
}
ArgType::ScalarTensor(scalar_type) | ArgType::ScalarNative(scalar_type) => {
node.outputs[0].ty = ArgType::ScalarNative(*scalar_type);
}
}
Ok(())
}
fn extract_config(&self, node: &RawNode, opset: usize) -> Result<Self::Config, ProcessError> {
// Check axes attribute (valid in opset < 13)
for (key, value) in node.attrs.iter() {
if key.as_str() == "axes" {
if opset >= 13 {
return Err(ProcessError::Custom(
"Squeeze: axes must be provided as input (not attribute) in opset 13+"
.to_string(),
));
}
let axes = value.clone().into_i64s();
if axes.is_empty() {
return Ok(SqueezeConfig { axes: None });
}
return Ok(SqueezeConfig {
axes: Some(SqueezeInput::Static(axes)),
});
}
}
// Fall through to input-based extraction (opset 13+ or opset < 13 without attribute)
fn get_squeeze_axes(node: &RawNode) -> Option<SqueezeInput> {
if node.inputs.len() < 2 {
return None; // No axes means squeeze all dims with size 1
}
let input = &node.inputs[1];
match input.value() {
None => {
// Runtime input - no static value available
Some(SqueezeInput::Runtime(RuntimeInputRef::new(
input.name.clone(),
1,
)))
}
Some(value) => match value.to_i64_vec() {
Ok(axes) => Some(SqueezeInput::Static(axes)),
Err(_) => None, // Invalid type
},
}
}
let axes = get_squeeze_axes(node);
let config = SqueezeConfig { axes };
Ok(config)
}
fn build_node(&self, builder: RawNode, opset: usize) -> Node {
let config = self
.extract_config(&builder, opset)
.expect("Config extraction failed");
Node::Squeeze(SqueezeNode {
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(axes: Option<Vec<i64>>, rank: usize) -> TestNodeBuilder {
let output_rank = if let Some(ref axes_vec) = axes {
rank - axes_vec.len()
} else {
// When no axes specified, we don't know how many dims will be squeezed
// without static shape info, but for testing we'll assume same as input
rank
};
let mut builder = TestNodeBuilder::new(NodeType::Squeeze, "test_squeeze")
.input_tensor_f32("data", rank, None)
.output_tensor_f32("squeezed", output_rank, None);
// Add axes as a second input (ONNX opset 13+ style)
if let Some(axes_val) = axes {
builder = builder.input_tensor_i64_data("axes", axes_val.clone(), vec![axes_val.len()]);
}
builder
}
fn create_runtime_squeeze_node() -> TestNodeBuilder {
TestNodeBuilder::new(NodeType::Squeeze, "test_runtime_squeeze")
.input_tensor_f32("data", 4, Some(vec![2, 3, 4, 5])) // Need some shape
.input_tensor_i64("axes", 0, None) // Runtime input - no static value
.output_tensor_f32("squeezed", 2, None)
}
#[test]
fn test_squeeze_config_with_axes_input() {
let node = create_test_node(Some(vec![0, 2]), 4).build_with_graph_data(16);
let mut node = node;
let processor = SqueezeProcessor;
let prefs = OutputPreferences::new();
let config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
assert!(matches!(config.axes, Some(SqueezeInput::Static(ref axes)) if axes == &vec![0, 2]));
}
#[test]
fn test_squeeze_config_no_axes_input() {
// Test with no axes input - need static shape with dims of size 1
let node = TestNodeBuilder::new(NodeType::Squeeze, "test_squeeze")
.input_tensor_f32("data", 4, Some(vec![2, 1, 3, 1])) // Has two dims of size 1
.output_tensor_f32("squeezed", 2, None) // Will squeeze to rank 2
.build();
let mut node = node;
let processor = SqueezeProcessor;
let prefs = OutputPreferences::new();
let config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
assert!(config.axes.is_none());
}
#[test]
fn test_squeeze_config_runtime_axes() {
let node = create_runtime_squeeze_node().build();
let mut node = node;
let processor = SqueezeProcessor;
let prefs = OutputPreferences::new();
let config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
assert!(matches!(config.axes, Some(SqueezeInput::Runtime(ref arg)) if arg.name == "axes"));
}
// TODO: Missing test for squeezing dimension that is not size 1 - should fail.
// E.g., input shape [2, 1, 3], axes=[0] should fail because dim 0 has size 2, not 1.
// TODO: Missing test for negative axes normalization and validation.
// E.g., axes=[-1] for rank-3 should squeeze last dimension.
// TODO: Missing test for duplicate axes - axes=[0, 0] should be rejected.
// TODO: Missing test for out-of-bounds axes - axes=[5] for rank-3 should be rejected.
// TODO: Missing test for opset < 13 behavior - axes as attribute vs input.
// Implementation requires opset 13+ but this transition isn't tested.
#[test]
fn test_squeeze_all_dims_to_scalar() {
// Test squeezing all dimensions produces Scalar, not Tensor(rank=0)
// This maintains consistency with proto conversion
let node = create_test_node(Some(vec![0]), 1).build_with_graph_data(16);
let mut node = node;
let processor = SqueezeProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 16, &prefs).unwrap();
// Verify output is ScalarTensor (stays on device)
match &node.outputs[0].ty {
ArgType::ScalarTensor(dtype) => {
assert_eq!(*dtype, crate::ir::DType::F32);
}
other => panic!("Expected ScalarTensor output, got {:?}", other),
}
}
#[test]
fn test_squeeze_propagates_static_shape() {
// Input [2, 1, 3, 1] with axes [1, 3] -> output [2, 3]
let mut node = TestNodeBuilder::new(NodeType::Squeeze, "test_squeeze")
.input_tensor_f32("data", 4, Some(vec![2, 1, 3, 1]))
.input_tensor_i64_data("axes", vec![1, 3], vec![2])
.output_tensor_f32("squeezed", 2, None)
.build_with_graph_data(16);
let processor = SqueezeProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(t) => {
assert_eq!(t.rank, 2);
assert_eq!(t.static_shape, Some(vec![Some(2), Some(3)]));
}
_ => panic!("Expected tensor output"),
}
}
#[test]
fn test_squeeze_no_axes_removes_size_1() {
// Input [2, 1, 3, 1] with no axes -> squeeze all 1s -> [2, 3]
let mut node = TestNodeBuilder::new(NodeType::Squeeze, "test_squeeze")
.input_tensor_f32("data", 4, Some(vec![2, 1, 3, 1]))
.output_tensor_f32("squeezed", 2, None)
.build();
let processor = SqueezeProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(t) => {
assert_eq!(t.rank, 2);
assert_eq!(t.static_shape, Some(vec![Some(2), Some(3)]));
}
_ => panic!("Expected tensor output"),
}
}
#[test]
fn test_squeeze_no_static_shape_with_axes() {
// Input rank 4, no static_shape, axes [1] -> output rank 3, all dims unknown
let node = create_test_node(Some(vec![1]), 4).build_with_graph_data(16);
let mut node = node;
let processor = SqueezeProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(t) => {
assert_eq!(t.rank, 3);
// Even without input static_shape, produces partial shape
assert_eq!(t.static_shape, Some(vec![None, None, None]));
}
_ => panic!("Expected tensor output"),
}
}
#[test]
fn test_squeeze_negative_axes() {
// Input [2, 1, 3] with axes [-2] -> output [2, 3]
let mut node = TestNodeBuilder::new(NodeType::Squeeze, "test_squeeze")
.input_tensor_f32("data", 3, Some(vec![2, 1, 3]))
.input_tensor_i64_data("axes", vec![-2], vec![1])
.output_tensor_f32("squeezed", 2, None)
.build_with_graph_data(16);
let processor = SqueezeProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(t) => {
assert_eq!(t.rank, 2);
assert_eq!(t.static_shape, Some(vec![Some(2), Some(3)]));
}
_ => panic!("Expected tensor output"),
}
}
}