test_GpuProfiler.cpp 5.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
L
liaogang 已提交
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. */

15
#ifdef PADDLE_WITH_CUDA
L
liaogang 已提交
16 17

#include <gtest/gtest.h>
Y
Yu Yang 已提交
18 19
#include "paddle/math/Matrix.h"
#include "paddle/math/SparseMatrix.h"
20
#include "paddle/testing/TestUtil.h"
L
liaogang 已提交
21
#include "paddle/utils/Stat.h"
Y
Yu Yang 已提交
22
#include "paddle/utils/Util.h"
L
liaogang 已提交
23 24 25 26 27 28 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

using namespace paddle;  // NOLINT
using namespace std;     // NOLINT

void MatrixCheckErr(const Matrix& matrix1, const Matrix& matrix2) {
  CHECK(matrix1.getHeight() == matrix2.getHeight());
  CHECK(matrix1.getWidth() == matrix2.getWidth());
#ifndef PADDLE_TYPE_DOUBLE
  real err = 1e-3;
#else
  real err = 1e-10;
#endif

  int height = matrix1.getHeight();
  int width = matrix1.getWidth();
  const real* data1 = matrix1.getData();
  const real* data2 = matrix2.getData();
  int count = 0;
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      real a = data1[i * width + j];
      real b = data2[i * width + j];
      if (fabs(a - b) > err) {
        if ((fabsf(a - b) / fabsf(a)) > (err / 10.0f)) {
          count++;
        }
      }
    }
  }
  EXPECT_EQ(count, 0) << "There are " << count << " different element.";
}

Y
Yu Yang 已提交
55 56 57
void testBilinearFwdBwd(int numSamples,
                        int imgSizeH,
                        int imgSizeW,
L
liaogang 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
                        int channels) {
  int inWidth = imgSizeH * imgSizeW * channels;
  int outWidth = 2 * imgSizeH * 2 * imgSizeW * channels;
  real ratioH = 0.5;
  real ratioW = 0.5;

  // forward
  MatrixPtr input = CpuMatrix::create(numSamples, inWidth, false, false);
  MatrixPtr inputGpu = GpuMatrix::create(numSamples, inWidth, false, true);

  MatrixPtr target = CpuMatrix::create(numSamples, outWidth, false, false);
  MatrixPtr targetGpu = GpuMatrix::create(numSamples, outWidth, false, true);
  MatrixPtr targetCheck = CpuMatrix::create(numSamples, outWidth, false, false);

  input->randomizeUniform();
  inputGpu->copyFrom(*input);

75 76 77
  {
    // nvprof: GPU Proflier
    REGISTER_GPU_PROFILER("testBilinearFwdBwd");
Y
Yu Yang 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    target->bilinearForward(*input,
                            imgSizeH,
                            imgSizeW,
                            2 * imgSizeH,
                            2 * imgSizeW,
                            channels,
                            ratioH,
                            ratioW);
    targetGpu->bilinearForward(*inputGpu,
                               imgSizeH,
                               imgSizeW,
                               2 * imgSizeH,
                               2 * imgSizeW,
                               channels,
                               ratioH,
                               ratioW);
94
  }
L
liaogang 已提交
95 96 97 98 99 100 101 102 103 104

  // check
  targetCheck->copyFrom(*targetGpu);
  MatrixCheckErr(*target, *targetCheck);

  // backward
  MatrixPtr inputGrad = CpuMatrix::create(numSamples, inWidth, false, false);
  MatrixPtr inputGpuGrad = GpuMatrix::create(numSamples, inWidth, false, true);

  MatrixPtr targetGrad = CpuMatrix::create(numSamples, outWidth, false, false);
Y
Yu Yang 已提交
105 106
  MatrixPtr targetGpuGrad =
      GpuMatrix::create(numSamples, outWidth, false, true);
L
liaogang 已提交
107 108 109 110 111 112 113 114
  MatrixPtr targetCheckGrad =
      CpuMatrix::create(numSamples, inWidth, false, false);

  inputGrad->randomizeUniform();
  targetGrad->randomizeUniform();
  inputGpuGrad->copyFrom(*inputGrad);
  targetGpuGrad->copyFrom(*targetGrad);

Y
Yu Yang 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  inputGrad->bilinearBackward(*targetGrad,
                              2 * imgSizeH,
                              2 * imgSizeW,
                              imgSizeH,
                              imgSizeW,
                              channels,
                              ratioH,
                              ratioW);
  inputGpuGrad->bilinearBackward(*targetGpuGrad,
                                 2 * imgSizeH,
                                 2 * imgSizeW,
                                 imgSizeH,
                                 imgSizeW,
                                 channels,
                                 ratioH,
                                 ratioW);
L
liaogang 已提交
131 132 133 134 135 136

  // check
  targetCheckGrad->copyFrom(*inputGpuGrad);
  MatrixCheckErr(*inputGrad, *targetCheckGrad);
}

137
TEST(Profiler, testBilinearFwdBwd) {
L
liaogang 已提交
138 139 140
  auto numSamples = 10;
  auto channels = 16;
  auto imgSize = 64;
L
liaogang 已提交
141 142
  {
    // nvprof: GPU Proflier
143
    REGISTER_GPU_PROFILER("testBilinearFwdBwd");
L
liaogang 已提交
144
    // Paddle built-in timer
Y
Yu Yang 已提交
145 146 147
    REGISTER_TIMER_INFO(
        "testBilinearFwdBwd",
        "numSamples = 10, channels = 16, imgSizeX = 64, imgSizeY = 64");
L
liaogang 已提交
148 149
    testBilinearFwdBwd(numSamples, imgSize, imgSize, channels);
  }
150
  globalStat.printAllStatus();
L
liaogang 已提交
151 152 153 154 155
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  initMain(argc, argv);
156 157

  // nvprof: GPU Proflier
Y
Yu Yang 已提交
158 159 160
  REGISTER_GPU_PROFILER(
      "RecursiveProfilingTest",
      "numSamples = 10, channels = 16, imgSizeX = 64, imgSizeY = 64");
161

L
liaogang 已提交
162 163 164
  return RUN_ALL_TESTS();
}

L
Luo Tao 已提交
165
#endif