TestUtils.h 7.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
H
hedaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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

H
hedaoyuan 已提交
15 16
#pragma once

H
hedaoyuan 已提交
17
/**
18
 * This file provides a AutoCompare calss to simplify the comparison
H
hedaoyuan 已提交
19
 * of CPU and GPU member functions.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * This takes two steps
 * 1. Construct an AutoCompare object.
 *    When constructing an AutoCompare object, you can set the err argument
 * to specify the maximum error for CPU and GPU functions.
 *
 * 2. Use the template functions cmpWithArg or cmpWithoutArg.
 * A. [cmpWithArg] Requires the caller construct the cpu arguments.
 *
 *  AutoCompare test;
 *  Init Argument arg1,arg2...
 *  test.cmpWithArg(function, arg1, arg2....)
 *
 * B. [cmpWithoutArg] The caller do not need construct arguments.
 *    If matrix used in these functions arguments is the same size.
 *    Such as the element wise function and the aggregate function
 *    defined in the BaseMatrix.cpp.
 *
 *  AutoCompare test;
 *  test.cmpWithoutArg<I...>(function, height, width)
L
liaogang 已提交
40
 */
H
hedaoyuan 已提交
41 42

#include <gtest/gtest.h>
Y
Yu Yang 已提交
43
#include "TensorCheck.h"
H
hedaoyuan 已提交
44
#include "paddle/math/Matrix.h"
45
#include "paddle/math/SparseMatrix.h"
H
hedaoyuan 已提交
46

H
hedaoyuan 已提交
47 48
namespace autotest {

49
using paddle::BaseMatrix;
50 51
using paddle::CpuMatrix;
using paddle::GpuMatrix;
52 53 54 55
using paddle::CpuIVector;
using paddle::GpuIVector;
using paddle::CpuSparseMatrix;
using paddle::GpuSparseMatrix;
H
hedaoyuan 已提交
56

57
template <typename T1, typename T2>
H
hedaoyuan 已提交
58 59 60 61 62
class ReplaceType {
public:
  typedef T1 type;
};

63
template <>
H
hedaoyuan 已提交
64 65 66 67 68
class ReplaceType<BaseMatrix, CpuMatrix> {
public:
  typedef CpuMatrix type;
};

69
template <>
H
hedaoyuan 已提交
70 71 72 73 74
class ReplaceType<BaseMatrix, GpuMatrix> {
public:
  typedef GpuMatrix type;
};

75 76 77 78 79 80 81 82 83 84 85 86
template <>
class ReplaceType<Matrix, CpuMatrix> {
public:
  typedef CpuMatrix type;
};

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

H
hedaoyuan 已提交
87
// construct a argument
88 89
template <typename T>
T construct(int height, int width);
90

91 92
template <>
float construct(int height, int width) {
H
hedaoyuan 已提交
93
  return 0.5;
94
}
95

H
hedaoyuan 已提交
96 97 98 99 100
template <>
double construct(int height, int width) {
  return 0.5;
}

101 102 103 104 105 106
template <>
size_t construct(int height, int width) {
  size_t offset = std::rand() % (height < width ? height : width);
  return offset;
}

107 108
template <>
CpuMatrix construct(int height, int width) {
H
hedaoyuan 已提交
109 110 111
  CpuMatrix a(height, width);
  return a;
}
112

113 114
template <>
GpuMatrix construct(int height, int width) {
H
hedaoyuan 已提交
115 116 117 118 119
  GpuMatrix a(height, width);
  return a;
}

// init a argument
120
template <typename T>
H
hedaoyuan 已提交
121
void init(T& v) {
122 123 124
  return;
}

125 126 127 128
template <>
void init(CpuMatrix& v) {
  v.randomizeUniform();
}
129

130 131 132 133
template <>
void init(GpuMatrix& v) {
  v.randomizeUniform();
}
H
hedaoyuan 已提交
134 135

// init a tuple which contains a set of arguments.
136 137 138
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 已提交
139

140 141 142
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 已提交
143 144 145 146 147
  init(std::get<I>(t));
  initTuple<I + 1>(t);
}

// copy a argument, copy src to dest
148
template <typename T1, typename T2>
H
hedaoyuan 已提交
149
void copy(T1& dest, T2& src) {
150 151 152
  dest = src;
}

153 154
template <>
void copy(GpuMatrix& dest, CpuMatrix& src) {
H
hedaoyuan 已提交
155 156 157 158
  dest.copyFrom(src);
}

// copy a tuple, copy src to dest
159 160 161 162 163 164 165 166
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 已提交
167 168 169 170 171 172
  copy(std::get<I>(dest), std::get<I>(src));
  copyTuple<I + 1>(dest, src);
}

// call member function
template <typename C,
173 174 175 176
          typename FC,
          typename R,
          typename... FArgs,
          typename... Args>
H
hedaoyuan 已提交
177 178 179 180
R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) {
  return (obj.*f)(args...);
}

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
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>
H
hedaoyuan 已提交
206
typename ReturnType<T>::type autoArgs(T& v) {
207 208 209 210
  return v;
}

template <>
H
hedaoyuan 已提交
211
GpuMatrix autoArgs(CpuMatrix& v) {
212 213 214 215 216 217
  GpuMatrix a(v.getHeight(), v.getWidth());
  a.copyFrom(v);
  return a;
}

template <>
H
hedaoyuan 已提交
218
GpuIVector autoArgs(CpuIVector& v) {
219 220 221 222 223 224
  GpuIVector a(v.getSize());
  a.copyFrom(v);
  return a;
}

template <>
H
hedaoyuan 已提交
225
GpuSparseMatrix autoArgs(CpuSparseMatrix& v) {
226 227 228 229 230 231 232 233 234 235 236 237
  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:
238 239 240 241 242 243 244
  /**
   * err is the allowed calculation error.
   * The smaller the value of err,
   * the stricter the comparison is between CPU and GPU calculations.
   */
  AutoCompare(size_t height, size_t width, real err = 1e-3)
      : cpu(height, width), gpu(height, width), compare(err) {
245 246 247 248 249
    init(cpu);
    copy(gpu, cpu);
  }

  template <typename C, typename R, typename... FArgs, typename... Args>
250 251 252
  void cmpWithArg(R (C::*f)(FArgs...), Args&&... args) {
    static_assert(sizeof...(FArgs) == sizeof...(Args),
                  "size of parameter packs are not equal");
253 254 255
    call(cpu, f, args...);
    call(gpu, f, autoArgs(args)...);

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
    TensorCheck(compare, cpu, gpu);
  }

  template <std::size_t... I, typename C, typename R, typename... Args>
  void cmpWithoutArg(R (C::*f)(Args...), size_t height, size_t width) {
    static_assert(sizeof...(I) == sizeof...(Args),
                  "size of parameter packs are not equal");
    (void)height;
    (void)width;
    auto tuple1 = std::make_tuple(
        construct<typename ReplaceType<
            typename std::decay<
                typename std::tuple_element<I,
                                            std::tuple<Args...>>::type>::type,
            CpuMatrix>::type>(height, width)...);

    auto tuple2 = std::make_tuple(
        construct<typename ReplaceType<
            typename std::decay<
                typename std::tuple_element<I,
                                            std::tuple<Args...>>::type>::type,
            GpuMatrix>::type>(height, width)...);

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

    call(cpu, f, std::get<I>(tuple1)...);
    call(gpu, f, std::get<I>(tuple2)...);

    TensorCheck(compare, cpu, gpu);
286 287 288 289 290
  }

protected:
  CpuMatrix cpu;
  GpuMatrix gpu;
291
  AssertEqual compare;
292 293
};

H
hedaoyuan 已提交
294
}  // namespace autotest