use derive_new::new;
use onnx_ir_derive::NodeBuilder;
use crate::ir::{ArgType, Argument, Node, RawNode, TensorType};
use crate::processor::{
InputSpec, NodeProcessor, NodeSpec, OutputPreferences, OutputSpec, ProcessError,
};
#[derive(Debug, Clone, new)]
pub struct FlattenConfig {
pub axis: usize,
}
#[derive(Debug, Clone, NodeBuilder)]
pub struct FlattenNode {
pub name: String,
pub inputs: Vec<Argument>,
pub outputs: Vec<Argument>,
pub config: FlattenConfig,
}
pub(crate) struct FlattenProcessor;
impl NodeProcessor for FlattenProcessor {
type Config = FlattenConfig;
fn spec(&self) -> NodeSpec {
NodeSpec {
min_opset: 1,
max_opset: None,
inputs: InputSpec::Exact(1),
outputs: OutputSpec::Exact(1),
}
}
fn infer_types(
&self,
node: &mut RawNode,
opset: usize,
_output_preferences: &OutputPreferences,
) -> Result<(), ProcessError> {
let tensor = match &node.inputs.first().unwrap().ty {
ArgType::Tensor(tensor) => tensor.clone(),
_ => {
return Err(ProcessError::TypeMismatch {
expected: "Tensor".to_string(),
actual: format!("{:?}", node.inputs.first().unwrap().ty),
});
}
};
if tensor.rank < 2 {
return Err(ProcessError::Custom(format!(
"Flatten: input tensor must have at least 2 dimensions (got {})",
tensor.rank
)));
}
let config = self
.extract_config(node, opset)
.expect("Config extraction failed");
let static_shape = if let Some(input_shape) = &tensor.static_shape {
let axis = config.axis;
let left = input_shape[..axis]
.iter()
.try_fold(1usize, |acc, dim| dim.map(|d| acc * d));
let right = input_shape[axis..]
.iter()
.try_fold(1usize, |acc, dim| dim.map(|d| acc * d));
Some(vec![left, right])
} else {
Some(vec![None, None])
};
node.outputs[0].ty = ArgType::Tensor(TensorType {
dtype: tensor.dtype,
rank: 2,
static_shape,
});
Ok(())
}
fn is_noop(&self, node: &RawNode) -> bool {
if let ArgType::Tensor(in_t) = &node.inputs[0].ty {
if in_t.rank != 2 {
return false;
}
let axis = node
.attrs
.get("axis")
.map(|v| v.clone().into_i64())
.unwrap_or(1);
return axis == 1 || axis == -(in_t.rank as i64 - 1);
}
false
}
fn extract_config(&self, node: &RawNode, _opset: usize) -> Result<Self::Config, ProcessError> {
let tensor = match &node.inputs.first().unwrap().ty {
ArgType::Tensor(tensor) => tensor.clone(),
_ => {
return Err(ProcessError::TypeMismatch {
expected: "Tensor".to_string(),
actual: format!("{:?}", node.inputs.first().unwrap().ty),
});
}
};
let mut axis: i64 = 1;
for (key, value) in node.attrs.iter() {
match key.as_str() {
"axis" => axis = value.clone().into_i64(),
_ => {
return Err(ProcessError::InvalidAttribute {
name: key.clone(),
reason: format!("Unexpected attribute for Flatten: {}", key),
});
}
}
}
if axis < 0 {
axis += tensor.rank as i64;
}
let config = FlattenConfig {
axis: axis as usize,
};
Ok(config)
}
fn build_node(&self, builder: RawNode, opset: usize) -> Node {
let config = self
.extract_config(&builder, opset)
.expect("Config extraction failed");
Node::Flatten(FlattenNode {
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(axis: i64) -> TestNodeBuilder {
TestNodeBuilder::new(NodeType::Flatten, "test_flatten")
.input_tensor_f32("data", 4, None)
.output_tensor_f32("output", 2, None)
.attr_int("axis", axis)
}
#[test]
fn test_flatten_config_basic() {
let node = create_test_node(1).process(FlattenProcessor, 16);
let processor = FlattenProcessor;
let config = processor.extract_config(&node, 16).unwrap();
assert_eq!(config.axis, 1);
}
#[test]
fn test_flatten_config_with_negative_axis() {
let node = create_test_node(-2).process(FlattenProcessor, 16);
let processor = FlattenProcessor;
let config = processor.extract_config(&node, 16).unwrap();
assert_eq!(config.axis, 2); }
#[test]
fn test_flatten_config_with_low_rank() {
let mut node = create_test_node(1).build();
let input = TestNodeBuilder::new(NodeType::Identity, "temp")
.input_tensor_f32("x", 1, None)
.build()
.inputs
.pop()
.unwrap();
node.inputs[0] = input;
let processor = FlattenProcessor;
let prefs = OutputPreferences::new();
let _config = processor.extract_config(&node, 16).unwrap();
let result = processor.infer_types(&mut node, 16, &prefs);
assert!(matches!(result, Err(ProcessError::Custom(_))));
}
#[test]
fn test_flatten_config_with_multiple_inputs() {
let mut node = create_test_node(1).build();
let extra_input = TestNodeBuilder::new(NodeType::Identity, "temp")
.input_tensor_f32("extra", 1, None)
.build()
.inputs
.pop()
.unwrap();
node.inputs.push(extra_input);
let processor = FlattenProcessor;
let spec = processor.spec();
let result = crate::processor::validate_node_spec(&node, 16, &spec);
assert!(matches!(
result,
Err(ProcessError::InvalidInputCount {
expected: 1,
actual: 2
})
));
}
#[test]
fn test_flatten_rank2_axis1_is_noop() {
let node = TestNodeBuilder::new(NodeType::Flatten, "test")
.input_tensor_f32("data", 2, None)
.output_tensor_f32("output", 2, None)
.attr_int("axis", 1)
.build();
assert!(FlattenProcessor.is_noop(&node));
}
#[test]
fn test_flatten_rank2_axis_neg1_is_noop() {
let node = TestNodeBuilder::new(NodeType::Flatten, "test")
.input_tensor_f32("data", 2, None)
.output_tensor_f32("output", 2, None)
.attr_int("axis", -1)
.build();
assert!(FlattenProcessor.is_noop(&node));
}
#[test]
fn test_flatten_rank2_axis0_is_not_noop() {
let node = TestNodeBuilder::new(NodeType::Flatten, "test")
.input_tensor_f32("data", 2, None)
.output_tensor_f32("output", 2, None)
.attr_int("axis", 0)
.build();
assert!(!FlattenProcessor.is_noop(&node));
}
#[test]
fn test_flatten_rank3_axis1_is_not_noop() {
let node = TestNodeBuilder::new(NodeType::Flatten, "test")
.input_tensor_f32("data", 3, None)
.output_tensor_f32("output", 2, None)
.attr_int("axis", 1)
.build();
assert!(!FlattenProcessor.is_noop(&node));
}
#[test]
fn test_flatten_static_shape_known() {
let mut node = TestNodeBuilder::new(NodeType::Flatten, "test")
.input_tensor_f32("data", 4, Some(vec![2, 3, 4, 5]))
.output_tensor_f32("output", 2, None)
.attr_int("axis", 2)
.build();
let processor = FlattenProcessor;
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(6), Some(20)]));
}
_ => panic!("Expected tensor output"),
}
}
#[test]
fn test_flatten_static_shape_partial() {
let mut node = TestNodeBuilder::new(NodeType::Flatten, "test")
.add_input(
"data",
ArgType::Tensor(TensorType {
dtype: crate::ir::DType::F32,
rank: 3,
static_shape: Some(vec![None, Some(3), Some(4)]),
}),
)
.output_tensor_f32("output", 2, None)
.attr_int("axis", 1)
.build();
let processor = FlattenProcessor;
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![None, Some(12)]));
}
_ => panic!("Expected tensor output"),
}
}
#[test]
fn test_flatten_no_static_shape() {
let mut node = create_test_node(1).build();
let processor = FlattenProcessor;
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![None, None]));
}
_ => panic!("Expected tensor output"),
}
}
#[test]
fn test_flatten_axis_0() {
let mut node = TestNodeBuilder::new(NodeType::Flatten, "test")
.input_tensor_f32("data", 3, Some(vec![2, 3, 4]))
.output_tensor_f32("output", 2, None)
.attr_int("axis", 0)
.build();
let processor = FlattenProcessor;
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(1), Some(24)]));
}
_ => panic!("Expected tensor output"),
}
}
}