test_BatchNorm.cpp 6.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

#include <gtest/gtest.h>
#include <string>
Y
Yu Yang 已提交
17
#include <vector>
18
#include "ModelConfig.pb.h"
Y
Yu Yang 已提交
19
#include "paddle/gserver/layers/DataLayer.h"
20 21 22 23
#include "paddle/trainer/Trainer.h"
#include "paddle/utils/GlobalConstants.h"

#include "LayerGradUtil.h"
24 25
#include "paddle/cuda/include/hl_batch_norm.h"
#include "paddle/math/tests/TensorCheck.h"
26
#include "paddle/testing/TestUtil.h"
27 28 29 30

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

31 32 33 34 35
DECLARE_bool(use_gpu);
DECLARE_int32(gpu_id);
DECLARE_double(checkgrad_eps);
DECLARE_bool(thread_local_rand_use_global_seed);
DECLARE_bool(prev_batch_state);
36

37
// Test that the batchNormLayer can be followed by a ConvLayer
38
TEST(Layer, batchNorm) {
Y
Yu Yang 已提交
39 40 41 42 43 44 45 46 47 48 49
  FLAGS_use_gpu = false;
  TestConfig configBN;
  const int CHANNELS = 6272;
  const int IMG_SIZE = 1;
  configBN.layerConfig.set_type("batch_norm");
  configBN.layerConfig.set_name("bn");
  configBN.layerConfig.set_size(CHANNELS * IMG_SIZE * IMG_SIZE);
  configBN.layerConfig.set_active_type("relu");
  configBN.biasSize = CHANNELS;
  configBN.inputDefs.push_back({INPUT_DATA,
                                "layer_0",
50 51 52
                                /* dim= */ IMG_SIZE * IMG_SIZE * CHANNELS,
                                /* paraSize= */ CHANNELS});

Y
Yu Yang 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  configBN.inputDefs.push_back(
      {INPUT_DATA, "layer_1_running_mean", 1, CHANNELS});
  configBN.inputDefs.back().isStatic = true;
  configBN.inputDefs.push_back(
      {INPUT_DATA, "layer_2_running_var", 1, CHANNELS});
  configBN.inputDefs.back().isStatic = true;

  LayerInputConfig* input = configBN.layerConfig.add_inputs();
  configBN.layerConfig.add_inputs();
  configBN.layerConfig.add_inputs();

  ImageConfig* img_conf = input->mutable_image_conf();
  img_conf->set_channels(CHANNELS);
  img_conf->set_img_size(IMG_SIZE);

  // Setting up conv-layer config
  TestConfig config;
  config.biasSize = 64;
  config.layerConfig.set_type("exconv");
  config.layerConfig.set_num_filters(64);
  config.layerConfig.set_partial_sum(1);
  config.layerConfig.set_shared_biases(true);

  config.inputDefs.push_back({INPUT_DATA, "bn", 6272, 204800});
  input = config.layerConfig.add_inputs();
  ConvConfig* conv = input->mutable_conv_conf();
  conv->set_filter_size(5);
  conv->set_filter_size_y(5);
  conv->set_channels(128);
  conv->set_padding(1);
  conv->set_padding_y(1);
  conv->set_stride(2);
  conv->set_stride_y(2);
  conv->set_groups(1);
  conv->set_filter_channels(conv->channels() / conv->groups());
  conv->set_img_size(7);
  conv->set_output_x(3);
  config.layerConfig.set_size(conv->output_x() * conv->output_x() *
                              config.layerConfig.num_filters());
  config.layerConfig.set_name("conv");

  // data layer initialize
  std::vector<DataLayerPtr> dataLayers;
  LayerMap layerMap;
  vector<Argument> datas;
  initDataLayer(configBN,
                &dataLayers,
                &datas,
                &layerMap,
                "batch_norm",
                100,
                false,
                false);
  // test layer initialize
  std::vector<ParameterPtr> parameters;
  LayerPtr bnLayer;
  initTestLayer(configBN, &layerMap, &parameters, &bnLayer);

  std::vector<ParameterPtr> parameters2;
  LayerPtr convLayer;
  initTestLayer(config, &layerMap, &parameters2, &convLayer);

  bnLayer->forward(PASS_GC);
  convLayer->forward(PASS_GC);

L
liaogang 已提交
118 119
  CHECK_EQ(static_cast<int>(convLayer->getOutputValue()->getHeight()), 100);
  CHECK_EQ(static_cast<int>(convLayer->getOutputValue()->getWidth()), 576);
120 121
}

122
#ifdef PADDLE_WITH_CUDA
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
void batchNormInference(int n, int c, int h, int w) {
  MatrixPtr input = std::make_shared<GpuMatrix>(n, c * h * w);
  MatrixPtr cudnnOut = std::make_shared<GpuMatrix>(n, c * h * w);
  MatrixPtr cudaOut = std::make_shared<GpuMatrix>(n, c * h * w);
  MatrixPtr cudnnCheck = std::make_shared<CpuMatrix>(n, c * h * w);
  MatrixPtr cudaCheck = std::make_shared<CpuMatrix>(n, c * h * w);
  input->randomizeUniform();
  cudnnOut->zeroMem();
  cudaOut->zeroMem();

  MatrixPtr scale = std::make_shared<GpuMatrix>(1, c);
  scale->randomizeUniform();
  MatrixPtr bias = std::make_shared<GpuMatrix>(1, c);
  bias->randomizeUniform();

  MatrixPtr movingMean = std::make_shared<GpuMatrix>(1, c);
  movingMean->randomizeUniform();

  MatrixPtr movingVar = std::make_shared<GpuMatrix>(1, c);
  movingVar->randomizeUniform();
  movingVar->clip(0.01, 50);

  hl_tensor_descriptor ioDesc;
  hl_tensor_descriptor bnDesc;
  hl_create_tensor_descriptor(&ioDesc);
  hl_create_tensor_descriptor(&bnDesc);
  hl_tensor_reshape(ioDesc, n, c, h, w);
  hl_tensor_reshape(bnDesc, 1, c, 1, 1);

  double EPS = 1E-5;
  hl_batch_norm_forward_inference(ioDesc,
                                  input->getData(),
                                  ioDesc,
                                  cudnnOut->getData(),
                                  bnDesc,
                                  scale->getData(),
                                  bias->getData(),
                                  movingMean->getData(),
                                  movingVar->getData(),
                                  EPS);

  hl_batch_norm_cuda_inference(input->getData(),
                               cudaOut->getData(),
                               scale->getData(),
                               bias->getData(),
                               movingMean->getData(),
                               movingVar->getData(),
                               EPS,
                               n,
                               c,
                               h,
                               w);

  cudnnCheck->copyFrom(*cudnnOut);
  cudaCheck->copyFrom(*cudaOut);
  autotest::TensorCheckErr(*cudnnCheck, *cudaCheck);

  hl_destroy_tensor_descriptor(ioDesc);
  hl_destroy_tensor_descriptor(bnDesc);
}

TEST(BatchNorm, Inference) {
  batchNormInference(33, 267, 1, 1);
  batchNormInference(19, 105, 4, 4);
}
#endif

190 191 192 193 194 195 196
int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  initMain(argc, argv);
  FLAGS_thread_local_rand_use_global_seed = true;
  srand(1);
  return RUN_ALL_TESTS();
}