TestUtils.h 8.4 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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. */

/**
 * TestUtils.h is used to automatically compare CPU and GPU code is consistent.
17
 * Refer test_Matrix.cpp and test_BaseMatrix.cpp for how to use autotest.
H
hedaoyuan 已提交
18 19 20 21
*/

#include <gtest/gtest.h>
#include "paddle/math/Matrix.h"
22
#include "paddle/math/SparseMatrix.h"
H
hedaoyuan 已提交
23 24
#include "TensorCheck.h"

25 26 27 28 29
using paddle::BaseMatrix;
using paddle::CpuIVector;
using paddle::GpuIVector;
using paddle::CpuSparseMatrix;
using paddle::GpuSparseMatrix;
H
hedaoyuan 已提交
30 31 32

namespace autotest {

33
template <typename T1, typename T2>
H
hedaoyuan 已提交
34 35 36 37 38
class ReplaceType {
public:
  typedef T1 type;
};

39
template <>
H
hedaoyuan 已提交
40 41 42 43 44
class ReplaceType<BaseMatrix, CpuMatrix> {
public:
  typedef CpuMatrix type;
};

45
template <>
H
hedaoyuan 已提交
46 47 48 49 50
class ReplaceType<BaseMatrix, GpuMatrix> {
public:
  typedef GpuMatrix type;
};

51 52 53 54 55 56 57 58 59 60 61 62
template <>
class ReplaceType<Matrix, CpuMatrix> {
public:
  typedef CpuMatrix type;
};

template <>
class ReplaceType<Matrix, GpuMatrix> {
public:
  typedef GpuMatrix type;
};

H
hedaoyuan 已提交
63
// construct a argument
64 65
template <typename T>
T construct(int height, int width);
66

67 68 69 70
template <>
float construct(int height, int width) {
  return 0.0;
}
71 72 73 74 75 76 77

template <>
size_t construct(int height, int width) {
  size_t offset = std::rand() % (height < width ? height : width);
  return offset;
}

78 79
template <>
CpuMatrix construct(int height, int width) {
H
hedaoyuan 已提交
80 81 82
  CpuMatrix a(height, width);
  return a;
}
83

84 85
template <>
GpuMatrix construct(int height, int width) {
H
hedaoyuan 已提交
86 87 88 89 90
  GpuMatrix a(height, width);
  return a;
}

// init a argument
91 92
template <typename T>
void init(T& v);
93

94 95 96 97
template <>
void init(float& v) {
  v = 0.5;
}
98 99 100 101 102 103

template <>
void init(size_t& v) {
  return;
}

104 105 106 107
template <>
void init(CpuMatrix& v) {
  v.randomizeUniform();
}
108

109 110 111 112
template <>
void init(GpuMatrix& v) {
  v.randomizeUniform();
}
H
hedaoyuan 已提交
113 114

// init a tuple which contains a set of arguments.
115 116 117
template <std::size_t I = 0, typename... Args>
inline typename std::enable_if<I == sizeof...(Args), void>::type initTuple(
    std::tuple<Args...>& t) {}
H
hedaoyuan 已提交
118

119 120 121
template <std::size_t I = 0, typename... Args>
    inline typename std::enable_if <
    I<sizeof...(Args), void>::type initTuple(std::tuple<Args...>& t) {
H
hedaoyuan 已提交
122 123 124 125 126
  init(std::get<I>(t));
  initTuple<I + 1>(t);
}

// copy a argument, copy src to dest
127 128
template <typename T1, typename T2>
void copy(T1& dest, T2& src);
129

130 131 132 133
template <>
void copy(float& dest, float& src) {
  dest = src;
}
134 135 136 137 138 139

template <>
void copy(size_t& dest, size_t& src) {
  dest = src;
}

140 141
template <>
void copy(GpuMatrix& dest, CpuMatrix& src) {
H
hedaoyuan 已提交
142 143 144 145
  dest.copyFrom(src);
}

// copy a tuple, copy src to dest
146 147 148 149 150 151 152 153
template <std::size_t I = 0, typename... Args1, typename... Args2>
inline typename std::enable_if<I == sizeof...(Args1), void>::type copyTuple(
    std::tuple<Args1...>& dest, std::tuple<Args2...>& src) {}

template <std::size_t I = 0, typename... Args1, typename... Args2>
    inline typename std::enable_if <
    I<sizeof...(Args1), void>::type copyTuple(std::tuple<Args1...>& dest,
                                              std::tuple<Args2...>& src) {
H
hedaoyuan 已提交
154 155 156 157
  copy(std::get<I>(dest), std::get<I>(src));
  copyTuple<I + 1>(dest, src);
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
// Compare output
template <std::size_t I = 0,
          typename... Args1,
          typename... Args2,
          typename AssertEq>
inline typename std::enable_if<I == sizeof...(Args1), void>::type checkTuple(
    std::tuple<Args1...>& args1,
    std::tuple<Args2...>& args2,
    AssertEq compare) {}

template <std::size_t I = 0,
          typename... Args1,
          typename... Args2,
          typename AssertEq>
    inline typename std::enable_if <
    I<sizeof...(Args1), void>::type checkTuple(std::tuple<Args1...>& args1,
                                               std::tuple<Args2...>& args2,
                                               AssertEq compare) {
  TensorCheck(compare, std::get<I>(args1), std::get<I>(args2));
  checkTuple<I + 1>(args1, args2, compare);
}

H
hedaoyuan 已提交
180 181
// call member function
template <typename C,
182 183 184 185
          typename FC,
          typename R,
          typename... FArgs,
          typename... Args>
H
hedaoyuan 已提交
186 187 188 189
R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) {
  return (obj.*f)(args...);
}

190 191
template <bool AsRowVector,
          bool AsColVector,
192 193 194 195
          std::size_t... I,
          typename C,
          typename R,
          typename... Args,
196
          typename AssertEq>
197
void BaseMatrixCompare(R (C::*f)(Args...), AssertEq compare) {
H
hedaoyuan 已提交
198
  for (auto height : {1, 11, 73, 128, 200, 330}) {
199
    for (auto width : {1, 3, 32, 100, 512, 1000}) {
200 201
      CpuMatrix obj1(AsRowVector ? 1 : height, AsColVector ? 1 : width);
      GpuMatrix obj2(AsRowVector ? 1 : height, AsColVector ? 1 : width);
H
hedaoyuan 已提交
202 203 204 205
      init(obj1);
      copy(obj2, obj1);

      auto tuple1 = std::make_tuple(
206 207 208 209 210
          construct<typename ReplaceType<
              typename std::decay<
                  typename std::tuple_element<I,
                                              std::tuple<Args...>>::type>::type,
              CpuMatrix>::type>(height, width)...);
H
hedaoyuan 已提交
211 212

      auto tuple2 = std::make_tuple(
213 214 215 216 217
          construct<typename ReplaceType<
              typename std::decay<
                  typename std::tuple_element<I,
                                              std::tuple<Args...>>::type>::type,
              GpuMatrix>::type>(height, width)...);
H
hedaoyuan 已提交
218 219 220 221 222 223 224

      initTuple(tuple1);
      copyTuple(tuple2, tuple1);

      call(obj1, f, std::get<I>(tuple1)...);
      call(obj2, f, std::get<I>(tuple2)...);

225
      TensorCheck(compare, obj1, obj2);
H
hedaoyuan 已提交
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 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
template <typename T>
class ReturnType {
public:
  typedef T type;
};

template <>
class ReturnType<CpuMatrix> {
public:
  typedef GpuMatrix type;
};

template <>
class ReturnType<CpuIVector> {
public:
  typedef GpuIVector type;
};

template <>
class ReturnType<CpuSparseMatrix> {
public:
  typedef GpuSparseMatrix type;
};

template <typename T>
typename ReturnType<T>::type autoArgs(T v) {
  return v;
}

template <>
GpuMatrix autoArgs(CpuMatrix v) {
  GpuMatrix a(v.getHeight(), v.getWidth());
  a.copyFrom(v);
  return a;
}

template <>
GpuIVector autoArgs(CpuIVector v) {
  GpuIVector a(v.getSize());
  a.copyFrom(v);
  return a;
}

template <>
GpuSparseMatrix autoArgs(CpuSparseMatrix v) {
  GpuSparseMatrix a(v.getHeight(),
                    v.getWidth(),
                    v.getElementCnt(),
                    v.getValueType(),
                    v.getFormat());

  a.copyFrom(v, HPPL_STREAM_DEFAULT);
  hl_stream_synchronize(HPPL_STREAM_DEFAULT);
  return a;
}

class AutoCompare {
public:
  AutoCompare(size_t height, size_t width)
      : cpu(height, width), gpu(height, width) {
    init(cpu);
    copy(gpu, cpu);
  }

  template <typename C, typename R, typename... FArgs, typename... Args>
  void operator()(R (C::*f)(FArgs...), Args&&... args) {
    call(cpu, f, args...);
    call(gpu, f, autoArgs(args)...);

    TensorCheckErr(cpu, gpu);
  }

protected:
  CpuMatrix cpu;
  GpuMatrix gpu;
};

H
hedaoyuan 已提交
307 308
}  // namespace autotest

309
template <std::size_t... I, typename C, typename R, typename... Args>
310
void BaseMatrixCompare(R (C::*f)(Args...)) {
H
hedaoyuan 已提交
311
  static_assert(sizeof...(I) == sizeof...(Args),
312
                "size of parameter packs are not equal");
H
hedaoyuan 已提交
313

314 315 316 317 318 319
#ifndef PADDLE_TYPE_DOUBLE
  autotest::AssertEqual compare(1e-5);
#else
  autotest::AssertEqual compare(1e-10);
#endif

320
  autotest::BaseMatrixCompare<false, false, I...>(f, compare);
321 322
}

323
template <std::size_t... I, typename C, typename R, typename... Args>
324
void BaseMatrixAsColVector(R (C::*f)(Args...)) {
325
  static_assert(sizeof...(I) == sizeof...(Args),
326
                "size of parameter packs are not equal");
327 328 329 330 331 332 333

#ifndef PADDLE_TYPE_DOUBLE
  autotest::AssertEqual compare(1e-3);
#else
  autotest::AssertEqual compare(1e-8);
#endif

334
  autotest::BaseMatrixCompare<false, true, I...>(f, compare);
335 336
}

337
template <std::size_t... I, typename C, typename R, typename... Args>
338
void BaseMatrixAsRowVector(R (C::*f)(Args...)) {
339
  static_assert(sizeof...(I) == sizeof...(Args),
340
                "size of parameter packs are not equal");
341 342 343 344 345 346

#ifndef PADDLE_TYPE_DOUBLE
  autotest::AssertEqual compare(1e-3);
#else
  autotest::AssertEqual compare(1e-8);
#endif
347
  autotest::BaseMatrixCompare<true, false, I...>(f, compare);
H
hedaoyuan 已提交
348
}