use derive_new::new;
use onnx_ir_derive::NodeBuilder;
use crate::ir::Argument;
use crate::ir::{ArgType, AttributeValue, DType, Node, RawNode, TensorType};
use crate::processor::{
InputSpec, NodeProcessor, NodeSpec, OutputPreferences, OutputSpec, ProcessError,
};
use crate::proto_conversion::element_type_from_proto;
#[derive(Debug, Clone, new)]
pub struct CastConfig {
pub to: DType,
}
#[derive(Debug, Clone, NodeBuilder)]
pub struct CastNode {
pub name: String,
pub inputs: Vec<Argument>,
pub outputs: Vec<Argument>,
pub config: CastConfig,
}
pub(crate) struct CastProcessor;
impl NodeProcessor for CastProcessor {
type Config = CastConfig;
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 config = self
.extract_config(node, opset)
.expect("Config extraction failed");
let elem_type = config.to;
let input = &mut node.inputs[0];
let output = &mut node.outputs[0];
match input.ty.clone() {
ArgType::Tensor(tensor) => {
if tensor.rank == 0 {
output.ty = ArgType::Scalar(elem_type);
input.ty = ArgType::Scalar(tensor.dtype);
} else {
output.ty = ArgType::Tensor(TensorType {
dtype: elem_type,
rank: tensor.rank,
static_shape: tensor.static_shape, });
}
}
ArgType::Scalar(_) => output.ty = ArgType::Scalar(elem_type),
ArgType::Shape(rank) => {
if elem_type.is_float() || elem_type.is_bool() {
output.ty = ArgType::Tensor(TensorType {
dtype: elem_type,
rank: 1,
static_shape: Some(vec![rank]),
});
} else {
output.ty = ArgType::Shape(rank);
}
}
}
Ok(())
}
fn extract_config(&self, node: &RawNode, _opset: usize) -> Result<Self::Config, ProcessError> {
let elem_type = match node.attrs.get("to") {
Some(AttributeValue::Int64(type_id)) => element_type_from_proto(*type_id as i32)
.map_err(|_| ProcessError::InvalidAttribute {
name: "to".to_string(),
reason: format!("unsupported dtype: {}", type_id),
})?,
Some(_) => {
return Err(ProcessError::InvalidAttribute {
name: "to".to_string(),
reason: "must be Int64".to_string(),
});
}
None => {
return Err(ProcessError::MissingAttribute("to".to_string()));
}
};
let config = CastConfig::new(elem_type);
Ok(config)
}
fn build_node(&self, builder: RawNode, opset: usize) -> Node {
let config = self
.extract_config(&builder, opset)
.expect("Config extraction failed");
Node::Cast(CastNode {
name: builder.name,
inputs: builder.inputs,
outputs: builder.outputs,
config,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::{Argument, NodeType, TensorType};
use crate::node::test_utils::TestNodeBuilder;
use crate::protos::tensor_proto::DataType;
use protobuf::Enum;
fn create_test_node(input_rank: usize, to_type: i64) -> RawNode {
TestNodeBuilder::new(NodeType::Cast, "test_cast")
.input_tensor_f32("X", input_rank, None)
.output_tensor_f32("Y", input_rank, None) .attr_int("to", to_type)
.build()
}
fn create_scalar_test_node(to_type: i64) -> RawNode {
TestNodeBuilder::new(NodeType::Cast, "test_cast")
.input_scalar_f32("X")
.output_scalar_f32("Y") .attr_int("to", to_type)
.build()
}
#[test]
fn test_cast_config() {
let mut node = create_test_node(2, DataType::INT64.value() as i64);
let processor = CastProcessor;
let prefs = OutputPreferences::new();
let config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
assert_eq!(config.to, DType::I64);
let mut node = create_test_node(2, DataType::FLOAT.value() as i64);
let processor = CastProcessor;
let prefs = OutputPreferences::new();
let config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
assert_eq!(config.to, DType::F32);
let mut node = create_test_node(2, DataType::BOOL.value() as i64);
let processor = CastProcessor;
let prefs = OutputPreferences::new();
let config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
assert_eq!(config.to, DType::Bool);
}
#[test]
fn test_cast_float_to_int64() {
let mut node = create_test_node(2, DataType::INT64.value() as i64);
let processor = CastProcessor;
let prefs = OutputPreferences::new();
let _config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(tensor) => {
assert_eq!(tensor.dtype, DType::I64);
assert_eq!(tensor.rank, 2);
}
_ => panic!("Expected tensor output"),
}
}
#[test]
fn test_cast_scalar_handling() {
let mut node = create_test_node(0, DataType::BOOL.value() as i64);
let processor = CastProcessor;
let prefs = OutputPreferences::new();
let _config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Scalar(elem_type) => {
assert_eq!(*elem_type, DType::Bool);
}
_ => panic!("Expected scalar output for 0-rank tensor"),
}
match &node.inputs[0].ty {
ArgType::Scalar(elem_type) => {
assert_eq!(*elem_type, DType::F32);
}
_ => panic!("Input should have been converted to scalar"),
}
}
#[test]
fn test_cast_multiple_inputs() {
let mut node = create_test_node(2, DataType::INT64.value() as i64);
node.inputs.push(Argument {
name: "extra".to_string(),
ty: ArgType::Tensor(TensorType {
dtype: DType::F32,
rank: 1,
static_shape: None,
}),
value_source: crate::ir::ValueSource::Dynamic,
value_store: None,
});
let processor = CastProcessor;
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_cast_scalar_to_bool() {
let mut node = create_scalar_test_node(DataType::BOOL.value() as i64);
let processor = CastProcessor;
let prefs = OutputPreferences::new();
let _config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Scalar(elem_type) => {
assert_eq!(*elem_type, DType::Bool);
}
_ => panic!("Expected scalar output"),
}
}
#[test]
fn test_cast_shape_to_float32() {
let mut node = TestNodeBuilder::new(NodeType::Cast, "test_cast")
.input_shape("shape_input", 3)
.output_shape("output", 3) .attr_int("to", DataType::FLOAT.value() as i64)
.build();
let processor = CastProcessor;
let prefs = OutputPreferences::new();
let _config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(tensor) => {
assert_eq!(tensor.dtype, DType::F32);
assert_eq!(tensor.rank, 1);
assert_eq!(tensor.static_shape, Some(vec![3]));
}
_ => panic!("Expected rank-1 tensor output when casting Shape to float"),
}
}
#[test]
fn test_cast_shape_to_int64_remains_shape() {
let mut node = TestNodeBuilder::new(NodeType::Cast, "test_cast")
.input_shape("shape_input", 4)
.output_shape("output", 4) .attr_int("to", DataType::INT64.value() as i64)
.build();
let processor = CastProcessor;
let prefs = OutputPreferences::new();
let _config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Shape(rank) => {
assert_eq!(*rank, 4);
}
_ => panic!("Expected Shape output when casting Shape to int64"),
}
}
#[test]
fn test_cast_shape_to_bool() {
let mut node = TestNodeBuilder::new(NodeType::Cast, "test_cast")
.input_shape("shape_input", 3)
.output_shape("output", 3) .attr_int("to", DataType::BOOL.value() as i64)
.build();
let processor = CastProcessor;
let prefs = OutputPreferences::new();
let _config = processor.extract_config(&node, 16).unwrap();
processor.infer_types(&mut node, 16, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(tensor) => {
assert_eq!(tensor.dtype, DType::Bool);
assert_eq!(tensor.rank, 1);
assert_eq!(tensor.static_shape, Some(vec![3]));
}
_ => panic!("Expected rank-1 bool tensor output when casting Shape to bool"),
}
}
}