test_BaseMatrix.cpp 7.1 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
/* 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
using namespace std;     // NOLINT

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

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

  #undef BASEMATRIXCOMPARE
}

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

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

  #undef BASEMATRIXCOMPARE
}

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

  BASEMATRIXCOMPARE(add);
  BASEMATRIXCOMPARE(clip);

  #undef BASEMATRIXCOMPARE
}

/**
 * Test member functions which prototype is
 * void (BaseMatrix::*)(BaseMatrix&).
 */
TEST(BaseMatrix, BaseMatrix) {
  typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&);
  #define BASEMATRIXCOMPARE(function) \
    BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));

  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);

  #undef BASEMATRIXCOMPARE
}

/**
 * Test member functions which prototype is
 * void (BaseMatrix::*)(BaseMatrix&, real).
 */
TEST(BaseMatrix, BaseMatrix_real) {
  typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, real);
  #define BASEMATRIXCOMPARE(function) \
    BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
H
hedaoyuan 已提交
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 180 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  BASEMATRIXCOMPARE(addBias);
  BASEMATRIXCOMPARE(add);
  BASEMATRIXCOMPARE(sub);
  BASEMATRIXCOMPARE(pow);
  BASEMATRIXCOMPARE(addScalar);
  BASEMATRIXCOMPARE(subScalar);
  BASEMATRIXCOMPARE(mulScalar);
  BASEMATRIXCOMPARE(divScalar);
  BASEMATRIXCOMPARE(scalarDiv);
  BASEMATRIXCOMPARE(addSquare);

  BASEMATRIXCOMPARE(isEqualTo);

  #undef BASEMATRIXCOMPARE
}

/**
 * Test member functions which prototype is
 * void (BaseMatrix::*)(BaseMatrix&, BaseMatrix&).
 */
TEST(BaseMatrix, BaseMatrix_BaseMatrix) {
  typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, BaseMatrix&);
  #define BASEMATRIXCOMPARE(function) \
    BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));

  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);

  #undef BASEMATRIXCOMPARE
}

/**
 * Test aggregate member functions which prototype is
 * void (BaseMatrix::*)(BaseMatrix&).
 */
TEST(Aggregate, BaseMatrix) {
  typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&);
  #define BASEMATRIXAPPLYROW(function) \
    BaseMatrixApplyRow<0>(static_cast<FunctionProto>(&BaseMatrix::function));

 #define BASEMATRIXAPPLYCOL(function) \
    BaseMatrixApplyCol<0>(static_cast<FunctionProto>(&BaseMatrix::function));

  BASEMATRIXAPPLYROW(maxRows);
  BASEMATRIXAPPLYROW(minRows);

  BASEMATRIXAPPLYCOL(sumCols);
  BASEMATRIXAPPLYCOL(maxCols);
  BASEMATRIXAPPLYCOL(minCols);

  #undef BASEMATRIXAPPLYROW
  #undef BASEMATRIXAPPLYCOL
}

/**
 * Test aggregate member functions which prototype is
 * void (BaseMatrix::*)(BaseMatrix&, BaseMatrix&).
 */
TEST(Aggregate, BaseMatrix_BaseMatrix) {
  typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, BaseMatrix&);
  #define BASEMATRIXAPPLYROW(function) \
    BaseMatrixApplyRow<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));

  #define BASEMATRIXAPPLYCOL(function) \
    BaseMatrixApplyCol<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));

  BASEMATRIXAPPLYCOL(addDotMulVMM);

  #undef BASEMATRIXAPPLYROW
  #undef BASEMATRIXAPPLYCOL
}

/**
 * Test aggregate member functions which prototype is
 * void (BaseMatrix::*)(BaseMatrix&, real, real).
 */
TEST(Aggregate, BaseMatrix_real_real) {
  typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, real, real);
  #define BASEMATRIXAPPLYROW(function) \
    BaseMatrixApplyRow<0, 1, 2>(\
      static_cast<FunctionProto>(&BaseMatrix::function));
H
hedaoyuan 已提交
237

238 239 240
  #define BASEMATRIXAPPLYCOL(function) \
    BaseMatrixApplyCol<0, 1, 2>(\
      static_cast<FunctionProto>(&BaseMatrix::function));
H
hedaoyuan 已提交
241

242 243
  BASEMATRIXAPPLYROW(sumRows);
  BASEMATRIXAPPLYCOL(sumCols);
H
hedaoyuan 已提交
244

245 246
  #undef BASEMATRIXAPPLYROW
  #undef BASEMATRIXAPPLYCOL
H
hedaoyuan 已提交
247 248 249 250 251 252 253 254 255
}

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

#endif