From 1b07b022e2bbc2f58a313ef6db33d33069fba97e Mon Sep 17 00:00:00 2001 From: Feiyue Chen Date: Tue, 20 Sep 2022 10:30:04 +0800 Subject: [PATCH] added sign & softsign --- include/tim/vx/ops/activations.h | 2 ++ src/tim/vx/ops/activations.cc | 4 ++- src/tim/vx/ops/activations_test.cc | 58 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/include/tim/vx/ops/activations.h b/include/tim/vx/ops/activations.h index 629e54e..9f8f7e5 100644 --- a/include/tim/vx/ops/activations.h +++ b/include/tim/vx/ops/activations.h @@ -89,6 +89,8 @@ DECLARE_NO_PARAMETER_ACTIVATION(Swish) DECLARE_NO_PARAMETER_ACTIVATION(HardSwish) DECLARE_NO_PARAMETER_ACTIVATION(Mish) DECLARE_NO_PARAMETER_ACTIVATION(SoftRelu) +DECLARE_NO_PARAMETER_ACTIVATION(Sign) +DECLARE_NO_PARAMETER_ACTIVATION(SoftSign) #undef DEFINE_NO_PARAMETER_ACTIVATION diff --git a/src/tim/vx/ops/activations.cc b/src/tim/vx/ops/activations.cc index e9bc324..9bf41e0 100644 --- a/src/tim/vx/ops/activations.cc +++ b/src/tim/vx/ops/activations.cc @@ -43,6 +43,8 @@ DEFINE_NO_PARAMETER_ACTIVATION(Relu6, VSI_NN_OP_RELU6) DEFINE_NO_PARAMETER_ACTIVATION(Sigmoid, VSI_NN_OP_SIGMOID) DEFINE_NO_PARAMETER_ACTIVATION(Mish, VSI_NN_OP_MISH) DEFINE_NO_PARAMETER_ACTIVATION(SoftRelu, VSI_NN_OP_SOFTRELU) +DEFINE_NO_PARAMETER_ACTIVATION(Sign,VSI_NN_OP_SIGN) +DEFINE_NO_PARAMETER_ACTIVATION(SoftSign,VSI_NN_OP_SOFTSIGN) #undef DEFINE_NO_PARAMETER_ACTIVATION @@ -154,7 +156,7 @@ Celu::Celu(Graph* graph, float alpha) } std::shared_ptr Celu::Clone(std::shared_ptr& graph) const { - return graph->CreateOperation(this->alpha_); + return graph->CreateOperation(this->alpha_); } #endif diff --git a/src/tim/vx/ops/activations_test.cc b/src/tim/vx/ops/activations_test.cc index 644fd71..4e93e3e 100644 --- a/src/tim/vx/ops/activations_test.cc +++ b/src/tim/vx/ops/activations_test.cc @@ -400,3 +400,61 @@ TEST(Celu, shape_2_2) { EXPECT_TRUE(ArraysMatch(golden, output, 1e-5f)); } #endif + +TEST(Sign, shape_5_1_fp32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType io_shape({5, 1}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, io_shape, + tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, io_shape, + tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor = graph->CreateTensor(output_spec); + + std::vector in_data = {-3.7, -1, 0, 0.5, 12}; + std::vector golden = {-1, -1, 0, 1, 1}; + + EXPECT_TRUE( + input_tensor->CopyDataToTensor(in_data.data(), in_data.size() * 4)); + + auto op = graph->CreateOperation(); + (*op).BindInputs({input_tensor}).BindOutputs({output_tensor}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + std::vector output(5, 0); + EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); + EXPECT_EQ(golden, output); +} + +TEST(SoftSign, shape_5_1_fp32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType io_shape({5, 1}); + tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32, io_shape, + tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32, io_shape, + tim::vx::TensorAttribute::OUTPUT); + + auto input_tensor = graph->CreateTensor(input_spec); + auto output_tensor = graph->CreateTensor(output_spec); + + std::vector in_data = {-3.7, -1, 0, 0.5, 12}; + std::vector golden = {-0.78723, -0.5, 0, 0.33333, 0.92308}; + + EXPECT_TRUE( + input_tensor->CopyDataToTensor(in_data.data(), in_data.size() * 4)); + + auto op = graph->CreateOperation(); + (*op).BindInputs({input_tensor}).BindOutputs({output_tensor}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + std::vector output(5, 0); + EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data())); + EXPECT_TRUE(ArraysMatch(golden, output, 1e-5f)); +} \ No newline at end of file