TensorCheck.h 5.4 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 17 18 19 20 21
#pragma once

/**
 * This file provides a TensorCheck template function, which can be used to
 * compare CpuMatrix and GpuMatrix, CpuVector and GpuVector, and so on.
 */

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

H
hedaoyuan 已提交
25 26
namespace autotest {

27 28 29 30 31 32
using paddle::Matrix;
using paddle::CpuMatrix;
using paddle::GpuMatrix;
using paddle::VectorT;
using paddle::CpuVectorT;
using paddle::GpuVectorT;
H
hedaoyuan 已提交
33

34
class AssertEqual {
H
hedaoyuan 已提交
35
public:
36
  AssertEqual(real err = 0) : err_(err) {}
H
hedaoyuan 已提交
37

38 39 40 41 42 43 44 45 46 47
  inline bool operator()(real a, real b) {
    if (err_ == 0) {
      if (a != b) {
        return false;
      }
    } else {
      if (std::fabs(a - b) > err_) {
        if ((std::fabs(a - b) / std::fabs(a)) > (err_ / 10.0f)) {
          return false;
        }
H
hedaoyuan 已提交
48 49
      }
    }
50

51
    return true;
H
hedaoyuan 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  }

private:
  real err_;
};

template <typename Tensor>
class CopyToCpu;

template <>
class CopyToCpu<CpuMatrix> {
public:
  explicit CopyToCpu(const CpuMatrix& arg) : arg_(arg) {}
  const CpuMatrix& copiedArg() const { return arg_; }

private:
  const CpuMatrix& arg_;
};

template <>
class CopyToCpu<GpuMatrix> {
public:
  explicit CopyToCpu(const GpuMatrix& arg)
75
      : arg_(arg.getHeight(), arg.getWidth()) {
H
hedaoyuan 已提交
76 77 78 79 80 81 82 83
    arg_.copyFrom(arg);
  }
  CpuMatrix& copiedArg() { return arg_; }

private:
  CpuMatrix arg_;
};

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
template <>
class CopyToCpu<Matrix> {
public:
  explicit CopyToCpu(const Matrix& arg)
      : arg_(arg.getHeight(), arg.getWidth()) {
    arg_.copyFrom(arg);
  }
  CpuMatrix& copiedArg() { return arg_; }

private:
  CpuMatrix arg_;
};

template <typename T>
class CopyToCpu<CpuVectorT<T>> {
public:
  explicit CopyToCpu(const CpuVectorT<T>& arg) : arg_(arg) {}
  const CpuVectorT<T>& copiedArg() const { return arg_; }

private:
  const CpuVectorT<T>& arg_;
};

template <typename T>
class CopyToCpu<GpuVectorT<T>> {
public:
  explicit CopyToCpu(const GpuVectorT<T>& arg) : arg_(arg.getSize()) {
    arg_.copyFrom(arg);
  }
  CpuVectorT<T>& copiedArg() { return arg_; }

private:
  CpuVectorT<T> arg_;
};

template <typename T>
class CopyToCpu<VectorT<T>> {
public:
  explicit CopyToCpu(const VectorT<T>& arg) : arg_(arg.getSize()) {
    arg_.copyFrom(arg);
  }
  CpuVectorT<T>& copiedArg() { return arg_; }

private:
  CpuVectorT<T> arg_;
};

131
template <typename AssertEq>
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
void TensorCheck(AssertEq compare,
                 const CpuMatrix& matrix1,
                 const CpuMatrix& matrix2) {
  CHECK(matrix1.getHeight() == matrix2.getHeight());
  CHECK(matrix1.getWidth() == matrix2.getWidth());

  int height = matrix1.getHeight();
  int width = matrix1.getWidth();
  const real* data1 = matrix1.getData();
  const real* data2 = matrix2.getData();
  int count = 0;
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      real a = data1[i * width + j];
      real b = data2[i * width + j];
      if (!compare(a, b)) {
        count++;
      }
    }
  }
  EXPECT_EQ(count, 0) << "There are " << count << " different element.";
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
template <typename AssertEq, class T>
void TensorCheck(AssertEq compare,
                 const CpuVectorT<T>& vector1,
                 const CpuVectorT<T>& vector2) {
  CHECK(vector1.getSize() == vector2.getSize());

  const T* data1 = vector1.getData();
  const T* data2 = vector2.getData();
  size_t size = vector1.getSize();
  int count = 0;
  for (size_t i = 0; i < size; i++) {
    real a = data1[i];
    real b = data2[i];
    if (!compare(a, b)) {
      count++;
    }
  }
  EXPECT_EQ(count, 0) << "There are " << count << " different element.";
}

175
template <typename AssertEq, typename Tensor1, typename Tensor2>
176 177 178
void TensorCheck(AssertEq compare,
                 const Tensor1& tensor1,
                 const Tensor2& tensor2) {
179 180 181
  TensorCheck(compare,
              CopyToCpu<Tensor1>(tensor1).copiedArg(),
              CopyToCpu<Tensor2>(tensor2).copiedArg());
H
hedaoyuan 已提交
182 183
}

184 185 186 187 188
template <typename AssertEq>
void TensorCheck(AssertEq compare, real args1, real args2) {
  EXPECT_EQ(compare(args1, args2), true) << "[Test error] args1 = " << args1
                                         << ", args2 = " << args2;
}
H
hedaoyuan 已提交
189

190 191 192 193 194 195
template <typename AssertEq>
void TensorCheck(AssertEq compare, size_t args1, size_t args2) {
  EXPECT_EQ(args1, args2) << "[Test error] args1 = " << args1
                          << ", args2 = " << args2;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
template <typename Tensor1, typename Tensor2>
void TensorCheckEqual(const Tensor1& tensor1, const Tensor2& tensor2) {
  AssertEqual compare(0);
  TensorCheck(compare,
              CopyToCpu<Tensor1>(tensor1).copiedArg(),
              CopyToCpu<Tensor2>(tensor2).copiedArg());
}

template <typename Tensor1, typename Tensor2>
void TensorCheckErr(const Tensor1& tensor1, const Tensor2& tensor2) {
#ifndef PADDLE_TYPE_DOUBLE
  AssertEqual compare(1e-3);
#else
  AssertEqual compare(1e-10);
#endif
  TensorCheck(compare,
              CopyToCpu<Tensor1>(tensor1).copiedArg(),
              CopyToCpu<Tensor2>(tensor2).copiedArg());
}

216
}  // namespace autotest