test_ConvUnify.cpp 11.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
W
wangyang59 已提交
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>
W
wangyang59 已提交
18
#include "ModelConfig.pb.h"
Y
Yu Yang 已提交
19
#include "paddle/gserver/layers/DataLayer.h"
W
wangyang59 已提交
20 21
#include "paddle/gserver/layers/ExpandConvTransLayer.h"
#include "paddle/math/MathUtils.h"
Y
Yu Yang 已提交
22 23
#include "paddle/trainer/Trainer.h"
#include "paddle/utils/GlobalConstants.h"
W
wangyang59 已提交
24 25

#include "LayerGradUtil.h"
26
#include "paddle/testing/TestUtil.h"
W
wangyang59 已提交
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);
W
wangyang59 已提交
36

W
wangyang59 已提交
37
// Do one forward pass of ConvLayer using either exconv or cudnn_conv
Y
Yu Yang 已提交
38 39 40 41 42 43 44 45 46 47
MatrixPtr doOneConvTest(size_t imgSize,
                        size_t output_x,
                        size_t stride,
                        size_t padding,
                        size_t filter_size,
                        size_t channel,
                        size_t numfilters,
                        size_t groups,
                        MatrixPtr& inputData,
                        real* param,
48 49
                        bool useGpu,
                        bool isDeconv = false) {
50 51
  TestConfig config;
  config.biasSize = numfilters;
52
  string layerType;
53
  if (useGpu) {
54
    layerType = (isDeconv) ? "cudnn_convt" : "cudnn_conv";
55
  } else {
56
    layerType = (isDeconv) ? "exconvt" : "exconv";
57
  }
58
  config.layerConfig.set_type(layerType);
59 60 61 62
  config.layerConfig.set_num_filters(numfilters);
  config.layerConfig.set_partial_sum(1);
  config.layerConfig.set_shared_biases(true);

Y
Yu Yang 已提交
63 64
  size_t weightSize = channel * filter_size * filter_size *
                      config.layerConfig.num_filters() / groups;
65 66 67 68 69 70 71 72 73 74 75 76
  if (isDeconv) {
    config.inputDefs.push_back(
        {INPUT_DATA, "layer_0", output_x * output_x * channel, weightSize});
    config.layerConfig.set_size(imgSize * imgSize *
                                config.layerConfig.num_filters());
  } else {
    config.inputDefs.push_back(
        {INPUT_DATA, "layer_0", imgSize * imgSize * channel, weightSize});
    config.layerConfig.set_size(output_x * output_x *
                                config.layerConfig.num_filters());
  }

77 78 79 80 81 82 83 84 85
  LayerInputConfig* input = config.layerConfig.add_inputs();
  ConvConfig* conv = input->mutable_conv_conf();
  conv->set_filter_size(filter_size);
  conv->set_filter_size_y(filter_size);
  conv->set_channels(channel);
  conv->set_padding(padding);
  conv->set_padding_y(padding);
  conv->set_stride(stride);
  conv->set_stride_y(stride);
86
  conv->set_groups(groups);
87 88 89
  conv->set_img_size(imgSize);
  conv->set_output_x(output_x);

90 91 92 93 94 95
  if (isDeconv) {
    conv->set_filter_channels(numfilters / groups);
  } else {
    conv->set_filter_channels(channel / groups);
  }

96 97 98 99 100
  config.layerConfig.set_name("conv");

  std::vector<DataLayerPtr> dataLayers;
  LayerMap layerMap;
  vector<Argument> datas;
Y
Yu Yang 已提交
101 102
  initDataLayer(
      config, &dataLayers, &datas, &layerMap, "conv", 1, false, useGpu);
103 104 105 106 107 108 109 110 111
  dataLayers[0]->getOutputValue()->zeroMem();
  dataLayers[0]->getOutputValue()->copyFrom(*inputData);

  // test layer initialize
  std::vector<ParameterPtr> parameters;
  LayerPtr convLayer;
  initTestLayer(config, &layerMap, &parameters, &convLayer);
  convLayer->getBiasParameter()->zeroMem();
  convLayer->getParameters()[0]->zeroMem();
Y
Yu Yang 已提交
112 113 114
  convLayer->getParameters()[0]
      ->getBuf(PARAMETER_VALUE)
      ->copyFrom(param, weightSize);
115 116 117
  convLayer->forward(PASS_GC);

  return convLayer->getOutputValue();
W
wangyang59 已提交
118 119
}

120
TEST(Layer, convParaUnified) {
Y
Yu Yang 已提交
121 122
#ifndef PADDLE_ONLY_CPU
  MatrixPtr input, resultCpu, resultGpu;
123 124

  /// TEST1 for conv ///
Y
Yu Yang 已提交
125
  input = Matrix::create(1, 4 * 4, false, false);
P
Peng Li 已提交
126 127
  real inputData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  real param[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1};
Y
Yu Yang 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140

  input->setData(inputData);

  resultCpu = doOneConvTest(/* imgSize */ 4,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 3,
                            /*channel*/ 1,
                            /*numfilters*/ 2,
                            /*groups*/ 1,
                            input,
                            param,
141
                            /*useGpu*/ false);
Y
Yu Yang 已提交
142 143 144 145 146 147 148 149 150 151 152

  resultGpu = doOneConvTest(/* imgSize */ 4,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 3,
                            /*channel*/ 1,
                            /*numfilters*/ 2,
                            /*groups*/ 1,
                            input,
                            param,
153
                            /*useGpu*/ true);
Y
Yu Yang 已提交
154 155
  checkMatrixEqual(resultCpu, resultGpu);

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
  /// TEST1 for deconv ///
  input = Matrix::create(1, 2 * 2, false, false);
  real inputDataT[] = {1, 2, 3, 4};
  input->setData(inputDataT);

  resultCpu = doOneConvTest(/* imgSize */ 4,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 3,
                            /*channel*/ 1,
                            /*numfilters*/ 2,
                            /*groups*/ 1,
                            input,
                            param,
                            /*useGpu*/ false,
                            /*isDeconv*/ true);

  resultGpu = doOneConvTest(/* imgSize */ 4,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 3,
                            /*channel*/ 1,
                            /*numfilters*/ 2,
                            /*groups*/ 1,
                            input,
                            param,
                            /*useGpu*/ true,
                            /*isDeconv*/ true);
  checkMatrixEqual(resultCpu, resultGpu);

  /// TEST2 for conv ///
Y
Yu Yang 已提交
189
  input = Matrix::create(1, 3 * 3 * 2, false, false);
P
Peng Li 已提交
190 191
  real inputData2[] = {
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
P
Peng Li 已提交
192
  real param2[] = {1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1};
Y
Yu Yang 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205

  input->setData(inputData2);

  resultCpu = doOneConvTest(/* imgSize */ 3,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 2,
                            /*channel*/ 2,
                            /*numfilters*/ 2,
                            /*groups*/ 1,
                            input,
                            param2,
206
                            /*useGpu*/ false);
Y
Yu Yang 已提交
207 208 209 210 211 212 213 214 215 216 217

  resultGpu = doOneConvTest(/* imgSize */ 3,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 2,
                            /*channel*/ 2,
                            /*numfilters*/ 2,
                            /*groups*/ 1,
                            input,
                            param2,
218
                            /*useGpu*/ true);
Y
Yu Yang 已提交
219 220
  checkMatrixEqual(resultCpu, resultGpu);

221
  /// TEST3 for conv ///
P
Peng Li 已提交
222
  real param3[] = {1, 2, 3, 4, 4, 3, 2, 1};
Y
Yu Yang 已提交
223 224 225 226 227 228 229 230 231 232 233

  resultCpu = doOneConvTest(/* imgSize */ 3,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 2,
                            /*channel*/ 2,
                            /*numfilters*/ 2,
                            /*groups*/ 2,
                            input,
                            param3,
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
                            /*useGpu*/ false);

  resultGpu = doOneConvTest(/* imgSize */ 3,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 2,
                            /*channel*/ 2,
                            /*numfilters*/ 2,
                            /*groups*/ 2,
                            input,
                            param3,
                            /*useGpu*/ true);
  checkMatrixEqual(resultCpu, resultGpu);

  /// TEST2 for deconv ///
  input = Matrix::create(1, 2 * 2 * 2, false, false);
  real inputData2T[] = {1, 2, 3, 4, 5, 6, 7, 8};
  input->setData(inputData2T);

  resultCpu = doOneConvTest(/* imgSize */ 3,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 2,
                            /*channel*/ 2,
                            /*numfilters*/ 2,
                            /*groups*/ 1,
                            input,
                            param2,
                            /*useGpu*/ false,
                            /*isDeconv*/ true);

  resultGpu = doOneConvTest(/* imgSize */ 3,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 2,
                            /*channel*/ 2,
                            /*numfilters*/ 2,
                            /*groups*/ 1,
                            input,
                            param2,
                            /*useGpu*/ true,
                            /*isDeconv*/ true);
  checkMatrixEqual(resultCpu, resultGpu);

  /// TEST3 for deconv ///
  resultCpu = doOneConvTest(/* imgSize */ 3,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 2,
                            /*channel*/ 2,
                            /*numfilters*/ 2,
                            /*groups*/ 2,
                            input,
                            param3,
                            /*useGpu*/ false,
                            /*isDeconv*/ true);
Y
Yu Yang 已提交
294 295 296 297 298 299 300 301 302 303 304

  resultGpu = doOneConvTest(/* imgSize */ 3,
                            /* output_x */ 2,
                            /* stride */ 1,
                            /* padding */ 0,
                            /* filter_size */ 2,
                            /*channel*/ 2,
                            /*numfilters*/ 2,
                            /*groups*/ 2,
                            input,
                            param3,
305 306
                            /*useGpu*/ true,
                            /*isDeconv*/ true);
Y
Yu Yang 已提交
307 308
  checkMatrixEqual(resultCpu, resultGpu);
#endif
W
wangyang59 已提交
309 310 311 312 313 314 315 316 317
}

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();
}