From 68c166bc4e89c77c718ae98250bf069c2bf334ff Mon Sep 17 00:00:00 2001 From: Ionut Mihalcea Date: Tue, 26 Nov 2024 22:56:38 +0100 Subject: [PATCH 1/2] Support for Sign operation #2641 --- candle-onnx/src/eval.rs | 6 ++++++ candle-onnx/tests/ops.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/candle-onnx/src/eval.rs b/candle-onnx/src/eval.rs index 358af7acff..2c60ed2f23 100644 --- a/candle-onnx/src/eval.rs +++ b/candle-onnx/src/eval.rs @@ -1944,6 +1944,12 @@ fn simple_eval_( values.insert(node.output[0].clone(), out); } + // https://onnx.ai/onnx/operators/onnx__Sign.html + "Sign" => { + let input = get(&node.input[0])?; + let output = input.sign()?; + values.insert(node.output[0].clone(), output); + } op_type => bail!("unsupported op_type {op_type} for op {node:?}"), } } diff --git a/candle-onnx/tests/ops.rs b/candle-onnx/tests/ops.rs index a84ba481ee..8d1a240356 100644 --- a/candle-onnx/tests/ops.rs +++ b/candle-onnx/tests/ops.rs @@ -5869,3 +5869,41 @@ fn test_xor() -> Result<()> { } Ok(()) } + +#[test] +fn test_sign_operation() -> Result<()> { + let manual_graph = create_model_proto_with_graph(Some(GraphProto { + node: vec![NodeProto { + op_type: "Sign".to_string(), + domain: "".to_string(), + attribute: vec![], + input: vec![INPUT_X.to_string()], + output: vec![OUTPUT_Z.to_string()], + name: "".to_string(), + doc_string: "".to_string(), + }], + name: "".to_string(), + initializer: vec![], + input: vec![], + output: vec![ValueInfoProto { + name: OUTPUT_Z.to_string(), + doc_string: "".to_string(), + r#type: None, + }], + value_info: vec![], + doc_string: "".to_string(), + sparse_initializer: vec![], + quantization_annotation: vec![], + })); + + let mut inputs: HashMap = HashMap::new(); + inputs.insert(INPUT_X.to_string(), Tensor::new(vec![-2f32, -1., 0., 1., 2.], &Device::Cpu)?); + let eval = candle_onnx::simple_eval(&manual_graph, inputs)?; + + let z = eval.get(OUTPUT_Z).expect("Output 'z' not found"); + assert_eq!( + z.to_dtype(candle::DType::I64)?.to_vec1::()?.to_vec(), + vec![-1, -1, 0, 1, 1] + ); + Ok(()) +} \ No newline at end of file From 9b95a1d51caf4b1edf18e9392d5901e681d24d4e Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 26 Nov 2024 23:04:51 +0100 Subject: [PATCH 2/2] Apply rustfmt. --- candle-onnx/tests/ops.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/candle-onnx/tests/ops.rs b/candle-onnx/tests/ops.rs index 8d1a240356..3586bfbd68 100644 --- a/candle-onnx/tests/ops.rs +++ b/candle-onnx/tests/ops.rs @@ -5897,7 +5897,10 @@ fn test_sign_operation() -> Result<()> { })); let mut inputs: HashMap = HashMap::new(); - inputs.insert(INPUT_X.to_string(), Tensor::new(vec![-2f32, -1., 0., 1., 2.], &Device::Cpu)?); + inputs.insert( + INPUT_X.to_string(), + Tensor::new(vec![-2f32, -1., 0., 1., 2.], &Device::Cpu)?, + ); let eval = candle_onnx::simple_eval(&manual_graph, inputs)?; let z = eval.get(OUTPUT_Z).expect("Output 'z' not found"); @@ -5906,4 +5909,4 @@ fn test_sign_operation() -> Result<()> { vec![-1, -1, 0, 1, 1] ); Ok(()) -} \ No newline at end of file +}