test_BaseMatrix.cpp 8.3 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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
/**
17 18 19
 * This test file use autotest::AutoCompare and cmpWithoutArg to compares the
 * implementation of CPU and GPU member function in
 * BaseMatrix.cpp and Matrix.cpp.
H
hedaoyuan 已提交
20 21 22 23 24 25
 */

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

26 27 28
using paddle::BaseMatrix;
using paddle::Matrix;
using autotest::AutoCompare;
H
hedaoyuan 已提交
29

30
// Test all void (BaseMatrix::*)() function
31
TEST(BaseMatrix, void) {
32 33 34 35 36 37 38 39
  for (auto height : {1, 3, 11, 73, 128, 200, 330}) {
    for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) {
      auto compare = [height, width](void (BaseMatrix::*f)()) {
        AutoCompare test(height, width, 1e-5);
        test.cmpWithoutArg(f, height, width);
      };

      compare(&BaseMatrix::neg);
H
hedaoyuan 已提交
40 41 42 43 44 45 46
      compare(&BaseMatrix::exp2);
      compare(&BaseMatrix::log2);
      compare(&BaseMatrix::sqrt2);
      compare(&BaseMatrix::square2);
      compare(&BaseMatrix::reciprocal2);
      compare(&BaseMatrix::abs2);
      compare(&BaseMatrix::sign2);
47 48 49 50
      compare(&BaseMatrix::zero);
      compare(&BaseMatrix::one);
    }
  }
51 52
}

53
// Test all void (BaseMatrix::*)(real) function
54
TEST(BaseMatrix, real) {
55 56 57 58 59 60 61
  for (auto height : {1, 3, 11, 73, 128, 200, 330}) {
    for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) {
      auto compare = [height, width](void (BaseMatrix::*f)(real)) {
        AutoCompare test(height, width, 1e-5);
        test.cmpWithoutArg<0>(f, height, width);
      };

H
hedaoyuan 已提交
62
      compare(&BaseMatrix::pow2);
63 64 65 66 67 68 69 70 71
      compare(&BaseMatrix::subScalar);
      compare(&BaseMatrix::mulScalar);
      compare(&BaseMatrix::divScalar);
      compare(&BaseMatrix::assign);
      compare(&BaseMatrix::add);
      compare(&BaseMatrix::biggerThanScalar);
      compare(&BaseMatrix::downClip);
    }
  }
72 73
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
// Test all void (BaseMatrix::*)(BaseMatrix&) function
TEST(BaseMatrix, BaseMatrix) {
  for (auto height : {1, 3, 11, 73, 128, 200, 330}) {
    for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) {
      auto compare = [height, width](void (BaseMatrix::*f)(BaseMatrix&)) {
        AutoCompare test(height, width, 1e-5);
        test.cmpWithoutArg<0>(f, height, width);
      };

      compare(&BaseMatrix::assign);
      compare(&BaseMatrix::add);
      compare(&BaseMatrix::relu);
      compare(&BaseMatrix::reluDerivative);
      compare(&BaseMatrix::softrelu);
      compare(&BaseMatrix::softreluDerivative);
      compare(&BaseMatrix::brelu);
      compare(&BaseMatrix::breluDerivative);
H
hedaoyuan 已提交
91
      compare(&BaseMatrix::square2);
92 93 94
      compare(&BaseMatrix::squareDerivative);
      compare(&BaseMatrix::tanh);
      compare(&BaseMatrix::tanhDerivative);
H
hedaoyuan 已提交
95
      compare(&BaseMatrix::reciprocal2);
96
      compare(&BaseMatrix::reciprocalDerivative);
H
hedaoyuan 已提交
97
      compare(&BaseMatrix::abs2);
98 99 100 101
      compare(&BaseMatrix::absDerivative);
      compare(&BaseMatrix::sigmoid);
      compare(&BaseMatrix::sigmoidDerivative);
      compare(&BaseMatrix::expDerivative);
H
hedaoyuan 已提交
102 103 104 105
      compare(&BaseMatrix::sign2);
      compare(&BaseMatrix::exp2);
      compare(&BaseMatrix::log2);
      compare(&BaseMatrix::sqrt2);
106 107 108 109 110 111 112 113 114 115 116
      compare(&BaseMatrix::dotMul);
      compare(&BaseMatrix::dotMulSquare);
      compare(&BaseMatrix::dotSquareMul);
      compare(&BaseMatrix::addColVector);
      compare(&BaseMatrix::addRowVector);
      compare(&BaseMatrix::mulRowVector);
      compare(&BaseMatrix::divRowVector);
      compare(&BaseMatrix::addP2P);
      compare(&BaseMatrix::invSqrt);
    }
  }
117 118
}

119 120 121 122 123 124 125 126 127 128 129 130 131
// Test all void (BaseMatrix::*)(real, real) function
TEST(BaseMatrix, real_real) {
  for (auto height : {1, 3, 11, 73, 128, 200, 330}) {
    for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) {
      auto compare = [height, width](void (BaseMatrix::*f)(real, real)) {
        AutoCompare test(height, width, 1e-5);
        test.cmpWithoutArg<0, 1>(f, height, width);
      };

      compare(&BaseMatrix::add);
      compare(&BaseMatrix::clip);
    }
  }
132 133
}

134
// Test all void (BaseMatrix::*)(BaseMatrix&, real) function
135
TEST(BaseMatrix, BaseMatrix_real) {
136 137 138 139 140 141 142 143 144 145
  for (auto height : {1, 3, 11, 73, 128, 200, 330}) {
    for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) {
      auto compare = [height, width](void (BaseMatrix::*f)(BaseMatrix&, real)) {
        AutoCompare test(height, width, 1e-5);
        test.cmpWithoutArg<0, 1>(f, height, width);
      };

      compare(&BaseMatrix::addBias);
      compare(&BaseMatrix::add);
      compare(&BaseMatrix::sub);
H
hedaoyuan 已提交
146
      compare(&BaseMatrix::pow2);
147 148 149 150 151 152 153 154 155
      compare(&BaseMatrix::addScalar);
      compare(&BaseMatrix::subScalar);
      compare(&BaseMatrix::mulScalar);
      compare(&BaseMatrix::divScalar);
      compare(&BaseMatrix::scalarDiv);
      compare(&BaseMatrix::addSquare);
      compare(&BaseMatrix::isEqualTo);
    }
  }
156 157
}

158
// Test all void (BaseMatrix::*)(BaseMatrix&, BaseMatrix&) function
159
TEST(BaseMatrix, BaseMatrix_BaseMatrix) {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  for (auto height : {1, 3, 11, 73, 128, 200, 330}) {
    for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) {
      auto compare = [height,
                      width](void (BaseMatrix::*f)(BaseMatrix&, BaseMatrix&)) {
        AutoCompare test(height, width, 1e-5);
        test.cmpWithoutArg<0, 1>(f, height, width);
      };

      compare(&BaseMatrix::softCrossEntropy);
      compare(&BaseMatrix::softCrossEntropyBp);
      compare(&BaseMatrix::binaryLabelCrossEntropy);
      compare(&BaseMatrix::binaryLabelCrossEntropyBp);
      compare(&BaseMatrix::sub);
      compare(&BaseMatrix::add2);
      compare(&BaseMatrix::dotMul);
      compare(&BaseMatrix::dotDiv);
      compare(&BaseMatrix::logisticRegressionLoss);
      compare(&BaseMatrix::logisticRegressionLossBp);
      compare(&BaseMatrix::biggerThan);
H
hedaoyuan 已提交
179
      compare(&BaseMatrix::max2);
180 181 182 183
      compare(&BaseMatrix::dotMulSquare);
      compare(&BaseMatrix::dotSquareSquare);
    }
  }
184 185
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
void TestEelementWise(size_t height, size_t width) {
  AutoCompare rowScale(height, width);
  rowScale.cmpWithoutArg<0, 1, 2>(&BaseMatrix::rowScale, height, width);

  AutoCompare rowDotMul(height, width);
  rowDotMul.cmpWithoutArg<0, 1, 2>(&BaseMatrix::rowDotMul, height, width);

  AutoCompare binaryClassificationError(height, width);
  binaryClassificationError.cmpWithoutArg<0, 1, 2, 3>(
      &BaseMatrix::binaryClassificationError, height, width);

  AutoCompare sumOfSquaresBp(height, width);
  sumOfSquaresBp.cmpWithoutArg<0, 1>(&Matrix::sumOfSquaresBp, height, width);
}

void TestAggregateToRow(size_t height, size_t width) {
  AutoCompare maxCols(1, width);
  maxCols.cmpWithoutArg<0>(&BaseMatrix::maxCols, height, width);

  AutoCompare minCols(1, width);
  minCols.cmpWithoutArg<0>(&BaseMatrix::minCols, height, width);
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
  AutoCompare addDotMulVMM(1, width);
  addDotMulVMM.cmpWithoutArg<0, 1>(&BaseMatrix::addDotMulVMM, height, width);

  AutoCompare sumCols(1, width);
  sumCols.cmpWithoutArg<0, 1, 2>(&BaseMatrix::sumCols, height, width);

  AutoCompare collectBias(1, width);
  collectBias.cmpWithoutArg<0, 1>(
      static_cast<void (Matrix::*)(Matrix&, real)>(&Matrix::collectBias),
      height,
      width);
}

void TestAggregateToCol(size_t height, size_t width) {
  AutoCompare maxRows(height, 1);
  maxRows.cmpWithoutArg<0>(&BaseMatrix::maxRows, height, width);

  AutoCompare minRows(height, 1);
  minRows.cmpWithoutArg<0>(&BaseMatrix::minRows, height, width);

  AutoCompare sumRows(height, 1);
  sumRows.cmpWithoutArg<0, 1, 2>(&BaseMatrix::sumRows, height, width);

  AutoCompare sumOfSquares(height, 1);
  sumOfSquares.cmpWithoutArg<0, 1>(&Matrix::sumOfSquares, height, width);
233 234
}

235 236 237 238 239 240 241 242
TEST(BaseMatrix, Other) {
  for (auto height : {1, 3, 11, 73, 128, 200, 330}) {
    for (auto width : {1, 3, 32, 100, 512, 1000, 3210}) {
      TestEelementWise(height, width);
      TestAggregateToRow(height, width);
      TestAggregateToCol(height, width);
    }
  }
H
hedaoyuan 已提交
243 244 245 246
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
247
  paddle::initMain(argc, argv);
H
hedaoyuan 已提交
248 249 250 251
  return RUN_ALL_TESTS();
}

#endif