TestUtils.h 7.0 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

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)
H
hedaoyuan 已提交
40 41 42 43
*/

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

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 96 97 98 99 100 101

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

102 103
template <>
CpuMatrix construct(int height, int width) {
H
hedaoyuan 已提交
104 105 106
  CpuMatrix a(height, width);
  return a;
}
107

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

// init a argument
115
template <typename T>
H
hedaoyuan 已提交
116
void init(T& v) {
117 118 119
  return;
}

120 121 122 123
template <>
void init(CpuMatrix& v) {
  v.randomizeUniform();
}
124

125 126 127 128
template <>
void init(GpuMatrix& v) {
  v.randomizeUniform();
}
H
hedaoyuan 已提交
129 130

// init a tuple which contains a set of arguments.
131 132 133
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 已提交
134

135 136 137
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 已提交
138 139 140 141 142
  init(std::get<I>(t));
  initTuple<I + 1>(t);
}

// copy a argument, copy src to dest
143
template <typename T1, typename T2>
H
hedaoyuan 已提交
144
void copy(T1& dest, T2& src) {
145 146 147
  dest = src;
}

148 149
template <>
void copy(GpuMatrix& dest, CpuMatrix& src) {
H
hedaoyuan 已提交
150 151 152 153
  dest.copyFrom(src);
}

// copy a tuple, copy src to dest
154 155 156 157 158 159 160 161
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 已提交
162 163 164 165 166 167
  copy(std::get<I>(dest), std::get<I>(src));
  copyTuple<I + 1>(dest, src);
}

// call member function
template <typename C,
168 169 170 171
          typename FC,
          typename R,
          typename... FArgs,
          typename... Args>
H
hedaoyuan 已提交
172 173 174 175
R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) {
  return (obj.*f)(args...);
}

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
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 已提交
201
typename ReturnType<T>::type autoArgs(T& v) {
202 203 204 205
  return v;
}

template <>
H
hedaoyuan 已提交
206
GpuMatrix autoArgs(CpuMatrix& v) {
207 208 209 210 211 212
  GpuMatrix a(v.getHeight(), v.getWidth());
  a.copyFrom(v);
  return a;
}

template <>
H
hedaoyuan 已提交
213
GpuIVector autoArgs(CpuIVector& v) {
214 215 216 217 218 219
  GpuIVector a(v.getSize());
  a.copyFrom(v);
  return a;
}

template <>
H
hedaoyuan 已提交
220
GpuSparseMatrix autoArgs(CpuSparseMatrix& v) {
221 222 223 224 225 226 227 228 229 230 231 232
  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:
233 234 235 236 237 238 239
  /**
   * 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) {
240 241 242 243 244
    init(cpu);
    copy(gpu, cpu);
  }

  template <typename C, typename R, typename... FArgs, typename... Args>
245 246 247
  void cmpWithArg(R (C::*f)(FArgs...), Args&&... args) {
    static_assert(sizeof...(FArgs) == sizeof...(Args),
                  "size of parameter packs are not equal");
248 249 250
    call(cpu, f, args...);
    call(gpu, f, autoArgs(args)...);

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
    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);
281 282 283 284 285
  }

protected:
  CpuMatrix cpu;
  GpuMatrix gpu;
286
  AssertEqual compare;
287 288
};

H
hedaoyuan 已提交
289
}  // namespace autotest