conv_compute_test.cc 44.1 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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 "paddle/fluid/lite/kernels/arm/conv_compute.h"
#include <gtest/gtest.h>
S
shixiaowei02 已提交
17
#include <limits>
T
tensor-tang 已提交
18 19
#include <memory>
#include <utility>
T
tensor-tang 已提交
20
#include <vector>
S
shixiaowei02 已提交
21
#include "paddle/fluid/lite/arm/math/type_trans.h"
T
tensor-tang 已提交
22 23 24 25 26 27 28
#include "paddle/fluid/lite/core/op_registry.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace arm {

29 30 31 32
static int get_rand(int start, int end) {
  int i = rand();  // NOLINT
  i = (i % (end - start)) + start;
  return i;
S
shixiaowei02 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
}

template <typename Dtype1, typename Dtype2>
static void conv_basic(const Dtype1* din, Dtype2* dout, int num, int chout,
                       int hout, int wout, int chin, int hin, int win,
                       const Dtype1* weights, const Dtype2* bias, int group,
                       int kernel_w, int kernel_h, int stride_w, int stride_h,
                       int dila_w, int dila_h, int pad_w, int pad_h,
                       bool flag_bias, bool flag_relu) {
  Dtype2 beta = 0;
  auto src_data = din;
  auto dst_data_ref = dout;
  auto weights_data = weights;
  auto with_bias = flag_bias;
  auto bias_data = bias;

  int in_num = num;
  int out_channels = chout;
  int out_h = hout;
  int out_w = wout;
T
tensor-tang 已提交
53

S
shixiaowei02 已提交
54 55 56 57 58 59 60 61
  int in_channel = chin;
  int in_h = hin;
  int in_w = win;
  int out_c_group = out_channels / group;
  int in_c_group = in_channel / group;

  for (int n = 0; n < in_num; ++n) {
    for (int g = 0; g < group; ++g) {
T
tensor-tang 已提交
62
      for (int oc = 0; oc < out_c_group; ++oc) {
S
shixiaowei02 已提交
63 64 65 66 67 68 69 70
        for (int oh = 0; oh < out_h; ++oh) {
          for (int ow = 0; ow < out_w; ++ow) {
            int out_idx = n * group * out_c_group * out_h * out_w +
                          g * out_c_group * out_h * out_w + oc * out_h * out_w +
                          oh * out_w + ow;
            Dtype2 bias_d =
                with_bias ? (bias_data[g * out_c_group + oc]) : (Dtype2)0;
            dst_data_ref[out_idx] = bias_d;  // + dst_data_ref[out_idx] * beta;
T
tensor-tang 已提交
71 72 73
            for (int ic = 0; ic < in_c_group; ++ic) {
              for (int kh = 0; kh < kernel_h; ++kh) {
                for (int kw = 0; kw < kernel_w; ++kw) {
S
shixiaowei02 已提交
74 75 76 77
                  int iw = ow * stride_w - pad_w + kw * (dila_w);
                  int ih = oh * stride_h - pad_h + kh * (dila_h);
                  if (iw < 0 || iw >= in_w) continue;
                  if (ih < 0 || ih >= in_h) continue;
T
tensor-tang 已提交
78

S
shixiaowei02 已提交
79 80 81
                  int iidx = n * in_channel * in_h * in_w +
                             g * in_c_group * in_h * in_w + ic * in_h * in_w +
                             ih * in_w + iw;
T
tensor-tang 已提交
82 83 84 85 86
                  int widx =
                      g * out_c_group * in_c_group * kernel_h * kernel_w +
                      oc * in_c_group * kernel_h * kernel_w +
                      ic * kernel_h * kernel_w + kh * kernel_w + kw;

S
shixiaowei02 已提交
87
                  dst_data_ref[out_idx] += src_data[iidx] * weights_data[widx];
T
tensor-tang 已提交
88 89 90 91
                }
              }
            }
            if (flag_relu) {
S
shixiaowei02 已提交
92 93 94
              dst_data_ref[out_idx] = dst_data_ref[out_idx] > (Dtype2)0
                                          ? dst_data_ref[out_idx]
                                          : (Dtype2)0;
T
tensor-tang 已提交
95 96 97 98 99 100 101 102
            }
          }
        }
      }
    }
  }
}

S
shixiaowei02 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
template <typename Dtype1, typename Dtype2>
void conv_compute_ref(const operators::ConvParam& param) {
  const Dtype1* din = param.x->data<Dtype1>();
  Dtype2* dout = param.output->mutable_data<Dtype2>();

  int num = param.x->dims()[0];
  int chout = param.output->dims()[1];
  int hout = param.output->dims()[2];
  int wout = param.output->dims()[3];

  int chin = param.x->dims()[1];
  int hin = param.x->dims()[2];
  int win = param.x->dims()[3];

  const Dtype1* weights = param.filter->mutable_data<Dtype1>();
  Dtype2* bias = nullptr;
  if (param.bias != nullptr) {
    bias = param.bias->mutable_data<Dtype2>();
  }

  int group = param.groups;
  int kernel_w = param.filter->dims()[2];
  int kernel_h = param.filter->dims()[3];
  int stride_w = param.strides[0];
  int stride_h = param.strides[1];
  int dila_w = param.dilations[0];
  int dila_h = param.dilations[1];

  int pad_w = param.paddings[0];
  int pad_h = param.paddings[1];
  bool flag_bias = (param.bias != nullptr);
  bool flag_relu = param.fuse_relu;

  conv_basic(din, dout, num, chout, hout, wout, chin, hin, win, weights, bias,
             group, kernel_w, kernel_h, stride_w, stride_h, dila_w, dila_h,
             pad_w, pad_h, flag_bias, flag_relu);
}

T
tensor-tang 已提交
141
TEST(conv_arm, retrive_op) {
142 143
  auto conv = KernelRegistry::Global().Create<TARGET(kARM), PRECISION(kFloat)>(
      "conv2d");
T
tensor-tang 已提交
144 145 146 147
  ASSERT_FALSE(conv.empty());
  ASSERT_TRUE(conv.front());
}

S
shixiaowei02 已提交
148 149 150 151 152 153 154
TEST(conv_arm_int8, retrive_op) {
  auto conv =
      KernelRegistry::Global().Create<TARGET(kARM), PRECISION(kInt8)>("conv2d");
  ASSERT_FALSE(conv.empty());
  ASSERT_TRUE(conv.front());
}

T
tensor-tang 已提交
155 156 157 158 159 160
TEST(conv_arm, init) {
  ConvCompute conv;
  ASSERT_EQ(conv.precision(), PRECISION(kFloat));
  ASSERT_EQ(conv.target(), TARGET(kARM));
}

S
shixiaowei02 已提交
161 162 163 164 165 166 167 168 169
TEST(conv_arm_int8, init) {
  ConvComputeInt8<PRECISION(kFloat)> float_out;
  ASSERT_EQ(float_out.precision(), PRECISION(kInt8));
  ASSERT_EQ(float_out.target(), TARGET(kARM));
  ConvComputeInt8<PRECISION(kInt8)> int8_out;
  ASSERT_EQ(float_out.precision(), PRECISION(kInt8));
  ASSERT_EQ(float_out.target(), TARGET(kARM));
}

170
TEST(conv_arm_int8, int8_int32) {
S
shixiaowei02 已提交
171 172 173 174 175 176
  DeviceInfo::Init();
  for (auto n : {2}) {
    for (auto ic : {6}) {
      for (auto oc : {6}) {
        for (auto ih : {9}) {
          for (auto iw : {9}) {
177 178
            for (auto flag_bias : {false, true}) {
              for (auto flag_relu : {false, true}) {
S
Shixiaowei02 已提交
179
                for (auto depthwise : {false, /*true*/}) {
S
shixiaowei02 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
                  for (auto dilation : {1}) {
                    for (auto stride : {1}) {
                      for (auto padding : {0}) {
                        for (auto ks : {1}) {
                          int group = 1;
                          if (depthwise) {  // depthwise convolution ?
                            group = oc = ic;
                          }

                          const int dks = dilation * (ks - 1) + 1;
                          int oh = (ih + 2 * padding - dks) / stride + 1;
                          int ow = (iw + 2 * padding - dks) / stride + 1;
                          std::vector<int64_t> input_shape = {n, ic, ih, iw};
                          std::vector<int64_t> filter_shape = {oc, ic / group,
                                                               ks, ks};
                          std::vector<int64_t> output_shape({n, oc, oh, ow});

                          Tensor input_int8;
                          Tensor filter_int8;
                          Tensor output_int32, output_int32_ref;

                          input_int8.Resize(input_shape);
                          filter_int8.Resize(filter_shape);
                          output_int32.Resize(output_shape);
                          output_int32_ref.Resize(output_shape);

                          int8_t* input_int8_data =
                              input_int8.mutable_data<int8_t>();
                          int8_t* filter_int8_data =
                              filter_int8.mutable_data<int8_t>();
                          for (int i = 0; i < input_int8.dims().production();
                               i++) {
S
Shixiaowei02 已提交
212
                            input_int8_data[i] = i % 10 * (i % 3 - 1);
S
shixiaowei02 已提交
213 214 215
                          }
                          for (int i = 0; i < filter_int8.dims().production();
                               i++) {
S
Shixiaowei02 已提交
216
                            filter_int8_data[i] = i % 10 * (i % 3 - 1);
S
shixiaowei02 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
                          }

                          operators::ConvParam param;
                          param.x = &input_int8;
                          param.filter = &filter_int8;
                          param.bias = nullptr;
                          param.fuse_relu = false;
                          param.paddings = std::vector<int>({padding, padding});
                          param.strides = std::vector<int>({stride, stride});
                          param.dilations =
                              std::vector<int>({dilation, dilation});
                          param.groups = group;
                          param.output = &output_int32_ref;
                          conv_compute_ref<int8_t, int>(param);

                          param.output = &output_int32;
                          std::unique_ptr<KernelContext> ctx(new KernelContext);
                          lite::arm::math::GemmLikeConvInt8<PRECISION(kInt32)>
                              int8gemm_int32;
                          int8gemm_int32.init(param, &ctx->As<ARMContext>());
                          int8gemm_int32.create(param, &ctx->As<ARMContext>());
                          int8gemm_int32.run(param);

S
Shixiaowei02 已提交
240 241 242 243
                          int* output_int32_data =
                              output_int32.mutable_data<int>();
                          int* output_int32_ref_data =
                              output_int32_ref.mutable_data<int>();
S
shixiaowei02 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

                          for (int i = 0; i < output_int32.dims().production();
                               i++) {
                            EXPECT_NEAR(output_int32_data[i],
                                        output_int32_ref_data[i], 1e-3);
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

264 265 266 267 268 269 270 271 272
TEST(conv_arm_int8, int8_fp32) {
  DeviceInfo::Init();
  for (auto n : {2}) {
    for (auto ic : {6}) {
      for (auto oc : {6}) {
        for (auto ih : {9}) {
          for (auto iw : {9}) {
            for (auto flag_bias : {false, true}) {
              for (auto flag_relu : {false, true}) {
S
Shixiaowei02 已提交
273
                for (auto depthwise : {false, /*true*/}) {
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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
                  for (auto dilation : {1}) {
                    for (auto stride : {1}) {
                      for (auto padding : {0}) {
                        for (auto ks : {1}) {
                          int group = 1;
                          if (depthwise) {  // depthwise convolution ?
                            group = oc = ic;
                          }

                          const int dks = dilation * (ks - 1) + 1;
                          int oh = (ih + 2 * padding - dks) / stride + 1;
                          int ow = (iw + 2 * padding - dks) / stride + 1;
                          std::vector<int64_t> input_shape = {n, ic, ih, iw};
                          std::vector<int64_t> filter_shape = {oc, ic / group,
                                                               ks, ks};
                          std::vector<int64_t> bias_shape({1, oc, 1, 1});
                          std::vector<int64_t> output_shape({n, oc, oh, ow});

                          Tensor input_fp32, input_int8;
                          Tensor filter_fp32, filter_int8;
                          Tensor bias_fp32, bias_int8;
                          Tensor output_int32_ref, output_int32;
                          Tensor output_fp32_ref, output_fp32;
                          Tensor output_int8_ref, output_int8;

                          input_fp32.Resize(input_shape);
                          input_int8.Resize(input_shape);
                          filter_fp32.Resize(filter_shape);
                          filter_int8.Resize(filter_shape);
                          bias_fp32.Resize(bias_shape);
                          bias_int8.Resize(bias_shape);
                          output_int32.Resize(output_shape);
                          output_int32_ref.Resize(output_shape);
                          output_fp32_ref.Resize(output_shape);
                          output_fp32.Resize(output_shape);
                          output_int8_ref.Resize(output_shape);
                          output_int8.Resize(output_shape);

                          float* input_fp32_data =
                              input_fp32.mutable_data<float>();
                          int8_t* input_int8_data =
                              input_int8.mutable_data<int8_t>();

                          float* filter_fp32_data =
                              filter_fp32.mutable_data<float>();
                          int8_t* filter_int8_data =
                              filter_int8.mutable_data<int8_t>();

                          float* bias_fp32_data =
                              bias_fp32.mutable_data<float>();
                          int8_t* bias_int8_data =
                              bias_int8.mutable_data<int8_t>();

                          for (int i = 0; i < input_fp32.dims().production();
                               i++) {
S
Shixiaowei02 已提交
329
                            input_fp32_data[i] = i % 10 * (i % 3 - 1);
330 331 332
                          }
                          for (int i = 0; i < filter_fp32.dims().production();
                               i++) {
S
Shixiaowei02 已提交
333
                            filter_fp32_data[i] = i % 10 * (i % 3 - 1);
334 335 336
                          }
                          for (int i = 0; i < bias_fp32.dims().production();
                               i++) {
S
Shixiaowei02 已提交
337
                            bias_fp32_data[i] = i % 10 * (i % 3 - 1);
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
                          }

                          std::vector<float> in_scale;
                          lite::arm::math::get_tensor_scale<PRECISION(kFloat)>(
                              input_fp32, &in_scale, -1, 127.f);
                          lite::arm::math::trans_tensor_fp32_to_int8(
                              &input_fp32, &input_int8, in_scale[0]);

                          std::vector<float> w_scale;
                          lite::arm::math::get_tensor_scale<PRECISION(kFloat)>(
                              filter_fp32, &w_scale, -1, 127.f);
                          int axis_size = oc;
                          int inner_size = ic / group * ks * ks;
                          w_scale = lite::arm::math::get_tensor_scale_n(
                              filter_fp32_data, axis_size, inner_size, 127.f);
                          lite::arm::math::fp32_to_int8(
                              filter_fp32_data, filter_int8_data,
                              w_scale.data(), axis_size, 1, inner_size);

                          operators::ConvParam param;
                          param.x = &input_int8;
                          param.filter = &filter_int8;
                          param.bias = &bias_int8;
                          param.fuse_relu = false;
                          param.paddings = std::vector<int>({padding, padding});
                          param.strides = std::vector<int>({stride, stride});
                          param.dilations =
                              std::vector<int>({dilation, dilation});
                          param.groups = group;
                          param.output = &output_int32_ref;
                          conv_compute_ref<int8_t, int>(param);

S
Shixiaowei02 已提交
370 371
                          int* output_int32_ref_data =
                              output_int32_ref.mutable_data<int>();
372 373 374 375 376 377 378 379 380 381 382 383

                          // ============ int8gemm_int32 ============
                          param.output = &output_int32;
                          std::unique_ptr<KernelContext> ctx_int32(
                              new KernelContext);
                          lite::arm::math::GemmLikeConvInt8<PRECISION(kInt32)>
                              int8gemm_int32;
                          int8gemm_int32.init(param,
                                              &ctx_int32->As<ARMContext>());
                          int8gemm_int32.create(param,
                                                &ctx_int32->As<ARMContext>());
                          int8gemm_int32.run(param);
S
Shixiaowei02 已提交
384 385
                          int* output_int32_data =
                              output_int32.mutable_data<int>();
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
                          for (int i = 0; i < output_int32.dims().production();
                               i++) {
                            EXPECT_NEAR(output_int32_data[i],
                                        output_int32_ref_data[i], 1e-3);
                          }

                          // ============ int8gemm_int8 ============
                          int8_t* output_int8_ref_data =
                              output_int8_ref.mutable_data<int8_t>();
                          lite::arm::math::trans_tensor_int32_to_int8(
                              &output_int32_ref, &output_int8_ref, in_scale[0],
                              1, w_scale);
                          param.output = &output_int8;
                          param.input_scale = in_scale[0];
                          param.output_scale = 1;
S
update  
Shixiaowei02 已提交
401
                          param.weight_scale = w_scale;
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
                          std::unique_ptr<KernelContext> ctx_int8(
                              new KernelContext);
                          lite::arm::math::GemmLikeConvInt8<PRECISION(kInt8)>
                              int8gemm_int8;
                          int8gemm_int8.init(param,
                                             &ctx_int8->As<ARMContext>());
                          int8gemm_int8.create(param,
                                               &ctx_int8->As<ARMContext>());
                          int8gemm_int8.run(param);
                          int8_t* output_int8_data =
                              output_int8.mutable_data<int8_t>();
                          for (int i = 0; i < output_int8.dims().production();
                               i++) {
                            EXPECT_NEAR(output_int8_data[i],
                                        output_int8_ref_data[i], 1e-3);
                          }

                          // ============ int8gemm_float32 ============
                          float* output_fp32_ref_data =
                              output_fp32_ref.mutable_data<float>();
                          lite::arm::math::trans_tensor_int32_to_fp32(
                              &output_int32_ref, &output_fp32_ref, in_scale[0],
                              w_scale);
                          param.output = &output_fp32;
                          param.input_scale = in_scale[0];
                          param.output_scale = 1;
S
update  
Shixiaowei02 已提交
428
                          param.weight_scale = w_scale;
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
                          std::unique_ptr<KernelContext> ctx_fp32(
                              new KernelContext);
                          lite::arm::math::GemmLikeConvInt8<PRECISION(kFloat)>
                              int8gemm_fp32;
                          int8gemm_fp32.init(param,
                                             &ctx_fp32->As<ARMContext>());
                          int8gemm_fp32.create(param,
                                               &ctx_fp32->As<ARMContext>());
                          int8gemm_fp32.run(param);
                          float* output_fp32_data =
                              output_fp32.mutable_data<float>();
                          for (int i = 0; i < output_fp32.dims().production();
                               i++) {
                            EXPECT_NEAR(output_fp32_data[i],
                                        output_fp32_ref_data[i], 1e-3);
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 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 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
TEST(conv_direct_int8, compute) {
  DeviceInfo::Init();
  for (auto n : {1, 2}) {
    for (auto ic : {1, 3, 8}) {
      for (auto oc : {1, 3, 8}) {
        for (auto ih : {5, 15, 28}) {
          for (auto iw : {5, 15, 28}) {
            for (auto flag_bias : {false, true}) {
              for (auto flag_relu : {false, true}) {
                for (auto depthwise : {false, /*true*/}) {
                  for (auto dilation : {1}) {
                    for (auto stride : {1, 2}) {
                      for (auto padding : {1}) {
                        for (auto ks : {3}) {
                          int group = 1;
                          if (depthwise) {  // depthwise convolution ?
                            group = oc = ic;
                          }

                          const int dks = dilation * (ks - 1) + 1;
                          int oh = (ih + 2 * padding - dks) / stride + 1;
                          int ow = (iw + 2 * padding - dks) / stride + 1;
                          std::vector<int64_t> input_shape = {n, ic, ih, iw};
                          std::vector<int64_t> filter_shape = {oc, ic / group,
                                                               ks, ks};
                          std::vector<int64_t> bias_shape({1, oc, 1, 1});
                          std::vector<int64_t> output_shape({n, oc, oh, ow});

                          Tensor input_fp32, input_int8;
                          Tensor filter_fp32, filter_int8;
                          Tensor bias_int32;
                          Tensor output_int32_ref, output_int32;
                          Tensor output_fp32_ref, output_fp32;
                          Tensor output_int8_ref, output_int8;

                          input_fp32.Resize(input_shape);
                          input_int8.Resize(input_shape);
                          filter_fp32.Resize(filter_shape);
                          filter_int8.Resize(filter_shape);
                          bias_int32.Resize(bias_shape);
                          output_int32.Resize(output_shape);
                          output_int32_ref.Resize(output_shape);
                          output_fp32_ref.Resize(output_shape);
                          output_fp32.Resize(output_shape);
                          output_int8_ref.Resize(output_shape);
                          output_int8.Resize(output_shape);

                          float* input_fp32_data =
                              input_fp32.mutable_data<float>();
                          int8_t* input_int8_data =
                              input_int8.mutable_data<int8_t>();

                          float* filter_fp32_data =
                              filter_fp32.mutable_data<float>();
                          int8_t* filter_int8_data =
                              filter_int8.mutable_data<int8_t>();

                          int* bias_int32_data =
                              bias_int32.mutable_data<int32_t>();

                          for (int i = 0; i < input_fp32.dims().production();
                               i++) {
                            input_fp32_data[i] = i % 10 * (i % 3 - 1);
                          }
                          for (int i = 0; i < filter_fp32.dims().production();
                               i++) {
                            filter_fp32_data[i] = i % 10 * (i % 3 - 1);
                          }
                          for (int i = 0; i < bias_int32.dims().production();
                               i++) {
                            bias_int32_data[i] = i % 10 * (i % 3 - 1);
                          }

                          std::vector<float> in_scale;
                          lite::arm::math::get_tensor_scale<PRECISION(kFloat)>(
                              input_fp32, &in_scale, -1, 127.f);
                          lite::arm::math::trans_tensor_fp32_to_int8(
                              &input_fp32, &input_int8, in_scale[0]);

                          std::vector<float> w_scale;
                          lite::arm::math::get_tensor_scale<PRECISION(kFloat)>(
                              filter_fp32, &w_scale, -1, 127.f);
                          int axis_size = oc;
                          int inner_size = ic / group * ks * ks;
                          w_scale = lite::arm::math::get_tensor_scale_n(
                              filter_fp32_data, axis_size, inner_size, 127.f);
                          lite::arm::math::fp32_to_int8(
                              filter_fp32_data, filter_int8_data,
                              w_scale.data(), axis_size, 1, inner_size);

                          operators::ConvParam param;
                          param.x = &input_int8;
                          param.filter = &filter_int8;
                          if (flag_bias) {
                            param.bias = &bias_int32;
                          }
                          param.fuse_relu = false;
                          param.paddings = std::vector<int>({padding, padding});
                          param.strides = std::vector<int>({stride, stride});
                          param.dilations =
                              std::vector<int>({dilation, dilation});
                          param.groups = group;
                          param.output = &output_int32_ref;
                          conv_compute_ref<int8_t, int>(param);

                          int* output_int32_ref_data =
                              output_int32_ref.mutable_data<int>();

                          // ============ int8direct_int32 ============
                          param.output = &output_int32;
                          std::unique_ptr<KernelContext> ctx_int32(
                              new KernelContext);
                          lite::arm::math::DirectConvInt8<PRECISION(kInt32)>
                              int8direct_int32;
                          int8direct_int32.init(param,
                                                &ctx_int32->As<ARMContext>());
                          int8direct_int32.create(param,
                                                  &ctx_int32->As<ARMContext>());
                          int8direct_int32.run(param);
                          int* output_int32_data =
                              output_int32.mutable_data<int>();
                          for (int i = 0; i < output_int32.dims().production();
                               i++) {
                            EXPECT_NEAR(output_int32_data[i],
                                        output_int32_ref_data[i], 1e-3);
                          }

                          // ============ int8direct_int8 ============
                          int8_t* output_int8_ref_data =
                              output_int8_ref.mutable_data<int8_t>();
                          lite::arm::math::trans_tensor_int32_to_int8(
                              &output_int32_ref, &output_int8_ref, in_scale[0],
                              1, w_scale);
                          param.output = &output_int8;
                          param.input_scale = in_scale[0];
                          param.output_scale = 1;
                          param.weight_scale = w_scale;
                          std::unique_ptr<KernelContext> ctx_int8(
                              new KernelContext);
                          lite::arm::math::DirectConvInt8<PRECISION(kInt8)>
                              int8direct_int8;
                          int8direct_int8.init(param,
                                               &ctx_int8->As<ARMContext>());
                          int8direct_int8.create(param,
                                                 &ctx_int8->As<ARMContext>());
                          int8direct_int8.run(param);
                          int8_t* output_int8_data =
                              output_int8.mutable_data<int8_t>();
                          for (int i = 0; i < output_int8.dims().production();
                               i++) {
                            EXPECT_NEAR(output_int8_data[i],
                                        output_int8_ref_data[i], 1e-3);
                          }

                          // ============ int8direct_float32 ============
                          float* output_fp32_ref_data =
                              output_fp32_ref.mutable_data<float>();
                          lite::arm::math::trans_tensor_int32_to_fp32(
                              &output_int32_ref, &output_fp32_ref, in_scale[0],
                              w_scale);
                          param.output = &output_fp32;
                          param.input_scale = in_scale[0];
                          param.output_scale = 1;
                          param.weight_scale = w_scale;
                          std::unique_ptr<KernelContext> ctx_fp32(
                              new KernelContext);
                          lite::arm::math::DirectConvInt8<PRECISION(kFloat)>
                              int8direct_fp32;
                          int8direct_fp32.init(param,
                                               &ctx_fp32->As<ARMContext>());
                          int8direct_fp32.create(param,
                                                 &ctx_fp32->As<ARMContext>());
                          int8direct_fp32.run(param);
                          float* output_fp32_data =
                              output_fp32.mutable_data<float>();
                          for (int i = 0; i < output_fp32.dims().production();
                               i++) {
                            EXPECT_NEAR(output_fp32_data[i],
                                        output_fp32_ref_data[i], 1e-3);
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

TEST(conv_depthwise_int8, compute) {
  DeviceInfo::Init();
  for (auto n : {1, 2}) {
    for (auto ic : {1, 3, 8}) {
      for (auto ih : {5, 15, 28}) {
        for (auto iw : {5, 15, 28}) {
          for (auto flag_bias : {false, true}) {
            for (auto flag_relu : {false, true}) {
              for (auto dilation : {1}) {
                for (auto stride : {1, 2}) {
                  for (auto padding : {1, 2}) {
                    for (auto ks : {3, /*5 */}) {
                      int group = ic;
                      int oc = ic;

                      bool flag_dw_3x3 = (ks == 3) && (padding == 1) &&
                                         (stride == 1 || stride == 2);
                      bool flag_dw_5x5 =
                          (ks == 5 && stride == 1 && padding == 2);
                      bool flag_dw = flag_dw_3x3 || flag_dw_5x5;
                      if (!flag_dw) continue;

                      const int dks = dilation * (ks - 1) + 1;
                      int oh = (ih + 2 * padding - dks) / stride + 1;
                      int ow = (iw + 2 * padding - dks) / stride + 1;
                      std::vector<int64_t> input_shape = {n, ic, ih, iw};
                      std::vector<int64_t> filter_shape = {oc, ic / group, ks,
                                                           ks};
                      std::vector<int64_t> bias_shape({1, oc, 1, 1});
                      std::vector<int64_t> output_shape({n, oc, oh, ow});

                      Tensor input_fp32, input_int8;
                      Tensor filter_fp32, filter_int8;
                      Tensor bias_int32;
                      Tensor output_int32_ref, output_int32;
                      Tensor output_fp32_ref, output_fp32;
                      Tensor output_int8_ref, output_int8;

                      input_fp32.Resize(input_shape);
                      input_int8.Resize(input_shape);
                      filter_fp32.Resize(filter_shape);
                      filter_int8.Resize(filter_shape);
                      bias_int32.Resize(bias_shape);

                      output_int32.Resize(output_shape);
                      output_int32_ref.Resize(output_shape);
                      output_fp32_ref.Resize(output_shape);
                      output_fp32.Resize(output_shape);
                      output_int8_ref.Resize(output_shape);
                      output_int8.Resize(output_shape);

                      float* input_fp32_data = input_fp32.mutable_data<float>();
                      int8_t* input_int8_data =
                          input_int8.mutable_data<int8_t>();
                      float* filter_fp32_data =
                          filter_fp32.mutable_data<float>();
                      int8_t* filter_int8_data =
                          filter_int8.mutable_data<int8_t>();

                      int* bias_int32_data = bias_int32.mutable_data<int32_t>();

                      for (int i = 0; i < input_fp32.dims().production(); i++) {
                        input_fp32_data[i] = i % 10 * (i % 3 - 1);
                      }
                      for (int i = 0; i < filter_fp32.dims().production();
                           i++) {
                        filter_fp32_data[i] = i % 10 * (i % 3 - 1);
                      }
                      for (int i = 0; i < bias_int32.dims().production(); i++) {
                        bias_int32_data[i] = i % 10 * (i % 3 - 1);
                      }

                      std::vector<float> in_scale;
                      lite::arm::math::get_tensor_scale<PRECISION(kFloat)>(
                          input_fp32, &in_scale, -1, 127.f);
                      lite::arm::math::trans_tensor_fp32_to_int8(
                          &input_fp32, &input_int8, in_scale[0]);

                      std::vector<float> w_scale;
                      lite::arm::math::get_tensor_scale<PRECISION(kFloat)>(
                          filter_fp32, &w_scale, -1, 127.f);
                      int axis_size = oc;
                      int inner_size = ic / group * ks * ks;
                      w_scale = lite::arm::math::get_tensor_scale_n(
                          filter_fp32_data, axis_size, inner_size, 127.f);
                      lite::arm::math::fp32_to_int8(
                          filter_fp32_data, filter_int8_data, w_scale.data(),
                          axis_size, 1, inner_size);

                      operators::ConvParam param;
                      param.x = &input_int8;
                      param.filter = &filter_int8;
                      if (flag_bias) {
                        param.bias = &bias_int32;
                      }
                      param.fuse_relu = false;
                      param.paddings = std::vector<int>({padding, padding});
                      param.strides = std::vector<int>({stride, stride});
                      param.dilations = std::vector<int>({dilation, dilation});
                      param.groups = group;
                      param.output = &output_int32_ref;
                      conv_compute_ref<int8_t, int>(param);

                      int* output_int32_ref_data =
                          output_int32_ref.mutable_data<int>();

                      // ============ int8depthwise_int32 ============
                      param.output = &output_int32;
                      std::unique_ptr<KernelContext> ctx_int32(
                          new KernelContext);
                      lite::arm::math::DepthwiseConvInt8<PRECISION(kInt32)>
                          int8depthwise_int32;
                      int8depthwise_int32.init(param,
                                               &ctx_int32->As<ARMContext>());
                      int8depthwise_int32.create(param,
                                                 &ctx_int32->As<ARMContext>());
                      int8depthwise_int32.run(param);
                      int* output_int32_data = output_int32.mutable_data<int>();
                      for (int i = 0; i < output_int32.dims().production();
                           i++) {
                        EXPECT_NEAR(output_int32_data[i],
                                    output_int32_ref_data[i], 1e-3);
                      }

                      // ============ int8depthwise_int8============
                      int8_t* output_int8_ref_data =
                          output_int8_ref.mutable_data<int8_t>();
                      lite::arm::math::trans_tensor_int32_to_int8(
                          &output_int32_ref, &output_int8_ref, in_scale[0], 1,
                          w_scale);
                      param.output = &output_int8;
                      param.input_scale = in_scale[0];
                      param.output_scale = 1;
                      param.weight_scale = w_scale;
                      std::unique_ptr<KernelContext> ctx_int8(
                          new KernelContext);
                      lite::arm::math::DepthwiseConvInt8<PRECISION(kInt8)>
                          int8depthwise_int8;
                      int8depthwise_int8.init(param,
                                              &ctx_int8->As<ARMContext>());
                      int8depthwise_int8.create(param,
                                                &ctx_int8->As<ARMContext>());
                      int8depthwise_int8.run(param);
                      int8_t* output_int8_data =
                          output_int8.mutable_data<int8_t>();
                      for (int i = 0; i < output_int8.dims().production();
                           i++) {
                        EXPECT_NEAR(output_int8_data[i],
                                    output_int8_ref_data[i], 1e-3);
                      }

                      // ============int8depthwise_float32 ============
                      float* output_fp32_ref_data =
                          output_fp32_ref.mutable_data<float>();
                      lite::arm::math::trans_tensor_int32_to_fp32(
                          &output_int32_ref, &output_fp32_ref, in_scale[0],
                          w_scale);
                      param.output = &output_fp32;
                      param.input_scale = in_scale[0];
                      param.output_scale = 1;
                      param.weight_scale = w_scale;
                      std::unique_ptr<KernelContext> ctx_fp32(
                          new KernelContext);
                      lite::arm::math::DepthwiseConvInt8<PRECISION(kFloat)>
                          int8depthwise_fp32;
                      int8depthwise_fp32.init(param,
                                              &ctx_fp32->As<ARMContext>());
                      int8depthwise_fp32.create(param,
                                                &ctx_fp32->As<ARMContext>());
                      int8depthwise_fp32.run(param);
                      float* output_fp32_data =
                          output_fp32.mutable_data<float>();
                      for (int i = 0; i < output_fp32.dims().production();
                           i++) {
                        EXPECT_NEAR(output_fp32_data[i],
                                    output_fp32_ref_data[i], 1e-3);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

T
tensor-tang 已提交
842 843
TEST(conv_arm, compute) {
  DeviceInfo::Init();
T
tensor-tang 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857
#if 1
  for (auto n : {2}) {
    for (auto ic : {6}) {
      for (auto oc : {6}) {
        for (auto ih : {9}) {
          for (auto iw : {9}) {
            for (auto flag_bias : {false, true}) {
              for (auto flag_relu : {false, true}) {
                for (auto depthwise : {false, true}) {
                  for (auto dilation : {1}) {
                    for (auto stride : {1, 2}) {
                      for (auto padding : {0, 1, 2}) {
                        for (auto ks : {1, 3, 5}) {
#else
T
tensor-tang 已提交
858
  for (auto n : {1, 2}) {
859 860 861 862 863 864
    for (auto ic : {6, 32 /*, 128*/}) {
      for (auto oc : {6, 32 /*, 128*/}) {
        for (auto ih : {9, 18 /*, 56 , 112, 224, 512*/}) {
          for (auto iw : {9, 18 /*, 56, 112, 224, 512*/}) {
            for (auto flag_bias : {false, true}) {
              for (auto flag_relu : {false, true}) {
T
tensor-tang 已提交
865
                for (auto depthwise : {false, true}) {
866
                  for (auto dilation : {1, 2}) {
T
tensor-tang 已提交
867
                    for (auto stride : {1, 2}) {
868 869
                      for (auto padding : {0, 1, 2}) {
                        for (auto ks : {1, 3, 5}) {
T
tensor-tang 已提交
870
#endif
T
tensor-tang 已提交
871
                          int group = 1;
872 873
                          if (depthwise) {  // depthwise convolution ?
                            group = oc = ic;
T
tensor-tang 已提交
874 875
                          }
                          // get input, filter and output shape
876 877 878
                          std::vector<int64_t> input_shape = {n, ic, ih, iw};
                          std::vector<int64_t> filter_shape = {oc, ic / group,
                                                               ks, ks};
879 880 881 882
                          const int dks = dilation * (ks - 1) + 1;
                          int oh = (ih + 2 * padding - dks) / stride + 1;
                          int ow = (iw + 2 * padding - dks) / stride + 1;
                          std::vector<int64_t> output_shape({n, oc, oh, ow});
T
tensor-tang 已提交
883
                          // resize input, filter and output
884 885 886 887 888
                          Tensor input;
                          Tensor filter;
                          Tensor bias;
                          Tensor output;
                          Tensor output_ref;
889 890 891 892
                          input.Resize(input_shape);
                          filter.Resize(filter_shape);
                          output.Resize(output_shape);
                          output_ref.Resize(output_shape);
T
Tensor Tang 已提交
893 894 895 896 897 898
                          VLOG(3) << "input: " << input.dims();
                          VLOG(3) << "filter: " << filter.dims()
                                  << " padding:" << padding
                                  << " stride:" << stride
                                  << " dilation:" << dilation;
                          VLOG(3) << "output: " << output.dims();
T
tensor-tang 已提交
899 900 901 902
                          auto* input_data = input.mutable_data<float>();
                          auto* filter_data = filter.mutable_data<float>();
                          auto* output_data = output.mutable_data<float>();
                          for (int i = 0; i < input.dims().production(); i++) {
903 904
                            float sign = i % 3 == 0 ? -1.0f : 1.0f;
                            input_data[i] = sign * static_cast<float>(i % 128);
T
tensor-tang 已提交
905 906
                          }
                          for (int i = 0; i < filter.dims().production(); i++) {
907 908 909
                            filter_data[i] =
                                i * 0.001f /
                                static_cast<float>(filter.dims().production());
T
tensor-tang 已提交
910
                          }
911 912 913 914 915 916
                          // prepare kernel params and run
                          ConvCompute conv;
                          std::unique_ptr<KernelContext> ctx(new KernelContext);
                          ctx->As<ARMContext>();
                          conv.SetContext(std::move(ctx));
                          operators::ConvParam param;
T
tensor-tang 已提交
917 918 919 920
                          param.x = &input;
                          param.filter = &filter;
                          param.output = &output;
                          param.bias = nullptr;
921 922 923 924 925 926 927 928
                          if (flag_bias) {
                            bias.Resize({oc});
                            auto* bias_data = bias.mutable_data<float>();
                            for (int i = 0; i < bias.dims().production(); i++) {
                              bias_data[i] = static_cast<float>(i);
                            }
                            param.bias = &bias;
                          }
929
                          param.fuse_relu = flag_relu;
T
tensor-tang 已提交
930 931 932 933 934 935
                          param.paddings = std::vector<int>({padding, padding});
                          param.strides = std::vector<int>({stride, stride});
                          param.dilations =
                              std::vector<int>({dilation, dilation});
                          param.groups = group;
                          conv.SetParam(param);
936 937
                          conv.Launch();
                          // invoking ref implementation and compare results
T
tensor-tang 已提交
938
                          param.output = &output_ref;
S
shixiaowei02 已提交
939
                          conv_compute_ref<float, float>(param);
940 941
                          auto* output_ref_data =
                              output_ref.mutable_data<float>();
T
tensor-tang 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
                          for (int i = 0; i < output.dims().production(); i++) {
                            EXPECT_NEAR(output_data[i], output_ref_data[i],
                                        1e-3);
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
T
tensor-tang 已提交
958 959 960 961 962 963 964
}

}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

T
tensor-tang 已提交
965 966
USE_LITE_KERNEL(conv2d, kARM, kFloat, kNCHW, def);
USE_LITE_KERNEL(depthwise_conv2d, kARM, kFloat, kNCHW, def);