convolution.cpp 23.7 KB
Newer Older
1 2 3 4 5 6 7 8
/**
 * \file dnn/test/fallback/convolution.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
9 10
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 */
#include "test/fallback/fixture.h"

#include "test/common/benchmarker.h"
#include "test/common/checker.h"
#include "test/common/convolution.h"

#include "test/common/rng.h"

using namespace megdnn;
using namespace test;

#if MEGDNN_WITH_BENCHMARK

TEST_F(FALLBACK, BENCHMARK_CONVOLUTION_MATRIX_MUL) {
    using Param = Convolution::Param;
    auto run = [&](const TensorShapeArray& shapes, Param param) {
        Benchmarker<Convolution> benchmarker_float(handle());
        size_t RUN = 50;
        auto tfloat = benchmarker_float.set_display(false)
                              .set_dtype(0, dtype::Float32{})
                              .set_dtype(1, dtype::Float32{})
                              .set_times(RUN)
                              .set_param(param)
                              .exec(shapes);
        size_t IC = shapes[1][1];
        size_t FH = shapes[1][2];
        size_t FW = shapes[1][3];
        TensorLayout dst_layout;
        auto opr = handle()->create_operator<Convolution>();
        opr->param() = param;
        opr->deduce_layout({shapes[0], dtype::Float32()},
                           {shapes[1], dtype::Float32()}, dst_layout);
        printf("fp32 flops: %.3f mflops\n",
               (IC * dst_layout.total_nr_elems() * FH * FW * 2) /
                       (tfloat / RUN * 1000));
    };
    auto profile = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
                       size_t stride) {
        Param param;
        param.stride_h = stride;
        param.stride_w = stride;
        param.pad_h = kernel / 2;
        param.pad_w = kernel / 2;
        param.pad_h = 0;
        param.pad_w = 0;
        printf("oc: %zd ic: %zd w: %zd h: %zd stride: %zd kernel_size: %zd\n",
               oc, ic, w, h, stride, kernel);

        run({{1, ic, h, w}, {oc, ic, kernel, kernel}, {}}, param);
    };

    profile(48, 128, 56, 88, 1, 1);
    profile(56, 128, 64, 80, 1, 1);
    profile(24, 3, 256, 320, 3, 2);
    profile(16, 3, 224, 352, 5, 2);
    profile(16, 3, 256, 320, 7, 2);
    profile(8, 8, 56, 88, 3, 1);
    profile(8, 8, 7, 11, 3, 1);
    profile(4, 4, 64, 80, 3, 1);
    profile(108, 108, 7, 7, 3, 1);
    profile(54, 54, 7, 7, 3, 1);
    profile(3, 3, 128, 128, 3, 1);
    profile(3, 3, 112, 112, 3, 1);
}

77 78 79 80 81 82 83 84 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 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
TEST_F(FALLBACK, BENCHMARK_CONVOLUTION_MATRIX_MUL_8832) {
    using Param = Convolution::Param;
    auto run = [&](const TensorShapeArray& shapes, Param param) {
        Benchmarker<Convolution> benchmarker_float(handle());
        size_t RUN = 50;
        auto tfloat = benchmarker_float.set_display(false)
                              .set_dtype(0, dtype::Int8{})
                              .set_dtype(1, dtype::Int8{})
                              .set_dtype(2, dtype::Int32{})
                              .set_times(RUN)
                              .set_param(param)
                              .exec(shapes);
        size_t IC = shapes[1][1];
        size_t FH = shapes[1][2];
        size_t FW = shapes[1][3];
        TensorLayout dst_layout;
        auto opr = handle()->create_operator<Convolution>();
        opr->param() = param;
        opr->deduce_layout({shapes[0], dtype::Float32()},
                           {shapes[1], dtype::Float32()}, dst_layout);
        printf("fp32 flops: %.3f mflops\n",
               (IC * dst_layout.total_nr_elems() * FH * FW * 2) /
                       (tfloat / RUN * 1000));
    };
    auto profile = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
                       size_t stride) {
        Param param;
        param.stride_h = stride;
        param.stride_w = stride;
        param.pad_h = kernel / 2;
        param.pad_w = kernel / 2;
        param.pad_h = 0;
        param.pad_w = 0;
        printf("oc: %zd ic: %zd w: %zd h: %zd stride: %zd kernel_size: %zd\n",
               oc, ic, w, h, stride, kernel);

        run({{1, ic, h, w}, {oc, ic, kernel, kernel}, {}}, param);
    };

    profile(48, 128, 56, 88, 1, 1);
    profile(56, 128, 64, 80, 3, 1);
    profile(24, 3, 256, 320, 3, 2);
}

TEST_F(FALLBACK, BENCHMARK_CONVOLUTION_MATRIX_MUL_8816) {
    using Param = Convolution::Param;
    auto run = [&](const TensorShapeArray& shapes, Param param) {
        Benchmarker<Convolution> benchmarker_float(handle());
        size_t RUN = 50;
        auto tfloat = benchmarker_float.set_display(false)
                              .set_dtype(0, dtype::Int8{})
                              .set_dtype(1, dtype::Int8{})
                              .set_dtype(2, dtype::Int16{})
                              .set_times(RUN)
                              .set_param(param)
                              .exec(shapes);
        size_t IC = shapes[1][1];
        size_t FH = shapes[1][2];
        size_t FW = shapes[1][3];
        TensorLayout dst_layout;
        auto opr = handle()->create_operator<Convolution>();
        opr->param() = param;
        opr->deduce_layout({shapes[0], dtype::Float32()},
                           {shapes[1], dtype::Float32()}, dst_layout);
        printf("fp32 flops: %.3f mflops\n",
               (IC * dst_layout.total_nr_elems() * FH * FW * 2) /
                       (tfloat / RUN * 1000));
    };
    auto profile = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
                       size_t stride) {
        Param param;
        param.stride_h = stride;
        param.stride_w = stride;
        param.pad_h = kernel / 2;
        param.pad_w = kernel / 2;
        param.pad_h = 0;
        param.pad_w = 0;
        printf("oc: %zd ic: %zd w: %zd h: %zd stride: %zd kernel_size: %zd\n",
               oc, ic, w, h, stride, kernel);

        run({{1, ic, h, w}, {oc, ic, kernel, kernel}, {}}, param);
    };

    profile(48, 128, 56, 88, 1, 1);
    profile(48, 128, 56, 88, 1, 2);
    profile(56, 128, 64, 80, 3, 1);
    profile(24, 3, 256, 320, 3, 2);
}

166 167 168 169 170 171
TEST_F(FALLBACK, BENCHMARK_CONVOLUTION_BACKWARD_DATA) {
    using Param = ConvolutionBackwardData::Param;
    auto run = [&](const TensorLayoutArray& tensors, Param param) {
        Benchmarker<ConvolutionBackwardData> benchmarker_fallback(handle());
        size_t RUN = 500;
        benchmarker_fallback.set_display(false)
172 173 174 175 176 177 178 179 180 181 182 183 184 185
                .set_dtype(0, dtype::Float32{})
                .set_dtype(1, dtype::Float32{})
                .set_times(RUN)
                .set_param(param);
        auto tmatmul = benchmarker_fallback
                               .set_before_exec_callback(
                                       AlgoChecker<ConvolutionBackwardData>(
                                               "DeconvMatmul"))
                               .exec(tensors);
        auto tdirect = benchmarker_fallback
                               .set_before_exec_callback(
                                       AlgoChecker<ConvolutionBackwardData>(
                                               "DeconvDirect"))
                               .exec(tensors);
186 187 188 189 190 191 192
        size_t IC = tensors[0][1];
        size_t FH = tensors[0][2];
        size_t FW = tensors[0][3];
        size_t total_flops = IC * tensors[1].total_nr_elems() * FH * FW * 2;
        printf("Direct_time: %.3f ms  Direct_flops: %.3f mflops\n", tdirect,
               total_flops / (tdirect / RUN * 1000));
        printf("Matmul_time: %.3f ms  Matmul_flops: %.3f mflops\n", tmatmul,
193 194
               total_flops / (tmatmul / RUN * 1000));
        printf("speedup: %.3f\n", tdirect / tmatmul);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 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
    };

    auto profile = [&](size_t n, size_t ic, size_t oh, size_t ow, size_t oc,
                       size_t fh, size_t fw, size_t stride = 1,
                       size_t padding = 0) {
        Param param;
        param.pad_h = param.pad_w = padding;
        param.stride_h = param.stride_w = stride;
        printf("oc: %zd ic: %zd w: %zd h: %zd stride: %zd kernel_size: %zd\n",
               oc, ic, ow, oh, stride, fh);

        TensorLayout diff = TensorLayout{{n, oc, oh, ow}, dtype::Float32()};
        TensorLayout filter = TensorLayout{{oc, ic, fh, fw}, dtype::Float32()};
        TensorLayout grad;
        {
            auto opr = handle()->create_operator<ConvolutionBackwardData>();
            opr->param() = param;
            opr->deduce_layout(filter, diff, grad);
        }
        run(TensorLayoutArray{filter, diff, grad}, param);
    };

    profile(1, 1, 3, 3, 1, 2, 2);
    profile(1, 2, 3, 3, 2, 2, 2);
    profile(1, 4, 3, 3, 4, 2, 2);
    profile(1, 4, 3, 3, 8, 2, 2);
    profile(1, 8, 3, 3, 4, 2, 2);
    profile(1, 8, 3, 3, 8, 2, 2);
}

#endif

TEST_F(FALLBACK, CONVOLUTION_MATRIX_MUL) {
    Checker<Convolution> checker(handle());
    using Param = Convolution::Param;

    Param param;
    param.sparse = param::Convolution::Sparse::DENSE;
    auto run = [&](size_t n, size_t ic, size_t ih, size_t iw, size_t oc,
                   size_t fh, size_t fw) {
        param.pad_h = param.pad_w = 1;
        param.stride_h = param.stride_w = 1;
        checker.set_param(param);
        checker.execs({{n, ic, ih, iw}, {oc, ic, fh, fw}, {}});
    };

    run(1, 3, 128, 128, 5, 3, 3);
    run(1, 56, 128, 64, 80, 1, 1);
    run(1, 8, 8, 7, 11, 3, 1);
    run(1, 54, 54, 7, 7, 3, 1);
    run(1, 3, 3, 128, 128, 3, 1);
    run(1, 3, 3, 112, 112, 3, 1);
    run(1, 1, 1, 1, 1, 3, 3);
}
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

#if MEGDNN_X86
TEST_F(FALLBACK_MULTI_THREADS, CONVOLUTION_8816) {
    Checker<Convolution> checker(handle());
    using Param = Convolution::Param;
    checker.set_before_exec_callback(AlgoChecker<Convolution>(".+FB_GEMV.+"));
    auto run = [&](size_t n, size_t ic, size_t ih, size_t iw, size_t oc,
                   size_t fh, size_t fw, size_t pad, size_t stride,
                   size_t group) {
        Param param;
        param.sparse = group > 1 ? param::Convolution::Sparse::GROUP
                                 : param::Convolution::Sparse::DENSE;
        param.pad_h = param.pad_w = pad;
        param.stride_h = param.stride_w = stride;
        checker.set_param(param);
        if (group > 1) {
            checker.execl(
                    {{{n, ic, ih, iw}, dtype::Int8()},
                     {{group, oc / group, ic / group, fh, fw}, dtype::Int8()},
                     {{}, dtype::Int16()}});
        } else {
            checker.execl({{{n, ic, ih, iw}, dtype::Int8()},
                           {{oc, ic, fh, fw}, dtype::Int8()},
                           {{}, dtype::Int16()}});
        }
    };

    for (auto n : {1, 2})
        for (auto ic : {3, 4, 8, 12, 16})
            for (auto oc : {4, 8, 16, 32})
                for (auto ih : {7, 14, 15, 22})
                    for (auto iw : {7, 13, 11, 32})
                        for (auto filter : {1, 2, 3, 5, 7})
                            for (auto stride : {1, 2})
                                for (auto pad : {0, filter / 2}) {
                                    run(n, ic, ih, iw, oc, filter, filter, pad,
                                        stride, 1);
                                    if (ic == oc) {
                                        run(n, ic, ih, iw, oc, filter, filter,
                                            pad, stride, ic);
                                    }
                                }
}
#endif

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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
TEST_F(FALLBACK, CONVOLUTION_NAIVE_ALGO_FP16) {
    Checker<Convolution> checker(handle());
    using Param = Convolution::Param;
    checker.set_before_exec_callback(
            AlgoChecker<ConvolutionForward>("NAIVE_ALGO"));
    Param param;
    param.sparse = param::Convolution::Sparse::DENSE;
    auto run = [&](size_t n, size_t ic, size_t ih, size_t iw, size_t oc,
                   size_t fh, size_t fw) {
        param.pad_h = param.pad_w = 1;
        param.stride_h = param.stride_w = 1;
        for (auto cmode :
             std::vector<Param::ComputeMode>{Param::ComputeMode::DEFAULT,
                                             Param::ComputeMode::FLOAT32}) {
            param.compute_mode = cmode;
            checker.set_param(param)
                    .set_dtype(0, dtype::Float16())
                    .set_dtype(1, dtype::Float16())
                    // Use inferred output dtype.
                    .set_dtype(2, {});
            checker.execs({{n, ic, ih, iw}, {oc, ic, fh, fw}, {}});
        }
    };

    run(1, 3, 128, 128, 5, 3, 3);
    run(1, 8, 8, 7, 11, 3, 1);
}

TEST_F(FALLBACK_MULTI_THREADS, CONVOLUTION_NAIVE_FALLBACK) {
    Checker<Convolution> checker(handle());
    using Param = Convolution::Param;
    checker.set_before_exec_callback(
            AlgoChecker<ConvolutionForward>("FALLBACK_ALGO"));
    Param param;
    auto run = [&](size_t n, size_t group, size_t ic, size_t ih, size_t iw,
                   size_t oc, size_t fh, size_t fw) {
        param.sparse = param::Convolution::Sparse::GROUP;
        param.pad_h = param.pad_w = 1;
        param.stride_h = param.stride_w = 1;
        TensorShape src{n, ic, ih, iw},
                filter{group, oc / group, ic / group, fh, fw};
        checker.set_param(param)
                .set_dtype(0, dtype::Float32())
                .set_dtype(1, dtype::Float32())
                .set_dtype(2, {});
        checker.execs({src, filter, {}});
    };
    run(4, 1, 3, 21, 15, 5, 3, 3);
    run(1, 8, 56, 24, 31, 56, 1, 1);
    run(4, 8, 8, 8, 7, 8, 3, 1);
    run(8, 1, 54, 54, 7, 7, 3, 1);
    run(100, 1, 1, 1, 1, 1, 3, 3);
}

TEST_F(FALLBACK_MULTI_THREADS, CONVOLUTION_NAIVE_ALGO) {
    Checker<Convolution> checker(handle());
    using Param = Convolution::Param;
    checker.set_before_exec_callback(
            AlgoChecker<ConvolutionForward>("NAIVE_ALGO"));
    Param param;
    auto run = [&](size_t n, size_t group, size_t ic, size_t ih, size_t iw,
                   size_t oc, size_t fh, size_t fw) {
        param.sparse = param::Convolution::Sparse::GROUP;
        param.pad_h = param.pad_w = 1;
        param.stride_h = param.stride_w = 1;
        TensorShape src{n, ic, ih, iw},
                filter{group, oc / group, ic / group, fh, fw};
        checker.set_param(param).set_dtype(2, {});
362
        //! float32
363 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
        checker.set_dtype(0, dtype::Float32()).set_dtype(1, dtype::Float32());
        checker.execs({src, filter, {}});
        //! float16
        checker.set_dtype(0, dtype::Float16()).set_dtype(1, dtype::Float16());
        checker.execs({src, filter, {}});
        //! Qint8
        checker.set_dtype(0, dtype::QuantizedS8(3.34f))
                .set_dtype(1, dtype::QuantizedS8(0.32f));
        checker.execs({src, filter, {}});
        //! Quint8
        checker.set_dtype(0, dtype::Quantized8Asymm(3.34f,
                                                    static_cast<uint8_t>(21)))
                .set_dtype(1, dtype::Quantized8Asymm(0.32f,
                                                     static_cast<uint8_t>(15)));
        checker.execs({src, filter, {}});
    };
    run(4, 1, 3, 21, 15, 5, 3, 3);
    run(1, 8, 56, 24, 31, 56, 1, 1);
    run(4, 8, 8, 8, 7, 8, 3, 1);
    run(8, 1, 54, 54, 7, 7, 3, 1);
    run(100, 1, 1, 1, 1, 1, 3, 3);
}

TEST_F(FALLBACK, CONVOLUTION_MATRIX_MUL_SINT8) {
    Checker<Convolution> checker(handle());
    using Param = Convolution::Param;

    Param param;
    param.sparse = param::Convolution::Sparse::DENSE;
    auto run = [&](size_t n, size_t ic, size_t ih, size_t iw, size_t oc,
                   size_t fh, size_t fw) {
        param.pad_h = param.pad_w = 1;
        param.stride_h = param.stride_w = 1;
        checker.set_param(param)
397 398
                .set_dtype(0, dtype::QuantizedS8(0.2f))
                .set_dtype(1, dtype::QuantizedS8(0.2f))
399
                // Use inferred output dtype.
400
                .set_dtype(2, {});
401 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 428 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 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
        checker.execs({{n, ic, ih, iw}, {oc, ic, fh, fw}, {}});
    };

    run(1, 3, 128, 128, 5, 3, 3);
    run(1, 56, 128, 64, 80, 1, 1);
    run(1, 8, 8, 7, 11, 3, 1);
    run(1, 54, 54, 7, 7, 3, 1);
    run(1, 3, 3, 128, 128, 3, 1);
    run(1, 3, 3, 112, 112, 3, 1);
    run(1, 1, 1, 1, 1, 3, 3);
}

TEST_F(FALLBACK, CONVOLUTION_BACKWARD_DATA) {
    Checker<ConvolutionBackwardData> checker(handle());
    using Param = ConvolutionBackwardData::Param;

    Param param;
    auto run = [&](size_t n, size_t ic, size_t oh, size_t ow, size_t oc,
                   size_t fh, size_t fw, size_t stride, size_t padding,
                   size_t dilate = 1, size_t group = 1) {
        param.pad_h = param.pad_w = padding;
        param.stride_h = param.stride_w = stride;
        param.dilate_h = param.dilate_w = dilate;

        TensorLayout diff =
                TensorLayout{{n, oc * group, oh, ow}, dtype::Float32()};
        TensorLayout grad;
        TensorLayout filter;
        if (group == 1) {
            param.sparse = Param::Sparse::DENSE;
            filter = {{oc, ic, fh, fw}, dtype::Float32()};
        } else {
            param.sparse = Param::Sparse::GROUP;
            filter = {{group, oc, ic, fh, fw}, dtype::Float32()};
        }
        // TensorLayout grad;
        {
            auto opr = handle()->create_operator<ConvolutionBackwardData>();
            opr->param() = param;
            opr->deduce_layout(filter, diff, grad);
        }
        checker.set_param(param)
                .set_dtype(0, dtype::Float32())
                .set_dtype(1, dtype::Float32());
        checker.exec(TensorLayoutArray{filter, diff, grad});
    };

    for (auto mode :
         {Param::Mode::CONVOLUTION, Param::Mode::CROSS_CORRELATION}) {
        param.mode = mode;
        run(4, 3, 10, 13, 5, 1, 1, 1, 0, 1, 1);
        run(5, 5, 24, 43, 11, 9, 3, 3, 12, 1, 2);
        run(4, 3, 10, 45, 2, 1, 1, 1, 0, 4, 3);
        run(2, 3, 9, 12, 2, 4, 6, 1, 0, 1, 2);
        run(3, 4, 17, 32, 2, 3, 2, 5, 4, 4, 3);
        run(5, 5, 24, 43, 11, 9, 3, 3, 12, 2, 2);
        run(2, 3, 20, 33, 3, 5, 7, 4, 15, 2, 3);
        run(4, 4, 6, 7, 9, 3, 2, 2, 1, 3, 2);
    }
}

TEST_F(FALLBACK, CONVOLUTION_BACKWARD_DATA_INT8_INT8_INT32) {
    Checker<ConvolutionBackwardData> checker(handle());
    using Param = ConvolutionBackwardData::Param;
    Param param;

    auto run = [&](size_t n, size_t ic, size_t oh, size_t ow, size_t oc,
                   size_t fh, size_t fw, size_t stride, size_t padding,
                   size_t dilate = 1, size_t group = 1) {
        param.pad_h = param.pad_w = padding;
        param.stride_h = param.stride_w = stride;
        param.dilate_h = param.dilate_w = dilate;

        TensorLayout diff =
                TensorLayout{{n, oc * group, oh, ow}, dtype::Int8()};
        TensorLayout grad;
        TensorLayout filter;
        if (group == 1) {
            param.sparse = Param::Sparse::DENSE;
            filter = {{oc, ic, fh, fw}, dtype::Int8()};
        } else {
            param.sparse = Param::Sparse::GROUP;
            filter = {{group, oc, ic, fh, fw}, dtype::Int8()};
        }
        // TensorLayout grad;
        {
            auto opr = handle()->create_operator<ConvolutionBackwardData>();
            opr->param() = param;
            opr->deduce_layout(filter, diff, grad);
        }
        checker.set_param(param)
                .set_dtype(0, dtype::Int8())
                .set_dtype(1, dtype::Int8())
                .set_dtype(2, dtype::Int32());
        checker.exec(TensorLayoutArray{filter, diff, grad});
    };

    for (auto mode :
         {Param::Mode::CONVOLUTION, Param::Mode::CROSS_CORRELATION}) {
        param.mode = mode;
        run(4, 3, 10, 13, 5, 1, 1, 1, 0, 1, 1);
        run(5, 5, 24, 43, 11, 9, 3, 3, 12, 1, 2);
        run(4, 3, 10, 45, 2, 1, 1, 1, 0, 4, 3);
        run(2, 3, 9, 12, 2, 4, 6, 1, 0, 1, 2);
        run(3, 4, 17, 32, 2, 3, 2, 5, 4, 4, 3);
        run(5, 5, 24, 43, 11, 9, 3, 3, 12, 2, 2);
        run(2, 3, 20, 33, 3, 5, 7, 4, 15, 2, 3);
        run(4, 4, 6, 7, 9, 3, 2, 2, 1, 3, 2);
    }
}

TEST_F(FALLBACK, CONVOLUTION_BACKWARD_DATA_SINT8) {
    Checker<ConvolutionBackwardData> checker(handle());
    using Param = ConvolutionBackwardData::Param;
    Param param;

    auto run = [&](size_t n, size_t ic, size_t oh, size_t ow, size_t oc,
                   size_t fh, size_t fw, size_t stride, size_t padding,
                   size_t dilate = 1, size_t group = 1) {
        param.pad_h = param.pad_w = padding;
        param.stride_h = param.stride_w = stride;
        param.dilate_h = param.dilate_w = dilate;

        TensorLayout diff =
                TensorLayout{{n, oc * group, oh, ow}, dtype::QuantizedS8(0.2f)};
        TensorLayout grad;
        TensorLayout filter;
        if (group == 1) {
            param.sparse = Param::Sparse::DENSE;
            filter = {{oc, ic, fh, fw}, dtype::QuantizedS8(0.2f)};
        } else {
            param.sparse = Param::Sparse::GROUP;
            filter = {{group, oc, ic, fh, fw}, dtype::QuantizedS8(0.2f)};
        }
        // TensorLayout grad;
        {
            auto opr = handle()->create_operator<ConvolutionBackwardData>();
            opr->param() = param;
            opr->deduce_layout(filter, diff, grad);
        }
        checker.set_param(param)
                .set_dtype(0, dtype::QuantizedS8(0.2f))
                .set_dtype(1, dtype::QuantizedS8(0.2f))
                .set_dtype(2, {});
        checker.exec(TensorLayoutArray{filter, diff, grad});
    };

    for (auto mode :
         {Param::Mode::CONVOLUTION, Param::Mode::CROSS_CORRELATION}) {
        param.mode = mode;
        run(4, 3, 10, 13, 5, 1, 1, 1, 0, 1, 1);
        run(5, 5, 24, 43, 11, 9, 3, 3, 12, 1, 2);
        run(4, 3, 10, 45, 2, 1, 1, 1, 0, 4, 3);
        run(2, 3, 9, 12, 2, 4, 6, 1, 0, 1, 2);
        run(3, 4, 17, 32, 2, 3, 2, 5, 4, 4, 3);
        run(5, 5, 24, 43, 11, 9, 3, 3, 12, 2, 2);
        run(2, 3, 20, 33, 3, 5, 7, 4, 15, 2, 3);
        run(4, 4, 6, 7, 9, 3, 2, 2, 1, 3, 2);
    }
}

TEST_F(FALLBACK, CONVOLUTION_BACKWARD_DATA_QUINT8) {
    Checker<ConvolutionBackwardData> checker(handle());
    using Param = ConvolutionBackwardData::Param;
    Param param;

    auto run = [&](size_t n, size_t ic, size_t oh, size_t ow, size_t oc,
                   size_t fh, size_t fw, size_t stride, size_t padding,
                   size_t dilate = 1, size_t group = 1) {
        param.pad_h = param.pad_w = padding;
        param.stride_h = param.stride_w = stride;
        param.dilate_h = param.dilate_w = dilate;

        TensorLayout diff =
                TensorLayout{{n, oc * group, oh, ow},
                             dtype::Quantized8Asymm(1.3f, (uint8_t)129)};
        TensorLayout grad;
        TensorLayout filter;
        if (group == 1) {
            param.sparse = Param::Sparse::DENSE;
            filter = {{oc, ic, fh, fw},
                      dtype::Quantized8Asymm(1.2f, (uint8_t)127)};
        } else {
            param.sparse = Param::Sparse::GROUP;
            filter = {{group, oc, ic, fh, fw},
                      dtype::Quantized8Asymm(1.2f, (uint8_t)127)};
        }
        // TensorLayout grad;
        {
            auto opr = handle()->create_operator<ConvolutionBackwardData>();
            opr->param() = param;
            opr->deduce_layout(filter, diff, grad);
        }
        NormalRNG rng(128.f);
        checker.set_param(param)
                .set_dtype(0, dtype::Quantized8Asymm(1.2f, (uint8_t)127))
                .set_dtype(1, dtype::Quantized8Asymm(1.3f, (uint8_t)129))
                .set_dtype(2, {});
        checker.set_rng(0, &rng).set_rng(1, &rng);
        checker.exec(TensorLayoutArray{filter, diff, grad});
    };

    for (auto mode :
         {Param::Mode::CONVOLUTION, Param::Mode::CROSS_CORRELATION}) {
        param.mode = mode;
        run(4, 3, 10, 13, 5, 1, 1, 1, 0, 1, 1);
        run(5, 5, 24, 43, 11, 9, 3, 3, 12, 1, 2);
        run(4, 3, 10, 45, 2, 1, 1, 1, 0, 4, 3);
        run(2, 3, 9, 12, 2, 4, 6, 1, 0, 1, 2);
        run(3, 4, 17, 32, 2, 3, 2, 5, 4, 4, 3);
        run(5, 5, 24, 43, 11, 9, 3, 3, 12, 2, 2);
        run(2, 3, 20, 33, 3, 5, 7, 4, 15, 2, 3);
        run(4, 4, 6, 7, 9, 3, 2, 2, 1, 3, 2);
    }
}

// vim: syntax=cpp.doxygen