test_ConvUnify.cpp 6.7 KB
Newer Older
W
wangyang59 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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 <vector>
#include <string>
#include "paddle/gserver/layers/DataLayer.h"
#include "ModelConfig.pb.h"
#include "paddle/trainer/Trainer.h"
#include "paddle/utils/GlobalConstants.h"
#include "paddle/gserver/layers/ExpandConvTransLayer.h"
#include "paddle/math/MathUtils.h"

#include "TestUtil.h"
#include "LayerGradUtil.h"

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

P_DECLARE_bool(use_gpu);
P_DECLARE_int32(gpu_id);
P_DECLARE_double(checkgrad_eps);
P_DECLARE_bool(thread_local_rand_use_global_seed);
P_DECLARE_bool(prev_batch_state);

// Do one forward pass of convTrans layer and check to see if its output
// matches the given result
MatrixPtr doOneConvTest(size_t imgSize, size_t output_x, size_t stride,
                    size_t padding, size_t filter_size, size_t channel,
41
                    size_t numfilters, size_t groups, MatrixPtr& inputData,
W
wangyang59 已提交
42
                    real* param, bool useGpu) {
43 44 45 46 47 48 49 50 51 52 53
  TestConfig config;
  config.biasSize = numfilters;
  if (useGpu) {
    config.layerConfig.set_type("cudnn_conv");
  } else {
    config.layerConfig.set_type("exconv");
  }
  config.layerConfig.set_num_filters(numfilters);
  config.layerConfig.set_partial_sum(1);
  config.layerConfig.set_shared_biases(true);

54 55
  size_t weightSize = channel* filter_size * filter_size *
      config.layerConfig.num_filters() / groups;
56 57
  config.inputDefs.push_back({INPUT_DATA, "layer_0",
                              imgSize * imgSize * channel,
58
                              weightSize});
59 60 61 62 63 64 65 66 67
  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);
68 69
  conv->set_groups(groups);
  conv->set_filter_channels(channel/groups);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  conv->set_img_size(imgSize);
  conv->set_output_x(output_x);

  config.layerConfig.set_size(conv->output_x() * conv->output_x() *
                              config.layerConfig.num_filters());
  config.layerConfig.set_name("conv");

  std::vector<DataLayerPtr> dataLayers;
  LayerMap layerMap;
  vector<Argument> datas;
  initDataLayer(config, &dataLayers, &datas, &layerMap, "conv",
                1, false, useGpu);
  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();
  convLayer->getParameters()[0]->getBuf(PARAMETER_VALUE)->copyFrom(param,
92
      weightSize);
93 94 95
  convLayer->forward(PASS_GC);

  return convLayer->getOutputValue();
W
wangyang59 已提交
96 97
}

98
TEST(Layer, convParaUnified) {
99
  #ifndef PADDLE_ONLY_CPU
W
wangyang59 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    MatrixPtr input, resultCpu, resultGpu;
    input = Matrix::create(1, 4 * 4, false, false);
    float inputData[] = {1, 2, 3, 4,
                         5, 6, 7, 8,
                         9, 10, 11, 12,
                         13, 14, 15, 16};
    float param[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,
                     9, 8, 7, 6, 5, 4, 3, 2, 1};

    input->setData(inputData);

    resultCpu = doOneConvTest(/* imgSize */ 4,
                   /* output_x */ 2,
                   /* stride */ 1,
                   /* padding */ 0,
                   /* filter_size */ 3,
                   /*channel*/ 1,
                   /*numfilters*/ 2,
118
                   /*groups*/ 1,
W
wangyang59 已提交
119 120 121 122 123 124 125 126 127
                   input, param, false);

    resultGpu = doOneConvTest(/* imgSize */ 4,
                       /* output_x */ 2,
                       /* stride */ 1,
                       /* padding */ 0,
                       /* filter_size */ 3,
                       /*channel*/ 1,
                       /*numfilters*/ 2,
128
                       /*groups*/ 1,
W
wangyang59 已提交
129 130
                       input, param, true);
    checkMatrixEqual(resultCpu, resultGpu);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

    input = Matrix::create(1, 3 * 3 * 2, false, false);
    float inputData2[] = {1, 2, 3,
                          4, 5, 6,
                          7, 8, 9,

                          10, 11, 12,
                          13, 14, 15,
                          16, 17, 18};
    float param2[] = {1, 2, 3, 4, 5, 6, 7, 8,
                      8, 7, 6, 5, 4, 3, 2, 1};

    input->setData(inputData2);

    resultCpu = doOneConvTest(/* imgSize */ 3,
                   /* output_x */ 2,
                   /* stride */ 1,
                   /* padding */ 0,
                   /* filter_size */ 2,
                   /*channel*/ 2,
                   /*numfilters*/ 2,
152
                   /*groups*/ 1,
153 154 155 156 157 158 159 160 161
                   input, param2, false);

    resultGpu = doOneConvTest(/* imgSize */ 3,
                       /* output_x */ 2,
                       /* stride */ 1,
                       /* padding */ 0,
                       /* filter_size */ 2,
                       /*channel*/ 2,
                       /*numfilters*/ 2,
162
                       /*groups*/ 1,
163 164
                       input, param2, true);
    checkMatrixEqual(resultCpu, resultGpu);
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


    float param3[] = {1, 2, 3, 4,
                      4, 3, 2, 1};

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

    resultGpu = doOneConvTest(/* imgSize */ 3,
                       /* output_x */ 2,
                       /* stride */ 1,
                       /* padding */ 0,
                       /* filter_size */ 2,
                       /*channel*/ 2,
                       /*numfilters*/ 2,
                       /*groups*/ 2,
                       input, param3, true);
    checkMatrixEqual(resultCpu, resultGpu);
190
  #endif
W
wangyang59 已提交
191 192 193 194 195 196 197 198 199
}

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