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;
const FLOAT8E4M3FN: i64 = 17;
const FLOAT8E4M3FNUZ: i64 = 18;
const FLOAT8E5M2: i64 = 19;
const FLOAT8E5M2FNUZ: i64 = 20;
const FLOAT8E8M0: i64 = 24;
#[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 is_noop(&self, node: &RawNode) -> bool {
node.inputs[0].ty == node.outputs[0].ty
}
fn infer_types(
&self,
node: &mut RawNode,
opset: usize,
_output_preferences: &OutputPreferences,
) -> Result<(), ProcessError> {
let config = self.extract_config(node, opset)?;
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::ScalarNative(elem_type);
input.ty = ArgType::ScalarNative(tensor.dtype);
} else {
output.ty = ArgType::Tensor(TensorType {
dtype: elem_type,
rank: tensor.rank,
static_shape: tensor.static_shape, });
}
}
ArgType::ScalarTensor(_) => output.ty = ArgType::ScalarTensor(elem_type),
ArgType::ScalarNative(_) => output.ty = ArgType::ScalarNative(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![Some(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)) => {
if matches!(
*type_id,
FLOAT8E4M3FN | FLOAT8E4M3FNUZ | FLOAT8E5M2 | FLOAT8E5M2FNUZ | FLOAT8E8M0
) {
return Err(ProcessError::InvalidAttribute {
name: "to".to_string(),
reason: format!(
"Float8 dtype (type_id={type_id}) is not supported. \
The saturate and round_mode attributes are float8-specific \
and are therefore also unsupported."
),
});
}
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, BoolStore, 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(BoolStore::Native));
}
#[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::ScalarNative(elem_type) => {
assert_eq!(*elem_type, DType::Bool(BoolStore::Native));
}
_ => panic!("Expected scalar output for 0-rank tensor"),
}
match &node.inputs[0].ty {
ArgType::ScalarNative(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::ScalarNative(elem_type) => {
assert_eq!(*elem_type, DType::Bool(BoolStore::Native));
}
_ => 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![Some(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(BoolStore::Native));
assert_eq!(tensor.rank, 1);
assert_eq!(tensor.static_shape, Some(vec![Some(3)]));
}
_ => panic!("Expected rank-1 bool tensor output when casting Shape to bool"),
}
}
#[test]
fn test_cast_is_noop_same_type() {
let mut node = create_test_node(2, DataType::FLOAT.value() as i64);
let processor = CastProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 16, &prefs).unwrap();
assert!(processor.is_noop(&node));
}
#[test]
fn test_cast_is_not_noop_different_type() {
let mut node = create_test_node(2, DataType::INT64.value() as i64);
let processor = CastProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 16, &prefs).unwrap();
assert!(!processor.is_noop(&node));
}
#[test]
fn test_cast_float8e4m3fn_rejected() {
let node = create_test_node(2, FLOAT8E4M3FN);
let processor = CastProcessor;
let result = processor.extract_config(&node, 19);
assert!(
matches!(
result,
Err(ProcessError::InvalidAttribute { ref name, .. }) if name == "to"
),
"Expected InvalidAttribute error for Float8 type, got: {:?}",
result
);
if let Err(ProcessError::InvalidAttribute { ref reason, .. }) = result {
assert!(
reason.contains("Float8"),
"Error reason should mention Float8: {}",
reason
);
}
}
#[test]
fn test_cast_float8e5m2fnuz_rejected() {
let node = create_test_node(2, FLOAT8E5M2FNUZ);
let processor = CastProcessor;
let result = processor.extract_config(&node, 19);
assert!(
matches!(
result,
Err(ProcessError::InvalidAttribute { ref name, .. }) if name == "to"
),
"Expected InvalidAttribute error for Float8 type, got: {:?}",
result
);
}
#[test]
fn test_cast_float8e8m0_rejected() {
let node = create_test_node(2, FLOAT8E8M0);
let processor = CastProcessor;
let result = processor.extract_config(&node, 21);
assert!(
matches!(
result,
Err(ProcessError::InvalidAttribute { ref name, .. }) if name == "to"
),
"Expected InvalidAttribute error for Float8E8M0 type, got: {:?}",
result
);
}
}