TensorCheck.h 4.9 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/**
 * test_Tensor.cpp
 *
 * Author: hedaoyuan (hedaoyuan@baidu.com)
 * Created on: 2016-06-06
 *
 * Copyright (c) Baidu.com, Inc. All Rights Reserved
 */

#include <gtest/gtest.h>
#include "paddle/math/Matrix.h"
using namespace paddle;  // NOLINT
using namespace std;     // NOLINT

template<typename Tensor>
extern void TensorCheckEqual(const Tensor& tensor1, const Tensor& tensor2);

void TensorCheckEqual(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++) {
      if (data1[i * width + j] != data2[i * width + j]) {
        count++;
      }
    }
  }
  EXPECT_EQ(count, 0) << "There are " << count << " different element.";
}

void TensorCheckEqual(const GpuMatrix& matrix1, const GpuMatrix& matrix2) {
  CpuMatrix cpu1(matrix1.getHeight(), matrix1.getWidth());
  CpuMatrix cpu2(matrix2.getHeight(), matrix2.getWidth());
  cpu1.copyFrom(matrix1);
  cpu2.copyFrom(matrix2);
  TensorCheckEqual(cpu1, cpu2);
}

void TensorCheckErr(const CpuMatrix& matrix1, const CpuMatrix& matrix2) {
  CHECK(matrix1.getHeight() == matrix2.getHeight());
  CHECK(matrix1.getWidth() == matrix2.getWidth());
#ifndef PADDLE_TYPE_DOUBLE
  real err = 1e-5;
#else
  real err = 1e-10;
#endif

  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 (fabs(a - b) > err) {
        if ((fabsf(a - b) / fabsf(a)) > (err / 10.0f)) {
          count++;
        }
      }
    }
  }
  EXPECT_EQ(count, 0) << "There are " << count << " different element.";
}

void TensorCheckErr(const GpuMatrix& matrix1, const GpuMatrix& matrix2) {
  CpuMatrix cpu1(matrix1.getHeight(), matrix1.getWidth());
  CpuMatrix cpu2(matrix2.getHeight(), matrix2.getWidth());
  cpu1.copyFrom(matrix1);
  cpu2.copyFrom(matrix2);
  TensorCheckErr(cpu1, cpu2);
}

template<class T>
void TensorCheckEqual(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++) {
    if (data1[i] != data2[i]) {
      count++;
    }
  }
  EXPECT_EQ(count, 0) << "There are " << count << " different element.";
}

template<class T>
void TensorCheckEqual(const GpuVectorT<T>& vector1,
                      const GpuVectorT<T>& vector2) {
  CpuVectorT<T> cpu1(vector1.getSize());
  CpuVectorT<T> cpu2(vector2.getSize());
  cpu1.copyFrom(vector1);
  cpu2.copyFrom(vector2);
  TensorCheckEqual(cpu1, cpu2);
}

int VectorCheckErr(const Vector& vector1, const Vector& vector2) {
  CHECK(vector1.getSize() == vector2.getSize());

  const real* data1 = vector1.getData();
  const real* 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 (fabs(a - b) > FLAGS_max_diff) {
      if ((fabsf(a - b) / fabsf(a)) > (FLAGS_max_diff / 10.0f)) {
        count++;
      }
    }
  }

  return count;
}

#define INIT_UNARY(A1, A2)                  \
    Tensor A1(height, width);               \
    Tensor A2(height, width);               \
    A1.randomizeUniform();                  \
    A2.copyFrom(A1)
#define INIT_BINARY(A1, A2, B)              \
    INIT_UNARY(A1, A2);                     \
    Tensor B(height, width);                \
    B.randomizeUniform()
#define INIT_TERNARY(A1, A2, B, C)          \
    INIT_BINARY(A1, A2, B);                 \
    Tensor C(height, width);                \
    C.randomizeUniform()
#define INIT_QUATERNARY(A1, A2, B, C, D)    \
    INIT_TERNARY(A1, A2, B, C);             \
    Tensor D(height, width);                \
    D.randomizeUniform()

// Performance Check
#ifdef PADDLE_DISABLE_TIMER

#define CHECK_VECTORPTR(vector1, vector2)   \
    EXPECT_EQ(VectorCheckErr(vector1, vector2), 0)

#define EXPRESSION_PERFORMANCE(expression)  \
    expression;

#else

#include "paddle/utils/Stat.h"

#define CHECK_VECTORPTR(vector1, vector2)

#define EXPRESSION_PERFORMANCE(expression) \
  do {\
    char expr[30];\
    strncpy(expr, #expression, 30);\
    if (expr[29] != '\0') {\
      expr[27] = '.'; expr[28] = '.'; expr[29] = '\0';\
    }\
    expression;\
    for (int i = 0; i < 20; i++) {\
      REGISTER_TIMER(expr);\
      expression;\
    }\
    LOG(INFO) << std::setiosflags(std::ios::left) << std::setfill(' ')\
      << *globalStat.getStat(expr);\
    globalStat.reset();\
  } while (0)

#endif