jit_kernel_test.cc 3.8 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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 "paddle/fluid/operators/math/jit_kernel.h"
T
tensor-tang 已提交
16
#include <sys/time.h>
T
tensor-tang 已提交
17 18 19 20 21 22
#include <string>
#include <vector>
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "gtest/gtest.h"

T
tensor-tang 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
inline double GetCurrentUS() {
  struct timeval time;
  gettimeofday(&time, NULL);
  return 1e+6 * time.tv_sec + time.tv_usec;
}

template <typename T>
void RandomVec(const int n, T* a) {
  static unsigned int seed = 100;
  std::mt19937 rng(seed++);
  std::uniform_real_distribution<double> uniform_dist(0, 1);
  const T lower = static_cast<T>(-20.f);
  const T upper = static_cast<T>(20.f);
  for (int i = 0; i < n; ++i) {
    a[i] = static_cast<T>(uniform_dist(rng) * (upper - lower) + lower);
  }
}

constexpr int repeat = 10000;

TEST(JitKernel, vmul) {
  namespace jit = paddle::operators::math::jitkernel;

  auto ref = [](const int n, const float* x, const float* y, float* z) {
    for (int i = 0; i < n; ++i) {
      z[i] = x[i] * y[i];
    }
  };

  for (int d : {7, 8, 15, 16, 30, 256}) {
    std::vector<float> x(d), y(d);
    std::vector<float> zref(d), ztgt(d);
    RandomVec<float>(d, x.data());
    RandomVec<float>(d, y.data());
    const auto& ker =
        jit::KernelPool::Instance().template Get<jit::VMulKernel<float>>(d);

    const float* x_data = x.data();
    const float* y_data = y.data();
    float* ztgt_data = ztgt.data();
    float* zref_data = zref.data();
    auto st = GetCurrentUS();
    for (int i = 0; i < repeat; ++i) {
      ker->Compute(d, x_data, y_data, ztgt_data);
    }
    auto mt = GetCurrentUS();
    for (int i = 0; i < repeat; ++i) {
      ref(d, x_data, y_data, zref_data);
    }
    auto et = GetCurrentUS();

    VLOG(3) << "Vec size " << d << ": refer takes: " << (et - mt) / repeat
            << " us, tgt takes: " << (mt - st) / repeat;
    for (int i = 0; i < d; ++i) {
      EXPECT_NEAR(ztgt_data[i], zref_data[i], 1e-3);
    }
  }
}

T
tensor-tang 已提交
82 83 84 85
TEST(JitKernel, pool) {
  namespace jit = paddle::operators::math::jitkernel;
  const int frame_size = 4;
  std::string act_gate = "sigmoid", act_cand = "tanh", act_cell = "tanh";
T
tensor-tang 已提交
86
  const auto& plstm1 =
T
tensor-tang 已提交
87 88 89 90
      jit::KernelPool::Instance()
          .template Get<jit::LSTMKernel<float>, int, const std::string&,
                        const std::string&, const std::string&>(
              frame_size, act_gate, act_cand, act_cell);
T
tensor-tang 已提交
91
  const auto& plstm2 =
T
tensor-tang 已提交
92 93 94 95
      jit::KernelPool::Instance()
          .template Get<jit::LSTMKernel<float>, int, const std::string&,
                        const std::string&, const std::string&>(
              frame_size, act_gate, act_cand, act_cell);
T
tensor-tang 已提交
96
  EXPECT_EQ(plstm1, plstm2);
T
tensor-tang 已提交
97

T
tensor-tang 已提交
98
  const auto& pvmul_f =
T
tensor-tang 已提交
99
      jit::KernelPool::Instance().template Get<jit::VMulKernel<float>>(4);
T
tensor-tang 已提交
100 101
  EXPECT_TRUE(std::dynamic_pointer_cast<jit::Kernel>(plstm2) !=
              std::dynamic_pointer_cast<jit::Kernel>(pvmul_f));
T
tensor-tang 已提交
102

T
tensor-tang 已提交
103
  const auto& pvmul_d =
T
tensor-tang 已提交
104
      jit::KernelPool::Instance().template Get<jit::VMulKernel<double>>(4);
T
tensor-tang 已提交
105 106 107 108 109 110 111
  EXPECT_TRUE(std::dynamic_pointer_cast<jit::Kernel>(pvmul_f) !=
              std::dynamic_pointer_cast<jit::Kernel>(pvmul_d));

  const auto& pvmul_from_key = jit::KernelPool::Instance().Get("vmulf4");
  EXPECT_TRUE(pvmul_f == pvmul_from_key);
  const auto& pvmul_from_key2 = jit::KernelPool::Instance().Get("vmulf5");
  EXPECT_TRUE(pvmul_from_key2 == nullptr);
T
tensor-tang 已提交
112
}