test_MKLDNN.cpp 14.7 KB
Newer Older
1
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved.
T
tensor-tang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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>
X
Xin Pan 已提交
16
#include <paddle/legacy/utils/PythonUtil.h>
T
tensor-tang 已提交
17 18
#include <string>
#include <vector>
19
#include "MKLDNNTester.h"
T
tensor-tang 已提交
20
#include "ModelConfig.pb.h"
X
Xin Pan 已提交
21
#include "paddle/legacy/gserver/activations/MKLDNNActivation.h"
X
Xin Pan 已提交
22
#include "paddle/legacy/math/MathUtils.h"
T
tensor-tang 已提交
23 24 25 26 27 28 29

using namespace paddle;  // NOLINT

DECLARE_bool(thread_local_rand_use_global_seed);
DECLARE_bool(use_gpu);
DECLARE_bool(use_mkldnn);

30 31 32 33 34 35 36 37 38 39 40 41
#define RUN_MKLDNN_TEST(DNN_CONFIG, REF_CONFIG, DESC)         \
  MKLDNNTester tester;                                        \
  for (auto bs : {DESC.bs, 1}) {                              \
    tester.run(DNN_CONFIG, REF_CONFIG, bs, DESC.ih, DESC.iw); \
  }

#define RUN_MKLDNN_TEST_LAYER(DNN_CONFIG, REF_TYPE, DESC) \
  TestConfig ref = DNN_CONFIG;                            \
  ref.layerConfig.set_type(REF_TYPE);                     \
  RUN_MKLDNN_TEST(DNN_CONFIG, ref, DESC)

struct testFcDesc {
T
tensor-tang 已提交
42 43 44
  int bs;
  int ic;
  int ih, iw;  // oh == ow == 1
45
  int oc;
T
tensor-tang 已提交
46 47
};

48 49
static void getMKLDNNFcConfig(TestConfig& cfg, const testFcDesc& pm) {
  cfg.layerConfig.set_type("mkldnn_fc");
50
  cfg.layerConfig.set_active_type("relu");
T
tensor-tang 已提交
51 52 53 54 55 56 57
  cfg.layerConfig.set_size(pm.oc);
  cfg.inputDefs.push_back(
      {INPUT_DATA,
       "layer_0",
       /* size of input layer= */ size_t(pm.ic * pm.ih * pm.iw),
       /* size of weight= */ size_t(pm.oc * pm.ic * pm.ih * pm.iw)});
  cfg.layerConfig.add_inputs();
58
}
T
tensor-tang 已提交
59

60 61 62
void testFcLayer(const testFcDesc& pm) {
  TestConfig dnnConfig;
  getMKLDNNFcConfig(dnnConfig, pm);
T
tensor-tang 已提交
63
  for (auto biasSize : {pm.oc, 0}) {
64 65
    dnnConfig.biasSize = biasSize;
    RUN_MKLDNN_TEST_LAYER(dnnConfig, "fc", pm)
T
tensor-tang 已提交
66 67 68
  }
}

69
TEST(MKLDNNLayer, FcLayer) {
70 71 72 73 74 75 76
  /* bs, ic, ih, iw, oc */
  testFcLayer({2, 2, 1, 1, 3});
  testFcLayer({3, 7, 1, 1, 19});
  testFcLayer({8, 16, 13, 13, 32});
  testFcLayer({4, 12, 13, 13, 18});
  testFcLayer({2, 64, 16, 16, 32});
  testFcLayer({15, 3, 16, 16, 6});
T
tensor-tang 已提交
77 78
}

T
tensor-tang 已提交
79 80 81 82 83 84 85 86 87 88
struct testConvDesc {
  int bs, gp;
  int ic, ih, iw;
  int oc, oh, ow;
  int fh, fw;
  int ph, pw;
  int sh, sw;
  int dh, dw;
};

89 90
static void getMKLDNNConvConfig(TestConfig& cfg, const testConvDesc& pm) {
  cfg.layerConfig.set_type("mkldnn_conv");
91
  cfg.layerConfig.set_active_type("relu");
T
tensor-tang 已提交
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 118 119 120 121 122 123 124 125 126
  cfg.layerConfig.set_num_filters(pm.oc);
  cfg.layerConfig.set_size(pm.oc * pm.oh * pm.ow);
  cfg.layerConfig.set_shared_biases(true);
  cfg.inputDefs.push_back(
      {INPUT_DATA,
       "layer_0",
       /* size of input layer= */ size_t(pm.ic * pm.ih * pm.iw),
       /* size of weight= */ size_t(pm.oc * pm.ic * pm.fh * pm.fw / pm.gp)});
  LayerInputConfig* input = cfg.layerConfig.add_inputs();
  ConvConfig* conv = input->mutable_conv_conf();
  conv->set_groups(pm.gp);
  conv->set_img_size(pm.iw);
  conv->set_img_size_y(pm.ih);
  conv->set_output_x(pm.ow);
  conv->set_output_y(pm.oh);
  conv->set_filter_size(pm.fw);
  conv->set_filter_size_y(pm.fh);
  conv->set_channels(pm.ic);
  conv->set_padding(pm.pw);
  conv->set_padding_y(pm.ph);
  conv->set_stride(pm.sw);
  conv->set_stride_y(pm.sh);
  conv->set_dilation(pm.dw);
  conv->set_dilation_y(pm.dh);
  conv->set_caffe_mode(true);
  conv->set_filter_channels(conv->channels() / conv->groups());
  CHECK_EQ(conv->filter_channels() * pm.gp, conv->channels())
      << "it is indivisible";

  int fh = (pm.fh - 1) * pm.dh + 1;
  int fw = (pm.fw - 1) * pm.dw + 1;
  int ow = outputSize(pm.iw, fw, pm.pw, pm.sw, true);
  int oh = outputSize(pm.ih, fh, pm.ph, pm.sh, true);
  CHECK_EQ(ow, pm.ow) << "output size check failed";
  CHECK_EQ(oh, pm.oh) << "output size check failed";
127
}
T
tensor-tang 已提交
128

129 130 131
void testConvLayer(const testConvDesc& pm) {
  TestConfig dnnConfig;
  getMKLDNNConvConfig(dnnConfig, pm);
T
tensor-tang 已提交
132
  for (auto biasSize : {pm.oc, 0}) {
133 134
    dnnConfig.biasSize = biasSize;
    RUN_MKLDNN_TEST_LAYER(dnnConfig, "exconv", pm)
T
tensor-tang 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  }
}

TEST(MKLDNNLayer, ConvLayer) {
  /* bs, gp, ic, ih, iw, oc, oh, ow, fh, fw, ph, pw, sh, sw, dh, dw */
  testConvLayer({2, 1, 3, 32, 32, 16, 32, 32, 3, 3, 1, 1, 1, 1, 1, 1});
  testConvLayer({2, 1, 8, 16, 16, 8, 16, 16, 3, 3, 1, 1, 1, 1, 1, 1});
  testConvLayer({3, 1, 16, 32, 32, 3, 32, 32, 3, 3, 1, 1, 1, 1, 1, 1});
  testConvLayer({8, 1, 16, 18, 18, 32, 18, 18, 3, 3, 1, 1, 1, 1, 1, 1});
  testConvLayer({16, 1, 1, 42, 31, 32, 23, 11, 4, 5, 3, 2, 2, 3, 1, 1});
  testConvLayer({2, 1, 8, 16, 16, 8, 8, 8, 3, 3, 1, 1, 2, 2, 1, 1});
  testConvLayer({3, 1, 8, 13, 13, 8, 7, 7, 3, 3, 1, 1, 2, 2, 1, 1});
  // with groups
  testConvLayer({2, 2, 4, 5, 5, 8, 5, 5, 3, 3, 1, 1, 1, 1, 1, 1});
  testConvLayer({2, 3, 3, 5, 5, 3, 5, 5, 3, 3, 1, 1, 1, 1, 1, 1});
  testConvLayer({4, 4, 16, 3, 3, 16, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1});
}

153
struct testPoolDesc {
154
  int bs, ic;  // input channel and output channel are the same
155 156 157 158 159 160 161
  int ih, iw;
  int oh, ow;
  int fh, fw;
  int ph, pw;
  int sh, sw;
};

162 163
static void getMKLDNNPoolConfig(TestConfig& cfg, const testPoolDesc& pm) {
  cfg.layerConfig.set_type("mkldnn_pool");
164
  cfg.layerConfig.set_active_type("relu");
165
  cfg.layerConfig.set_size(pm.ic * pm.oh * pm.ow);
166 167 168
  cfg.inputDefs.push_back(
      {INPUT_DATA,
       "layer_0",
169
       /* size of input layer= */ size_t(pm.ic * pm.ih * pm.iw),
170 171 172
       0});
  LayerInputConfig* input = cfg.layerConfig.add_inputs();
  PoolConfig* pool = input->mutable_pool_conf();
173 174
  pool->set_pool_type("avg-projection");
  pool->set_channels(pm.ic);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  pool->set_img_size(pm.iw);
  pool->set_img_size_y(pm.ih);
  pool->set_output_x(pm.ow);
  pool->set_output_y(pm.oh);
  pool->set_size_x(pm.fw);
  pool->set_size_y(pm.fh);
  pool->set_padding(pm.pw);
  pool->set_padding_y(pm.ph);
  pool->set_stride(pm.sw);
  pool->set_stride_y(pm.sh);

  int oh = outputSize(pm.ih, pm.fh, pm.ph, pm.sh, false);
  int ow = outputSize(pm.iw, pm.fw, pm.pw, pm.sw, false);
  CHECK_EQ(ow, pm.ow) << "output size check failed";
  CHECK_EQ(oh, pm.oh) << "output size check failed";
190
}
191

192 193 194 195 196
void testPoolLayer(const testPoolDesc& pm) {
  TestConfig dnnConfig;
  getMKLDNNPoolConfig(dnnConfig, pm);
  LayerInputConfig* input = dnnConfig.layerConfig.mutable_inputs(0);
  PoolConfig* pool = input->mutable_pool_conf();
197 198
  for (auto type : {"max-projection", "avg-projection"}) {
    pool->set_pool_type(type);
199
    RUN_MKLDNN_TEST_LAYER(dnnConfig, "pool", pm)
200 201 202
  }
}

203
TEST(MKLDNNLayer, PoolLayer) {
204
  /* bs, ch, ih, iw, oh, ow, fh, fw, ph, pw, sh, sw */
205 206 207 208 209 210 211
  testPoolLayer({2, 1, 4, 4, 2, 2, 3, 3, 0, 0, 2, 2});
  testPoolLayer({10, 8, 16, 16, 8, 8, 2, 2, 0, 0, 2, 2});
  testPoolLayer({4, 2, 5, 5, 3, 3, 3, 3, 1, 1, 2, 2});
  testPoolLayer({8, 16, 56, 56, 28, 28, 3, 3, 0, 0, 2, 2});
  testPoolLayer({8, 16, 14, 14, 7, 7, 3, 3, 0, 0, 2, 2});
  testPoolLayer({4, 16, 7, 7, 1, 1, 7, 7, 0, 0, 1, 1});
  testPoolLayer({4, 2, 5, 5, 3, 3, 5, 5, 1, 1, 1, 1});
212
  testPoolLayer({2, 8, 56, 56, 29, 29, 3, 3, 1, 1, 2, 2});
213 214
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
struct testBatchNormDesc {
  int bs;
  int ic;
  int ih, iw;
};

static void getMKLDNNBatchNormConfig(TestConfig& cfg,
                                     const testBatchNormDesc& pm) {
  cfg.layerConfig.set_size(pm.ic * pm.ih * pm.iw);
  cfg.layerConfig.set_type("mkldnn_batch_norm");
  cfg.biasSize = pm.ic;
  cfg.inputDefs.push_back(
      {INPUT_DATA,
       "layer_0",
       /* size of input layer= */ size_t(pm.ic * pm.ih * pm.iw),
       /* size of weight= */ size_t(pm.ic)});
  cfg.inputDefs.push_back(
      {INPUT_DATA, "layer_1_moving_mean", 1, size_t(pm.ic)});
  cfg.inputDefs.back().isStatic = true;
  cfg.inputDefs.push_back({INPUT_DATA, "layer_2_moving_var", 1, size_t(pm.ic)});
  cfg.inputDefs.back().isStatic = true;
  LayerInputConfig* input = cfg.layerConfig.add_inputs();
237
  cfg.layerConfig.set_active_type("relu");
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
  cfg.layerConfig.add_inputs();
  cfg.layerConfig.add_inputs();
  ImageConfig* img_conf = input->mutable_image_conf();
  img_conf->set_channels(pm.ic);
  img_conf->set_img_size_y(pm.ih);
  img_conf->set_img_size(pm.iw);
}

void testBatchNormLayer(const testBatchNormDesc& pm) {
  TestConfig dnnConfig;
  getMKLDNNBatchNormConfig(dnnConfig, pm);
  TestConfig refConfig = dnnConfig;
  refConfig.layerConfig.set_type("batch_norm");
  // for PASS_TRAIN, use_global_stats always should be false, and batchsize != 1
  VLOG(MKLDNN_TESTS) << "check train phase";
  dnnConfig.layerConfig.set_use_global_stats(false);
  refConfig.layerConfig.set_use_global_stats(false);
  MKLDNNTester tester;
  tester.run(dnnConfig, refConfig, pm.bs, pm.ih, pm.iw, PASS_TRAIN);
  // for PASS_TEST, check use_global_stats true and false, and batchsize 1
  VLOG(MKLDNN_TESTS) << "check test phase";
  for (auto useGS : {false, true}) {
    dnnConfig.layerConfig.set_use_global_stats(useGS);
    refConfig.layerConfig.set_use_global_stats(useGS);
    MKLDNNTester tester;
    for (auto bs : {pm.bs, 1}) {
      tester.run(dnnConfig, refConfig, bs, pm.ih, pm.iw, PASS_TEST);
    }
  }
}

TEST(MKLDNNLayer, BatchNormLayer) {
  testBatchNormLayer({4, 10, 6, 6});
  testBatchNormLayer({16, 32, 16, 16});
T
tensor-tang 已提交
272
  testBatchNormLayer({4, 16, 8, 10});
273 274
}

T
tensor-tang 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
struct testLRNDesc {
  int bs, ic, ih, iw;
  float scale, pow;
  int localSize;
};

void getMKLDNNLRNConfig(TestConfig& cfg, const testLRNDesc& pm) {
  cfg.layerConfig.set_type("mkldnn_lrn");
  cfg.layerConfig.set_active_type("relu");
  size_t layerSize = pm.ic * pm.ih * pm.iw;
  cfg.inputDefs.push_back({INPUT_DATA, "layer_0", layerSize, 0});
  LayerInputConfig* input = cfg.layerConfig.add_inputs();
  NormConfig* norm = input->mutable_norm_conf();
  norm->set_channels(pm.ic);
  norm->set_size(pm.localSize);
  norm->set_scale(pm.scale);
  norm->set_pow(pm.pow);
  norm->set_blocked(0);
  norm->set_img_size(pm.iw);
  norm->set_img_size_y(pm.ih);
  norm->set_output_x(norm->img_size());
  norm->set_output_y(norm->img_size_y());
  cfg.layerConfig.set_size(layerSize);
  cfg.biasSize = 0;
}

void testLRNLayer(const testLRNDesc& pm) {
  TestConfig dnnConfig;
  getMKLDNNLRNConfig(dnnConfig, pm);
  // mkldnn_lrn <==> norm with cmrnorm-projection type
  TestConfig refConfig = dnnConfig;
  refConfig.layerConfig.set_type("norm");
  LayerInputConfig* input = refConfig.layerConfig.mutable_inputs(0);
  NormConfig* norm = input->mutable_norm_conf();
  norm->set_norm_type("cmrnorm-projection");
  norm->set_scale(norm->scale() / norm->size());
  RUN_MKLDNN_TEST(dnnConfig, refConfig, pm)
}

TEST(MKLDNNLayer, LRNLayer) {
  testLRNLayer({4, 10, 12, 12, 0.001f, 0.75f, 5});
  testLRNLayer({2, 32, 6, 6, 0.001f, 0.75f, 5});
  testLRNLayer({4, 16, 8, 10, 0.01f, 0.5f, 5});
}

320
struct testImageDesc {
321
  int bs, ic, ih, iw;
322 323
};

324 325 326
static void getAddtoConfig(TestConfig& cfg,
                           const testImageDesc& pm,
                           const size_t nInputs = 1) {
327 328
  cfg.biasSize = 0;
  cfg.layerConfig.set_type("addto");
T
tensor-tang 已提交
329
  size_t layerSize = pm.ic * pm.ih * pm.iw;
330
  cfg.layerConfig.set_size(layerSize);
331 332 333 334 335 336 337 338 339 340 341 342 343 344
  cfg.layerConfig.set_active_type("relu");
  for (size_t i = 0; i < nInputs; ++i) {
    std::stringstream ss;
    ss << "layer_" << i;
    cfg.inputDefs.push_back({INPUT_DATA, ss.str(), layerSize, 0});
    LayerInputConfig* input = cfg.layerConfig.add_inputs();
    ImageConfig* img_conf = input->mutable_image_conf();
    img_conf->set_channels(pm.ic);
    img_conf->set_img_size_y(pm.ih);
    img_conf->set_img_size(pm.iw);
  }
}

void testAddtoLayer(const testImageDesc& pm, const size_t nInputs) {
345
  CHECK_GE(nInputs, 1UL);
346 347 348
  TestConfig dnnConfig;
  getAddtoConfig(dnnConfig, pm, nInputs);
  dnnConfig.layerConfig.set_type("mkldnn_addto");
T
tensor-tang 已提交
349 350
  for (auto withBias : {false, true}) {
    dnnConfig.biasSize = withBias ? pm.ic * pm.ih * pm.iw : 0;
351 352 353 354 355 356 357 358
    RUN_MKLDNN_TEST_LAYER(dnnConfig, "addto", pm)
  }
}

TEST(MKLDNNLayer, AddtoLayer) {
  testAddtoLayer({16, 5, 14, 14}, 1);
  testAddtoLayer({8, 10, 8, 8}, 2);
  testAddtoLayer({4, 12, 1, 1}, 3);
359 360
}

T
tensor-tang 已提交
361 362
static void getMKLDNNConcatConfig(TestConfig& cfg,
                                  const std::vector<testImageDesc>& inputs) {
363
  CHECK_GE(inputs.size(), 2UL) << "at least two inputs";
T
tensor-tang 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
  int oc = inputs[0].ic;
  for (size_t i = 1; i < inputs.size(); ++i) {
    CHECK_EQ(inputs[i].bs, inputs[0].bs);
    CHECK_EQ(inputs[i].ih, inputs[0].ih);
    CHECK_EQ(inputs[i].iw, inputs[0].iw);
    oc += inputs[i].ic;
  }
  cfg.biasSize = 0;
  cfg.layerConfig.set_type("mkldnn_concat");
  cfg.layerConfig.set_size(oc * inputs[0].ih * inputs[0].iw);
  cfg.layerConfig.set_active_type("relu");
  for (size_t i = 0; i < inputs.size(); ++i) {
    std::stringstream ss;
    ss << "layer_" << i;
    cfg.inputDefs.push_back(
        {INPUT_DATA,
         ss.str(),
         (size_t)(inputs[i].ic) * inputs[i].ih * inputs[i].iw,
         0});
    LayerInputConfig* input = cfg.layerConfig.add_inputs();
    ImageConfig* img_conf = input->mutable_image_conf();
    img_conf->set_channels(inputs[i].ic);
    img_conf->set_img_size_y(inputs[i].ih);
    img_conf->set_img_size(inputs[i].iw);
  }
}

void testConcatLayer(const std::vector<testImageDesc>& inputs) {
  TestConfig dnnConfig;
  getMKLDNNConcatConfig(dnnConfig, inputs);
  RUN_MKLDNN_TEST_LAYER(dnnConfig, "concat", inputs[0])
}

TEST(MKLDNNLayer, ConcatLayer) {
  testConcatLayer({{64, 128, 1, 1}, {64, 32, 1, 1}, {64, 64, 1, 1}});
  testConcatLayer({{32, 100, 8, 8}, {32, 10, 8, 8}});
}

402
void testActivation(std::string actType, const testImageDesc& pm) {
T
tensor-tang 已提交
403 404
  // TODO(TJ): remove me when paddle support elu activation
  if (actType == "mkldnn_elu") {
405 406 407
    return;
  }
  const std::string compareTypes[] = {actType, actType.erase(0, 7)};
408 409 410 411 412
  TestConfig cfg;
  getAddtoConfig(cfg, pm);
  TestConfig ref = cfg;
  cfg.layerConfig.set_active_type(compareTypes[0]);
  ref.layerConfig.set_active_type(compareTypes[1]);
413
  RUN_MKLDNN_TEST(cfg, ref, pm)
414 415 416 417 418
}

TEST(MKLDNNActivation, Activations) {
  auto types = MKLDNNActivation::getAllRegisteredTypes();
  for (auto type : types) {
419
    /* bs, c, h, w*/
420
    testActivation(type, {16, 64, 32, 32});
T
tensor-tang 已提交
421
    testActivation(type, {2, 8, 1, 1});
422 423 424
  }
}

425
DECLARE_string(config_args);
426 427
TEST(MKLDNNNet, net) {
  std::vector<std::string> cases = {"simple", "branch"};
428
  for (auto name : cases) {
X
Xin Pan 已提交
429
    std::string config = "./legacy/gserver/tests/mkldnn_" + name + "_net.conf";
430 431 432 433
    for (auto channels : {2, 32}) {
      std::ostringstream oss;
      oss << "channels=" << channels;
      FLAGS_config_args = oss.str();
434
      MKLDNNTester::runNetTest(config);
435
    }
436 437
  }
}
T
tensor-tang 已提交
438 439 440 441 442 443

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  FLAGS_use_gpu = false;
  FLAGS_use_mkldnn = true;
  initMain(argc, argv);
444
  initPython(argc, argv);
T
tensor-tang 已提交
445 446 447 448
  FLAGS_thread_local_rand_use_global_seed = true;
  srand(1);
  return RUN_ALL_TESTS();
}