added sign & softsign

This commit is contained in:
Feiyue Chen 2022-09-20 10:30:04 +08:00 committed by Sven
parent f4d5e170de
commit 1b07b022e2
3 changed files with 63 additions and 1 deletions

View File

@ -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

View File

@ -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<Operation> Celu::Clone(std::shared_ptr<Graph>& graph) const {
return graph->CreateOperation<Selu>(this->alpha_);
return graph->CreateOperation<Celu>(this->alpha_);
}
#endif

View File

@ -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<float> in_data = {-3.7, -1, 0, 0.5, 12};
std::vector<float> golden = {-1, -1, 0, 1, 1};
EXPECT_TRUE(
input_tensor->CopyDataToTensor(in_data.data(), in_data.size() * 4));
auto op = graph->CreateOperation<tim::vx::ops::Sign>();
(*op).BindInputs({input_tensor}).BindOutputs({output_tensor});
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> 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<float> in_data = {-3.7, -1, 0, 0.5, 12};
std::vector<float> 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<tim::vx::ops::SoftSign>();
(*op).BindInputs({input_tensor}).BindOutputs({output_tensor});
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> output(5, 0);
EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data()));
EXPECT_TRUE(ArraysMatch(golden, output, 1e-5f));
}