conv_int8_compute_test.cc 29.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// 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 <gflags/gflags.h>
#include <gtest/gtest.h>
#include "lite/core/context.h"
18
#include "lite/core/profile/timer.h"
19 20 21 22 23 24 25 26
#include "lite/operators/op_params.h"
#include "lite/tests/utils/naive_math_impl.h"
#include "lite/tests/utils/tensor_utils.h"

#ifdef LITE_WITH_ARM
#include "lite/kernels/arm/conv_compute.h"
#endif  // LITE_WITH_ARM

27 28 29 30 31 32 33
DEFINE_int32(power_mode,
             3,
             "power mode: "
             "0 for POWER_HIGH;"
             "1 for POWER_LOW;"
             "2 for POWER_FULL;"
             "3 for NO_BIND");
34 35 36
DEFINE_int32(threads, 1, "threads num");
DEFINE_int32(warmup, 0, "warmup times");
DEFINE_int32(repeats, 1, "repeats times");
37
DEFINE_bool(basic_test, true, "do all tests");
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
DEFINE_bool(check_result, true, "check the result");

DEFINE_int32(batch, 1, "batch size");
DEFINE_int32(in_channel, 32, "input channel");
DEFINE_int32(in_height, 112, "input height");
DEFINE_int32(in_width, 112, "input width");

DEFINE_int32(out_channel, 32, "output channel");
DEFINE_int32(group, 1, "group");
DEFINE_int32(kernel_h, 3, "kernel height");
DEFINE_int32(kernel_w, 3, "kernel width");
DEFINE_int32(pad_h, 1, "pad height");
DEFINE_int32(pad_w, 1, "pad width");
DEFINE_int32(stride_h, 1, "stride height");
DEFINE_int32(stride_w, 1, "stride width");
DEFINE_int32(dila_h, 1, "dilation height");
DEFINE_int32(dila_w, 1, "dilation width");

C
chenjiaoAngel 已提交
56
DEFINE_bool(flag_act, true, "do act");
57
DEFINE_bool(flag_bias, true, "with bias");
C
chenjiaoAngel 已提交
58 59
DEFINE_double(clipped_coef, 1.0, "clipped relu coef");
DEFINE_double(leakey_relu_alpha, 8.88, "leakey relu alpha");
60 61 62 63

typedef paddle::lite::DDim DDim;
typedef paddle::lite::Tensor Tensor;
typedef paddle::lite::operators::ConvParam ConvParam;
C
chenjiaoAngel 已提交
64
typedef paddle::lite::operators::ActivationParam ActivationParam;
65
using paddle::lite::profile::Timer;
66 67 68

DDim compute_out_dim(const DDim& dim_in,
                     const paddle::lite::operators::ConvParam& param) {
H
HappyAngel 已提交
69 70
  auto paddings = *param.paddings;
  auto dilations = *param.dilations;
71 72 73 74 75 76
  DDim dim_out = dim_in;
  dim_out[1] = param.filter->dims()[0];
  auto kernel_h = param.filter->dims()[2];
  auto kernel_w = param.filter->dims()[3];
  auto h = dim_in[2];
  auto w = dim_in[3];
H
HappyAngel 已提交
77 78
  int dila_h = dilations[0];
  int dila_w = dilations[1];
79 80 81
  int stride_h = param.strides[0];
  int stride_w = param.strides[1];
  auto kernel_exten = dila_h * (kernel_h - 1) + 1;
H
HappyAngel 已提交
82
  auto hout = (h + paddings[0] + paddings[1] - kernel_exten) / stride_h + 1;
83
  kernel_exten = dila_w * (kernel_w - 1) + 1;
H
HappyAngel 已提交
84
  auto wout = (w + paddings[2] + paddings[3] - kernel_exten) / stride_w + 1;
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
  dim_out[2] = hout;
  dim_out[3] = wout;
  return dim_out;
}

template <paddle::lite::PrecisionType ptype>
void get_conv_param(const DDim& dim_w,
                    int g,
                    const std::vector<int>& strides,
                    const std::vector<int>& pads,
                    const std::vector<int>& dila,
                    bool flag_bias,
                    bool flag_relu,
                    ConvParam* param) {
  param->x = new Tensor;
  param->x->set_precision(PRECISION(kInt8));
  param->filter = new Tensor;
  param->filter->Resize(dim_w);
  param->filter->set_precision(PRECISION(kInt8));
  if (flag_bias) {
    param->bias = new Tensor;
    param->bias->Resize({dim_w[0]});
    param->bias->set_precision(PRECISION(kFloat));
  }
  param->strides = strides;
H
HappyAngel 已提交
110 111
  param->paddings = std::make_shared<std::vector<int>>(pads);
  param->dilations = std::make_shared<std::vector<int>>(dila);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  param->fuse_relu = flag_relu;
  param->groups = g;

  param->output = new Tensor;
  param->output->set_precision(ptype);
}

void release_param(ConvParam* param) {
  delete param->x;
  delete param->filter;
  delete param->output;
  delete param->bias;
}

#ifdef LITE_WITH_ARM
#include "lite/backends/arm/math/funcs.h"
void test_conv_int8(const std::vector<DDim>& input_dims,
                    const DDim& weight_dim,
                    int group,
                    const std::vector<int>& strides,
                    const std::vector<int>& pads,
                    const std::vector<int>& dilas,
                    bool flag_bias,
C
chenjiaoAngel 已提交
135
                    int flag_act,
136
                    const std::vector<int>& thread_num,
C
chenjiaoAngel 已提交
137 138 139
                    const std::vector<int>& power_mode,
                    const float six = 6.f,
                    const float alpha = 1.f) {
140 141 142 143 144 145 146 147 148 149
  paddle::lite::DeviceInfo::Init();
  ConvParam param_int8_out;
  ConvParam param_fp32_out;

  get_conv_param<PRECISION(kInt8)>(weight_dim,
                                   group,
                                   strides,
                                   pads,
                                   dilas,
                                   flag_bias,
C
chenjiaoAngel 已提交
150
                                   flag_act > 0,
151 152 153 154 155 156 157 158
                                   &param_int8_out);

  get_conv_param<PRECISION(kFloat)>(weight_dim,
                                    group,
                                    strides,
                                    pads,
                                    dilas,
                                    flag_bias,
C
chenjiaoAngel 已提交
159
                                    flag_act > 0,
160 161 162 163 164 165 166 167 168 169 170 171 172
                                    &param_fp32_out);
  Tensor weight_fp32;
  Tensor bias_fp32;
  weight_fp32.Resize(weight_dim);
  paddle::lite::fill_tensor_rand(*param_int8_out.filter, -127, 127);
  param_fp32_out.filter->CopyDataFrom(*param_int8_out.filter);
  if (flag_bias) {
    auto dim_b = param_int8_out.bias->dims();
    bias_fp32.Resize(dim_b);
    paddle::lite::fill_tensor_rand(*param_int8_out.bias, -1.f, 1.f);
    param_fp32_out.bias->CopyDataFrom(*param_int8_out.bias);
    bias_fp32.CopyDataFrom(*param_int8_out.bias);
  }
C
chenjiaoAngel 已提交
173 174 175 176 177 178
  if (flag_act > 0) {
    ActivationParam act_param;
    act_param.has_active = true;
    act_param.active_type = (paddle::lite_api::ActivationType)
        flag_act;  // 1-relu, 2-relu6, 4-leakyrelu
    if (flag_act == 1) {
C
chenjiaoAngel 已提交
179 180
      param_fp32_out.fuse_relu = true;
      param_int8_out.fuse_relu = true;
C
chenjiaoAngel 已提交
181 182 183
    } else if (flag_act == 2) {
      act_param.Relu_clipped_coef = six;
    } else if (flag_act == 4) {
C
chenjiaoAngel 已提交
184
      act_param.Leaky_relu_alpha = alpha;
C
chenjiaoAngel 已提交
185
    }
C
chenjiaoAngel 已提交
186 187
    param_fp32_out.activation_param = act_param;
    param_int8_out.activation_param = act_param;
C
chenjiaoAngel 已提交
188
  }
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

  std::vector<float> scale_in{1.f / 127};
  std::vector<float> scale_out{weight_dim.count(1, 4) / 127.f};
  std::vector<float> scale_w(weight_dim[0], 1.f / 127);

  param_int8_out.input_scale = scale_in[0];
  param_int8_out.output_scale = scale_out[0];
  param_int8_out.weight_scale = scale_w;

  param_fp32_out.input_scale = scale_in[0];
  param_fp32_out.output_scale = scale_out[0];
  param_fp32_out.weight_scale = scale_w;

  auto wptr_fp32 = weight_fp32.mutable_data<float>();
  auto bptr_fp32 = flag_bias ? bias_fp32.data<float>() : nullptr;

  paddle::lite::arm::math::int8_to_fp32(param_int8_out.filter->data<int8_t>(),
                                        wptr_fp32,
                                        scale_w.data(),
                                        weight_dim[0],
                                        1,
                                        weight_dim.count(1, 4));

212
  for (auto& cls : power_mode) {
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    for (auto& th : thread_num) {
      std::unique_ptr<paddle::lite::KernelContext> ctx1(
          new paddle::lite::KernelContext);
      std::unique_ptr<paddle::lite::KernelContext> ctx2(
          new paddle::lite::KernelContext);
      auto& ctx_tmp1 = ctx1->As<paddle::lite::ARMContext>();
      ctx_tmp1.SetRunMode(static_cast<paddle::lite_api::PowerMode>(cls), th);
      auto& ctx_tmp2 = ctx2->As<paddle::lite::ARMContext>();
      ctx_tmp2.SetRunMode(static_cast<paddle::lite_api::PowerMode>(cls), th);

      paddle::lite::kernels::arm::ConvCompute<PRECISION(kInt8),
                                              PRECISION(kInt8)>
          conv_int8_int8;
      paddle::lite::kernels::arm::ConvCompute<PRECISION(kInt8),
                                              PRECISION(kFloat)>
          conv_int8_fp32;
      conv_int8_int8.SetContext(std::move(ctx1));
      conv_int8_fp32.SetContext(std::move(ctx2));

      /// set param and context
      for (auto& dim_in : input_dims) {
        param_int8_out.x->Resize(dim_in);
        DDim out_tmp_dims = compute_out_dim(dim_in, param_int8_out);
        if (out_tmp_dims[2] < 1 || out_tmp_dims[3] < 1) {
          continue;
        }
        param_fp32_out.x->Resize(dim_in);
        param_int8_out.output->Resize(out_tmp_dims);
        param_fp32_out.output->Resize(out_tmp_dims);
        break;
      }
      conv_int8_int8.SetParam(param_int8_out);
      conv_int8_fp32.SetParam(param_fp32_out);
      /// prepare for run
      conv_int8_int8.PrepareForRun();
      conv_int8_fp32.PrepareForRun();

      for (auto& dim_in : input_dims) {
        CHECK_EQ(weight_dim[1] * group, dim_in[1])
            << "input channel must equal to weights channel";
        DDim dim_out = compute_out_dim(dim_in, param_int8_out);
        if (dim_out[2] < 1 || dim_out[3] < 1) {
          continue;
        }
        delete param_fp32_out.output;
        param_fp32_out.output = new Tensor;
        param_fp32_out.output->set_precision(PRECISION(kFloat));
        delete param_int8_out.output;
        param_int8_out.output = new Tensor;
        param_int8_out.output->set_precision(PRECISION(kInt8));

        param_int8_out.x->Resize(dim_in);
        param_int8_out.output->Resize(dim_out);
        param_fp32_out.x->Resize(dim_in);
        param_fp32_out.output->Resize(dim_out);

        Tensor tin_fp32;
        tin_fp32.Resize(dim_in);
        tin_fp32.set_precision(PRECISION(kFloat));
        Tensor tout_basic_fp32;
        Tensor tout_basic_int8;

        paddle::lite::fill_tensor_rand(*param_int8_out.x, -127, 127);
        param_fp32_out.x->CopyDataFrom(*param_int8_out.x);

        auto din_fp32 = tin_fp32.mutable_data<float>();
        paddle::lite::arm::math::int8_to_fp32(param_int8_out.x->data<int8_t>(),
                                              din_fp32,
                                              scale_in.data(),
                                              1,
                                              1,
                                              dim_in.production());

        if (FLAGS_check_result) {
          tout_basic_fp32.set_precision(PRECISION(kFloat));
          tout_basic_fp32.Resize(dim_out);
          tout_basic_int8.set_precision(PRECISION(kInt8));
          tout_basic_int8.Resize(dim_out);
          fill_tensor_const(tout_basic_fp32, 0.f);
          auto dout_basic_fp32 = tout_basic_fp32.mutable_data<float>();
          auto dout_basic_int8 = tout_basic_int8.mutable_data<int8_t>();
          conv_basic<float, float>(din_fp32,
                                   dout_basic_fp32,
                                   dim_in[0],
                                   dim_out[1],
                                   dim_out[2],
                                   dim_out[3],
                                   dim_in[1],
                                   dim_in[2],
                                   dim_in[3],
                                   wptr_fp32,
                                   bptr_fp32,
                                   group,
                                   weight_dim[3],
                                   weight_dim[2],
                                   strides[1],
                                   strides[0],
                                   dilas[1],
                                   dilas[0],
H
HappyAngel 已提交
312
                                   pads[2],
313 314
                                   pads[0],
                                   flag_bias,
C
chenjiaoAngel 已提交
315 316 317
                                   flag_act,
                                   six,
                                   alpha);
318 319 320 321 322 323
          paddle::lite::arm::math::fp32_to_int8(dout_basic_fp32,
                                                dout_basic_int8,
                                                scale_out.data(),
                                                1,
                                                1,
                                                dim_out.production());
C
chenjiaoAngel 已提交
324 325 326 327 328 329 330 331 332 333 334 335
          if (flag_act == 2) { // relu6
             for (int i = 0; i < dim_out.production(); i++) {
                 dout_basic_int8[i] = dout_basic_int8[i] > six ? six : dout_basic_int8[i];
             }
          } else if (flag_act == 4) { // leakyRelu
             for (int i = 0; i < dim_out.production(); i++) {
               float tmp = dout_basic_fp32[i] / scale_out.data()[0];
               tmp = tmp > 0 ? tmp : tmp * alpha;
               dout_basic_int8[i] = static_cast<int8_t>(roundf(tmp));
               dout_basic_int8[i] = dout_basic_int8[i] < -127 ? -127: dout_basic_int8[i];
            }
          }
336 337 338 339 340 341 342 343 344
        }

        double gops = 2.0 * dim_out.production() * dim_in[1] * weight_dim[2] *
                      weight_dim[3] / group;
        /// warm up
        for (int i = 0; i < FLAGS_warmup; ++i) {
          conv_int8_int8.Launch();
        }
        /// compute fp32 output
345
        Timer t0;
346
        for (int i = 0; i < FLAGS_repeats; ++i) {
347
          t0.Start();
348
          conv_int8_fp32.Launch();
349
          t0.Stop();
350 351
        }
        LOG(INFO) << "int8 conv, fp32 output: output shape" << dim_out
352 353
                  << ",running time, avg: " << t0.LapTimes().Avg()
                  << ", min time: " << t0.LapTimes().Min()
354
                  << ", total GOPS: " << 1e-9 * gops
355 356
                  << " GOPS, avg GOPs: " << 1e-6 * gops / t0.LapTimes().Avg()
                  << " GOPs, max GOPs: " << 1e-6 * gops / t0.LapTimes().Min();
357 358

        /// compute int8 output
359
        t0.Reset();
360
        for (int i = 0; i < FLAGS_repeats; ++i) {
361
          t0.Start();
362
          conv_int8_int8.Launch();
363
          t0.Stop();
364 365
        }
        LOG(INFO) << "int8 conv, int8 output: output shape" << dim_out
366 367
                  << ",running time, avg: " << t0.LapTimes().Avg()
                  << ", min time: " << t0.LapTimes().Min()
368
                  << ", total GOPS: " << 1e-9 * gops
369 370
                  << " GOPS, avg GOPs: " << 1e-6 * gops / t0.LapTimes().Avg()
                  << " GOPs, max GOPs: " << 1e-6 * gops / t0.LapTimes().Min();
371 372 373 374 375 376 377 378 379 380 381 382 383

        /// compare result fp32 output
        if (FLAGS_check_result) {
          double max_ratio = 0;
          double max_diff = 0;
          tensor_cmp_host(
              tout_basic_fp32, *param_fp32_out.output, max_ratio, max_diff);
          LOG(INFO) << "FP32 compare result, max diff: " << max_diff
                    << ", max ratio: " << max_ratio;
          if (std::abs(max_ratio) > 1e-5f) {
            if (max_diff > 5e-5f) {
              LOG(WARNING) << "basic result";
              print_tensor(tout_basic_fp32);
X
Xiaoyang LI 已提交
384
              LOG(WARNING) << "lite result";
385 386 387 388 389 390 391 392 393 394 395
              print_tensor(*param_fp32_out.output);
              Tensor tdiff;
              tdiff.Resize(tout_basic_fp32.dims());
              tdiff.set_precision(PRECISION(kFloat));
              tensor_diff(tout_basic_fp32, *param_fp32_out.output, tdiff);
              print_tensor(tdiff);
              release_param(&param_int8_out);
              release_param(&param_fp32_out);
              LOG(FATAL) << "test int8 conv, fp32 out: input: " << dim_in
                         << ", output: " << dim_out
                         << ", weight dim: " << weight_dim
H
HappyAngel 已提交
396 397
                         << ", pad: " << pads[0] << ", " << pads[1] << ", "
                         << pads[2] << ", " << pads[3]
398 399
                         << ", stride: " << strides[0] << ", " << strides[1]
                         << ", dila_: " << dilas[0] << ", " << dilas[1]
400
                         << ", group: " << group
401
                         << ", bias: " << (flag_bias ? "true" : "false")
C
chenjiaoAngel 已提交
402 403
                         << ", act: " << flag_act << ", threads: " << th
                         << ", power_mode: " << cls << " failed!!\n";
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
            }
          }
        }
        /// compare result int8 output
        if (FLAGS_check_result) {
          double max_ratio = 0;
          double max_diff = 0;
          // ! int8
          tensor_cmp_host(
              tout_basic_int8, *param_int8_out.output, max_ratio, max_diff);
          LOG(INFO) << "int8 compare result, max diff: " << max_diff
                    << ", max ratio: " << max_ratio;
          if (fabs(max_diff) > 0) {
            Tensor tdiff;
            tdiff.Resize(tout_basic_int8.dims());
            tdiff.set_precision(PRECISION(kInt8));
            tensor_diff(tout_basic_int8, *param_int8_out.output, tdiff);
            auto ptr = tdiff.data<int8_t>();
            auto ptr_basic_fp32 = tout_basic_fp32.data<float>();
            float count = 0;
            bool check = true;
            for (int i = 0; i < tdiff.numel(); ++i) {
              if (abs(ptr[i]) > 1) {
                check = false;
                LOG(ERROR) << "basic float data: " << ptr_basic_fp32[i]
                           << ", after scale: "
                           << ptr_basic_fp32[i] / scale_out[0];
                break;
              }
              if (ptr[i] != 0) {
                LOG(ERROR) << "basic float data: " << ptr_basic_fp32[i]
                           << ", after scale: "
                           << ptr_basic_fp32[i] / scale_out[0];
                count += 1;
              }
            }
            check =
                check &&
                count < std::max(10, static_cast<int>(0.01 * tdiff.numel()));
            if (!check) {
              LOG(WARNING) << "int8 basic result";
              print_tensor(tout_basic_int8);
X
Xiaoyang LI 已提交
446
              LOG(WARNING) << "int8 lite result";
447 448 449 450 451 452 453 454
              print_tensor(*param_int8_out.output);
              LOG(WARNING) << "int8 diff tensor";
              print_tensor(tdiff);
              release_param(&param_int8_out);
              release_param(&param_fp32_out);
              LOG(FATAL) << "test int8 conv, int8 out: input: " << dim_in
                         << ", output: " << dim_out
                         << ", weight dim: " << weight_dim
H
HappyAngel 已提交
455 456
                         << ", pad: " << pads[0] << ", " << pads[1] << ", "
                         << pads[2] << ", " << pads[3]
457 458 459
                         << ", stride: " << strides[0] << ", " << strides[1]
                         << ", dila_: " << dilas[0] << ", " << dilas[1]
                         << ", bias: " << (flag_bias ? "true" : "false")
C
chenjiaoAngel 已提交
460 461
                         << ", act: " << flag_act << ", threads: " << th
                         << ", power_mode: " << cls << " failed!!\n";
462 463 464 465 466
            }
          }
        }
        LOG(INFO) << "test int8 conv: input: " << dim_in
                  << ", output: " << dim_out << ", weight dim: " << weight_dim
H
HappyAngel 已提交
467 468 469
                  << ", pad: " << pads[0] << ", " << pads[1] << ", " << pads[2]
                  << ", " << pads[3] << ", stride: " << strides[0] << ", "
                  << strides[1] << ", dila_: " << dilas[0] << ", " << dilas[1]
470
                  << ", bias: " << (flag_bias ? "true" : "false")
C
chenjiaoAngel 已提交
471 472
                  << ", act: " << flag_act << ", threads: " << th
                  << ", power_mode: " << cls << " successed!!\n";
473 474 475 476 477 478 479 480 481 482 483 484 485 486
      }
    }
  }
  release_param(&param_int8_out);
  release_param(&param_fp32_out);
}
#else
void test_conv_int8(const std::vector<DDim>& input_dims,
                    const DDim& weight_dim,
                    int group,
                    const std::vector<int>& strides,
                    const std::vector<int>& pads,
                    const std::vector<int>& dilas,
                    bool flag_bias,
C
chenjiaoAngel 已提交
487
                    int flag_act,
488
                    const std::vector<int>& thread_num,
C
chenjiaoAngel 已提交
489 490 491
                    const std::vector<int>& power_mode,
                    float six = 6.f,
                    float alpha = 1.f) {}
492 493
#endif  // LITE_WITH_ARM

494
#if 1  /// 3x3dw
495 496 497 498 499
TEST(TestConv3x3DWInt8, test_conv3x3_depthwise) {
  if (FLAGS_basic_test) {
    for (auto& stride : {1, 2}) {
      for (auto& pad : {0, 1}) {
        for (auto& flag_bias : {false, true}) {
C
chenjiaoAngel 已提交
500
          for (auto& flag_act : {0, 1, 2, 4}) {
501 502 503 504
            for (auto& c : {1, 3, 5, 8, 16, 32}) {
              std::vector<DDim> dims;
              DDim weights_dim({c, 1, 3, 3});
              for (auto& batch : {1, 2}) {
505
                for (auto& h : {1, 3, 15, 33}) {
506 507 508 509 510 511 512
                  dims.push_back(DDim({batch, c, h, h}));
                }
              }
              test_conv_int8(dims,
                             weights_dim,
                             c,
                             {stride, stride},
H
HappyAngel 已提交
513
                             {pad, pad, pad, pad},
514 515
                             {1, 1},
                             flag_bias,
C
chenjiaoAngel 已提交
516
                             flag_act,
517
                             {4},
C
chenjiaoAngel 已提交
518 519 520
                             {FLAGS_power_mode},
                             FLAGS_clipped_coef,
                             FLAGS_leakey_relu_alpha);
521 522 523 524 525 526 527 528 529
            }
          }
        }
      }
    }
  }
}
#endif  /// 3x3dw

Y
yiicy 已提交
530
#if 1  /// 5x5dw
531 532
TEST(TestConv5x5DWInt8, test_conv5x5_depthwise) {
  if (FLAGS_basic_test) {
533
    for (auto& stride : {1, 2}) {
534
      for (auto& pad : {0, 1, 2, 3, 4}) {
535
        for (auto& flag_bias : {false, true}) {
C
chenjiaoAngel 已提交
536
          for (auto& flag_act: {0, 1, 2, 4}) {
537
            for (auto& c : {1, 5, 15, 33}) {
538 539 540
              std::vector<DDim> dims;
              DDim weights_dim({c, 1, 5, 5});
              for (auto& batch : {1, 2}) {
541
                for (auto& h : {1, 3, 15, 33, 112, 224}) {
542 543 544 545 546 547 548
                  dims.push_back(DDim({batch, c, h, h}));
                }
              }
              test_conv_int8(dims,
                             weights_dim,
                             c,
                             {stride, stride},
H
HappyAngel 已提交
549
                             {pad, pad, pad, pad},
550 551
                             {1, 1},
                             flag_bias,
C
chenjiaoAngel 已提交
552
                             flag_act,
553
                             {1, 4},
C
chenjiaoAngel 已提交
554 555 556
                             {FLAGS_power_mode},
                             FLAGS_clipped_coef,
                             FLAGS_leakey_relu_alpha);
557 558 559 560 561 562 563 564 565
            }
          }
        }
      }
    }
  }
}
#endif  /// 5x5dw

566
#if 1  /// conv1x1s1
567 568
TEST(TestConv1x1s1Int8, test_conv1x1s1) {
  if (FLAGS_basic_test) {
569 570
    for (auto& cin : {1, 3, 8, 32}) {
      for (auto& cout : {1, 5, 17}) {
571 572
        for (auto& g : {1, 2}) {
          for (auto& flag_bias : {false, true}) {
C
chenjiaoAngel 已提交
573
            for (auto& flag_act : {0, 1, 2, 4}) {
574 575 576 577 578 579
              std::vector<DDim> dims;
              if (cin % g != 0 || cout % g != 0) {
                continue;
              }
              DDim weights_dim({cout, cin / g, 1, 1});
              for (auto& batch : {1, 2}) {
580
                for (auto& h : {1, 9, 16, 33}) {
581 582 583 584 585 586 587
                  dims.push_back(DDim({batch, cin, h, h}));
                }
              }
              test_conv_int8(dims,
                             weights_dim,
                             g,
                             {1, 1},
H
HappyAngel 已提交
588
                             {0, 0, 0, 0},
589 590
                             {1, 1},
                             flag_bias,
C
chenjiaoAngel 已提交
591
                             flag_act,
592
                             {4},
C
chenjiaoAngel 已提交
593 594 595
                             {FLAGS_power_mode},
                             FLAGS_clipped_coef,
                             FLAGS_leakey_relu_alpha);
596 597 598 599 600 601 602 603 604
            }
          }
        }
      }
    }
  }
}
#endif  /// conv1x1s1

605
#if 1  /// conv3x3s1
606 607
TEST(TestConv3x3s1Int8, test_conv_3x3s1) {
  if (FLAGS_basic_test) {
608 609
    for (auto& cin : {1, 3, 8, 33}) {
      for (auto& cout : {1, 5, 33}) {
H
HappyAngel 已提交
610 611 612 613 614
        for (auto& pad_top : {1, 2}) {
          for (auto& pad_bottom : {1, 2}) {
            for (auto& pad_left : {1, 2}) {
              for (auto& pad_right : {1, 2}) {
                for (auto& flag_bias : {false, true}) {
C
chenjiaoAngel 已提交
615
                  for (auto& flag_act : {0, 1, 2, 4}) {
H
HappyAngel 已提交
616 617 618
                    std::vector<DDim> dims;
                    DDim weights_dim({cout, cin, 3, 3});
                    for (auto& batch : {1, 2}) {
619
                      for (auto& h : {1, 7, 17, 33}) {
H
HappyAngel 已提交
620 621 622 623 624 625 626 627 628 629
                        dims.push_back(DDim({batch, cin, h, h}));
                      }
                    }
                    test_conv_int8(dims,
                                   weights_dim,
                                   1,
                                   {1, 1},
                                   {pad_top, pad_bottom, pad_left, pad_right},
                                   {1, 1},
                                   flag_bias,
C
chenjiaoAngel 已提交
630
                                   flag_act,
631
                                   {4},
C
chenjiaoAngel 已提交
632 633 634
                                   {FLAGS_power_mode},
                                   FLAGS_clipped_coef,
                                   FLAGS_leakey_relu_alpha);
H
HappyAngel 已提交
635
                  }
636 637 638 639 640 641 642 643 644 645 646
                }
              }
            }
          }
        }
      }
    }
  }
}
#endif  /// conv3x3s1

647
#if 1  /// conv3x3s2
648 649
TEST(TestConv3x3s2Int8, test_conv_3x3s2) {
  if (FLAGS_basic_test) {
650 651
    for (auto& cin : {1, 3, 31}) {
      for (auto& cout : {1, 5, 33}) {
H
HappyAngel 已提交
652 653 654 655 656
        for (auto& pad_top : {1, 2}) {
          for (auto& pad_bottom : {1, 2}) {
            for (auto& pad_left : {1, 2}) {
              for (auto& pad_right : {1, 2}) {
                for (auto& flag_bias : {false, true}) {
C
chenjiaoAngel 已提交
657
                  for (auto& flag_act : {0, 1, 2, 4}) {
H
HappyAngel 已提交
658 659 660
                    std::vector<DDim> dims;
                    DDim weights_dim({cout, cin, 3, 3});
                    for (auto& batch : {1, 2}) {
661
                      for (auto& h : {1, 7, 19, 33}) {
H
HappyAngel 已提交
662 663 664 665 666 667 668 669 670 671
                        dims.push_back(DDim({batch, cin, h, h}));
                      }
                    }
                    test_conv_int8(dims,
                                   weights_dim,
                                   1,
                                   {2, 2},
                                   {pad_top, pad_bottom, pad_left, pad_right},
                                   {1, 1},
                                   flag_bias,
C
chenjiaoAngel 已提交
672
                                   flag_act,
673
                                   {4},
C
chenjiaoAngel 已提交
674 675 676
                                   {FLAGS_power_mode},
                                   FLAGS_clipped_coef,
                                   FLAGS_leakey_relu_alpha);
H
HappyAngel 已提交
677
                  }
678 679 680 681 682 683 684 685 686 687 688
                }
              }
            }
          }
        }
      }
    }
  }
}
#endif  /// conv3x3s2

689
#if 0   /// random param conv
690 691
TEST(TestConvRandInt8, test_conv_rand) {
  if (FLAGS_basic_test) {
692 693
    for (auto& cin : {1, 17}) {
      for (auto& cout : {1, 8, 17}) {
694 695 696 697
        for (auto& g : {1, 2}) {
          for (auto& kw : {1, 2, 3}) {
            for (auto& kh : {1, 2, 3}) {
              for (auto& stride : {1, 2}) {
H
HappyAngel 已提交
698 699 700 701 702 703
                for (auto& pad_top : {0, 1, 2}) {
                  for (auto& pad_bottom : {0, 1, 2}) {
                    for (auto& pad_left : {0, 1, 2}) {
                      for (auto& pad_right : {0, 1, 2}) {
                        for (auto& dila : {1, 2}) {
                          for (auto& flag_bias : {false, true}) {
C
chenjiaoAngel 已提交
704
                            for (auto& flag_act : {0, 1, 2, 4}) {
H
HappyAngel 已提交
705
                              if (cin % g != 0 || cout % g != 0) {
706
                                break;
H
HappyAngel 已提交
707 708 709 710
                              }
                              std::vector<DDim> dims;
                              DDim weights_dim({cout, cin / g, kh, kw});
                              for (auto& batch : {1, 2}) {
711
                                for (auto& h : {1, 3, 5, 19}) {
H
HappyAngel 已提交
712 713 714 715 716 717 718 719 720 721 722
                                  dims.push_back(DDim({batch, cin, h, h}));
                                }
                              }
                              test_conv_int8(
                                  dims,
                                  weights_dim,
                                  g,
                                  {stride, stride},
                                  {pad_top, pad_bottom, pad_left, pad_right},
                                  {dila, dila},
                                  flag_bias,
C
chenjiaoAngel 已提交
723
                                  flag_act,
724
                                  {4},
C
chenjiaoAngel 已提交
725 726 727
                                  {FLAGS_power_mode},
                                  FLAGS_clipped_coef,
                                  FLAGS_leakey_relu_alpha);
H
HappyAngel 已提交
728
                            }
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
#endif  /// random param conv

#if 1  /// custom
TEST(TestConvCustomInt8, test_conv_custom_size) {
  CHECK_EQ(FLAGS_in_channel % FLAGS_group, 0)
      << "input channel must be divided by group";
  CHECK_EQ(FLAGS_out_channel % FLAGS_group, 0)
      << "num_output must be divided by group";
  test_conv_int8(
      {DDim({FLAGS_batch, FLAGS_in_channel, FLAGS_in_height, FLAGS_in_width})},
      DDim({FLAGS_out_channel,
            FLAGS_in_channel / FLAGS_group,
            FLAGS_kernel_h,
            FLAGS_kernel_w}),
      FLAGS_group,
      {FLAGS_stride_h, FLAGS_stride_w},
H
HappyAngel 已提交
759
      {FLAGS_pad_h, FLAGS_pad_h, FLAGS_pad_w, FLAGS_pad_w},
760 761
      {FLAGS_dila_h, FLAGS_dila_w},
      FLAGS_flag_bias,
C
chenjiaoAngel 已提交
762
      FLAGS_flag_act,
763
      {FLAGS_threads},
C
chenjiaoAngel 已提交
764 765 766
      {FLAGS_power_mode},
      FLAGS_clipped_coef,
      FLAGS_leakey_relu_alpha);
767 768
}
#endif  // custom