test_common.cpp 5.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

X
Xin Pan 已提交
15
#include <paddle/legacy/utils/Util.h>
Y
Yu Yang 已提交
16
#include <stdlib.h>
Z
zhangjinchao01 已提交
17 18

#include <gtest/gtest.h>
X
Xin Pan 已提交
19
#include <paddle/legacy/parameter/ParameterUpdateFunctions.h>
X
Xin Pan 已提交
20 21 22
#include <paddle/legacy/utils/Flags.h>
#include <paddle/legacy/utils/Stat.h>
#include <paddle/legacy/utils/Thread.h>
Z
zhangjinchao01 已提交
23 24 25 26

using namespace paddle;  // NOLINT

class CommonTest : public ::testing::Test {
W
Wu Yi 已提交
27
 protected:
Z
zhangjinchao01 已提交
28 29 30
  CommonTest() : testStat_("test") {}
  virtual ~CommonTest() {}
  virtual void SetUp() {
31 32
    const size_t buffSize[] = {
        100, 128, 500, 1024, 4096, 10240, 102400, 1000000};
Z
zhangjinchao01 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46
    sizeVec_.resize(8);
    memcpy(&sizeVec_[0], &buffSize[0], 8 * sizeof(size_t));
    valueUint_.resize(4);
    valueUint_[0].first = 0.0;
    valueUint_[0].second = 0.0;
    valueUint_[1].first = 0.0;
    valueUint_[1].second = 1.0;
    valueUint_[2].first = 1.0;
    valueUint_[2].second = 0.0;
    valueUint_[3].first = 1.0;
    valueUint_[3].second = 1.0;
    learningRate_ = 1.0;
  }

47 48 49 50
  void test_sgdUpadate(real* gradientBuffer,
                       real* valueBuffer,
                       real* momentumBuffer,
                       size_t size);
Z
zhangjinchao01 已提交
51 52 53

  virtual void TreaDown() { LOG(INFO) << "All Test Finished."; }

W
Wu Yi 已提交
54
 protected:
Z
zhangjinchao01 已提交
55 56 57 58 59 60
  std::vector<std::pair<real, real>> valueUint_;
  std::vector<size_t> sizeVec_;
  real learningRate_;
  StatSet testStat_;
};

61 62 63 64
void CommonTest::test_sgdUpadate(real* gradientBuffer,
                                 real* valueBuffer,
                                 real* momentumBuffer,
                                 size_t size) {
Z
zhangjinchao01 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
// sgdUpdateAvx has no double version yet
#if defined(__AVX__) && !defined(PADDLE_TYPE_DOUBLE)
  real valueSum1 = 0, valueSum2 = 0, momSum1 = 0, momSum2 = 0;
  real* gradTmp = new real[size];
  real* valueTmp = new real[size];
  real* momentumTmp = new real[size];
  memcpy(gradTmp, gradientBuffer, size * sizeof(real));
  memcpy(valueTmp, valueBuffer, size * sizeof(real));
  memcpy(momentumTmp, momentumBuffer, size * sizeof(real));
  for (auto& arg : valueUint_) {
    {
      {
        struct timeval t;
        REGISTER_TIMER("gettimeofday", 0, testStat_);
        gettimeofday(&t, NULL);
      }
      REGISTER_TIMER("avxTimer", 0);
82 83 84 85 86 87 88
      sgdUpdateAvx(learningRate_,
                   arg.first,
                   arg.second,
                   size,
                   valueBuffer,
                   gradientBuffer,
                   momentumBuffer);
Z
zhangjinchao01 已提交
89 90 91 92 93 94 95 96 97 98 99
    }
    for (size_t i = 0; i < size; i++) {
      valueSum1 += valueBuffer[i];
      momSum1 += momentumBuffer[i];
      // std::cout << "["
      //          << valueBuffer[i]
      //          << "," << momentumBuffer[i]
      //          << "," << gradientBuffer[i] << "],";
    }
    {
      REGISTER_TIMER("cpuTimer", 0);
100 101 102 103 104 105 106
      sgdUpdateCpu(learningRate_,
                   arg.first,
                   arg.second,
                   size,
                   valueTmp,
                   gradTmp,
                   momentumTmp);
Z
zhangjinchao01 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }
    for (size_t i = 0; i < size; i++) {
      valueSum2 += valueTmp[i];
      momSum2 += momentumTmp[i];
      // std::cout << "["
      //          << valueTmp[i]
      //          << "," << momentumTmp[i]
      //          << "," << gradTmp[i] << "],";
    }

    VLOG(3) << "valueSum1 = " << valueSum1 << " ; valueSum2 = " << valueSum2;
    VLOG(3) << "momSum1 = " << momSum1 << " ; momSum2 = " << momSum2;
    ASSERT_EQ(valueSum1, valueSum2);
    ASSERT_EQ(momSum1, momSum2);
  }
  delete[] gradTmp;
  delete[] valueTmp;
  delete[] momentumTmp;
#endif
}

TEST_F(CommonTest, sgdUpdate) {
  const size_t alignHeader[] = {0, 2, 3, 5, 7, 8};
  for (auto& size : sizeVec_) {
131
    real *gradientBuffer, *valueBuffer, *momentumBuffer;
Y
Yu Yang 已提交
132
    CHECK_EQ(posix_memalign((void**)&gradientBuffer, 32, sizeof(real) * size),
133
             0);
Y
Yu Yang 已提交
134 135
    CHECK_EQ(posix_memalign((void**)&valueBuffer, 32, sizeof(real) * size), 0);
    CHECK_EQ(posix_memalign((void**)&momentumBuffer, 32, sizeof(real) * size),
136
             0);
137

Z
zhangjinchao01 已提交
138 139 140 141 142 143 144 145 146 147
    for (size_t i = 0; i < size; i++) {
      gradientBuffer[i] = 1.0;
      valueBuffer[i] = 2.0;
      momentumBuffer[i] = 3.0;
    }
    for (int i = 0; i < 6; i++) {
      LOG(INFO) << "----------------------" << size << ":" << alignHeader[i]
                << "-------------------------";
      test_sgdUpadate(&gradientBuffer[alignHeader[i]],
                      &valueBuffer[alignHeader[i]],
148 149
                      &momentumBuffer[alignHeader[i]],
                      size - alignHeader[i]);
Z
zhangjinchao01 已提交
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
    }
    free(gradientBuffer);
    free(valueBuffer);
    free(momentumBuffer);
  }
  globalStat.printAllStatus();
  testStat_.printAllStatus();
}

TEST_F(CommonTest, syncThreadPool) {
  SyncThreadPool pool(10);

  std::vector<int> nums;
  nums.resize(10);

  pool.exec([&](int tid, size_t numThreads) { nums[tid] = tid; });
  for (size_t i = 0; i < nums.size(); ++i) {
    EXPECT_EQ((int)i, nums[i]);
  }

  pool.exec([&](int tid, size_t numThreads) { nums[tid] -= tid; });
  for (size_t i = 0; i < nums.size(); ++i) {
    EXPECT_EQ((int)0, nums[i]);
  }
}