fix softplus (#10576)

This commit is contained in:
Guoyu Wang 2022-02-27 15:27:07 -08:00 committed by GitHub
parent f2ca43fe0d
commit 240f31ef6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 19 deletions

View file

@ -98,7 +98,7 @@ struct Softplus : public ElementWiseRangedTransform<T> {
T* output_ptr = this->output + first;
ConstEigenVectorArrayMap<T> xm(this->input + first, len);
EigenVectorArrayMap<T> ym(output_ptr, len);
ym = (xm > 0).select(xm + ((-xm).exp() + 1.0f).log(), ((xm).exp() + 1.0f).log());
ym = (xm > 0).select(xm + ((-xm).exp()).log1p(), ((xm).exp()).log1p());
}
};

View file

@ -132,11 +132,11 @@ TEST_F(ActivationOpTest, Sigmoid_fp16) {
#endif
OpTester test("Sigmoid", 14);
auto formula = [](float x) {
auto y = 1.f / (1.f + std::exp(-std::abs(x))); // safe sigmoid
y = x > 0 ? y : 1 - y;
return y;
};
auto formula = [](float x) {
auto y = 1.f / (1.f + std::exp(-std::abs(x))); // safe sigmoid
y = x > 0 ? y : 1 - y;
return y;
};
std::vector<float> X = input_values.front();
std::vector<float> Y;
@ -151,7 +151,7 @@ TEST_F(ActivationOpTest, Sigmoid_fp16) {
test.AddInput<MLFloat16>("X", dims, f_X);
test.AddOutput<MLFloat16>("Y", dims, f_Y);
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider});
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider});
}
TEST_F(ActivationOpTest, Tanh_fp16) {
@ -179,7 +179,7 @@ TEST_F(ActivationOpTest, Tanh_fp16) {
test.AddInput<MLFloat16>("X", dims, f_X);
test.AddOutput<MLFloat16>("Y", dims, f_Y);
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider});
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider});
}
TEST_F(ActivationOpTest, Relu_fp16) {
@ -207,7 +207,7 @@ TEST_F(ActivationOpTest, Relu_fp16) {
test.AddInput<MLFloat16>("X", dims, f_X);
test.AddOutput<MLFloat16>("Y", dims, f_Y);
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider});
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider});
}
#endif
@ -222,11 +222,11 @@ TEST_F(ActivationOpTest, Sigmoid_bfloat16) {
#endif
OpTester test("Sigmoid", 14);
auto formula = [](float x) {
auto y = 1.f / (1.f + std::exp(-std::abs(x))); // safe sigmoid
y = x > 0 ? y : 1 - y;
return y;
};
auto formula = [](float x) {
auto y = 1.f / (1.f + std::exp(-std::abs(x))); // safe sigmoid
y = x > 0 ? y : 1 - y;
return y;
};
std::vector<float> X = input_values.front();
std::vector<float> Y;
@ -244,7 +244,7 @@ TEST_F(ActivationOpTest, Sigmoid_bfloat16) {
execution_providers.push_back(DefaultCudaExecutionProvider());
#elif USE_ROCM
execution_providers.push_back(DefaultRocmExecutionProvider());
#endif
#endif
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {}, nullptr, &execution_providers);
}
@ -276,7 +276,7 @@ TEST_F(ActivationOpTest, Tanh_bfloat16) {
execution_providers.push_back(DefaultCudaExecutionProvider());
#elif USE_ROCM
execution_providers.push_back(DefaultRocmExecutionProvider());
#endif
#endif
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {}, nullptr, &execution_providers);
}
@ -308,7 +308,7 @@ TEST_F(ActivationOpTest, Relu_bfloat16) {
execution_providers.push_back(DefaultCudaExecutionProvider());
#elif USE_ROCM
execution_providers.push_back(DefaultRocmExecutionProvider());
#endif
#endif
test.Run(OpTester::ExpectResult::kExpectSuccess, "", {}, nullptr, &execution_providers);
}
#endif
@ -431,9 +431,9 @@ TEST_F(ActivationOpTest, Softplus) {
input_values,
[](float x) {
if (x > 0)
return x + logf(expf(-x) + 1);
return x + log1pf(expf(-x));
else
return logf(expf(x) + 1);
return log1pf(expf(x));
});
}