test_BaseMatrix.cpp 5.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
/* 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. */

#ifndef PADDLE_ONLY_CPU
/**
 * This test file compares the implementation of CPU and GPU function
 * in BaseMatrix.cpp.
 */

#include <gtest/gtest.h>
#include "paddle/utils/Util.h"
#include "paddle/math/BaseMatrix.h"
#include "TestUtils.h"

using namespace paddle;  // NOLINT

28 29 30 31 32 33
/**
 * Test member functions which prototype is
 * void (BaseMatrix::*)().
 */
TEST(BaseMatrix, void) {
  typedef void (BaseMatrix::*FunctionProto)();
34 35
#define BASEMATRIXCOMPARE(function) \
  BaseMatrixCompare(static_cast<FunctionProto>(&BaseMatrix::function));
36 37 38 39 40 41 42 43 44 45 46 47

  BASEMATRIXCOMPARE(neg);
  BASEMATRIXCOMPARE(exp);
  BASEMATRIXCOMPARE(log);
  BASEMATRIXCOMPARE(sqrt);
  BASEMATRIXCOMPARE(square);
  BASEMATRIXCOMPARE(reciprocal);
  BASEMATRIXCOMPARE(abs);
  BASEMATRIXCOMPARE(sign);
  BASEMATRIXCOMPARE(zero);
  BASEMATRIXCOMPARE(one);

48
#undef BASEMATRIXCOMPARE
49 50 51 52 53 54 55 56
}

/**
 * Test member functions which prototype is
 * void (BaseMatrix::*)(real).
 */
TEST(BaseMatrix, real) {
  typedef void (BaseMatrix::*FunctionProto)(real);
57 58
#define BASEMATRIXCOMPARE(function) \
  BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
59 60 61 62 63 64 65 66 67 68

  BASEMATRIXCOMPARE(pow);
  BASEMATRIXCOMPARE(subScalar);
  BASEMATRIXCOMPARE(mulScalar);
  BASEMATRIXCOMPARE(divScalar);
  BASEMATRIXCOMPARE(assign);
  BASEMATRIXCOMPARE(add);
  BASEMATRIXCOMPARE(biggerThanScalar);
  BASEMATRIXCOMPARE(downClip);

69
#undef BASEMATRIXCOMPARE
70 71 72 73 74 75 76 77
}

/**
 * Test member functions which prototype is
 * void (BaseMatrix::*)(real, real).
 */
TEST(BaseMatrix, real_real) {
  typedef void (BaseMatrix::*FunctionProto)(real, real);
78 79
#define BASEMATRIXCOMPARE(function) \
  BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
80 81 82 83

  BASEMATRIXCOMPARE(add);
  BASEMATRIXCOMPARE(clip);

84
#undef BASEMATRIXCOMPARE
85 86 87 88 89 90 91 92
}

/**
 * Test member functions which prototype is
 * void (BaseMatrix::*)(BaseMatrix&).
 */
TEST(BaseMatrix, BaseMatrix) {
  typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&);
93 94
#define BASEMATRIXCOMPARE(function) \
  BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
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

  BASEMATRIXCOMPARE(assign);
  BASEMATRIXCOMPARE(add);
  BASEMATRIXCOMPARE(relu);
  BASEMATRIXCOMPARE(reluDerivative);
  BASEMATRIXCOMPARE(softrelu);
  BASEMATRIXCOMPARE(softreluDerivative);
  BASEMATRIXCOMPARE(brelu);
  BASEMATRIXCOMPARE(breluDerivative);
  BASEMATRIXCOMPARE(square);
  BASEMATRIXCOMPARE(squareDerivative);
  BASEMATRIXCOMPARE(tanh);
  BASEMATRIXCOMPARE(tanhDerivative);

  BASEMATRIXCOMPARE(reciprocal);
  BASEMATRIXCOMPARE(reciprocalDerivative);
  BASEMATRIXCOMPARE(abs);
  BASEMATRIXCOMPARE(absDerivative);
  BASEMATRIXCOMPARE(sigmoid);
  BASEMATRIXCOMPARE(sigmoidDerivative);
  BASEMATRIXCOMPARE(expDerivative);
  BASEMATRIXCOMPARE(sign);
  BASEMATRIXCOMPARE(exp);
  BASEMATRIXCOMPARE(log);
  BASEMATRIXCOMPARE(sqrt);
  BASEMATRIXCOMPARE(dotMul);
  BASEMATRIXCOMPARE(dotMulSquare);
  BASEMATRIXCOMPARE(dotSquareMul);

  BASEMATRIXCOMPARE(addColVector);
  BASEMATRIXCOMPARE(addRowVector);
  BASEMATRIXCOMPARE(mulRowVector);
  BASEMATRIXCOMPARE(divRowVector);
  BASEMATRIXCOMPARE(addP2P);
  BASEMATRIXCOMPARE(invSqrt);

131
#undef BASEMATRIXCOMPARE
132 133 134 135 136 137 138 139
}

/**
 * Test member functions which prototype is
 * void (BaseMatrix::*)(BaseMatrix&, real).
 */
TEST(BaseMatrix, BaseMatrix_real) {
  typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, real);
140 141
#define BASEMATRIXCOMPARE(function) \
  BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
H
hedaoyuan 已提交
142

143 144 145 146 147 148 149 150 151 152 153 154 155
  BASEMATRIXCOMPARE(addBias);
  BASEMATRIXCOMPARE(add);
  BASEMATRIXCOMPARE(sub);
  BASEMATRIXCOMPARE(pow);
  BASEMATRIXCOMPARE(addScalar);
  BASEMATRIXCOMPARE(subScalar);
  BASEMATRIXCOMPARE(mulScalar);
  BASEMATRIXCOMPARE(divScalar);
  BASEMATRIXCOMPARE(scalarDiv);
  BASEMATRIXCOMPARE(addSquare);

  BASEMATRIXCOMPARE(isEqualTo);

156
#undef BASEMATRIXCOMPARE
157 158 159 160 161 162 163 164
}

/**
 * Test member functions which prototype is
 * void (BaseMatrix::*)(BaseMatrix&, BaseMatrix&).
 */
TEST(BaseMatrix, BaseMatrix_BaseMatrix) {
  typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, BaseMatrix&);
165 166
#define BASEMATRIXCOMPARE(function) \
  BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

  BASEMATRIXCOMPARE(softCrossEntropy);
  BASEMATRIXCOMPARE(softCrossEntropyBp);
  BASEMATRIXCOMPARE(binaryLabelCrossEntropy);
  BASEMATRIXCOMPARE(binaryLabelCrossEntropyBp);
  BASEMATRIXCOMPARE(sub);
  BASEMATRIXCOMPARE(add2);
  BASEMATRIXCOMPARE(dotMul);
  BASEMATRIXCOMPARE(dotDiv);
  BASEMATRIXCOMPARE(logisticRegressionLoss);
  BASEMATRIXCOMPARE(logisticRegressionLossBp);
  BASEMATRIXCOMPARE(biggerThan);
  BASEMATRIXCOMPARE(max);
  BASEMATRIXCOMPARE(dotMulSquare);
  BASEMATRIXCOMPARE(dotSquareSquare);

183
#undef BASEMATRIXCOMPARE
184 185
}

186 187 188 189 190
// member function without overloaded
TEST(BaseMatrix, Other) {
  BaseMatrixCompare<0, 1, 2>(&BaseMatrix::rowScale);
  BaseMatrixCompare<0, 1, 2>(&BaseMatrix::rowDotMul);
  BaseMatrixCompare<0, 1, 2, 3>(&BaseMatrix::binaryClassificationError);
191 192
}

193 194 195 196
TEST(BaseMatrix, Aggregate) {
  BaseMatrixAsColVector<0>(&BaseMatrix::maxRows);
  BaseMatrixAsColVector<0>(&BaseMatrix::minRows);
  BaseMatrixAsColVector<0, 1, 2>(&BaseMatrix::sumRows);
H
hedaoyuan 已提交
197

198 199 200 201
  BaseMatrixAsRowVector<0>(&BaseMatrix::maxCols);
  BaseMatrixAsRowVector<0>(&BaseMatrix::minCols);
  BaseMatrixAsRowVector<0, 1>(&BaseMatrix::addDotMulVMM);
  BaseMatrixAsRowVector<0, 1, 2>(&BaseMatrix::sumCols);
H
hedaoyuan 已提交
202 203 204 205 206 207 208 209 210
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  initMain(argc, argv);
  return RUN_ALL_TESTS();
}

#endif