TestUtils.h 7.2 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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.
 *
 * Auto compare BaseMatrix member function:
 * Use case:
 * a. void BaseMatrix::tanh(BaseMatrixT& b);
 *   Compare method: BaseMatrixCompare<0>(&BaseMatrix::tanh);
 *
23
 * b.
H
hedaoyuan 已提交
24 25 26 27 28 29 30 31 32 33 34
 *
*/

#include <gtest/gtest.h>
#include "paddle/math/Matrix.h"
#include "TensorCheck.h"

using namespace paddle;  // NOLINT

namespace autotest {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// init a tuple which contains a set of arguments.
117 118 119
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 已提交
120

121 122 123
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 已提交
124 125 126 127 128
  init(std::get<I>(t));
  initTuple<I + 1>(t);
}

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

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

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

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

// copy a tuple, copy src to dest
148 149 150 151 152 153 154 155
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 已提交
156 157 158 159
  copy(std::get<I>(dest), std::get<I>(src));
  copyTuple<I + 1>(dest, src);
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
// 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 已提交
182 183
// call member function
template <typename C,
184 185 186 187
          typename FC,
          typename R,
          typename... FArgs,
          typename... Args>
H
hedaoyuan 已提交
188 189 190 191
R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) {
  return (obj.*f)(args...);
}

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

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

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

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

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

229
      TensorCheck(compare, obj1, obj2);
230 231 232
      if (checkArgs) {
        checkTuple(tuple1, tuple2, compare);
      }
H
hedaoyuan 已提交
233 234 235 236 237 238
    }
  }
}

}  // namespace autotest

239 240
template <std::size_t... I, typename C, typename R, typename... Args>
void BaseMatrixCompare(R (C::*f)(Args...), bool checkArgs = false) {
H
hedaoyuan 已提交
241
  static_assert(sizeof...(I) == sizeof...(Args),
242
                "size of parameter packs are not equal");
H
hedaoyuan 已提交
243

244 245 246 247 248 249
#ifndef PADDLE_TYPE_DOUBLE
  autotest::AssertEqual compare(1e-5);
#else
  autotest::AssertEqual compare(1e-10);
#endif

250
  autotest::BaseMatrixCompare<false, false, I...>(f, compare, checkArgs);
251 252
}

253
template <std::size_t... I, typename C, typename R, typename... Args>
254
void BaseMatrixAsColVector(R (C::*f)(Args...)) {
255
  static_assert(sizeof...(I) == sizeof...(Args),
256
                "size of parameter packs are not equal");
257 258 259 260 261 262 263

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

264
  autotest::BaseMatrixCompare<false, true, I...>(f, compare);
265 266
}

267
template <std::size_t... I, typename C, typename R, typename... Args>
268
void BaseMatrixAsRowVector(R (C::*f)(Args...)) {
269
  static_assert(sizeof...(I) == sizeof...(Args),
270
                "size of parameter packs are not equal");
271 272 273 274 275 276

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