test.cc 11.5 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/place.h"
T
tensor-tang 已提交
23

T
tensor-tang 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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) {
      EXPECT_NEAR(target[i], refer[i], 1e-3);
    }
  } else {
    for (int i = 0; i < n; ++i) {
      EXPECT_EQ(target[i], refer[i]);
    }
  }
}

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

60
template <typename T, typename KernelTuples>
61 62 63
void TestXYZNFunc(const typename KernelTuples::func_type tgt,
                  const std::vector<T>& x, const std::vector<T>& y,
                  const std::vector<T>& zref) {
T
tensor-tang 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  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);
}

87 88
template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void TestXYZNKernel() {
T
tensor-tang 已提交
89
  namespace jit = paddle::operators::jit;
90
  VLOG(10) << "===== Test JITKernel " << jit::to_string(KT);
T
tensor-tang 已提交
91
  for (int d : TestSizes()) {
92
    auto ref = jit::GetRefer<KT, jit::XYZNTuples<T>>();
T
tensor-tang 已提交
93 94
    EXPECT_TRUE(ref != nullptr);

T
tensor-tang 已提交
95
    std::vector<T> x(d), y(d), zref(d);
T
tensor-tang 已提交
96 97 98
    RandomVec<T>(d, x.data());
    RandomVec<T>(d, y.data());

T
tensor-tang 已提交
99 100 101 102 103 104 105 106 107 108 109
    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 已提交
110
    ref(x_data, y_data, zref_data, d);
T
tensor-tang 已提交
111 112 113 114 115 116
    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);

    // test jitcode
117
    auto jitcode = jit::GetJitCode<KT, jit::XYZNTuples<T>, PlaceType>(d);
T
tensor-tang 已提交
118
    if (jitcode) {
119
      VLOG(10) << "Test Jitcode Kernel, size: " << d;
120
      TestXYZNFunc<T, jit::XYZNTuples<T>>(jitcode, x, y, zref);
T
tensor-tang 已提交
121 122 123 124 125 126 127 128 129
    }

    // 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) {
130
        auto i = dynamic_cast<const jit::KernelImpl<jit::XYZNTuples<T>>*>(
T
tensor-tang 已提交
131
            impl.get());
T
tensor-tang 已提交
132 133 134
        if (i && i->UseMe(d)) {
          auto more = i->GetFunc();
          VLOG(10) << "Test More Kernel, size: " << d;
135
          TestXYZNFunc<T, jit::XYZNTuples<T>>(more, x, y, zref);
T
tensor-tang 已提交
136 137 138 139 140
        }
      }
    }
    // Test result from Get function
    VLOG(10) << "Test Get function, size: " << d;
141
    auto tgt = jit::Get<KT, jit::XYZNTuples<T>, PlaceType>(d);
142
    TestXYZNFunc<T, jit::XYZNTuples<T>>(tgt, x, y, zref);
T
tensor-tang 已提交
143 144
  }
}
T
tensor-tang 已提交
145

146 147 148
TEST(JITKernel, vmul) {
  namespace jit = paddle::operators::jit;
  TestXYZNKernel<jit::vmul, float, paddle::platform::CPUPlace>();
T
tensor-tang 已提交
149
  TestXYZNKernel<jit::vmul, double, paddle::platform::CPUPlace>();
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
}

TEST(JITKernel, vadd) {
  namespace jit = paddle::operators::jit;
  TestXYZNKernel<jit::vadd, float, paddle::platform::CPUPlace>();
  TestXYZNKernel<jit::vadd, double, paddle::platform::CPUPlace>();
}

TEST(JITKernel, vaddrelu) {
  namespace jit = paddle::operators::jit;
  TestXYZNKernel<jit::vaddrelu, float, paddle::platform::CPUPlace>();
  TestXYZNKernel<jit::vaddrelu, double, paddle::platform::CPUPlace>();
}

TEST(JITKernel, vsub) {
  namespace jit = paddle::operators::jit;
  TestXYZNKernel<jit::vsub, float, paddle::platform::CPUPlace>();
  TestXYZNKernel<jit::vsub, double, paddle::platform::CPUPlace>();
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
template <typename T, typename KernelTuples>
void TestAXYNFunc(const typename KernelTuples::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);
}

template <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void TestAXYNKernel() {
  namespace jit = paddle::operators::jit;
  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);

    // test jitcode
    auto jitcode = jit::GetJitCode<KT, jit::AXYNTuples<T>, PlaceType>(d);
    if (jitcode) {
      VLOG(10) << "Test Jitcode Kernel, size: " << d;
      TestAXYNFunc<T, jit::AXYNTuples<T>>(jitcode, a, x, yref);
    }

    // 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) {
        auto i = dynamic_cast<const jit::KernelImpl<jit::AXYNTuples<T>>*>(
            impl.get());
        if (i && i->UseMe(d)) {
          auto more = i->GetFunc();
          VLOG(10) << "Test More Kernel, size: " << d;
          TestAXYNFunc<T, jit::AXYNTuples<T>>(more, a, x, yref);
        }
      }
    }
    // Test result from Get function
    VLOG(10) << "Test Get function, size: " << d;
    auto tgt = jit::Get<KT, jit::AXYNTuples<T>, PlaceType>(d);
    TestAXYNFunc<T, jit::AXYNTuples<T>>(tgt, a, x, yref);
  }
}

TEST(JITKernel, vscal) {
  namespace jit = paddle::operators::jit;
  TestAXYNKernel<jit::vscal, float, paddle::platform::CPUPlace>();
  TestAXYNKernel<jit::vscal, double, paddle::platform::CPUPlace>();
}

TEST(JITKernel, vaddbias) {
  namespace jit = paddle::operators::jit;
  TestAXYNKernel<jit::vaddbias, float, paddle::platform::CPUPlace>();
  TestAXYNKernel<jit::vaddbias, double, paddle::platform::CPUPlace>();
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 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 352
template <typename T, typename KernelTuples>
void TestXYNFunc(const typename KernelTuples::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 <paddle::operators::jit::KernelType KT, typename T, typename PlaceType>
void TestXYNKernel() {
  namespace jit = paddle::operators::jit;
  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
    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(x_data, yref_data, d);
    ref(xinp_data, xinp_data, d);
    ExpectEQ<T>(xinp_data, yref_data, d);

    // test jitcode
    auto jitcode = jit::GetJitCode<KT, jit::XYNTuples<T>, PlaceType>(d);
    if (jitcode) {
      VLOG(10) << "Test Jitcode Kernel, size: " << d;
      TestXYNFunc<T, jit::XYNTuples<T>>(jitcode, x, yref);
    }

    // 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) {
        auto i =
            dynamic_cast<const jit::KernelImpl<jit::XYNTuples<T>>*>(impl.get());
        if (i && i->UseMe(d)) {
          auto more = i->GetFunc();
          VLOG(10) << "Test More Kernel, size: " << d;
          TestXYNFunc<T, jit::XYNTuples<T>>(more, x, yref);
        }
      }
    }
    // Test result from Get function
    VLOG(10) << "Test Get function, size: " << d;
    auto tgt = jit::Get<KT, jit::XYNTuples<T>, PlaceType>(d);
    TestXYNFunc<T, jit::XYNTuples<T>>(tgt, x, yref);
  }
}

TEST(JITKernel, vrelu) {
  namespace jit = paddle::operators::jit;
  TestXYNKernel<jit::vrelu, float, paddle::platform::CPUPlace>();
  TestXYNKernel<jit::vrelu, double, paddle::platform::CPUPlace>();
}

TEST(JITKernel, videntity) {
  namespace jit = paddle::operators::jit;
  TestXYNKernel<jit::videntity, float, paddle::platform::CPUPlace>();
  TestXYNKernel<jit::videntity, double, paddle::platform::CPUPlace>();
}

TEST(JITKernel, vexp) {
  namespace jit = paddle::operators::jit;
  TestXYNKernel<jit::vexp, float, paddle::platform::CPUPlace>();
  TestXYNKernel<jit::vexp, double, paddle::platform::CPUPlace>();
}

TEST(JITKernel, vsigmoid) {
  namespace jit = paddle::operators::jit;
  TestXYNKernel<jit::vsigmoid, float, paddle::platform::CPUPlace>();
  TestXYNKernel<jit::vsigmoid, double, paddle::platform::CPUPlace>();
}

TEST(JITKernel, vtanh) {
  namespace jit = paddle::operators::jit;
  TestXYNKernel<jit::vtanh, float, paddle::platform::CPUPlace>();
  TestXYNKernel<jit::vtanh, double, paddle::platform::CPUPlace>();
}

353 354 355
TEST(JITKernel, pool) {
  // TODO(TJ): add some test
}