/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include #include #include #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/phi/kernels/funcs/cpu_vec.h" namespace phi { namespace tests { inline double GetCurrentUS() { struct timeval time; gettimeofday(&time, NULL); return 1e+6 * time.tv_sec + time.tv_usec; } constexpr int repeat = 1000; template inline T _sigmoid(T x) { const T min = SIGMOID_THRESHOLD_MIN; const T max = SIGMOID_THRESHOLD_MAX; T tmp = (x < min) ? min : ((x > max) ? max : x); return static_cast(1) / (static_cast(1) + std::exp(-tmp)); } template inline T _tanh(T x) { return static_cast(2) * _sigmoid(static_cast(2) * x) - static_cast(1); } template void ref_sigmoid(const int n, const T* x, T* y) { for (int i = 0; i < n; ++i) { y[i] = _sigmoid(x[i]); } } template void ref_tanh(const int n, const T* x, T* y) { for (int i = 0; i < n; ++i) { y[i] = _tanh(x[i]); } } template void ref_relu(const int n, const T* x, T* y) { for (int i = 0; i < n; ++i) { y[i] = x[i] > 0 ? x[i] : 0; } } template void RandomVec(const int n, T* a, const T lower = static_cast(-20.f), const T upper = static_cast(20.f)) { static unsigned int seed = 100; std::mt19937 rng(seed++); std::uniform_real_distribution uniform_dist(0, 1); for (int i = 0; i < n; ++i) { a[i] = static_cast(uniform_dist(rng) * (upper - lower) + lower); } } template void TestAndBench(const int n, std::function tgt, std::function ref) { std::vector x(n); std::vector ytgt(n), yref(n); RandomVec(n, x.data()); const T* x_data = x.data(); T* ytgt_data = ytgt.data(); T* yref_data = yref.data(); auto st = GetCurrentUS(); for (int i = 0; i < repeat; ++i) { tgt(n, x_data, ytgt_data); } auto mt = GetCurrentUS(); for (int i = 0; i < repeat; ++i) { ref(n, x_data, yref_data); } auto et = GetCurrentUS(); VLOG(3) << "Vec size " << n << ": refer takes: " << (et - mt) / repeat << " us, tgt takes: " << (mt - st) / repeat; for (int i = 0; i < n; ++i) { EXPECT_NEAR(ytgt_data[i], yref_data[i], 1e-3); } } TEST(CpuVecTest, sigmoid) { using namespace phi::funcs; // NOLINT for (auto sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { TestAndBench(sz, vec_sigmoid, ref_sigmoid); TestAndBench( sz, vec_sigmoid, ref_sigmoid); TestAndBench( sz, vec_sigmoid, ref_sigmoid); TestAndBench( sz, vec_sigmoid, ref_sigmoid); } TestAndBench(30, vec_sigmoid, ref_sigmoid); } TEST(CpuVecTest, tanh) { using namespace phi::funcs; // NOLINT for (auto sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { TestAndBench(sz, vec_tanh, ref_tanh); TestAndBench( sz, vec_tanh, ref_tanh); TestAndBench( sz, vec_tanh, ref_tanh); TestAndBench( sz, vec_tanh, ref_tanh); } TestAndBench(30, vec_tanh, ref_tanh); } TEST(CpuVecTest, relu) { using namespace phi::funcs; // NOLINT for (auto sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { TestAndBench(sz, vec_relu, ref_relu); TestAndBench( sz, vec_relu, ref_relu); TestAndBench( sz, vec_relu, ref_relu); TestAndBench( sz, vec_relu, ref_relu); } TestAndBench(30, vec_relu, ref_relu); } template void compare_sum(size_t n, std::function tgt, std::function ref) { std::vector x(n); T ytgt_data, yref_data; RandomVec(n, x.data(), static_cast(-2), static_cast(2)); const T* x_data = x.data(); tgt(n, x_data, &ytgt_data); ref(n, x_data, &yref_data); EXPECT_NEAR(ytgt_data, yref_data, 1e-3); } TEST(CpuVecTest, vec_sum) { using namespace phi::funcs; // NOLINT for (size_t sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { compare_sum( sz, vec_sum, vec_sum); compare_sum(sz, vec_sum, vec_sum); } compare_sum( 30U, vec_sum, vec_sum); } template void compare_clip( size_t n, T threshold, std::function tgt, std::function ref) { std::vector x(n); std::vector ytgt(n), yref(n); RandomVec(n, x.data(), static_cast(-2), static_cast(2)); const T* x_data = x.data(); T* yref_data = yref.data(); T* ytgt_data = ytgt.data(); tgt(n, threshold, x_data, ytgt_data); ref(n, threshold, x_data, yref_data); for (size_t i = 0; i < n; ++i) { EXPECT_NEAR(ytgt_data[i], yref_data[i], 1e-3); } } TEST(CpuVecTest, vec_clip) { using namespace phi::funcs; // NOLINT for (size_t sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { compare_clip( sz, -4.f, vec_clip, vec_clip); compare_clip(sz, -1.1f, vec_clip, vec_clip); } compare_clip( 30U, 1.0, vec_clip, vec_clip); } template void compare_mul( size_t n, std::function tgt, std::function ref) { std::vector x(n), y(n); std::vector ztgt(n), zref(n); RandomVec(n, x.data(), static_cast(-2), static_cast(2)); RandomVec(n, y.data(), static_cast(-2), static_cast(2)); const T* x_data = x.data(); const T* y_data = y.data(); T* ztgt_data = ztgt.data(); T* zref_data = zref.data(); tgt(n, x_data, y_data, ztgt_data); ref(n, x_data, y_data, zref_data); for (size_t i = 0; i < n; ++i) { EXPECT_NEAR(ztgt_data[i], zref_data[i], 1e-3); } } TEST(CpuVecTest, vec_mul) { using namespace phi::funcs; // NOLINT for (size_t sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { compare_mul( sz, vec_mul, vec_mul); compare_mul(sz, vec_mul, vec_mul); } compare_mul( 30U, vec_mul, vec_mul); } template void compare_mul_reduce( size_t n, std::function tgt, std::function ref) { std::vector x(n), y(n); T ztgt_data, zref_data; RandomVec(n, x.data(), static_cast(-2), static_cast(2)); RandomVec(n, y.data(), static_cast(-2), static_cast(2)); const T* x_data = x.data(); const T* y_data = y.data(); tgt(n, x_data, y_data, &ztgt_data); ref(n, x_data, y_data, &zref_data); EXPECT_NEAR(ztgt_data, zref_data, 1e-3); } TEST(CpuVecTest, vec_mul_reduce) { using namespace phi::funcs; // NOLINT for (size_t sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { compare_mul_reduce(sz, vec_mul_reduce, vec_mul_reduce); compare_mul_reduce(sz, vec_mul_reduce, vec_mul_reduce); } compare_mul_reduce(30U, vec_mul_reduce, vec_mul_reduce); } template void TestInplace(const int n, std::function tgt, std::function ref) { std::vector x(n); std::vector ytgt(n), yref(n); RandomVec(n, x.data()); const T* x_data = x.data(); T* yref_data = yref.data(); T* ytgt_data = ytgt.data(); std::memcpy(yref_data, x_data, sizeof(T) * n); std::memcpy(ytgt_data, x_data, sizeof(T) * n); ref(n, yref_data, yref_data); tgt(n, ytgt_data, ytgt_data); for (int i = 0; i < n; ++i) { EXPECT_NEAR(ytgt_data[i], yref_data[i], 1e-3); } } TEST(CpuVecTest, inplace_sigmoid) { using namespace phi::funcs; // NOLINT for (auto sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { TestInplace(sz, vec_sigmoid, ref_sigmoid); TestInplace( sz, vec_sigmoid, ref_sigmoid); TestInplace( sz, vec_sigmoid, ref_sigmoid); TestInplace( sz, vec_sigmoid, ref_sigmoid); } TestInplace(30, vec_sigmoid, ref_sigmoid); } TEST(CpuVecTest, inplace_tanh) { using namespace phi::funcs; // NOLINT for (auto sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { TestInplace(sz, vec_tanh, ref_tanh); TestInplace( sz, vec_tanh, ref_tanh); TestInplace( sz, vec_tanh, ref_tanh); TestInplace( sz, vec_tanh, ref_tanh); } TestInplace(30, vec_tanh, ref_tanh); } TEST(CpuVecTest, inplace_relu) { using namespace phi::funcs; // NOLINT for (auto sz : {1, 2, 15, 16, 30, 32, 128, 200, 512}) { TestInplace(sz, vec_relu, ref_relu); TestInplace( sz, vec_relu, ref_relu); TestInplace( sz, vec_relu, ref_relu); TestInplace( sz, vec_relu, ref_relu); } TestInplace(30, vec_relu, ref_relu); } } // namespace tests } // namespace phi