/* 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 #include "gflags/gflags.h" #include "glog/logging.h" #include "gtest/gtest.h" #include "paddle/fluid/operators/math/cpu_vec.h" #include "paddle/fluid/platform/port.h" 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) { static unsigned int seed = 100; std::mt19937 rng(seed++); std::uniform_real_distribution uniform_dist(0, 1); const T lower = static_cast(-20.f); const T upper = static_cast(20.f); 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) { namespace jit = paddle::platform::jit; using namespace paddle::operators::math; // 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) { namespace jit = paddle::platform::jit; using namespace paddle::operators::math; // 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) { namespace jit = paddle::platform::jit; using namespace paddle::operators::math; // 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 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) { namespace jit = paddle::platform::jit; using namespace paddle::operators::math; // 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) { namespace jit = paddle::platform::jit; using namespace paddle::operators::math; // 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) { namespace jit = paddle::platform::jit; using namespace paddle::operators::math; // 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); }