test.cc 31.0 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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 <random>
#include <string>
#include <vector>
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "gtest/gtest.h"
T
tensor-tang 已提交
21
#include "paddle/fluid/operators/jit/kernels.h"
T
tensor-tang 已提交
22
#include "paddle/fluid/platform/cpu_info.h"
T
tensor-tang 已提交
23
#include "paddle/fluid/platform/place.h"
T
tensor-tang 已提交
24

25
DEFINE_double(acc, 1e-5, "Test accuracy threshold.");
26

T
tensor-tang 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
template <typename T>
void RandomVec(const int n, T* a, const T lower = static_cast<T>(-20.f),
               const T upper = static_cast<T>(20.f)) {
  static unsigned int seed = 100;
  std::mt19937 rng(seed++);
  std::uniform_real_distribution<double> uniform_dist(0, 1);
  for (int i = 0; i < n; ++i) {
    a[i] = static_cast<T>(uniform_dist(rng) * (upper - lower) + lower);
  }
}

template <typename T>
void ExpectEQ(const T* target, const T* refer, int n) {
  if (std::is_floating_point<T>::value) {
    for (int i = 0; i < n; ++i) {
42
      EXPECT_NEAR(target[i], refer[i], FLAGS_acc);
T
tensor-tang 已提交
43 44 45 46 47 48 49 50
    }
  } else {
    for (int i = 0; i < n; ++i) {
      EXPECT_EQ(target[i], refer[i]);
    }
  }
}

T
tensor-tang 已提交
51 52
std::vector<int> TestSizes() {
  std::vector<int> s;
T
tensor-tang 已提交
53
  for (int i = 1; i < 32; ++i) {
T
tensor-tang 已提交
54 55
    s.push_back(i);
  }
T
tensor-tang 已提交
56 57 58 59
  // test some large size
  s.push_back(100);
  s.push_back(1000);
  s.push_back(2000);
T
tensor-tang 已提交
60 61 62
  return s;
}

T
tensor-tang 已提交
63
namespace jit = paddle::operators::jit;
64
using CPUPlace = paddle::platform::CPUPlace;
T
tensor-tang 已提交
65 66 67

template <typename KernelTuples, typename... Args>
struct TestFuncWithRefer {
68 69 70
  void operator()(const typename KernelTuples::func_type tgt, Args... args) {
    LOG(FATAL) << "Should specify this function.";
  }
T
tensor-tang 已提交
71 72 73 74 75 76
};

template <typename T>
struct TestFuncWithRefer<jit::XYZNTuples<T>, std::vector<T>, std::vector<T>,
                         std::vector<T>> {
  void operator()(const typename jit::XYZNTuples<T>::func_type tgt,
77 78
                  const std::vector<T>& x, const std::vector<T>& y,
                  const std::vector<T>& zref) {
T
tensor-tang 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    EXPECT_TRUE(tgt != nullptr);
    EXPECT_EQ(zref.size(), x.size());
    EXPECT_EQ(zref.size(), y.size());
    const T* x_data = x.data();
    const T* y_data = y.data();
    const T* zref_data = zref.data();
    const int d = zref.size();

    std::vector<T> ztgt(d);
    T* ztgt_data = ztgt.data();
    // test normal
    tgt(x_data, y_data, ztgt_data, d);
    ExpectEQ<T>(ztgt_data, zref_data, d);
    // test inplace x
    std::copy(x.begin(), x.end(), ztgt.begin());
    tgt(ztgt_data, y_data, ztgt_data, d);
    ExpectEQ<T>(ztgt_data, zref_data, d);
    // test inplace y
    std::copy(y.begin(), y.end(), ztgt.begin());
    tgt(x_data, ztgt_data, ztgt_data, d);
    ExpectEQ<T>(ztgt_data, zref_data, d);
  }
};

template <typename T>
struct TestFuncWithRefer<jit::AXYNTuples<T>, T, std::vector<T>,
                         std::vector<T>> {
  void operator()(const typename jit::AXYNTuples<T>::func_type tgt, const T a,
                  const std::vector<T>& x, const std::vector<T>& yref) {
    EXPECT_TRUE(tgt != nullptr);
    EXPECT_EQ(yref.size(), x.size());
    const T* x_data = x.data();
    const T* yref_data = yref.data();
    const int d = yref.size();
    std::vector<T> ytgt(d);
    T* ytgt_data = ytgt.data();
    // test normal
    tgt(&a, x_data, ytgt_data, d);
    ExpectEQ<T>(ytgt_data, yref_data, d);
    // test inplace x
    std::copy(x.begin(), x.end(), ytgt.begin());
    tgt(&a, ytgt_data, ytgt_data, d);
    ExpectEQ<T>(ytgt_data, yref_data, d);
  }
};

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
template <typename T>
struct TestFuncWithRefer<jit::SoftmaxTuples<T>, std::vector<T>, std::vector<T>,
                         int, int> {
  void operator()(const typename jit::SoftmaxTuples<T>::func_type tgt,
                  const std::vector<T>& x, const std::vector<T>& yref, int n,
                  int bs) {
    EXPECT_TRUE(tgt != nullptr);
    EXPECT_EQ(yref.size(), x.size());
    EXPECT_EQ(x.size(), static_cast<size_t>(n * bs));
    const T* x_data = x.data();
    const T* yref_data = yref.data();
    std::vector<T> ytgt(n * bs);
    T* ytgt_data = ytgt.data();
    // test normal
    tgt(x_data, ytgt_data, n, bs);
    ExpectEQ<T>(ytgt_data, yref_data, n * bs);
    // test inplace x
    std::copy(x.begin(), x.end(), ytgt.begin());
    tgt(ytgt_data, ytgt_data, n, bs);
    ExpectEQ<T>(ytgt_data, yref_data, n * bs);
  }
};

template <typename T>
struct TestFuncWithRefer<jit::XRNTuples<T>, std::vector<T>, T> {
  void operator()(const typename jit::XRNTuples<T>::func_type tgt,
                  const std::vector<T>& x, const T ref_res) {
    EXPECT_TRUE(tgt != nullptr);
    T tgt_res;
    tgt(x.data(), &tgt_res, x.size());
    ExpectEQ<T>(&tgt_res, &ref_res, 1);
  }
};

T
tensor-tang 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
template <typename T>
struct TestFuncWithRefer<jit::XYNTuples<T>, std::vector<T>, std::vector<T>> {
  void operator()(const typename jit::XYNTuples<T>::func_type tgt,
                  const std::vector<T>& x, const std::vector<T>& yref) {
    EXPECT_TRUE(tgt != nullptr);
    EXPECT_EQ(yref.size(), x.size());
    const T* x_data = x.data();
    const T* yref_data = yref.data();
    const int d = yref.size();
    std::vector<T> ytgt(d);
    T* ytgt_data = ytgt.data();
    // test normal
    tgt(x_data, ytgt_data, d);
    ExpectEQ<T>(ytgt_data, yref_data, d);
    // test inplace x
    std::copy(x.begin(), x.end(), ytgt.begin());
    tgt(ytgt_data, ytgt_data, d);
    ExpectEQ<T>(ytgt_data, yref_data, d);
  }
};

template <typename T>
struct TestFuncWithRefer<jit::LSTMTuples<T>, std::vector<T>, std::vector<T>,
182 183
                         std::vector<T>, std::vector<T>, std::vector<T>,
                         typename jit::LSTMTuples<T>::attr_type> {
T
tensor-tang 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  void operator()(const typename jit::LSTMTuples<T>::func_type tgt,
                  const std::vector<T>& xsrc, const std::vector<T>& wp,
                  const std::vector<T>& ct_1, const std::vector<T>& ct_ref,
                  const std::vector<T>& ht_ref,
                  const typename jit::LSTMTuples<T>::attr_type& attr) {
    EXPECT_TRUE(tgt != nullptr);
    EXPECT_EQ(ct_ref.size(), ht_ref.size());
    EXPECT_EQ(ct_1.size(), ht_ref.size());
    EXPECT_EQ(xsrc.size(), 4 * ht_ref.size());
    EXPECT_EQ(wp.size(), 3 * ht_ref.size());

    // x could be changed after compute, so copy to save src
    int d = ht_ref.size();
    std::vector<T> x(xsrc.size()), ct(ct_ref.size()), ht(ht_ref.size());
    std::vector<T> checked(2 * d);
    std::copy(xsrc.begin(), xsrc.end(), x.begin());

    const T* ct_1_data = ct_1.data();
    const T* wp_data = wp.data();
    const T* ct_ref_data = ct_ref.data();
    const T* ht_ref_data = ht_ref.data();
    T* x_data = x.data();
    T* ct_data = ct.data();
    T* ht_data = ht.data();
    T* checked_data = checked.data();

210
    jit::lstm_t step;
T
tensor-tang 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    step.gates = x_data;
    step.ct_1 = ct_1_data;
    step.ct = ct_data;
    step.ht = ht_data;
    if (attr.use_peephole) {
      step.wp = wp_data;
      step.checked = checked_data;
    }

    tgt(&step, &attr);
    ExpectEQ<T>(ct_data, ct_ref_data, d);
    ExpectEQ<T>(ht_data, ht_ref_data, d);
  }
};

template <typename T>
struct TestFuncWithRefer<jit::GRUTuples<T>, std::vector<T>, std::vector<T>,
228 229
                         std::vector<T>,
                         typename jit::GRUTuples<T>::attr_type> {
T
tensor-tang 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  void operator()(const typename jit::GRUTuples<T>::func_type tgt,
                  const std::vector<T>& xsrc, const std::vector<T>& ht_1,
                  const std::vector<T>& ht_ref,
                  const typename jit::GRUTuples<T>::attr_type& attr) {
    EXPECT_TRUE(tgt != nullptr);
    EXPECT_EQ(ht_1.size(), ht_ref.size());
    EXPECT_EQ(xsrc.size(), 3 * ht_ref.size());

    // x could be changed after compute, so copy to save src
    int d = ht_ref.size();
    std::vector<T> x(xsrc.size()), ht(ht_ref.size());
    std::copy(xsrc.begin(), xsrc.end(), x.begin());
    const T* ht_1_data = ht_1.data();
    const T* ht_ref_data = ht_ref.data();
    T* x_data = x.data();
    T* ht_data = ht.data();
246
    jit::gru_t step;
T
tensor-tang 已提交
247 248 249 250 251 252 253 254
    step.gates = x_data;
    step.ht_1 = ht_1_data;
    step.ht = ht_data;
    tgt(&step, &attr);
    ExpectEQ<T>(ht_data, ht_ref_data, d);
  }
};

255
template <typename T>
256 257
struct TestFuncWithRefer<jit::SeqPoolTuples<T>, std::vector<T>, std::vector<T>,
                         typename jit::SeqPoolTuples<T>::attr_type> {
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  void operator()(const typename jit::SeqPoolTuples<T>::func_type tgt,
                  const std::vector<T>& x, const std::vector<T>& yref,
                  const typename jit::SeqPoolTuples<T>::attr_type& attr) {
    EXPECT_TRUE(tgt != nullptr);
    EXPECT_EQ(x.size() % yref.size(), 0);
    int w = yref.size();
    std::vector<T> y(w);
    const T* x_data = x.data();
    const T* yref_data = yref.data();
    T* y_data = y.data();
    tgt(x_data, y_data, &attr);
    ExpectEQ<T>(y_data, yref_data, w);
  }
};

T
tensor-tang 已提交
273
template <typename T>
274
struct TestFuncWithRefer<jit::MatMulTuples<T>, std::vector<T>, std::vector<T>,
275 276
                         std::vector<T>,
                         typename jit::MatMulTuples<T>::attr_type> {
T
tensor-tang 已提交
277 278
  void operator()(const typename jit::MatMulTuples<T>::func_type tgt,
                  const std::vector<T>& a, const std::vector<T>& b,
279 280
                  const std::vector<T>& cref,
                  const typename jit::MatMulTuples<T>::attr_type& attr) {
T
tensor-tang 已提交
281
    EXPECT_TRUE(tgt != nullptr);
282 283 284
    EXPECT_EQ(a.size(), static_cast<size_t>(attr.m * attr.k));
    EXPECT_EQ(b.size(), static_cast<size_t>(attr.k * attr.n));
    EXPECT_EQ(cref.size(), static_cast<size_t>(attr.m * attr.n));
T
tensor-tang 已提交
285 286 287 288 289
    std::vector<T> c(cref.size());
    const T* a_data = a.data();
    const T* b_data = b.data();
    const T* cref_data = cref.data();
    T* c_data = c.data();
290 291
    tgt(a_data, b_data, c_data, &attr);
    ExpectEQ<T>(c_data, cref_data, attr.m * attr.n);
T
tensor-tang 已提交
292 293 294
  }
};

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
template <typename T>
struct TestFuncWithRefer<jit::LayerNormTuples<T>, std::vector<T>,
                         std::vector<T>, std::vector<T>, std::vector<T>,
                         std::vector<T>, std::vector<T>, int, float, int> {
  void operator()(const typename jit::LayerNormTuples<T>::func_type tgt,
                  std::vector<T>& x, std::vector<T>& outref,  // NOLINT
                  std::vector<T>& mean, std::vector<T>& var,  // NOLINT
                  const std::vector<T>& scale, const std::vector<T>& bias,
                  int left, const float epsilon, int right) {
    EXPECT_TRUE(tgt != nullptr);
    EXPECT_EQ(x.size(), static_cast<size_t>(left * right));
    EXPECT_EQ(outref.size(), static_cast<size_t>(left * right));
    EXPECT_EQ(mean.size(), static_cast<size_t>(left));
    EXPECT_EQ(var.size(), static_cast<size_t>(left));
    EXPECT_EQ(scale.size(), static_cast<size_t>(right));
    EXPECT_EQ(bias.size(), static_cast<size_t>(right));
    std::vector<T> outtgt(outref.size());
    const T* scale_data = scale.data();
    const T* bias_data = bias.data();
    T* x_data = x.data();
    T* mean_data = mean.data();
    T* var_data = var.data();
    T* outref_data = outref.data();
    T* outtgt_data = outtgt.data();

    tgt(x_data, outtgt_data, mean_data, var_data, scale_data, bias_data, left,
        epsilon, right);
    ExpectEQ<T>(outtgt_data, outref_data, left * right);
  }
};

template <typename T>
struct TestFuncWithRefer<jit::CRFDecodingTuples<T>, int, std::vector<T>,
                         std::vector<T>, std::vector<T>, std::vector<int>,
                         int> {
  void operator()(const typename jit::CRFDecodingTuples<T>::func_type tgt,
                  const int seq_len, const std::vector<T>& x,
                  const std::vector<T>& w, std::vector<T>& alpharef,  // NOLINT
                  std::vector<int>& trackref, int tag_num) {          // NOLINT
    constexpr int state_trans_base_idx = 2;
    EXPECT_TRUE(tgt != nullptr);
    EXPECT_EQ(x.size(), static_cast<size_t>(seq_len * tag_num));
    EXPECT_EQ(w.size(),
              static_cast<size_t>((tag_num + state_trans_base_idx) * tag_num));
    EXPECT_EQ(alpharef.size(), static_cast<size_t>(seq_len * tag_num));
    EXPECT_EQ(trackref.size(), static_cast<size_t>(seq_len * tag_num));
    std::vector<T> alphatgt(alpharef.size());
    std::vector<int> tracktgt(trackref.size());

    memcpy(trackref.data(), tracktgt.data(), tag_num * sizeof(int));
    tgt(seq_len, (const T*)x.data(), (const T*)w.data(), alphatgt.data(),
        tracktgt.data(), tag_num);
    ExpectEQ<T>(alpharef.data(), alphatgt.data(), seq_len * tag_num);
    ExpectEQ<int>(trackref.data(), tracktgt.data(), seq_len * tag_num);
  }
};

352 353
template <jit::KernelType KT, typename KernelTuples, typename PlaceType,
          typename... Args>
T
tensor-tang 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
void TestAllImpls(const typename KernelTuples::attr_type& attr, Args... args) {
  TestFuncWithRefer<KernelTuples, Args...> test;
  // test jitcode
  auto jitcode = jit::GetJitCode<KT, KernelTuples, PlaceType>(attr);
  if (jitcode) {
    VLOG(10) << "Test Jitcode Kernel ";
    test(jitcode, args...);
  }
  // test all impls in more
  jit::KernelKey kkey(KT, PlaceType());
  auto& pool = jit::KernelPool().Instance().AllKernels();
  auto iter = pool.find(kkey);
  if (iter != pool.end()) {
    auto& impls = iter->second;
    for (auto& impl : impls) {
T
tensor-tang 已提交
369
      auto i = dynamic_cast<const jit::KernelMore<KernelTuples>*>(impl.get());
T
tensor-tang 已提交
370 371
      if (i && i->UseMe(attr)) {
        auto more = i->GetFunc();
T
tensor-tang 已提交
372
        VLOG(10) << "Test More Kernel : " << i->ImplType();
T
tensor-tang 已提交
373 374 375 376 377
        test(more, args...);
      }
    }
  }
  // test result from Get function
T
tensor-tang 已提交
378
  // VLOG(10) << "Test Get function ";
T
tensor-tang 已提交
379 380
  auto tgt = jit::Get<KT, KernelTuples, PlaceType>(attr);
  test(tgt, args...);
T
tensor-tang 已提交
381 382
}

383
template <jit::KernelType KT, typename T, typename PlaceType>
384
void TestXYZNKernel() {
385
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
T
tensor-tang 已提交
386
  for (int d : TestSizes()) {
387
    auto ref = jit::GetRefer<KT, jit::XYZNTuples<T>>();
T
tensor-tang 已提交
388 389
    EXPECT_TRUE(ref != nullptr);

T
tensor-tang 已提交
390
    std::vector<T> x(d), y(d), zref(d);
T
tensor-tang 已提交
391 392 393
    RandomVec<T>(d, x.data());
    RandomVec<T>(d, y.data());

T
tensor-tang 已提交
394 395 396 397 398 399 400 401 402 403 404
    std::vector<T> xinp(d), yinp(d);  // inplace test
    std::copy(x.begin(), x.end(), xinp.begin());
    std::copy(y.begin(), y.end(), yinp.begin());

    const T* x_data = x.data();
    const T* y_data = y.data();
    T* zref_data = zref.data();
    T* xinp_data = xinp.data();
    T* yinp_data = yinp.data();

    // test refer code inplace
T
tensor-tang 已提交
405
    ref(x_data, y_data, zref_data, d);
T
tensor-tang 已提交
406 407 408 409 410
    ref(x_data, yinp_data, yinp_data, d);
    ref(xinp_data, y_data, xinp_data, d);
    ExpectEQ<T>(xinp_data, zref_data, d);
    ExpectEQ<T>(yinp_data, zref_data, d);

T
tensor-tang 已提交
411 412
    TestAllImpls<KT, jit::XYZNTuples<T>, PlaceType, std::vector<T>,
                 std::vector<T>, std::vector<T>>(d, x, y, zref);
T
tensor-tang 已提交
413 414
  }
}
T
tensor-tang 已提交
415

416
template <jit::KernelType KT, typename T, typename PlaceType>
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
void TestAXYNKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
  for (int d : TestSizes()) {
    auto ref = jit::GetRefer<KT, jit::AXYNTuples<T>>();
    EXPECT_TRUE(ref != nullptr);

    const T a = static_cast<T>(3);
    std::vector<T> x(d), yref(d);
    std::vector<T> xinp(d);  // inplace test
    RandomVec<T>(d, x.data());
    std::copy(x.begin(), x.end(), xinp.begin());

    const T* x_data = x.data();
    T* yref_data = yref.data();
    T* xinp_data = xinp.data();
    // test refer code inplace
    ref(&a, x_data, yref_data, d);
    ref(&a, xinp_data, xinp_data, d);
    ExpectEQ<T>(xinp_data, yref_data, d);

T
tensor-tang 已提交
437 438
    TestAllImpls<KT, jit::AXYNTuples<T>, PlaceType, T, std::vector<T>,
                 std::vector<T>>(d, a, x, yref);
439 440 441
  }
}

442 443 444
template <jit::KernelType KT, typename T, typename PlaceType>
void TestXRNKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
445 446
  auto last_acc = FLAGS_acc;
  FLAGS_acc = 1e-4;
447 448 449 450
  for (int d : TestSizes()) {
    auto ref = jit::GetRefer<KT, jit::XRNTuples<T>>();
    EXPECT_TRUE(ref != nullptr);
    std::vector<T> x(d);
T
tensor-tang 已提交
451
    RandomVec<T>(d, x.data(), -2.f, 2.f);
452 453 454 455 456
    T ref_res;
    ref(x.data(), &ref_res, d);
    TestAllImpls<KT, jit::XRNTuples<T>, PlaceType, std::vector<T>, T>(d, x,
                                                                      ref_res);
  }
457
  FLAGS_acc = last_acc;
458 459 460
}

template <jit::KernelType KT, typename T, typename PlaceType>
461 462 463 464 465 466 467 468
void TestXYNKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
  for (int d : TestSizes()) {
    auto ref = jit::GetRefer<KT, jit::XYNTuples<T>>();
    EXPECT_TRUE(ref != nullptr);

    std::vector<T> x(d), yref(d);
    std::vector<T> xinp(d);  // inplace test
469
    RandomVec<T>(d, x.data(), -2.f, 2.f);
470 471 472 473 474 475 476 477 478 479
    std::copy(x.begin(), x.end(), xinp.begin());

    const T* x_data = x.data();
    T* yref_data = yref.data();
    T* xinp_data = xinp.data();
    // test refer code inplace
    ref(x_data, yref_data, d);
    ref(xinp_data, xinp_data, d);
    ExpectEQ<T>(xinp_data, yref_data, d);

T
tensor-tang 已提交
480 481
    TestAllImpls<KT, jit::XYNTuples<T>, PlaceType, std::vector<T>,
                 std::vector<T>>(d, x, yref);
482 483 484
  }
}

485
template <jit::KernelType KT, typename T, typename PlaceType>
T
tensor-tang 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
void TestLSTMKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
  std::vector<std::string> all_acts = {"sigmoid", "tanh", "relu", "identity"};
  for (int d : TestSizes()) {
    for (bool use_peephole : {true, false}) {
      for (auto& act_gate : all_acts) {
        for (auto& act_cand : all_acts) {
          for (auto& act_cell : all_acts) {
            const jit::lstm_attr_t attr(
                d, jit::to_kerneltype(act_gate), jit::to_kerneltype(act_cand),
                jit::to_kerneltype(act_cell), use_peephole);
            auto ref = jit::GetRefer<KT, jit::LSTMTuples<T>>();
            EXPECT_TRUE(ref != nullptr);
            std::vector<T> xsrc(4 * d), wp(3 * d), ct_1(d);
            std::vector<T> ct_ref(d), ht_ref(d), checked(2 * d);
            RandomVec<T>(4 * d, xsrc.data(), -2.f, 2.f);
502 503
            RandomVec<T>(3 * d, wp.data(), -1.f, 1.f);
            RandomVec<T>(d, ct_1.data(), -1.f, 1.f);
T
tensor-tang 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
            // x could be changed after compute, so copy to save src
            std::vector<T> x(xsrc.size());
            std::copy(xsrc.begin(), xsrc.end(), x.begin());
            const T* ct_1_data = ct_1.data();
            const T* wp_data = wp.data();
            T* x_data = x.data();
            T* checked_data = checked.data();
            T* ct_ref_data = ct_ref.data();
            T* ht_ref_data = ht_ref.data();
            jit::lstm_t step;
            step.gates = x_data;
            step.ct_1 = ct_1_data;
            step.ct = ct_ref_data;
            step.ht = ht_ref_data;
            if (use_peephole) {
              step.wp = wp_data;
              step.checked = checked_data;
            }
            ref(&step, &attr);
T
tensor-tang 已提交
523
            VLOG(10) << attr;
T
tensor-tang 已提交
524 525 526 527
            TestAllImpls<KT, jit::LSTMTuples<T>, PlaceType, std::vector<T>,
                         std::vector<T>, std::vector<T>, std::vector<T>,
                         std::vector<T>>(attr, xsrc, wp, ct_1, ct_ref, ht_ref,
                                         attr);
T
tensor-tang 已提交
528 529 530 531 532 533 534
          }
        }
      }
    }
  }
}

535
template <jit::KernelType KT, typename T, typename PlaceType>
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
void TestGRUKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
  std::vector<std::string> all_acts = {"sigmoid", "tanh", "relu", "identity"};
  for (int d : TestSizes()) {
    for (auto& act_gate : all_acts) {
      for (auto& act_cand : all_acts) {
        const jit::gru_attr_t attr(d, jit::to_kerneltype(act_gate),
                                   jit::to_kerneltype(act_cand));
        auto ref = jit::GetRefer<KT, jit::GRUTuples<T>>();
        EXPECT_TRUE(ref != nullptr);
        std::vector<T> xsrc(3 * d), ht_1(d), ht_ref(d);
        RandomVec<T>(3 * d, xsrc.data(), -2.f, 2.f);
        RandomVec<T>(d, ht_1.data(), -2.f, 2.f);
        // x could be changed after compute, so copy to save src
        std::vector<T> x(xsrc.size());
        std::copy(xsrc.begin(), xsrc.end(), x.begin());
        const T* ht_1_data = ht_1.data();
        T* x_data = x.data();
        T* ht_ref_data = ht_ref.data();
        jit::gru_t step;
        step.gates = x_data;
        step.ht_1 = ht_1_data;
        step.ht = ht_ref_data;
        ref(&step, &attr);
T
tensor-tang 已提交
560
        VLOG(10) << attr;
T
tensor-tang 已提交
561 562 563
        TestAllImpls<KT, jit::GRUTuples<T>, PlaceType, std::vector<T>,
                     std::vector<T>, std::vector<T>>(attr, xsrc, ht_1, ht_ref,
                                                     attr);
564 565 566 567 568
      }
    }
  }
}

569
template <jit::KernelType KT, typename T, typename PlaceType>
570 571
void TestSeqPoolKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
572 573
  std::vector<jit::SeqPoolType> pool_types = {
      jit::SeqPoolType::kSum, jit::SeqPoolType::kAvg, jit::SeqPoolType::kSqrt};
574
  for (auto type : pool_types) {
T
tensor-tang 已提交
575 576 577 578
    for (int w : TestSizes()) {
      jit::seq_pool_attr_t attr(w, type);
      for (int h : TestSizes()) {
        attr.h = h;
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
        auto ref = jit::GetRefer<KT, jit::SeqPoolTuples<T>>();
        EXPECT_TRUE(ref != nullptr);
        std::vector<T> x(h * w), yref(w);
        RandomVec<T>(h * w, x.data(), -2.f, 2.f);
        const T* x_data = x.data();
        T* yref_data = yref.data();
        ref(x_data, yref_data, &attr);
        VLOG(10) << attr;
        TestAllImpls<KT, jit::SeqPoolTuples<T>, PlaceType, std::vector<T>,
                     std::vector<T>>(attr, x, yref, attr);
      }
    }
  }
}

594
template <jit::KernelType KT, typename T, typename PlaceType>
T
tensor-tang 已提交
595 596
void TestMatMulKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
597 598 599 600
  auto last_acc = FLAGS_acc;
  // TODO(intel): fix MKL acc issue
  // https://github.com/PaddlePaddle/Paddle/issues/15447
  FLAGS_acc = 1e-3;
T
tensor-tang 已提交
601 602 603 604 605 606
  for (int m : {1, 2, 3, 4}) {
    for (int n : {1, 2, 3, 4}) {
      for (int k : TestSizes()) {
        auto ref = jit::GetRefer<KT, jit::MatMulTuples<T>>();
        EXPECT_TRUE(ref != nullptr);
        std::vector<T> a(m * k), b(k * n), c(m * n);
607 608
        RandomVec<T>(m * k, a.data(), -2.f, 2.f);
        RandomVec<T>(k * n, b.data(), -2.f, 2.f);
T
tensor-tang 已提交
609 610 611
        const T* a_data = a.data();
        const T* b_data = b.data();
        T* c_data = c.data();
612 613
        const jit::matmul_attr_t attr{m, n, k};
        ref(a_data, b_data, c_data, &attr);
T
tensor-tang 已提交
614
        TestAllImpls<KT, jit::MatMulTuples<T>, PlaceType, std::vector<T>,
615
                     std::vector<T>, std::vector<T>>(attr, a, b, c, attr);
T
tensor-tang 已提交
616 617 618
      }
    }
  }
619
  FLAGS_acc = last_acc;
T
tensor-tang 已提交
620 621
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
template <jit::KernelType KT, typename T, typename PlaceType>
void TestSoftmaxKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
  for (int bs : {1, 2, 10}) {
    for (int n : TestSizes()) {
      auto ref = jit::GetRefer<KT, jit::SoftmaxTuples<T>>();
      EXPECT_TRUE(ref != nullptr);
      std::vector<T> x(bs * n), y(bs * n);
      RandomVec<T>(bs * n, x.data(), -2.f, 2.f);
      const T* x_data = x.data();
      T* y_data = y.data();

      std::vector<T> xinp(x.size());  // inplace test
      std::copy(x.begin(), x.end(), xinp.begin());
      ref(x_data, y_data, n, bs);
      T* xinp_data = xinp.data();
      ref(xinp_data, xinp_data, n, bs);
      ExpectEQ<T>(xinp_data, y_data, n * bs);

      TestAllImpls<KT, jit::SoftmaxTuples<T>, PlaceType, std::vector<T>,
                   std::vector<T>>(n, x, y, n, bs);
    }
  }
}

template <jit::KernelType KT, typename T, typename PlaceType>
T
tensor-tang 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
void TestNCHW16CMulNCKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
  const int n = 3, c = 16 * 4, h = 10, w = 10;
  auto ref = jit::GetRefer<KT, jit::NCHW16CMulNCTuples<T>>();
  EXPECT_TRUE(ref != nullptr);
  int sz = n * c * h * w;
  std::vector<T> x(sz), y(n * c), zref(sz);
  std::vector<T> ztgt(sz), zjit(sz);
  RandomVec<T>(sz, x.data(), -2.f, 2.f);
  RandomVec<T>(n * c, y.data(), -2.f, 2.f);

  const T* x_data = x.data();
  const T* y_data = y.data();
  T* zref_data = zref.data();
  T* ztgt_data = ztgt.data();
  T* zjit_data = zjit.data();
  constexpr int simd_width = ZMM_FLOAT_BLOCK;
  int C = c / simd_width;
  auto tgt = jit::Get<KT, jit::NCHW16CMulNCTuples<T>, PlaceType>(0);
  auto jitcode = jit::GetJitCode<KT, jit::NCHW16CMulNCTuples<T>, PlaceType>(0);
  EXPECT_TRUE(tgt != nullptr);

  if (std::is_same<T, float>::value &&
      paddle::platform::MayIUse(paddle::platform::avx512f)) {
    EXPECT_TRUE(jitcode != nullptr);
  }
  for (int ni = 0; ni < n; ni++) {
    for (int ci = 0; ci < C; ci++) {
      auto ptr_x =
          x_data + ni * C * h * w * simd_width + ci * h * w * simd_width;
      auto ptr_y = y_data + ni * C * simd_width + ci * simd_width;
      auto ptr_zref =
          zref_data + ni * C * h * w * simd_width + ci * h * w * simd_width;
      auto ptr_ztgt =
          ztgt_data + ni * C * h * w * simd_width + ci * h * w * simd_width;

      ref(ptr_x, ptr_y, ptr_zref, h, w);
      tgt(ptr_x, ptr_y, ptr_ztgt, h, w);

      if (jitcode) {
        auto ptr_zjit =
            zjit_data + ni * C * h * w * simd_width + ci * h * w * simd_width;
        jitcode(ptr_x, ptr_y, ptr_zjit, h, w);
      }
    }
  }
  ExpectEQ<T>(ztgt_data, zref_data, sz);
  if (jitcode) {
    ExpectEQ<T>(zjit_data, zref_data, sz);
  }
}

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void TestLayerNormKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
  const T epsilon = 9.99999975e-06;
  for (int n : {1, 2, 10}) {
    for (int x_dim_0 : {1, 9, 17, 50}) {
      int left = n * x_dim_0;
      for (int x_dim_1 : TestSizes()) {
        int right = x_dim_1;
        auto ref = jit::GetRefer<KT, jit::LayerNormTuples<T>>();
        EXPECT_TRUE(ref != nullptr);
        int sz = left * right;
        std::vector<T> x(sz), mean(left), var(left), scale(right), bias(right),
            outref(sz);
        RandomVec<T>(sz, x.data(), -2.f, 2.f);
        RandomVec<T>(left, mean.data(), -2.f, 2.f);
        RandomVec<T>(left, var.data(), -2.f, 2.f);
        RandomVec<T>(right, scale.data(), -2.f, 2.f);
        RandomVec<T>(right, bias.data(), -2.f, 2.f);

        const T* scale_data = scale.data();
        const T* bias_data = bias.data();
        T* x_data = x.data();
        T* mean_data = mean.data();
        T* var_data = var.data();
        T* outref_data = outref.data();

        ref(x_data, outref_data, mean_data, var_data, scale_data, bias_data,
            left, epsilon, right);

        TestAllImpls<KT, jit::LayerNormTuples<T>, PlaceType, std::vector<T>,
                     std::vector<T>, std::vector<T>, std::vector<T>,
                     std::vector<T>, std::vector<T>, int, float>(
            right, x, outref, mean, var, scale, bias, left, epsilon, right);
      }
    }
  }
}

template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void TestCRFDecodingKernel() {
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
  constexpr int state_trans_base_idx = 2;
  for (int seq_len : {1, 11, 17, 50}) {
    for (int tag_num : TestSizes()) {
      auto ref = jit::GetRefer<KT, jit::CRFDecodingTuples<T>>();
      EXPECT_TRUE(ref != nullptr);
      int x_sz = seq_len * tag_num;
      int w_sz = (tag_num + state_trans_base_idx) * tag_num;
      std::vector<T> x(x_sz), w(w_sz), alpharef(x_sz);
      std::vector<int> trackref(x_sz);
      RandomVec<T>(x_sz, x.data(), -2.f, 2.f);
      RandomVec<T>(w_sz, w.data(), -2.f, 2.f);

      ref(seq_len, (const T*)x.data(), (const T*)w.data(), alpharef.data(),
          trackref.data(), tag_num);

      TestAllImpls<KT, jit::CRFDecodingTuples<T>, PlaceType, int,
                   std::vector<T>, std::vector<T>, std::vector<T>,
                   std::vector<int>, int>(tag_num, seq_len, x, w, alpharef,
                                          trackref, tag_num);
    }
  }
}

T
tensor-tang 已提交
765
// XYZNTuple
T
tensor-tang 已提交
766
TEST(JITKernel, kVMul) {
767 768
  TestXYZNKernel<jit::kVMul, float, CPUPlace>();
  TestXYZNKernel<jit::kVMul, double, CPUPlace>();
T
tensor-tang 已提交
769 770
}

T
tensor-tang 已提交
771
TEST(JITKernel, kVAdd) {
772 773
  TestXYZNKernel<jit::kVAdd, float, CPUPlace>();
  TestXYZNKernel<jit::kVAdd, double, CPUPlace>();
T
tensor-tang 已提交
774 775
}

T
tensor-tang 已提交
776
TEST(JITKernel, kVAddRelu) {
777 778
  TestXYZNKernel<jit::kVAddRelu, float, CPUPlace>();
  TestXYZNKernel<jit::kVAddRelu, double, CPUPlace>();
T
tensor-tang 已提交
779 780
}

T
tensor-tang 已提交
781
TEST(JITKernel, kVSub) {
782 783
  TestXYZNKernel<jit::kVSub, float, CPUPlace>();
  TestXYZNKernel<jit::kVSub, double, CPUPlace>();
T
tensor-tang 已提交
784 785 786
}

// AXYNTuples
T
tensor-tang 已提交
787
TEST(JITKernel, kVScal) {
788 789
  TestAXYNKernel<jit::kVScal, float, CPUPlace>();
  TestAXYNKernel<jit::kVScal, double, CPUPlace>();
T
tensor-tang 已提交
790 791
}

T
tensor-tang 已提交
792
TEST(JITKernel, kVAddBias) {
793 794 795 796 797 798 799 800 801 802 803 804 805
  TestAXYNKernel<jit::kVAddBias, float, CPUPlace>();
  TestAXYNKernel<jit::kVAddBias, double, CPUPlace>();
}

// XRNTuples
TEST(JITKernel, kHMax) {
  TestXRNKernel<jit::kHMax, float, CPUPlace>();
  TestXRNKernel<jit::kHMax, double, CPUPlace>();
}

TEST(JITKernel, kHSum) {
  TestXRNKernel<jit::kHSum, float, CPUPlace>();
  TestXRNKernel<jit::kHSum, double, CPUPlace>();
T
tensor-tang 已提交
806 807 808
}

// XYNTuples
T
tensor-tang 已提交
809
TEST(JITKernel, kVRelu) {
810 811
  TestXYNKernel<jit::kVRelu, float, CPUPlace>();
  TestXYNKernel<jit::kVRelu, double, CPUPlace>();
T
tensor-tang 已提交
812 813
}

T
tensor-tang 已提交
814
TEST(JITKernel, kVIdentity) {
815 816
  TestXYNKernel<jit::kVIdentity, float, CPUPlace>();
  TestXYNKernel<jit::kVIdentity, double, CPUPlace>();
T
tensor-tang 已提交
817 818
}

T
tensor-tang 已提交
819
TEST(JITKernel, kVSquare) {
820 821
  TestXYNKernel<jit::kVSquare, float, CPUPlace>();
  TestXYNKernel<jit::kVSquare, double, CPUPlace>();
T
tensor-tang 已提交
822 823
}

T
tensor-tang 已提交
824
TEST(JITKernel, kVExp) {
825 826
  TestXYNKernel<jit::kVExp, float, CPUPlace>();
  TestXYNKernel<jit::kVExp, double, CPUPlace>();
T
tensor-tang 已提交
827 828
}

T
tensor-tang 已提交
829
TEST(JITKernel, kVSigmoid) {
830 831
  TestXYNKernel<jit::kVSigmoid, float, CPUPlace>();
  TestXYNKernel<jit::kVSigmoid, double, CPUPlace>();
T
tensor-tang 已提交
832 833
}

T
tensor-tang 已提交
834
TEST(JITKernel, kVTanh) {
835 836
  TestXYNKernel<jit::kVTanh, float, CPUPlace>();
  TestXYNKernel<jit::kVTanh, double, CPUPlace>();
T
tensor-tang 已提交
837 838 839
}

// LSTM
T
tensor-tang 已提交
840
TEST(JITKernel, kLSTMCtHt) {
841 842
  TestLSTMKernel<jit::kLSTMCtHt, float, CPUPlace>();
  TestLSTMKernel<jit::kLSTMCtHt, double, CPUPlace>();
T
tensor-tang 已提交
843 844
}

T
tensor-tang 已提交
845
TEST(JITKernel, kLSTMC1H1) {
846 847
  TestLSTMKernel<jit::kLSTMC1H1, float, CPUPlace>();
  TestLSTMKernel<jit::kLSTMC1H1, double, CPUPlace>();
T
tensor-tang 已提交
848 849 850
}

// GRU
T
tensor-tang 已提交
851
TEST(JITKernel, kGRUH1) {
852 853
  TestGRUKernel<jit::kGRUH1, float, CPUPlace>();
  TestGRUKernel<jit::kGRUH1, double, CPUPlace>();
854 855
}

T
tensor-tang 已提交
856
TEST(JITKernel, kGRUHtPart1) {
857 858
  TestGRUKernel<jit::kGRUHtPart1, float, CPUPlace>();
  TestGRUKernel<jit::kGRUHtPart1, double, CPUPlace>();
859 860
}

T
tensor-tang 已提交
861
TEST(JITKernel, kGRUHtPart2) {
862 863
  TestGRUKernel<jit::kGRUHtPart2, float, CPUPlace>();
  TestGRUKernel<jit::kGRUHtPart2, double, CPUPlace>();
864 865
}

866
TEST(JITKernel, kSeqPool) {
867 868
  TestSeqPoolKernel<jit::kSeqPool, float, CPUPlace>();
  TestSeqPoolKernel<jit::kSeqPool, double, CPUPlace>();
869 870
}

T
tensor-tang 已提交
871
TEST(JITKernel, kMatMul) {
872 873 874 875 876 877 878
  TestMatMulKernel<jit::kMatMul, float, CPUPlace>();
  TestMatMulKernel<jit::kMatMul, double, CPUPlace>();
}

TEST(JITKernel, kSoftmax) {
  TestSoftmaxKernel<jit::kSoftmax, float, CPUPlace>();
  TestSoftmaxKernel<jit::kSoftmax, double, CPUPlace>();
T
tensor-tang 已提交
879 880
}

T
tensor-tang 已提交
881
TEST(JITKernel, kNCHW16CMulNC) {
882 883
  TestNCHW16CMulNCKernel<jit::kNCHW16CMulNC, float, CPUPlace>();
  TestNCHW16CMulNCKernel<jit::kNCHW16CMulNC, double, CPUPlace>();
T
tensor-tang 已提交
884 885
}

886 887 888 889 890 891 892 893 894 895
TEST(JITKernel, kLayerNorm) {
  TestLayerNormKernel<jit::kLayerNorm, float, paddle::platform::CPUPlace>();
  TestLayerNormKernel<jit::kLayerNorm, double, paddle::platform::CPUPlace>();
}

TEST(JITKernel, kCRFDecoding) {
  TestCRFDecodingKernel<jit::kCRFDecoding, float, paddle::platform::CPUPlace>();
  TestCRFDecodingKernel<jit::kCRFDecoding, double,
                        paddle::platform::CPUPlace>();
}
T
tensor-tang 已提交
896

897 898 899
TEST(JITKernel, pool) {
  // TODO(TJ): add some test
}