test_lazyAssign.cu 3.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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

#include <gtest/gtest.h>
L
liaogang 已提交
16 17
#include "PerfUtils.h"
#include "TensorCheck.h"
H
hedaoyuan 已提交
18 19 20
#include "paddle/math/Matrix.h"
#include "paddle/math/TensorAssign.h"

21 22 23
using paddle::BaseMatrix;
using paddle::CpuMatrix;
using paddle::GpuMatrix;
H
hedaoyuan 已提交
24 25
using autotest::TensorCheckEqual;
using autotest::TensorCheckErr;
H
hedaoyuan 已提交
26 27 28 29

typedef std::function<void(int height, int width)> testMatrixFunc;
void testMatrixCase(testMatrixFunc matrixFunc) {
  for (auto height : {1}) {
L
liaogang 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    for (auto width : {1,
                       32,
                       64,
                       128,
                       512,
                       1024,
                       4096,
                       32768,
                       65536,
                       131072,
                       262144,
                       524288,
                       1048576,
                       2097152,
                       4194304,
                       8388608}) {
H
hedaoyuan 已提交
46 47 48 49 50
      matrixFunc(height, width);
    }
  }
}

L
liaogang 已提交
51
template <typename Tensor>
H
hedaoyuan 已提交
52
void testLazyAssign(int height, int width) {
H
hedaoyuan 已提交
53 54 55 56 57 58 59 60 61 62
  Tensor A1(height, width);
  Tensor A2(height, width);
  Tensor B(height, width);
  Tensor C(height, width);
  Tensor D(height, width);
  A1.randomizeUniform();
  B.randomizeUniform();
  C.randomizeUniform();
  D.randomizeUniform();
  A2.copyFrom(A1);
H
hedaoyuan 已提交
63 64 65

  EXPRESSION_PERFORMANCE(A1 = B + C; A1 = A1 * D;);

L
liaogang 已提交
66 67 68
  EXPRESSION_PERFORMANCE(auto expr1 = A2.lazyAssign(B + C);
                         auto expr2 = A2.lazyAssign(A2 * D);
                         AssignEvaluate(expr1, expr2););
H
hedaoyuan 已提交
69 70 71 72

  TensorCheckErr(A1, A2);
}

L
liaogang 已提交
73
TEST(lazyAssign, CPU) { testMatrixCase(testLazyAssign<CpuMatrix>); }
H
hedaoyuan 已提交
74 75

#ifndef PADDLE_ONLY_CPU
L
liaogang 已提交
76
TEST(lazyAssign, GPU) { testMatrixCase(testLazyAssign<GpuMatrix>); }
H
hedaoyuan 已提交
77 78
#endif

L
liaogang 已提交
79 80 81
template <typename Tensor>
void sgdUpdateTensor(
    Tensor& A, Tensor& B, Tensor& C, Tensor& D, real p1, real p2, real p3) {
H
hedaoyuan 已提交
82 83 84 85
  C = C * p2 - D * (B + A * p3) * p1;
  A += C;
}

L
liaogang 已提交
86 87 88 89 90 91 92
void sgdUpdateLazyAssign(BaseMatrix& A,
                         BaseMatrix& B,
                         BaseMatrix& C,
                         BaseMatrix& D,
                         real p1,
                         real p2,
                         real p3) {
H
hedaoyuan 已提交
93 94 95 96 97
  auto expr1 = C.lazyAssign(C * p2 - D * (B + A * p3) * p1);
  auto expr2 = A.lazyAssign(A + C);
  AssignEvaluate(expr1, expr2);
}

L
liaogang 已提交
98
template <typename Tensor>
H
hedaoyuan 已提交
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
void testSgdUpdate(int height, int width) {
  Tensor A1(height, width);
  Tensor A2(height, width);
  Tensor A3(height, width);
  A1.randomizeUniform();
  A2.copyFrom(A1);
  A3.copyFrom(A1);

  Tensor B(height, width);
  B.randomizeUniform();

  Tensor C1(height, width);
  Tensor C2(height, width);
  Tensor C3(height, width);
  C1.randomizeUniform();
  C2.copyFrom(C1);
  C3.copyFrom(C1);

  Tensor D(height, width);
  D.randomizeUniform();

  real p1 = 0.2;
  real p2 = 0.3;
  real p3 = 0.5;

  /**
   * c = p2 * c - p1 * (b + p3 * a);
   * a = a + c;
   */
  // BaseMatrix API
L
liaogang 已提交
129
  EXPRESSION_PERFORMANCE(A1.sgdUpdate(B, C1, D, p1, p2, p3););
H
hedaoyuan 已提交
130 131

  // Tensor expression
L
liaogang 已提交
132
  EXPRESSION_PERFORMANCE(sgdUpdateTensor(A2, B, C2, D, p1, p2, p3));
H
hedaoyuan 已提交
133 134

  // lazyAssign
L
liaogang 已提交
135
  EXPRESSION_PERFORMANCE(sgdUpdateLazyAssign(A3, B, C3, D, p1, p2, p3));
H
hedaoyuan 已提交
136 137 138 139 140 141 142

  TensorCheckErr(A1, A2);
  TensorCheckErr(A1, A3);
  TensorCheckErr(C1, C2);
  TensorCheckErr(C1, C3);
}

L
liaogang 已提交
143
TEST(sgdUpdate, CPU) { testMatrixCase(testSgdUpdate<CpuMatrix>); }
H
hedaoyuan 已提交
144 145

#ifndef PADDLE_ONLY_CPU
L
liaogang 已提交
146
TEST(sgdUpdate, GPU) { testMatrixCase(testSgdUpdate<GpuMatrix>); }
H
hedaoyuan 已提交
147
#endif