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, Default)]
pub struct GatherNDConfig {
pub batch_dims: usize,
}
#[derive(Debug, Clone, NodeBuilder)]
pub struct GatherNDNode {
pub name: String,
pub inputs: Vec<Argument>,
pub outputs: Vec<Argument>,
pub config: GatherNDConfig,
}
pub(crate) struct GatherNDProcessor;
impl NodeProcessor for GatherNDProcessor {
type Config = GatherNDConfig;
fn spec(&self) -> NodeSpec {
NodeSpec {
min_opset: 11,
max_opset: None,
inputs: InputSpec::Exact(2),
outputs: OutputSpec::Exact(1),
}
}
fn infer_types(
&self,
node: &mut RawNode,
_opset: usize,
_output_preferences: &OutputPreferences,
) -> Result<(), ProcessError> {
let data_tensor = match &node.inputs[0].ty {
ArgType::Tensor(t) => t.clone(),
other => {
return Err(ProcessError::Custom(format!(
"GatherND data input must be a tensor, got {:?}",
other
)));
}
};
let indices_tensor = match &node.inputs[1].ty {
ArgType::Tensor(t) => t.clone(),
other => {
return Err(ProcessError::Custom(format!(
"GatherND indices input must be a tensor, got {:?}",
other
)));
}
};
let r = data_tensor.rank;
let q = indices_tensor.rank;
let mut batch_dims: i64 = 0;
for (key, value) in node.attrs.iter() {
match key.as_str() {
"batch_dims" => batch_dims = value.clone().into_i64(),
_ => {
return Err(ProcessError::InvalidAttribute {
name: key.clone(),
reason: format!("Unexpected attribute for GatherND: {}", key),
});
}
}
}
let b = batch_dims as usize;
if b >= q.min(r) {
return Err(ProcessError::InvalidAttribute {
name: "batch_dims".to_string(),
reason: format!(
"batch_dims {} must be < min(indices_rank={}, data_rank={})",
b, q, r
),
});
}
let k = indices_tensor
.static_shape
.as_ref()
.and_then(|s| s.last().copied())
.flatten()
.ok_or_else(|| {
ProcessError::Custom(
"GatherND requires indices to have a known last dimension to determine output rank"
.to_string(),
)
})?;
if k > r - b {
return Err(ProcessError::Custom(format!(
"GatherND indices last dimension {} must be <= data_rank({}) - batch_dims({})",
k, r, b
)));
}
let output_rank = q + r - k - 1 - b;
if output_rank == 0 {
node.outputs[0].ty = ArgType::ScalarTensor(data_tensor.dtype);
} else {
node.outputs[0].ty = ArgType::Tensor(TensorType {
dtype: data_tensor.dtype,
rank: output_rank,
static_shape: None,
});
}
Ok(())
}
fn extract_config(&self, node: &RawNode, _opset: usize) -> Result<Self::Config, ProcessError> {
let mut batch_dims: i64 = 0;
for (key, value) in node.attrs.iter() {
match key.as_str() {
"batch_dims" => batch_dims = value.clone().into_i64(),
_ => {
return Err(ProcessError::InvalidAttribute {
name: key.clone(),
reason: format!("Unexpected attribute for GatherND: {}", key),
});
}
}
}
Ok(GatherNDConfig {
batch_dims: batch_dims as usize,
})
}
fn build_node(&self, builder: RawNode, opset: usize) -> Node {
let config = self
.extract_config(&builder, opset)
.expect("Config extraction failed");
Node::GatherND(GatherNDNode {
name: builder.name,
inputs: builder.inputs,
outputs: builder.outputs,
config,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::{DType, NodeType};
use crate::node::test_utils::TestNodeBuilder;
fn create_test_node(
data_rank: usize,
indices_rank: usize,
indices_last_dim: usize,
batch_dims: i64,
) -> TestNodeBuilder {
let mut builder = TestNodeBuilder::new(NodeType::GatherND, "test_gathernd");
if batch_dims != 0 {
builder = builder.attr_int("batch_dims", batch_dims);
}
let mut indices_shape = vec![2usize; indices_rank];
*indices_shape.last_mut().unwrap() = indices_last_dim;
builder
.input_tensor_f32("data", data_rank, None)
.input_tensor_i64("indices", indices_rank, Some(indices_shape))
.output_tensor_f32("output", 1, None) }
#[test]
fn test_config_default_batch_dims() {
let node = create_test_node(2, 2, 2, 0).build();
let processor = GatherNDProcessor;
let config = processor.extract_config(&node, 12).unwrap();
assert_eq!(config.batch_dims, 0);
}
#[test]
fn test_config_custom_batch_dims() {
let node = create_test_node(3, 2, 1, 1).build();
let processor = GatherNDProcessor;
let config = processor.extract_config(&node, 12).unwrap();
assert_eq!(config.batch_dims, 1);
}
#[test]
fn test_infer_example1() {
let mut node = create_test_node(2, 2, 2, 0).build();
let processor = GatherNDProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 12, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(t) => assert_eq!(t.rank, 1),
other => panic!("Expected tensor, got {:?}", other),
}
}
#[test]
fn test_infer_example2() {
let mut node = create_test_node(2, 2, 1, 0).build();
let processor = GatherNDProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 12, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(t) => assert_eq!(t.rank, 2),
other => panic!("Expected tensor, got {:?}", other),
}
}
#[test]
fn test_infer_example3() {
let mut node = create_test_node(3, 2, 2, 0).build();
let processor = GatherNDProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 12, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(t) => assert_eq!(t.rank, 2),
other => panic!("Expected tensor, got {:?}", other),
}
}
#[test]
fn test_infer_example4() {
let mut node = create_test_node(3, 3, 2, 0).build();
let processor = GatherNDProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 12, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(t) => assert_eq!(t.rank, 3),
other => panic!("Expected tensor, got {:?}", other),
}
}
#[test]
fn test_infer_example5_batch() {
let mut node = create_test_node(3, 2, 1, 1).build();
let processor = GatherNDProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 12, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::Tensor(t) => assert_eq!(t.rank, 2),
other => panic!("Expected tensor, got {:?}", other),
}
}
#[test]
fn test_infer_scalar_output() {
let mut node = create_test_node(1, 1, 1, 0).build();
let processor = GatherNDProcessor;
let prefs = OutputPreferences::new();
processor.infer_types(&mut node, 12, &prefs).unwrap();
match &node.outputs[0].ty {
ArgType::ScalarTensor(dtype) => assert_eq!(*dtype, DType::F32),
other => panic!("Expected scalar, got {:?}", other),
}
}
#[test]
fn test_invalid_batch_dims() {
let mut node = create_test_node(2, 2, 1, 2).build();
let processor = GatherNDProcessor;
let prefs = OutputPreferences::new();
let result = processor.infer_types(&mut node, 12, &prefs);
assert!(result.is_err());
}
#[test]
fn test_invalid_k_too_large() {
let mut node = create_test_node(2, 2, 3, 0).build();
let processor = GatherNDProcessor;
let prefs = OutputPreferences::new();
let result = processor.infer_types(&mut node, 12, &prefs);
assert!(result.is_err());
}
#[test]
fn test_unexpected_attribute() {
let node = create_test_node(2, 2, 2, 0)
.attr_int("unknown_attr", 42)
.build();
let processor = GatherNDProcessor;
let prefs = OutputPreferences::new();
let mut node = node;
let result = processor.infer_types(&mut node, 12, &prefs);
assert!(result.is_err());
}
}