test_TrainingAlgorithm.cpp 18.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
H
hedaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13

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. */
H
hedaoyuan 已提交
14 15 16

#include <gtest/gtest.h>
#include "OriginalOptimizerApi.h"
H
hedaoyuan 已提交
17
#include "PerfUtils.h"
Y
Yu Yang 已提交
18
#include "TensorCheck.h"
X
Xin Pan 已提交
19
#include "paddle/legacy/math/TrainingAlgorithmOp.h"
Y
Yu Yang 已提交
20
#include "paddle/utils/Util.h"
H
hedaoyuan 已提交
21 22 23 24

using namespace paddle;  // NOLINT

#ifndef PADDLE_TYPE_DOUBLE
25
DEFINE_double(max_diff, 1e-5, "max diff allowed");
H
hedaoyuan 已提交
26
#else
27
DEFINE_double(max_diff, 1e-13, "max diff allowed");
H
hedaoyuan 已提交
28 29 30
#endif

class SetMaxDiff {
W
Wu Yi 已提交
31
 public:
H
hedaoyuan 已提交
32 33 34 35
  explicit SetMaxDiff(double max_diff) {
    max_diff_ = FLAGS_max_diff;
    FLAGS_max_diff = max_diff;
  }
H
hedaoyuan 已提交
36 37
  ~SetMaxDiff() { FLAGS_max_diff = max_diff_; }

W
Wu Yi 已提交
38
 private:
H
hedaoyuan 已提交
39 40 41
  double max_diff_;
};

H
hedaoyuan 已提交
42 43 44 45 46 47 48 49
#define COPY_VECTOR_TO_CPU(cpuVec, vector)               \
  do {                                                   \
    if (vector->useGpu()) {                              \
      cpuVec = Vector::create(vector->getSize(), false); \
      cpuVec->copyFrom(*vector);                         \
    } else {                                             \
      cpuVec = vector;                                   \
    }                                                    \
H
hedaoyuan 已提交
50 51
  } while (0)

H
hedaoyuan 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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;
}

H
hedaoyuan 已提交
72 73 74 75 76 77 78 79
int VectorCheckErr(const VectorPtr& vector1, const VectorPtr& vector2) {
  VectorPtr tmp1;
  VectorPtr tmp2;
  COPY_VECTOR_TO_CPU(tmp1, vector1);
  COPY_VECTOR_TO_CPU(tmp2, vector2);
  return VectorCheckErr(*tmp1, *tmp2);
}

H
hedaoyuan 已提交
80 81
#ifdef PADDLE_DISABLE_TIMER

H
hedaoyuan 已提交
82 83
#define CHECK_VECTORPTR(vector1, vector2) \
  EXPECT_EQ(VectorCheckErr(vector1, vector2), 0)
H
hedaoyuan 已提交
84 85 86 87 88 89 90

#else

#define CHECK_VECTORPTR(vector1, vector2)

#endif

H
hedaoyuan 已提交
91 92 93
typedef std::function<void(size_t size, bool useGpu)> testMatrixFunc;

void testCase(testMatrixFunc matrixFunc) {
94
#ifdef PADDLE_WITH_CUDA
H
hedaoyuan 已提交
95
  for (auto useGpu : {false, true}) {
H
hedaoyuan 已提交
96 97 98
#else
  for (auto useGpu : {false}) {
#endif
H
hedaoyuan 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
    for (auto size : {1,
                      32,
                      64,
                      128,
                      512,
                      1024,
                      4096,
                      32768,
                      65536,
                      131072,
                      262144,
                      524288,
                      1048576,
                      2097152}) {
H
hedaoyuan 已提交
113 114 115 116 117 118 119
      LOG(INFO) << " size=" << size << " useGpu=" << useGpu;
      matrixFunc(size, useGpu);
    }
  }
}

#define INIT_VECTOR(vec1, vec2, type, size, useGpu) \
H
hedaoyuan 已提交
120 121 122 123
  vec1[type] = Vector::create(size, useGpu);        \
  vec2[type] = Vector::create(size, useGpu);        \
  vec1[type]->rand();                               \
  vec2[type]->copyFrom(*vec1[type]);
H
hedaoyuan 已提交
124 125 126 127 128 129 130 131 132 133 134

void testAdagrad(size_t size, bool useGpu) {
  VectorPtr bufs1[NUM_PARAMETER_TYPES];
  VectorPtr bufs2[NUM_PARAMETER_TYPES];
  INIT_VECTOR(bufs1, bufs2, PARAMETER_VALUE, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_MOMENTUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT_SQURESUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT_SQURESUM1, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_LEARNING_RATE, size, useGpu);

H
hedaoyuan 已提交
135
  real epsilon = (real)rand() / (real)RAND_MAX;       // NOLINT
H
hedaoyuan 已提交
136
  real learningRate = (real)rand() / (real)RAND_MAX;  // NOLINT
H
hedaoyuan 已提交
137 138
  real momentum = (real)rand() / (real)RAND_MAX;      // NOLINT
  real decayRate = (real)rand() / (real)RAND_MAX;     // NOLINT
H
hedaoyuan 已提交
139

H
hedaoyuan 已提交
140 141
  EXPRESSION_PERFORMANCE(AdagradParameterOptimizer(
      bufs1, epsilon, learningRate, momentum, decayRate));
H
hedaoyuan 已提交
142 143 144 145 146 147 148 149

  BaseMatrix& value = *bufs2[PARAMETER_VALUE];
  BaseMatrix& grad = *bufs2[PARAMETER_GRADIENT];
  BaseMatrix& mom = *bufs2[PARAMETER_MOMENTUM];
  BaseMatrix& accum_buffer = *bufs2[PARAMETER_GRADIENT_SQURESUM];
  BaseMatrix& accum = *bufs2[PARAMETER_GRADIENT_SQURESUM1];
  BaseMatrix& lr = *bufs2[PARAMETER_LEARNING_RATE];

H
hedaoyuan 已提交
150 151 152 153 154 155 156 157 158 159
  EXPRESSION_PERFORMANCE(adagradApply(value,
                                      grad,
                                      mom,
                                      accum_buffer,
                                      accum,
                                      lr,
                                      epsilon,
                                      learningRate,
                                      momentum,
                                      decayRate));
H
hedaoyuan 已提交
160 161 162 163 164 165 166 167 168

  CHECK_VECTORPTR(bufs1[PARAMETER_VALUE], bufs2[PARAMETER_VALUE]);
  CHECK_VECTORPTR(bufs1[PARAMETER_MOMENTUM], bufs2[PARAMETER_MOMENTUM]);
  CHECK_VECTORPTR(bufs1[PARAMETER_GRADIENT_SQURESUM1],
                  bufs2[PARAMETER_GRADIENT_SQURESUM1]);
  CHECK_VECTORPTR(bufs1[PARAMETER_LEARNING_RATE],
                  bufs2[PARAMETER_LEARNING_RATE]);
}

H
hedaoyuan 已提交
169
TEST(Training, Adagrad) { testCase(testAdagrad); }
H
hedaoyuan 已提交
170 171 172 173 174 175 176 177 178 179 180

void testAdaDelta(size_t size, bool useGpu) {
  VectorPtr bufs1[NUM_PARAMETER_TYPES];
  VectorPtr bufs2[NUM_PARAMETER_TYPES];
  INIT_VECTOR(bufs1, bufs2, PARAMETER_VALUE, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_MOMENTUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT_SQURESUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT_SQURESUM1, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_LEARNING_RATE, size, useGpu);

H
hedaoyuan 已提交
181 182
  real rou = (real)rand() / (real)RAND_MAX;           // NOLINT
  real epsilon = (real)rand() / (real)RAND_MAX;       // NOLINT
H
hedaoyuan 已提交
183
  real learningRate = (real)rand() / (real)RAND_MAX;  // NOLINT
H
hedaoyuan 已提交
184 185
  real momentum = (real)rand() / (real)RAND_MAX;      // NOLINT
  real decayRate = (real)rand() / (real)RAND_MAX;     // NOLINT
H
hedaoyuan 已提交
186

H
hedaoyuan 已提交
187 188
  EXPRESSION_PERFORMANCE(AdaDeltaParameterOptimizer(
      bufs1, rou, epsilon, learningRate, momentum, decayRate));
H
hedaoyuan 已提交
189 190 191 192 193 194 195 196

  BaseMatrix& value = *bufs2[PARAMETER_VALUE];
  BaseMatrix& grad = *bufs2[PARAMETER_GRADIENT];
  BaseMatrix& mom = *bufs2[PARAMETER_MOMENTUM];
  BaseMatrix& accum = *bufs2[PARAMETER_GRADIENT_SQURESUM];
  BaseMatrix& accum_update = *bufs2[PARAMETER_GRADIENT_SQURESUM1];
  BaseMatrix& lr = *bufs2[PARAMETER_LEARNING_RATE];

H
hedaoyuan 已提交
197 198 199 200 201 202 203 204 205 206 207
  EXPRESSION_PERFORMANCE(adadeltaApply(value,
                                       grad,
                                       mom,
                                       accum,
                                       accum_update,
                                       lr,
                                       rou,
                                       epsilon,
                                       learningRate,
                                       momentum,
                                       decayRate));
H
hedaoyuan 已提交
208 209 210 211 212 213 214 215 216 217 218

  CHECK_VECTORPTR(bufs1[PARAMETER_VALUE], bufs2[PARAMETER_VALUE]);
  CHECK_VECTORPTR(bufs1[PARAMETER_MOMENTUM], bufs2[PARAMETER_MOMENTUM]);
  CHECK_VECTORPTR(bufs1[PARAMETER_GRADIENT_SQURESUM],
                  bufs2[PARAMETER_GRADIENT_SQURESUM]);
  CHECK_VECTORPTR(bufs1[PARAMETER_GRADIENT_SQURESUM1],
                  bufs2[PARAMETER_GRADIENT_SQURESUM1]);
  CHECK_VECTORPTR(bufs1[PARAMETER_LEARNING_RATE],
                  bufs2[PARAMETER_LEARNING_RATE]);
}

H
hedaoyuan 已提交
219
TEST(Training, AdaDelta) { testCase(testAdaDelta); }
H
hedaoyuan 已提交
220

H
hedaoyuan 已提交
221
template <bool isFirstTime>
H
hedaoyuan 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234
void testRMSProp(size_t size, bool useGpu) {
  VectorPtr bufs1[NUM_PARAMETER_TYPES];
  VectorPtr bufs2[NUM_PARAMETER_TYPES];
  INIT_VECTOR(bufs1, bufs2, PARAMETER_VALUE, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_MOMENTUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT_SQURESUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT_SQURESUM1, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_LEARNING_RATE, size, useGpu);

  /* make sure 'g - f.square()' greater than 0 */
  bufs1[PARAMETER_GRADIENT_SQURESUM]->add(1.0);
  bufs2[PARAMETER_GRADIENT_SQURESUM]->copyFrom(
H
hedaoyuan 已提交
235
      *bufs1[PARAMETER_GRADIENT_SQURESUM]);
H
hedaoyuan 已提交
236

H
hedaoyuan 已提交
237 238
  real rou = (real)rand() / (real)RAND_MAX;           // NOLINT
  real epsilon = (real)rand() / (real)RAND_MAX;       // NOLINT
H
hedaoyuan 已提交
239
  real learningRate = (real)rand() / (real)RAND_MAX;  // NOLINT
H
hedaoyuan 已提交
240 241
  real momentum = (real)rand() / (real)RAND_MAX;      // NOLINT
  real decayRate = (real)rand() / (real)RAND_MAX;     // NOLINT
H
hedaoyuan 已提交
242 243 244
  real accumulatedRou = rou;

  EXPRESSION_PERFORMANCE(RMSPropParameterOptimizer(bufs1,
H
hedaoyuan 已提交
245 246 247 248 249 250 251
                                                   accumulatedRou,
                                                   rou,
                                                   epsilon,
                                                   learningRate,
                                                   momentum,
                                                   decayRate,
                                                   isFirstTime));
H
hedaoyuan 已提交
252 253 254 255 256 257 258 259

  BaseMatrix& value = *bufs2[PARAMETER_VALUE];
  BaseMatrix& grad = *bufs2[PARAMETER_GRADIENT];
  BaseMatrix& mom = *bufs2[PARAMETER_MOMENTUM];
  BaseMatrix& sum = *bufs2[PARAMETER_GRADIENT_SQURESUM];
  BaseMatrix& sum1 = *bufs2[PARAMETER_GRADIENT_SQURESUM1];
  BaseMatrix& lr = *bufs2[PARAMETER_LEARNING_RATE];

H
hedaoyuan 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272
  EXPRESSION_PERFORMANCE(rmspropApply(value,
                                      grad,
                                      mom,
                                      sum,
                                      sum1,
                                      lr,
                                      accumulatedRou,
                                      rou,
                                      epsilon,
                                      learningRate,
                                      momentum,
                                      decayRate,
                                      isFirstTime));
H
hedaoyuan 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

  CHECK_VECTORPTR(bufs1[PARAMETER_VALUE], bufs2[PARAMETER_VALUE]);
  CHECK_VECTORPTR(bufs1[PARAMETER_MOMENTUM], bufs2[PARAMETER_MOMENTUM]);
  CHECK_VECTORPTR(bufs1[PARAMETER_GRADIENT_SQURESUM],
                  bufs2[PARAMETER_GRADIENT_SQURESUM]);
  CHECK_VECTORPTR(bufs1[PARAMETER_GRADIENT_SQURESUM1],
                  bufs2[PARAMETER_GRADIENT_SQURESUM1]);
  CHECK_VECTORPTR(bufs1[PARAMETER_LEARNING_RATE],
                  bufs2[PARAMETER_LEARNING_RATE]);
}

TEST(Training, RMSProp) {
  testCase(testRMSProp<true>);
  testCase(testRMSProp<false>);
}

H
hedaoyuan 已提交
289
template <bool isFirstTime>
H
hedaoyuan 已提交
290 291 292 293 294 295 296 297 298
void testDecayedAdagrad(size_t size, bool useGpu) {
  VectorPtr bufs1[NUM_PARAMETER_TYPES];
  VectorPtr bufs2[NUM_PARAMETER_TYPES];
  INIT_VECTOR(bufs1, bufs2, PARAMETER_VALUE, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_MOMENTUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT_SQURESUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_LEARNING_RATE, size, useGpu);

H
hedaoyuan 已提交
299 300
  real rou = (real)rand() / (real)RAND_MAX;           // NOLINT
  real epsilon = (real)rand() / (real)RAND_MAX;       // NOLINT
H
hedaoyuan 已提交
301
  real learningRate = (real)rand() / (real)RAND_MAX;  // NOLINT
H
hedaoyuan 已提交
302 303
  real momentum = (real)rand() / (real)RAND_MAX;      // NOLINT
  real decayRate = (real)rand() / (real)RAND_MAX;     // NOLINT
H
hedaoyuan 已提交
304 305 306 307 308 309 310 311
  real accumulatedRou = rou;

  if (isFirstTime) {
    bufs1[PARAMETER_GRADIENT_SQURESUM]->zeroMem();
    bufs2[PARAMETER_GRADIENT_SQURESUM]->zeroMem();
  }

  EXPRESSION_PERFORMANCE(DecayedAdagradParameterOptimizer(bufs1,
H
hedaoyuan 已提交
312 313 314 315 316 317 318
                                                          accumulatedRou,
                                                          rou,
                                                          epsilon,
                                                          learningRate,
                                                          momentum,
                                                          decayRate,
                                                          isFirstTime));
H
hedaoyuan 已提交
319 320 321 322 323 324 325

  BaseMatrix& value = *bufs2[PARAMETER_VALUE];
  BaseMatrix& grad = *bufs2[PARAMETER_GRADIENT];
  BaseMatrix& mom = *bufs2[PARAMETER_MOMENTUM];
  BaseMatrix& sum = *bufs2[PARAMETER_GRADIENT_SQURESUM];
  BaseMatrix& lr = *bufs2[PARAMETER_LEARNING_RATE];

H
hedaoyuan 已提交
326 327 328 329 330 331 332 333 334 335 336 337
  EXPRESSION_PERFORMANCE(decayedAdagradApply(value,
                                             grad,
                                             mom,
                                             sum,
                                             lr,
                                             accumulatedRou,
                                             rou,
                                             epsilon,
                                             learningRate,
                                             momentum,
                                             decayRate,
                                             isFirstTime));
H
hedaoyuan 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

  CHECK_VECTORPTR(bufs1[PARAMETER_VALUE], bufs2[PARAMETER_VALUE]);
  CHECK_VECTORPTR(bufs1[PARAMETER_MOMENTUM], bufs2[PARAMETER_MOMENTUM]);
  CHECK_VECTORPTR(bufs1[PARAMETER_GRADIENT_SQURESUM],
                  bufs2[PARAMETER_GRADIENT_SQURESUM]);
  CHECK_VECTORPTR(bufs1[PARAMETER_LEARNING_RATE],
                  bufs2[PARAMETER_LEARNING_RATE]);
}

TEST(Training, DecayedAdagrad) {
  testCase(testDecayedAdagrad<false>);
  testCase(testDecayedAdagrad<true>);
}

void testAdam(size_t size, bool useGpu) {
  VectorPtr bufs1[NUM_PARAMETER_TYPES];
  VectorPtr bufs2[NUM_PARAMETER_TYPES];
  INIT_VECTOR(bufs1, bufs2, PARAMETER_VALUE, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_MOMENTUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_SECOND_MOMENTUM, size, useGpu);

H
hedaoyuan 已提交
360 361 362 363 364
  real beta1 = (real)rand() / (real)RAND_MAX;         // NOLINT
  real beta2 = (real)rand() / (real)RAND_MAX;         // NOLINT
  real beta1_power = (real)rand() / (real)RAND_MAX;   // NOLINT
  real beta2_power = (real)rand() / (real)RAND_MAX;   // NOLINT
  real epsilon = (real)rand() / (real)RAND_MAX;       // NOLINT
H
hedaoyuan 已提交
365 366
  real learningRate = (real)rand() / (real)RAND_MAX;  // NOLINT

H
hedaoyuan 已提交
367 368
  EXPRESSION_PERFORMANCE(AdamParameterOptimizer(
      bufs1, beta1, beta2, beta1_power, beta2_power, epsilon, learningRate));
H
hedaoyuan 已提交
369 370 371 372 373 374

  BaseMatrix& value = *bufs2[PARAMETER_VALUE];
  BaseMatrix& grad = *bufs2[PARAMETER_GRADIENT];
  BaseMatrix& mom = *bufs2[PARAMETER_MOMENTUM];
  BaseMatrix& v = *bufs2[PARAMETER_SECOND_MOMENTUM];

H
hedaoyuan 已提交
375 376 377 378 379 380 381 382 383 384
  EXPRESSION_PERFORMANCE(adamApply(value,
                                   grad,
                                   mom,
                                   v,
                                   beta1,
                                   beta2,
                                   beta1_power,
                                   beta2_power,
                                   epsilon,
                                   learningRate));
H
hedaoyuan 已提交
385 386 387 388 389 390 391

  CHECK_VECTORPTR(bufs1[PARAMETER_VALUE], bufs2[PARAMETER_VALUE]);
  CHECK_VECTORPTR(bufs1[PARAMETER_MOMENTUM], bufs2[PARAMETER_MOMENTUM]);
  CHECK_VECTORPTR(bufs1[PARAMETER_SECOND_MOMENTUM],
                  bufs2[PARAMETER_SECOND_MOMENTUM]);
}

H
hedaoyuan 已提交
392
TEST(Training, Adam) { testCase(testAdam); }
H
hedaoyuan 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406

void testAdamax(size_t size, bool useGpu) {
  VectorPtr bufs1[NUM_PARAMETER_TYPES];
  VectorPtr bufs2[NUM_PARAMETER_TYPES];
  INIT_VECTOR(bufs1, bufs2, PARAMETER_VALUE, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_MOMENTUM, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_WEIGHTED_INFINITY_NORM, size, useGpu);

  real beta1 = (real)rand() / (real)RAND_MAX;  // NOLINT
  real beta2 = (real)rand() / (real)RAND_MAX;  // NOLINT
  real alpha = (real)rand() / (real)RAND_MAX;  // NOLINT
  int64_t step = 2;

H
hedaoyuan 已提交
407 408
  EXPRESSION_PERFORMANCE(
      AdamaxParameterOptimizer(bufs1, beta1, beta2, step, alpha));
H
hedaoyuan 已提交
409 410 411 412 413 414

  BaseMatrix& value = *bufs2[PARAMETER_VALUE];
  BaseMatrix& grad = *bufs2[PARAMETER_GRADIENT];
  BaseMatrix& mom = *bufs2[PARAMETER_MOMENTUM];
  BaseMatrix& u = *bufs2[PARAMETER_WEIGHTED_INFINITY_NORM];

H
hedaoyuan 已提交
415 416
  EXPRESSION_PERFORMANCE(
      adamaxApply(value, grad, mom, u, beta1, beta2, step, alpha));
H
hedaoyuan 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

  CHECK_VECTORPTR(bufs1[PARAMETER_VALUE], bufs2[PARAMETER_VALUE]);
  CHECK_VECTORPTR(bufs1[PARAMETER_MOMENTUM], bufs2[PARAMETER_MOMENTUM]);
  CHECK_VECTORPTR(bufs1[PARAMETER_WEIGHTED_INFINITY_NORM],
                  bufs2[PARAMETER_WEIGHTED_INFINITY_NORM]);
}

TEST(Training, Adamax) {
#ifndef PADDLE_TYPE_DOUBLE
  SetMaxDiff diff(1e-4);
#endif
  testCase(testAdamax);
}

void testSparseMomentum(size_t size, bool useGpu) {
  VectorPtr bufs1[NUM_PARAMETER_TYPES];
  VectorPtr bufs2[NUM_PARAMETER_TYPES];
  INIT_VECTOR(bufs1, bufs2, PARAMETER_VALUE, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_GRADIENT, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_MOMENTUM_UT, size, useGpu);
  INIT_VECTOR(bufs1, bufs2, PARAMETER_MOMENTUM_VT, size, useGpu);

H
hedaoyuan 已提交
439 440 441 442
  real alpha = (real)rand() / (real)RAND_MAX;         // NOLINT
  real beta = (real)rand() / (real)RAND_MAX;          // NOLINT
  real gamma = (real)rand() / (real)RAND_MAX;         // NOLINT
  real tau = (real)rand() / (real)RAND_MAX;           // NOLINT
H
hedaoyuan 已提交
443 444
  real learningRate = (real)rand() / (real)RAND_MAX;  // NOLINT

H
hedaoyuan 已提交
445 446
  EXPRESSION_PERFORMANCE(SparseMomentumParameterOptimizer(
      bufs1, alpha, beta, gamma, tau, learningRate));
H
hedaoyuan 已提交
447 448 449 450 451 452

  BaseMatrix& value = *bufs2[PARAMETER_VALUE];
  BaseMatrix& grad = *bufs2[PARAMETER_GRADIENT];
  BaseMatrix& momU = *bufs2[PARAMETER_MOMENTUM_UT];
  BaseMatrix& momV = *bufs2[PARAMETER_MOMENTUM_VT];

H
hedaoyuan 已提交
453 454
  EXPRESSION_PERFORMANCE(sparseMomentumApply(
      value, grad, momU, momV, alpha, beta, gamma, tau, learningRate));
H
hedaoyuan 已提交
455 456

  CHECK_VECTORPTR(bufs1[PARAMETER_VALUE], bufs2[PARAMETER_VALUE]);
H
hedaoyuan 已提交
457 458
  CHECK_VECTORPTR(bufs1[PARAMETER_MOMENTUM_UT], bufs2[PARAMETER_MOMENTUM_UT]);
  CHECK_VECTORPTR(bufs1[PARAMETER_MOMENTUM_VT], bufs2[PARAMETER_MOMENTUM_VT]);
H
hedaoyuan 已提交
459 460
}

H
hedaoyuan 已提交
461
TEST(Training, SparseMomentum) { testCase(testSparseMomentum); }