conv_bias.cpp 118.7 KB
Newer Older
1 2 3 4
/**
 * \file dnn/test/x86/conv_bias.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6 7 8
 *
 * 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
 */
#include "src/x86/utils.h"
#include "test/x86/fixture.h"

#include "megdnn/opr_param_defs.h"
#include "megdnn/oprs.h"
#include "test/common/benchmarker.h"
#include "test/common/checker.h"
#include "test/common/conv_bias.h"
#include "test/common/rng.h"
#include "test/common/tensor.h"
#include "test/common/workspace_wrapper.h"
namespace megdnn {
namespace test {

TEST_F(X86, CONV_BIAS_FORWARD) {
    using namespace conv_bias;
    std::vector<TestArg> args = get_args();
    Checker<ConvBiasForward> checker(handle());
    NormalRNG default_rng;
    ConstValue const_val;
    for (auto&& arg : args) {
        checker.set_dtype(0, dtype::Float32())
                .set_dtype(1, dtype::Float32())
                .set_dtype(2, dtype::Float32())
                .set_rng(0, &default_rng)
                .set_rng(1, &default_rng)
                .set_rng(2, &default_rng)
                .set_epsilon(1e-3)
                .set_param(arg.param)
                .execs({arg.src, arg.filter, arg.bias, {}, {}});
    }
}
44

M
Megvii Engine Team 已提交
45 46
static void avx2_chanwise_direct_int8x8x32(
        Handle* handle, uint32_t stride, const char* algo) {
47 48 49 50 51 52 53 54
    using namespace conv_bias;
    std::vector<TestArg> args;

    auto run = [&](size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
55 56
        param.stride_h = stride;
        param.stride_w = stride;
57 58 59 60 61 62
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        param.sparse = param::ConvBias::Sparse::GROUP;
        //! no bias
M
Megvii Engine Team 已提交
63 64 65
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{ic, 1, 1, kernel, kernel},
                TensorShape{});
66
        //! bias channel
M
Megvii Engine Team 已提交
67 68 69
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{ic, 1, 1, kernel, kernel},
                TensorShape{1, ic, 1, 1});
70 71 72 73 74 75 76 77 78 79
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t pad : {0, 1})
            for (size_t ic : {1, 5, 17, 20})
                for (size_t h : {7, 16, 38, 40})
                    for (size_t w : {16, 25, 40, 55})
                        for (NonlineMode nonline_mode : {NonlineMode::IDENTITY})
                            run(ic, w, h, kernel, pad, nonline_mode);

80
    Checker<ConvBias> checker(handle);
81 82 83 84 85 86 87 88 89 90
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Int8())
            .set_dtype(1, dtype::Int8())
            .set_dtype(2, dtype::Int32())
            .set_dtype(4, dtype::Int32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3);
    checker.set_before_exec_callback(
91
            conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(algo));
92
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
93
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
94 95 96
    }
}

97
TEST_F(X86_MULTI_THREADS, AVX2_CHANWISE_DIRECT_STRIDE1_INT8x8x32) {
M
Megvii Engine Team 已提交
98 99
    avx2_chanwise_direct_int8x8x32(
            handle(), 1, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE1");
100 101 102
}

TEST_F(X86_MULTI_THREADS, AVX2_CHANWISE_DIRECT_STRIDE2_INT8x8x32) {
M
Megvii Engine Team 已提交
103 104
    avx2_chanwise_direct_int8x8x32(
            handle(), 2, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE2");
105 106
}

M
Megvii Engine Team 已提交
107 108
static void avx2_chanwise_direct_quantizeds32(
        Handle* handle, uint32_t stride, const char* algo) {
109 110 111 112 113 114 115 116
    using namespace conv_bias;
    std::vector<TestArg> args;

    auto run = [&](size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
117 118
        param.stride_h = stride;
        param.stride_w = stride;
119 120 121 122 123 124
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        param.sparse = param::ConvBias::Sparse::GROUP;
        //! no bias
M
Megvii Engine Team 已提交
125 126 127
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{ic, 1, 1, kernel, kernel},
                TensorShape{});
128
        //! bias channel
M
Megvii Engine Team 已提交
129 130 131
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{ic, 1, 1, kernel, kernel},
                TensorShape{1, ic, 1, 1});
132 133 134 135 136 137 138 139 140 141
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t pad : {0, 1})
            for (size_t ic : {1, 3, 5, 7, 17})
                for (size_t h : {10, 17, 25, 30})
                    for (size_t w : {19, 28, 58, 168})
                        for (NonlineMode nonline_mode : {NonlineMode::IDENTITY})
                            run(ic, w, h, kernel, pad, nonline_mode);

142
    Checker<ConvBias> checker(handle);
143 144 145 146 147 148 149 150 151 152
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::QuantizedS8(2.5f))
            .set_dtype(1, dtype::QuantizedS8(2.5f))
            .set_dtype(2, dtype::QuantizedS32(6.25f))
            .set_dtype(4, {})
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3);
    checker.set_before_exec_callback(
153
            conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(algo));
154
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
155
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
156 157 158
    }
}

159 160 161 162 163 164 165 166 167 168
TEST_F(X86_MULTI_THREADS, AVX2_CHANWISE_DIRECT_STRIDE1_QuantizedS32) {
    avx2_chanwise_direct_quantizeds32(
            handle(), 1, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE1");
}

TEST_F(X86_MULTI_THREADS, AVX2_CHANWISE_DIRECT_STRIDE2_QuantizedS32) {
    avx2_chanwise_direct_quantizeds32(
            handle(), 2, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE2");
}

M
Megvii Engine Team 已提交
169 170
static void avx2_chanwise_direct_quantizeds8x8x8(
        Handle* handle, uint32_t stride, const char* algo) {
171 172 173 174 175 176 177 178
    using namespace conv_bias;
    std::vector<TestArg> args;

    auto run = [&](size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
179 180
        param.stride_h = stride;
        param.stride_w = stride;
181 182 183 184 185 186
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        param.sparse = param::ConvBias::Sparse::GROUP;
        //! no bias
M
Megvii Engine Team 已提交
187 188 189
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{ic, 1, 1, kernel, kernel},
                TensorShape{});
190
        //! bias channel
M
Megvii Engine Team 已提交
191 192 193
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{ic, 1, 1, kernel, kernel},
                TensorShape{1, ic, 1, 1});
194 195 196 197 198 199 200 201 202 203 204 205
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t pad : {0, 1})
            for (size_t ic : {1, 3, 5, 7, 17})
                for (size_t h : {10, 15, 17, 30})
                    for (size_t w : {19, 28, 58, 168})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::IDENTITY, NonlineMode::H_SWISH,
                              NonlineMode::RELU})
                            run(ic, w, h, kernel, pad, nonline_mode);

206
    Checker<ConvBias> checker(handle);
207 208 209 210 211 212 213 214 215 216
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::QuantizedS8(2.5f))
            .set_dtype(1, dtype::QuantizedS8(2.5f))
            .set_dtype(2, dtype::QuantizedS32(6.25f))
            .set_dtype(4, dtype::QuantizedS8(60.25f))
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3);
    checker.set_before_exec_callback(
217
            conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(algo));
218
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
219
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
220 221 222
    }
}

223 224 225 226 227 228 229 230 231 232
TEST_F(X86_MULTI_THREADS, AVX2_CHANWISE_DIRECT_STRIDE1_QuantizedS8x8x8) {
    avx2_chanwise_direct_quantizeds8x8x8(
            handle(), 1, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE1");
}

TEST_F(X86_MULTI_THREADS, AVX2_CHANWISE_DIRECT_STRIDE2_QuantizedS8x8x8) {
    avx2_chanwise_direct_quantizeds8x8x8(
            handle(), 2, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE2");
}

233 234 235 236
TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE1_INT8x8x32) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
237 238
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
239 240 241 242 243 244 245 246 247 248 249
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        param.sparse = param::ConvBias::Sparse::DENSE;
        //! no bias
M
Megvii Engine Team 已提交
250 251 252
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
253 254 255

        param.sparse = param::ConvBias::Sparse::GROUP;
        //! no bias
M
Megvii Engine Team 已提交
256 257 258
        args.emplace_back(
                param, TensorShape{2, 2 * ic, h, w},
                TensorShape{2, oc / 2, ic, kernel, kernel}, TensorShape{});
259 260 261 262 263 264 265 266
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t pad : {0, 1})
            for (size_t oc : {4, 8, 13, 16, 24})
                for (size_t ic : {2, 3, 7, 10})
                    for (size_t h : {10, 11})
                        for (size_t w : {8, 10})
M
Megvii Engine Team 已提交
267
                            for (NonlineMode nonline_mode : {NonlineMode::IDENTITY})
268 269 270 271 272 273 274 275 276 277 278 279
                                run(oc, ic, w, h, kernel, pad, nonline_mode);

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Int8())
            .set_dtype(1, dtype::Int8())
            .set_dtype(2, dtype::Int32())
            .set_dtype(4, dtype::Int32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3);
M
Megvii Engine Team 已提交
280 281
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1"));
282
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
283
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
284 285 286 287 288 289
    }
}
TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE1_QuantizedS32) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
290 291
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
292 293 294 295 296 297 298 299 300 301 302
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        param.sparse = param::ConvBias::Sparse::DENSE;
        //! no bias
M
Megvii Engine Team 已提交
303 304 305
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
306 307 308

        param.sparse = param::ConvBias::Sparse::GROUP;
        //! no bias
M
Megvii Engine Team 已提交
309 310 311
        args.emplace_back(
                param, TensorShape{2, 2 * ic, h, w},
                TensorShape{2, oc / 2, ic, kernel, kernel}, TensorShape{});
312 313 314 315 316 317 318 319
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t pad : {0, 1})
            for (size_t oc : {4, 8, 13, 16, 24})
                for (size_t ic : {2, 3, 7, 10})
                    for (size_t h : {10, 11})
                        for (size_t w : {8, 10})
M
Megvii Engine Team 已提交
320
                            for (NonlineMode nonline_mode : {NonlineMode::IDENTITY})
321 322 323 324 325 326 327 328 329 330 331 332
                                run(oc, ic, w, h, kernel, pad, nonline_mode);

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::QuantizedS8(2.5f))
            .set_dtype(1, dtype::QuantizedS8(2.5f))
            .set_dtype(2, dtype::QuantizedS32(6.25f))
            .set_dtype(4, {})
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3);
M
Megvii Engine Team 已提交
333 334
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1"));
335
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
336
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
337 338 339 340 341 342 343
    }
}

TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE1_S8S8S8) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
344 345
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
346 347 348 349 350 351 352 353 354 355 356
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        param.sparse = param::ConvBias::Sparse::DENSE;
        //! no bias
M
Megvii Engine Team 已提交
357 358 359
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
360
        //! bias channel
M
Megvii Engine Team 已提交
361 362 363
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
364 365 366

        param.sparse = param::ConvBias::Sparse::GROUP;
        //! no bias
M
Megvii Engine Team 已提交
367 368 369
        args.emplace_back(
                param, TensorShape{2, 2 * ic, h, w},
                TensorShape{2, oc / 2, ic, kernel, kernel}, TensorShape{});
370
        //! bias channel
M
Megvii Engine Team 已提交
371 372 373
        args.emplace_back(
                param, TensorShape{2, 2 * ic, h, w},
                TensorShape{2, oc / 2, ic, kernel, kernel}, TensorShape{1, oc, 1, 1});
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t pad : {0, 1})
            for (size_t oc : {4, 8, 14, 16, 24})
                for (size_t ic : {2, 3, 7, 10})
                    for (size_t h : {10, 11})
                        for (size_t w : {8, 10})
                            for (NonlineMode nonline_mode :
                                 {NonlineMode::IDENTITY, NonlineMode::RELU,
                                  NonlineMode::H_SWISH})
                                run(oc, ic, w, h, kernel, pad, nonline_mode);

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::QuantizedS8(2.5f))
            .set_dtype(1, dtype::QuantizedS8(2.5f))
            .set_dtype(2, dtype::QuantizedS32(6.25f))
            .set_dtype(4, dtype::QuantizedS8(60.25f))
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3);
M
Megvii Engine Team 已提交
397 398
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1"));
399
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
400
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
401 402 403 404 405 406 407
    }
}

TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE2_INT8x8x32) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
408 409
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
410 411 412 413 414 415 416 417 418 419 420
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 2;
        param.stride_w = 2;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        param.sparse = param::ConvBias::Sparse::DENSE;
        //! no bias
M
Megvii Engine Team 已提交
421 422 423
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
424 425 426

        param.sparse = param::ConvBias::Sparse::GROUP;
        //! no bias
M
Megvii Engine Team 已提交
427 428 429
        args.emplace_back(
                param, TensorShape{2, 2 * ic, h, w},
                TensorShape{2, oc / 2, ic, kernel, kernel}, TensorShape{});
430 431 432 433 434 435 436 437
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t pad : {0, 1, 2, 5})
            for (size_t oc : {4, 8, 13, 16, 24})
                for (size_t ic : {2, 3, 7, 10})
                    for (size_t h : {10, 11})
                        for (size_t w : {8, 10, 20})
M
Megvii Engine Team 已提交
438
                            for (NonlineMode nonline_mode : {NonlineMode::IDENTITY})
439 440 441 442 443 444 445 446 447 448 449 450
                                run(oc, ic, w, h, kernel, pad, nonline_mode);

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Int8())
            .set_dtype(1, dtype::Int8())
            .set_dtype(2, dtype::Int32())
            .set_dtype(4, dtype::Int32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3);
M
Megvii Engine Team 已提交
451 452
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2"));
453
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
454
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
455 456 457 458 459 460
    }
}
TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE2_QuantizedS32) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
461 462
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
463 464 465 466 467 468 469 470 471 472 473
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 2;
        param.stride_w = 2;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        param.sparse = param::ConvBias::Sparse::DENSE;
        //! no bias
M
Megvii Engine Team 已提交
474 475 476
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
477 478 479

        param.sparse = param::ConvBias::Sparse::GROUP;
        //! no bias
M
Megvii Engine Team 已提交
480 481 482
        args.emplace_back(
                param, TensorShape{2, 2 * ic, h, w},
                TensorShape{2, oc / 2, ic, kernel, kernel}, TensorShape{});
483 484 485 486 487 488 489 490
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t pad : {0, 1, 3, 5})
            for (size_t oc : {4, 8, 13, 16, 24})
                for (size_t ic : {2, 3, 7, 10})
                    for (size_t h : {10, 11})
                        for (size_t w : {8, 10, 19})
M
Megvii Engine Team 已提交
491
                            for (NonlineMode nonline_mode : {NonlineMode::IDENTITY})
492 493 494 495 496 497 498 499 500 501 502 503
                                run(oc, ic, w, h, kernel, pad, nonline_mode);

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::QuantizedS8(2.5f))
            .set_dtype(1, dtype::QuantizedS8(2.5f))
            .set_dtype(2, dtype::QuantizedS32(6.25f))
            .set_dtype(4, {})
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3);
M
Megvii Engine Team 已提交
504 505
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2"));
506
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
507
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
508 509 510 511 512 513 514
    }
}

TEST_F(X86_MULTI_THREADS, AVX2_CONV_BIAS_DIRECT_STRIDE2_S8S8S8) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
515 516
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
517 518 519 520 521 522 523 524 525 526 527
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 2;
        param.stride_w = 2;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        param.sparse = param::ConvBias::Sparse::DENSE;
        //! no bias
M
Megvii Engine Team 已提交
528 529 530
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
531
        //! bias channel
M
Megvii Engine Team 已提交
532 533 534
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
535 536 537

        param.sparse = param::ConvBias::Sparse::GROUP;
        //! no bias
M
Megvii Engine Team 已提交
538 539 540
        args.emplace_back(
                param, TensorShape{2, 2 * ic, h, w},
                TensorShape{2, oc / 2, ic, kernel, kernel}, TensorShape{});
541
        //! bias channel
M
Megvii Engine Team 已提交
542 543 544
        args.emplace_back(
                param, TensorShape{2, 2 * ic, h, w},
                TensorShape{2, oc / 2, ic, kernel, kernel}, TensorShape{1, oc, 1, 1});
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t pad : {0, 1, 3, 5})
            for (size_t oc : {4, 8, 14, 16, 24})
                for (size_t ic : {2, 3, 7, 10})
                    for (size_t h : {10, 11})
                        for (size_t w : {8, 10, 18})
                            for (NonlineMode nonline_mode :
                                 {NonlineMode::IDENTITY, NonlineMode::RELU,
                                  NonlineMode::H_SWISH})
                                run(oc, ic, w, h, kernel, pad, nonline_mode);

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::QuantizedS8(2.5f))
            .set_dtype(1, dtype::QuantizedS8(2.5f))
            .set_dtype(2, dtype::QuantizedS32(6.25f))
            .set_dtype(4, dtype::QuantizedS8(60.25f))
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3);
M
Megvii Engine Team 已提交
568 569
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2"));
570
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
571
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
572 573 574
    }
}

575
TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_STRIDE1_DENSE) {
576 577 578
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
579 580
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
581 582 583 584 585 586 587 588 589 590
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
591 592 593
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
594
        //! bias channel
M
Megvii Engine Team 已提交
595 596 597
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
598
        //! bias
M
Megvii Engine Team 已提交
599 600 601 602 603
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{
                        2, oc, (h + param.pad_h * 2 - kernel) + 1,
                        (w + param.pad_w * 2 - kernel) + 1});
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    };

    for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 21, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::RELU, NonlineMode::SIGMOID,
                              NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Float32())
            .set_dtype(1, dtype::Float32())
            .set_dtype(2, dtype::Float32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng);
M
Megvii Engine Team 已提交
625 626
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP"));
627
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
628
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
629 630 631
    }
}

632
TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_STRIDE1_GROUP) {
633 634 635
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
636 637
    auto run = [&](size_t group, size_t channel, size_t w, size_t h, size_t kernel,
                   size_t p, NonlineMode nonline_mode) {
638 639 640 641 642 643 644 645
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;
646
        param.sparse = param::ConvBias::Sparse::GROUP;
647 648

        //! no bias
649 650 651 652
        args.emplace_back(
                param, TensorShape{1, channel, h, w},
                TensorShape{group, channel / group, channel / group, kernel, kernel},
                TensorShape{});
653
        //! bias channel
M
Megvii Engine Team 已提交
654 655 656 657
        args.emplace_back(
                param, TensorShape{2, channel, h, w},
                TensorShape{group, channel / group, channel / group, kernel, kernel},
                TensorShape{1, channel, 1, 1});
658
        //! bias
659 660
        args.emplace_back(
                param, TensorShape{2, channel, h, w},
M
Megvii Engine Team 已提交
661 662 663 664
                TensorShape{group, channel / group, channel / group, kernel, kernel},
                TensorShape{
                        2, channel, (h + param.pad_h * 2 - kernel) + 1,
                        (w + param.pad_w * 2 - kernel) + 1});
665 666 667
    };

    for (size_t kernel : {1, 2, 3, 4, 5, 6, 7})
668 669
        for (size_t channel : {4, 8, 16})
            for (size_t group : {1, 2, 4})
670 671 672 673 674
                for (size_t p : {0, 2})
                    for (size_t size : {20, 21, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::RELU, NonlineMode::SIGMOID,
                              NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
M
Megvii Engine Team 已提交
675
                            run(group, channel, size, size, kernel, p, nonline_mode);
676 677 678 679 680 681 682 683 684 685
                        }

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Float32())
            .set_dtype(1, dtype::Float32())
            .set_dtype(2, dtype::Float32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng);
M
Megvii Engine Team 已提交
686 687
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP"));
688
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
689
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
690 691 692
    }
}

693
TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_STRIDE2_DENSE) {
694 695 696
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
697 698
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
699 700 701 702 703 704 705 706 707 708
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 2;
        param.stride_w = 2;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
709 710 711
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 21, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::RELU, NonlineMode::SIGMOID,
                              NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Float32())
            .set_dtype(1, dtype::Float32())
            .set_dtype(2, dtype::Float32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng);
M
Megvii Engine Team 已提交
733 734
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_STRIDE2_LARGE_GROUP"));
735
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
736
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
737
    }
738 739 740 741 742 743
}

TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_STRIDE2_GROUP) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
744 745
    auto run = [&](size_t group, size_t channel, size_t w, size_t h, size_t kernel,
                   size_t p, NonlineMode nonline_mode) {
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 2;
        param.stride_w = 2;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;
        param.sparse = param::ConvBias::Sparse::GROUP;

        //! no bias
        args.emplace_back(
                param, TensorShape{1, channel, h, w},
                TensorShape{group, channel / group, channel / group, kernel, kernel},
                TensorShape{});
        //! bias channel
M
Megvii Engine Team 已提交
762 763 764 765
        args.emplace_back(
                param, TensorShape{2, channel, h, w},
                TensorShape{group, channel / group, channel / group, kernel, kernel},
                TensorShape{1, channel, 1, 1});
766 767 768
        //! bias
        args.emplace_back(
                param, TensorShape{2, channel, h, w},
M
Megvii Engine Team 已提交
769 770 771 772
                TensorShape{group, channel / group, channel / group, kernel, kernel},
                TensorShape{
                        2, channel, (h + param.pad_h * 2 - kernel) / 2 + 1,
                        (w + param.pad_w * 2 - kernel) / 2 + 1});
773 774 775 776 777 778 779 780 781 782
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t channel : {4, 8, 16})
            for (size_t group : {1, 2, 4})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 21, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::RELU, NonlineMode::SIGMOID,
                              NonlineMode::H_SWISH, NonlineMode::IDENTITY}) {
M
Megvii Engine Team 已提交
783
                            run(group, channel, size, size, kernel, p, nonline_mode);
784 785 786 787 788 789 790 791 792 793
                        }

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Float32())
            .set_dtype(1, dtype::Float32())
            .set_dtype(2, dtype::Float32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng);
M
Megvii Engine Team 已提交
794 795
    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBiasForward>(
            "X86_CONV_BIAS_DIRECT_STRIDE2_LARGE_GROUP"));
796
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
797
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
798 799 800
    }
}

801
TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_INT8X8X32) {
802 803 804
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
805 806
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
807 808 809 810 811 812 813 814 815 816
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
817 818 819 820 821 822 823 824 825
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, (h + 2 * p - kernel) + 1, (h + 2 * p - kernel) + 1});
826 827
    };

828
    for (size_t kernel : {2, 3, 4, 5, 6, 7})
829 830 831 832
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 21, 24})
M
Megvii Engine Team 已提交
833
                        for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
834 835 836
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }
    //! test OC block
837
    run(2046, 1, 8, 8, 2, 0, NonlineMode::IDENTITY);
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

    Checker<ConvBias> checker(handle());
    UniformIntRNG rng{-50, 50};
#define cb(algo_name)                                                          \
    checker.set_before_exec_callback(                                          \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));              \
    checker.set_dtype(0, dtype::Int8());                                       \
    checker.set_dtype(1, dtype::Int8());                                       \
    checker.set_dtype(2, dtype::Int32());                                      \
    checker.set_dtype(4, dtype::Int32());                                      \
    for (auto&& arg : args) {                                                  \
        checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}}); \
    }                                                                          \
    for (auto&& arg : args) {                                                  \
        checker.set_dtype(0, dtype::QuantizedS8(2.5f))                         \
                .set_dtype(1, dtype::QuantizedS8(2.5f))                        \
                .set_dtype(2, dtype::QuantizedS32(6.25f))                      \
                .set_dtype(4, {})                                              \
                .set_rng(0, &rng)                                              \
                .set_rng(1, &rng)                                              \
                .set_rng(2, &rng)                                              \
                .set_param(arg.param)                                          \
                .execs({arg.src, arg.filter, {}, {}, {}});                     \
    }
862 863 864 865 866 867 868 869 870 871
#define cb2(algo_name)                                                         \
    checker.set_before_exec_callback(                                          \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));              \
    checker.set_dtype(0, dtype::Int8());                                       \
    checker.set_dtype(1, dtype::Int8());                                       \
    checker.set_dtype(2, dtype::Int16());                                      \
    checker.set_dtype(4, dtype::Int16());                                      \
    for (auto&& arg : args) {                                                  \
        checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}}); \
    }
872

873
#if MEGDNN_X86_WITH_MKL_DNN
874 875 876 877 878 879 880 881 882 883 884 885
    if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
    }
#endif
#if MEGDNN_X86_WITH_VNNI
    if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
    }
#endif
    if (megdnn::x86::is_supported(x86::SIMDType::AVX2)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
        cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2");
886
        cb2("IM2COLMATMUL:X86_INT8X8X16_AVX2");
887 888 889
    }
    if (::megdnn::x86::is_supported(::megdnn::x86::SIMDType::SSE4_2)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_SSE_4X8X2");
890
        cb2("IM2COLMATMUL:X86_INT8X8X16_SSE");
891 892 893
    }

#undef cb
894
#undef cb2
895 896
}

897 898 899 900
TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_INT8X8X32_FILTER_PREPROCESS) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
901 902
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
903 904 905 906 907 908 909 910 911 912
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
913 914 915
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
916 917 918 919 920 921 922
    };

    for (size_t kernel : {2, 3, 4, 5, 6, 7})
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 21, 24})
M
Megvii Engine Team 已提交
923
                        for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }
    //! test OC block
    run(2046, 1, 8, 8, 2, 0, NonlineMode::IDENTITY);

    Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
            handle());
    UniformIntRNG rng{-50, 50};
#define cb(algo_name)                                                          \
    checker.set_before_exec_callback(                                          \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));              \
    checker.set_dtype(0, dtype::Int8());                                       \
    checker.set_dtype(1, dtype::Int8());                                       \
    checker.set_dtype(2, dtype::Int32());                                      \
    checker.set_dtype(4, dtype::Int32());                                      \
    for (auto&& arg : args) {                                                  \
        checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}}); \
    }                                                                          \
    for (auto&& arg : args) {                                                  \
        checker.set_dtype(0, dtype::QuantizedS8(2.5f))                         \
                .set_dtype(1, dtype::QuantizedS8(2.5f))                        \
                .set_dtype(2, dtype::QuantizedS32(6.25f))                      \
                .set_dtype(4, {})                                              \
                .set_rng(0, &rng)                                              \
                .set_rng(1, &rng)                                              \
                .set_rng(2, &rng)                                              \
                .set_param(arg.param)                                          \
                .execs({arg.src, arg.filter, {}, {}, {}});                     \
    }
#define cb2(algo_name)                                                         \
    checker.set_before_exec_callback(                                          \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));              \
    checker.set_dtype(0, dtype::Int8());                                       \
    checker.set_dtype(1, dtype::Int8());                                       \
    checker.set_dtype(2, dtype::Int16());                                      \
    checker.set_dtype(4, dtype::Int16());                                      \
    for (auto&& arg : args) {                                                  \
        checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}}); \
    }

#if MEGDNN_X86_WITH_MKL_DNN
    if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
966
        cb("IM2COLMATMUL");
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
    }
#endif
#if MEGDNN_X86_WITH_VNNI
    if (megdnn::x86::is_supported(x86::SIMDType::VNNI)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
    }
#endif
    if (megdnn::x86::is_supported(x86::SIMDType::AVX2)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
        cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2");
        cb2("IM2COLMATMUL:X86_INT8X8X16_AVX2");
    }
    if (::megdnn::x86::is_supported(::megdnn::x86::SIMDType::SSE4_2)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_SSE_4X8X2");
        cb2("IM2COLMATMUL:X86_INT8X8X16_SSE");
    }

#undef cb
#undef cb2
}

988 989 990 991
TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
992 993
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
994 995 996 997 998 999 1000 1001 1002 1003
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{
                        1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
                        (w + 2 * p - kernel) / param.stride_w + 1});
1015 1016
    };

1017
    for (size_t kernel : {2, 3, 4, 5, 6, 7})
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8, 16, 300})
                for (size_t p : {0, 2})
                    for (size_t size : {8, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::IDENTITY, NonlineMode::RELU}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
    Checker<ConvBias> checker(handle());
M
Megvii Engine Team 已提交
1029 1030 1031 1032 1033
#define cb(algo_name)                                                                \
    checker.set_before_exec_callback(                                                \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));                    \
    for (auto&& arg : args) {                                                        \
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
1034 1035
    }

1036
#if MEGDNN_X86_WITH_MKL || MEGDNN_X86_WITH_OPENBLAS
1037 1038 1039 1040 1041 1042
    cb("IM2COLMATMUL:X86_F32_BLAS");
#endif

#undef cb
}

1043 1044 1045 1046 1047
#if MEGDNN_X86_WITH_MKL || MEGDNN_X86_WITH_OPENBLAS
TEST_F(X86, CONV_BIAS_IM2COLMATMUL_FP32) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
1048 1049
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{
                        1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
                        (w + 2 * p - kernel) / param.stride_w + 1});
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    };

    for (size_t kernel : {2, 3, 4, 5, 6, 7})
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8, 16, 300})
                for (size_t p : {0, 2})
                    for (size_t size : {8, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::IDENTITY, NonlineMode::RELU}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
    Checker<ConvBias> checker(handle());
M
Megvii Engine Team 已提交
1085 1086 1087 1088 1089
#define cb(algo_name)                                                                \
    checker.set_before_exec_callback(                                                \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));                    \
    for (auto&& arg : args) {                                                        \
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
1090 1091 1092 1093 1094 1095
    }

    cb("IM2COLMATMUL:X86_F32_BLAS");

#undef cb
}
1096 1097 1098 1099 1100

TEST_F(X86, CONV_BIAS_IM2COLMATMUL_FP32_NOPACK_PREPROCESS) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
1101 1102
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{
                        1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
                        (w + 2 * p - kernel) / param.stride_w + 1});
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
    };

    for (size_t kernel : {2, 3, 4, 5, 6, 7})
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8, 16, 300})
                for (size_t p : {0, 2})
                    for (size_t size : {8, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::IDENTITY, NonlineMode::RELU}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);

    Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
            handle());
M
Megvii Engine Team 已提交
1140 1141 1142 1143 1144
#define cb(algo_name)                                                                \
    checker.set_before_exec_callback(                                                \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));                    \
    for (auto&& arg : args) {                                                        \
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
1145 1146 1147 1148 1149 1150
    }
    cb("IM2COLMATMUL:X86_F32_BLAS");

#undef cb
}

1151 1152
#endif

1153 1154 1155
TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_6x16) {
    using namespace conv_bias;
    std::vector<TestArg> args;
1156 1157
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{
                        1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
                        (w + 2 * p - kernel) / param.stride_w + 1});
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
    };

    for (size_t kernel : {2, 3, 4, 5, 6, 7})
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8, 16, 300})
                for (size_t p : {0, 2})
                    for (size_t size : {8, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::IDENTITY, NonlineMode::RELU}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
    Checker<ConvBias> checker(handle());

1194 1195 1196 1197 1198 1199
#define cb(algo_name)                                                                \
    checker.set_before_exec_callback(                                                \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));                    \
    for (auto&& arg : args) {                                                        \
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
    }
1200 1201 1202
    cb("IM2COLMATMUL:X86_F32_6x16:192");
}

1203
#if MEGDNN_X86_WITH_MKL && SUPPORT_MKL_PACKED_GEMM
1204 1205 1206 1207
TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_PACKA) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
1208 1209
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{
                        1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
                        (w + 2 * p - kernel) / param.stride_w + 1});
1231
        param.sparse = param::ConvBias::Sparse::GROUP;
M
Megvii Engine Team 已提交
1232 1233 1234 1235 1236 1237
        args.emplace_back(
                param, TensorShape{1, 2 * ic, h, w},
                TensorShape{2, oc, ic, kernel, kernel}, TensorShape{});
        args.emplace_back(
                param, TensorShape{1, 2 * ic, h, w},
                TensorShape{2, oc, ic, kernel, kernel}, TensorShape{1, oc * 2, 1, 1});
1238 1239 1240 1241

        args.emplace_back(
                param, TensorShape{1, 2 * ic, h, w},
                TensorShape{2, oc, ic, kernel, kernel},
M
Megvii Engine Team 已提交
1242 1243 1244
                TensorShape{
                        1, 2 * oc, (h + 2 * param.pad_h - kernel) / 1 + 1,
                        (w + 2 * param.pad_w - kernel) / 1 + 1});
1245 1246
    };

1247
    for (size_t kernel : {2, 3, 4, 5, 6, 7})
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8, 16})
                for (size_t p : {0, 1})
                    for (size_t size : {8, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::IDENTITY, NonlineMode::RELU}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
    Checker<ConvBias> checker(handle());
M
Megvii Engine Team 已提交
1259 1260 1261 1262 1263
#define cb(algo_name)                                                                \
    checker.set_before_exec_callback(                                                \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));                    \
    for (auto&& arg : args) {                                                        \
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
1264 1265 1266 1267 1268 1269
    }

    cb("IM2COLMATMUL:X86_F32_MKL_PACKA:192");

#undef cb
}
1270

1271 1272 1273 1274
TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_FP32_PACKA_FILTER_PREPROCESS) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
1275 1276
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{
                        1, oc, (h + 2 * p - kernel) / param.stride_h + 1,
                        (w + 2 * p - kernel) / param.stride_w + 1});
1298
        param.sparse = param::ConvBias::Sparse::GROUP;
M
Megvii Engine Team 已提交
1299 1300 1301 1302 1303 1304
        args.emplace_back(
                param, TensorShape{1, 2 * ic, h, w},
                TensorShape{2, oc, ic, kernel, kernel}, TensorShape{});
        args.emplace_back(
                param, TensorShape{1, 2 * ic, h, w},
                TensorShape{2, oc, ic, kernel, kernel}, TensorShape{1, oc * 2, 1, 1});
1305 1306 1307 1308

        args.emplace_back(
                param, TensorShape{1, 2 * ic, h, w},
                TensorShape{2, oc, ic, kernel, kernel},
M
Megvii Engine Team 已提交
1309 1310 1311
                TensorShape{
                        1, 2 * oc, (h + 2 * param.pad_h - kernel) / 1 + 1,
                        (w + 2 * param.pad_w - kernel) / 1 + 1});
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
    };

    for (size_t kernel : {2, 3, 4, 5, 6, 7})
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8, 16})
                for (size_t p : {0, 1})
                    for (size_t size : {8, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::IDENTITY, NonlineMode::RELU}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
    Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
            handle());
M
Megvii Engine Team 已提交
1327 1328 1329 1330 1331
#define cb(algo_name)                                                                \
    checker.set_before_exec_callback(                                                \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));                    \
    for (auto&& arg : args) {                                                        \
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}}); \
1332 1333 1334 1335 1336 1337 1338
    }

    cb("IM2COLMATMUL:X86_F32_MKL_PACKA:192");

#undef cb
}

1339 1340
/**************************** Conv1x1 PackA *************************/
namespace {
M
Megvii Engine Team 已提交
1341 1342 1343
void checker_conv_bias(
        std::vector<conv_bias::TestArg> args, Handle* handle, RNG* rng, float epsilon,
        DType type0, DType type1, DType type2, DType type3, const char* algo_name) {
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
    using namespace conv_bias;

    Checker<ConvBias> checker(handle);
    checker.set_before_exec_callback(
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
    checker.set_dtype(0, type0);
    checker.set_dtype(1, type1);
    checker.set_dtype(2, type2);
    checker.set_dtype(4, type3);
    checker.set_epsilon(epsilon);
    if (NULL != rng) {
        checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
    }
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
1358
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
1359 1360
    }
}
1361

M
Megvii Engine Team 已提交
1362 1363 1364
void checker_conv_bias_preprocess(
        std::vector<conv_bias::TestArg> args, Handle* handle, RNG* rng, float epsilon,
        DType type0, DType type1, DType type2, DType type3, const char* algo_name) {
1365 1366
    using namespace conv_bias;

M
Megvii Engine Team 已提交
1367
    Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(handle);
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
    checker.set_before_exec_callback(
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
    checker.set_dtype(0, type0);
    checker.set_dtype(1, type1);
    checker.set_dtype(2, type2);
    checker.set_dtype(4, type3);
    checker.set_epsilon(epsilon);
    if (NULL != rng) {
        checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
    }
    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
1379
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
1380 1381 1382
    }
}

1383 1384 1385 1386 1387 1388 1389 1390 1391
}  // namespace

#if MEGDNN_X86_WITH_MKL
TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_PACKA) {
    using namespace conv_bias;
    std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
    check_conv_bias(args, handle(), "CONV1x1:X86_F32_MKL_PACKA:24");
}

1392 1393 1394
TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_PACKA_PREPROCESS) {
    using namespace conv_bias;
    std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
M
Megvii Engine Team 已提交
1395 1396 1397
    checker_conv_bias_preprocess(
            args, handle(), nullptr, 0.001, dtype::Float32{}, dtype::Float32{},
            dtype::Float32{}, dtype::Float32{}, "CONV1x1:X86_F32_MKL_PACKA:24");
1398 1399
}

1400 1401 1402 1403 1404
TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_BLAS) {
    using namespace conv_bias;
    std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
    check_conv_bias(args, handle(), "CONV1x1:X86_F32_BLAS:48");
}
1405 1406 1407 1408

TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_BLAS_NOPACK_REPROCESS) {
    using namespace conv_bias;
    std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
M
Megvii Engine Team 已提交
1409 1410 1411
    checker_conv_bias_preprocess(
            args, handle(), nullptr, 0.001, dtype::Float32{}, dtype::Float32{},
            dtype::Float32{}, dtype::Float32{}, "CONV1x1:X86_F32_BLAS:24");
1412
}
1413 1414
#endif

1415
TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_INT8X8X32) {
1416 1417 1418
    using namespace conv_bias;
    UniformIntRNG rng{-50, 50};
    float epsilon = 0.001;
1419
    std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, true);
1420 1421
#if MEGDNN_X86_WITH_MKL_DNN
    if (x86::is_supported(x86::SIMDType::VNNI)) {
M
Megvii Engine Team 已提交
1422 1423 1424
        checker_conv_bias(
                args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
                dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_MKLDNN:24");
1425 1426 1427 1428
    }
#endif
#if MEGDNN_X86_WITH_VNNI
    if (x86::is_supported(x86::SIMDType::VNNI)) {
M
Megvii Engine Team 已提交
1429 1430 1431
        checker_conv_bias(
                args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
                dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_VNNI:24");
1432 1433 1434
    }
#endif
    if (x86::is_supported(x86::SIMDType::AVX2)) {
M
Megvii Engine Team 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
        checker_conv_bias(
                args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
                dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_AVX2_4X16X2:24");
        checker_conv_bias(
                args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
                dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_AVX2_2X4X16:24");
        checker_conv_bias(
                args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
                dtype::Int16{}, dtype::Int16{}, "CONV1x1:X86_INT8X8X16_AVX2");
    }
    checker_conv_bias(
            args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
            dtype::Int32{}, "CONV1x1:X86_INT8X8X32_SSE_4X8X2:48");
    checker_conv_bias(
            args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{}, dtype::Int16{},
            dtype::Int16{}, "CONV1x1:X86_INT8X8X16_SSE");
1451
}
1452 1453 1454 1455 1456

TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_INT8X8X32_PREPROCESS) {
    using namespace conv_bias;
    UniformIntRNG rng{-50, 50};
    float epsilon = 0.001;
1457
    std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, true);
1458 1459
#if MEGDNN_X86_WITH_VNNI
    if (x86::is_supported(x86::SIMDType::VNNI)) {
M
Megvii Engine Team 已提交
1460 1461 1462
        checker_conv_bias_preprocess(
                args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
                dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_VNNI:24");
1463 1464 1465
    }
#endif
    if (x86::is_supported(x86::SIMDType::AVX2)) {
M
Megvii Engine Team 已提交
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
        checker_conv_bias_preprocess(
                args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
                dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_AVX2_4X16X2:24");
        checker_conv_bias_preprocess(
                args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
                dtype::Int32{}, dtype::Int32{}, "CONV1x1:X86_INT8X8X32_AVX2_2X4X16:24");
        checker_conv_bias_preprocess(
                args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{},
                dtype::Int16{}, dtype::Int16{}, "CONV1x1:X86_INT8X8X16_AVX2");
    }
    checker_conv_bias_preprocess(
            args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{}, dtype::Int32{},
            dtype::Int32{}, "CONV1x1:X86_INT8X8X32_SSE_4X8X2:48");
    checker_conv_bias_preprocess(
            args, handle(), &rng, epsilon, dtype::Int8{}, dtype::Int8{}, dtype::Int16{},
            dtype::Int16{}, "CONV1x1:X86_INT8X8X16_SSE");
1482 1483
}

1484 1485
/************************* End Conv1x1 PackA ************************/

1486 1487
#endif

1488 1489 1490 1491 1492 1493
TEST_F(X86_MULTI_THREADS, CONV_BIAS_CONV1X1_S1_FP32_6x16) {
    using namespace conv_bias;
    std::vector<conv_bias::TestArg> args = get_conv_bias_1x1_args(false, false);
    check_conv_bias(args, handle(), "CONV1x1:X86_F32_6x16:48");
}

1494 1495 1496 1497
TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_QINT8) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
1498 1499
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
1510 1511 1512
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
1513
        //! bias channel
M
Megvii Engine Team 已提交
1514 1515 1516
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
1517 1518
    };

1519
    for (size_t kernel : {2, 3, 4, 5, 6, 7})
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 21, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::IDENTITY, NonlineMode::RELU,
                              NonlineMode::H_SWISH}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }
    run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
    Checker<ConvBias> checker(handle());
#define cb(algo_name)                                             \
    checker.set_before_exec_callback(                             \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
    UniformIntRNG rng{-50, 50};                                   \
    for (auto&& arg : args) {                                     \
        checker.set_dtype(0, dtype::QuantizedS8(2.5f))            \
                .set_dtype(1, dtype::QuantizedS8(2.5f))           \
                .set_dtype(2, dtype::QuantizedS32(6.25f))         \
                .set_dtype(4, dtype::QuantizedS8(60.25))          \
                .set_rng(0, &rng)                                 \
                .set_rng(1, &rng)                                 \
                .set_rng(2, &rng)                                 \
                .set_param(arg.param)                             \
                .execs({arg.src, arg.filter, {}, {}, {}});        \
    }

1547
#if MEGDNN_X86_WITH_MKL_DNN
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
    if (x86::is_supported(x86::SIMDType::VNNI)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
    }
#endif
#if MEGDNN_X86_WITH_VNNI
    if (x86::is_supported(x86::SIMDType::VNNI)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
    }
#endif
    if (x86::is_supported(x86::SIMDType::AVX2)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
    }

#undef cb
}

1564 1565 1566 1567
TEST_F(X86_MULTI_THREADS, CONV_BIAS_IM2COLMATMUL_QINT8_FILTER_PREPROCESS) {
    using namespace conv_bias;
    std::vector<TestArg> args;

M
Megvii Engine Team 已提交
1568 1569
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
1580 1581 1582
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
1583
        //! bias channel
M
Megvii Engine Team 已提交
1584 1585 1586
        args.emplace_back(
                param, TensorShape{2, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{1, oc, 1, 1});
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
    };

    for (size_t kernel : {2, 3, 4, 5, 6, 7})
        for (size_t ic : {1, 4, 8, 16})
            for (size_t oc : {1, 4, 8})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 21, 24})
                        for (NonlineMode nonline_mode :
                             {NonlineMode::IDENTITY, NonlineMode::RELU,
                              NonlineMode::H_SWISH}) {
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }
    run(2046, 8, 20, 20, 3, 1, NonlineMode::IDENTITY);
    Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
            handle());
#define cb(algo_name)                                             \
    checker.set_before_exec_callback(                             \
            conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name)); \
    UniformIntRNG rng{-50, 50};                                   \
    for (auto&& arg : args) {                                     \
        checker.set_dtype(0, dtype::QuantizedS8(2.5f))            \
                .set_dtype(1, dtype::QuantizedS8(2.5f))           \
                .set_dtype(2, dtype::QuantizedS32(6.25f))         \
                .set_dtype(4, dtype::QuantizedS8(60.25))          \
                .set_rng(0, &rng)                                 \
                .set_rng(1, &rng)                                 \
                .set_rng(2, &rng)                                 \
                .set_param(arg.param)                             \
                .execs({arg.src, arg.filter, {}, {}, {}});        \
    }

#if MEGDNN_X86_WITH_MKL_DNN
    if (x86::is_supported(x86::SIMDType::VNNI)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_MKLDNN");
    }
#endif
#if MEGDNN_X86_WITH_VNNI
    if (x86::is_supported(x86::SIMDType::VNNI)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_VNNI");
    }
#endif
    if (x86::is_supported(x86::SIMDType::AVX2)) {
        cb("IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16");
    }

#undef cb
}

1635
#if MEGDNN_WITH_BENCHMARK
1636
#if MEGDNN_X86_WITH_MKL_DNN
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
static void x86_benchmark_fp32_mkldnn(Handle* handle) {
    constexpr size_t RUNS = 30;
    param::ConvBias param;

    Benchmarker<ConvBias> benchmarker_mkldnn(handle);
    benchmarker_mkldnn.set_display(false).set_times(RUNS);
    benchmarker_mkldnn.set_before_exec_callback(
            AlgoChecker<ConvBias>("MKLDNN_CONV_FP32"));

    Benchmarker<ConvBias> benchmarker_im2col(handle);
    benchmarker_im2col.set_display(false).set_times(RUNS);
    benchmarker_im2col.set_before_exec_callback(
            AlgoChecker<ConvBias>("IM2COLMATMUL.+"));
M
Megvii Engine Team 已提交
1650 1651 1652 1653
    auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
                   size_t SZ, size_t GROUP = 1) {
        TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}), bias({1, OC, 1, 1}),
                z({}), dst({N, OC, H / SZ, W / SZ});
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
        param.pad_h = FS / 2;
        param.pad_w = FS / 2;
        param.stride_h = SZ;
        param.stride_w = SZ;
        param.format = param::ConvBias::Format::NCHW;
        param.sparse = param::ConvBias::Sparse::DENSE;
        if (GROUP > 1) {
            param.sparse = param::ConvBias::Sparse::GROUP;
            filter = {GROUP, OC / GROUP, IC / GROUP, FS, FS};
        }
M
Megvii Engine Team 已提交
1664 1665 1666
        auto im2col_used =
                benchmarker_im2col.set_param(param).exec({src, filter, bias, z, dst}) /
                RUNS;
1667

M
Megvii Engine Team 已提交
1668
        src = IC < 8 ? TensorShape{N, IC, H, W} : TensorShape{N, IC / 8, H, W, 8};
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680

        filter = IC < 8 ? TensorShape{OC / 8, FS, FS, IC, 8}
                        : TensorShape{OC / 8, IC / 8, FS, FS, 8, 8};
        if (GROUP > 1 && OC == GROUP && IC == GROUP) {
            filter = {GROUP / 8, 1, 1, FS, FS, 8};
        } else if (GROUP > 1 && OC / GROUP % 8 == 0 && IC / GROUP % 8 == 0) {
            filter = {GROUP, OC / GROUP / 8, IC / GROUP / 8, FS, FS, 8, 8};
        }
        bias = {1, OC / 8, 1, 1, 8};
        z = {};
        dst = {N, OC / 8, H / SZ, W / SZ, 8};
        param.format = param::ConvBias::Format::NCHW88;
M
Megvii Engine Team 已提交
1681 1682 1683
        auto mkldnn_used =
                benchmarker_mkldnn.set_param(param).exec({src, filter, bias, z, dst}) /
                RUNS;
1684 1685
        float computations =
                (IC / GROUP * FS * FS + 1) * dst.total_nr_elems() * 2 * 1e-6;
M
Megvii Engine Team 已提交
1686 1687
        std::cout << "run " << src.to_string() << " " << filter.to_string() << " "
                  << bias.to_string() << " " << dst.to_string() << std::endl;
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
        std::cout << "im2col: " << im2col_used << " ms, "
                  << (computations / im2col_used) << " Gops, ";
        std::cout << "mkldnn: " << mkldnn_used << " ms, "
                  << (computations / mkldnn_used) << " Gops, "
                  << "spped up: " << (im2col_used / mkldnn_used) << ", ";
        std::cout << std::endl;
    };

    run(1, 64, 64, 56, 56, 3, 1);

    run(1, 3, 64, 224, 224, 3, 1);
    run(1, 3, 64, 224, 224, 7, 2);

    run(1, 64, 64, 56, 56, 3, 1);
    run(1, 128, 128, 28, 28, 3, 1);
    run(1, 256, 256, 14, 14, 3, 1);
    run(1, 512, 512, 7, 7, 3, 1);
    run(1, 256, 64, 56, 56, 1, 1);
    run(1, 512, 128, 28, 28, 1, 1);
    run(1, 1024, 256, 14, 14, 1, 1);
    run(1, 2048, 512, 7, 7, 1, 1);

    run(1, 32, 32, 112, 112, 3, 1, 32);
    run(1, 144, 144, 56, 56, 3, 1, 144);
    run(1, 192, 192, 28, 28, 3, 1, 192);
    run(1, 384, 384, 28, 28, 3, 1, 384);
    run(1, 576, 576, 14, 14, 3, 1, 576);
    run(1, 960, 960, 7, 7, 3, 1, 960);

    run(1, 256, 128, 56, 56, 1, 2, 1);
    run(1, 512, 256, 28, 28, 1, 2, 1);
    run(1, 1024, 512, 14, 14, 1, 2, 1);
    run(1, 96, 96, 112, 112, 3, 2, 96);
    run(1, 144, 144, 56, 56, 3, 2, 144);
    run(1, 384, 384, 28, 28, 3, 2, 384);
    run(1, 576, 576, 14, 14, 3, 2, 576);
}
TEST_F(X86, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
    x86_benchmark_fp32_mkldnn(handle());
}
TEST_F(X86_MULTI_THREADS, BENCHMARK_CONVBIAS_FP32_MKLDNN) {
    x86_benchmark_fp32_mkldnn(handle());
}
#endif
#endif

/************************* Winograd ****************************/
1735
namespace {
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
std::vector<conv_bias::TestArg> get_winograd_mk_nchw88_args() {
    std::vector<conv_bias::TestArg> args;
    param::ConvBias cur_param;
    cur_param.format = param::ConvBias::Format::NCHW88;
    using NLMode = param::ConvBias::NonlineMode;

    // clang-format off
    for (auto nlmode :
         {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
    for (size_t ic : {1, 2}) {
    for (size_t oc : {1, 2}) {
    for (size_t i : {9, 63}) {

        cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
        cur_param.nonlineMode = nlmode;

        cur_param.sparse = param::ConvBias::Sparse::DENSE;
        cur_param.pad_h = cur_param.pad_w = 1;

        args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
                          TensorShape{oc, ic, 3, 3, 8, 8},
                          TensorShape{1, oc, 1, 1, 8});
        args.emplace_back(cur_param, TensorShape{1, ic, i, i, 8},
                          TensorShape{oc, ic, 3, 3, 8, 8},TensorShape{});
        //! bias
        args.emplace_back(cur_param, TensorShape{2, ic, i, i, 8},
1762 1763
                          TensorShape{oc, ic, 3, 3, 8, 8},
                          TensorShape{2, oc, i, i, 8});
1764 1765 1766 1767 1768 1769

        /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
        args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i, 8},
                          TensorShape{2, oc, ic, 3, 3, 8, 8},
                          TensorShape{1, 2 * oc, 1, 1, 8});*/
    }}}
1770 1771 1772 1773
        // clang-format on
        //! test for multi-thread OC parallel
        cur_param.sparse = param::ConvBias::Sparse::DENSE;
        cur_param.pad_h = cur_param.pad_w = 1;
M
Megvii Engine Team 已提交
1774 1775 1776
        args.emplace_back(
                cur_param, TensorShape{2, 1, 9, 9, 8}, TensorShape{128, 1, 3, 3, 8, 8},
                TensorShape{1, 128, 1, 1, 8});
1777 1778 1779 1780
        /*cur_param.sparse = param::ConvBias::Sparse::GROUP;
        args.emplace_back(cur_param, TensorShape{2, 2, 9, 9, 8},
                          TensorShape{2, 128, 1, 3, 3, 8, 8},
                          TensorShape{1, 2 * 128, 1, 1, 8});*/
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
    }
    return args;
}
}  // namespace

TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63) {
    using namespace conv_bias;
    std::vector<TestArg> args = get_winograd_mk_nchw88_args();
    Checker<ConvBiasForward> checker(handle());

    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
            ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));

    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
1795
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
1796 1797 1798
    }
}

1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F63_WEIGHT_PREPROCESS) {
    using namespace conv_bias;
    std::vector<TestArg> args = get_winograd_mk_nchw88_args();
    Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
            handle());

    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
            ssprintf("WINOGRAD:X86_F32MK8_8X8:8:6").c_str()));

    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
1809
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
1810 1811 1812
    }
}

1813 1814 1815 1816 1817 1818 1819 1820 1821
TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23) {
    using namespace conv_bias;
    std::vector<TestArg> args = get_winograd_mk_nchw88_args();
    Checker<ConvBiasForward> checker(handle());

    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
            ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));

    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
1822
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
1823 1824 1825
    }
}

1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_NCHW88_F23_WEIGHT_PREPROCESS) {
    using namespace conv_bias;
    std::vector<TestArg> args = get_winograd_mk_nchw88_args();
    Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
            handle());

    checker.set_before_exec_callback(conv_bias::ConvBiasAlgoChecker<ConvBias>(
            ssprintf("WINOGRAD:X86_F32MK8_8X8:8:2").c_str()));

    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
1836
        checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
1837 1838 1839
    }
}

1840 1841 1842 1843 1844
TEST_F(X86_MULTI_THREADS, CONV_BIAS_WINOGRAD_WEIGHT_PREPROCESS) {
    using namespace conv_bias;
    std::vector<TestArg> args = get_winograd_mk_nchw88_args();
    Checker<ConvBiasForward> checker(handle());

M
Megvii Engine Team 已提交
1845 1846 1847
    auto run = [&checker](
                       const std::vector<TestArg>& args, DType A_dtype, DType B_dtype,
                       DType C_dtype, DType D_dtype, const float eps) {
1848
        for (auto&& arg : args) {
1849 1850 1851 1852 1853 1854 1855
            checker.set_dtype(0, A_dtype)
                    .set_dtype(1, B_dtype)
                    .set_dtype(2, C_dtype)
                    .set_dtype(4, D_dtype)
                    .set_epsilon(eps)
                    .set_param(arg.param)
                    .execs({arg.src, arg.filter, arg.bias, {}, {}});
1856 1857
        }
    };
M
Megvii Engine Team 已提交
1858 1859
    run(args, dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32(),
        1e-3f);
1860 1861 1862
}

/*********************************** End winograd ************************/
1863
#if MEGDNN_X86_WITH_MKL_DNN
1864 1865
static void x86_correctness_fp32_mkldnn_run(
        Checker<ConvBias>& checker, UniformIntRNG& rng, Handle* handle,
M
Megvii Engine Team 已提交
1866 1867 1868
        ConvBiasForward::BiasMode bias_mode, param::ConvBias::NonlineMode noline_mode,
        size_t n, size_t stride, size_t kernel, size_t oc, size_t ic, size_t h,
        size_t w, size_t group) {
1869 1870 1871
    auto oc_per_group = oc / group;
    auto ic_per_group = ic / group;
    bool ok_group = oc_per_group % 8 == 0 && oc_per_group > 0 &&
M
Megvii Engine Team 已提交
1872
                    (ic_per_group % 8 == 0 || ic_per_group == 3) && ic_per_group > 0;
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
    bool ok_depthwise = oc == ic && oc == group;
    if (!(ok_group || ok_depthwise)) {
        return;
    }
    size_t pad = kernel / 2;
    size_t kernel_h = kernel;
    size_t kernel_w = kernel;
    param::ConvBias param;
    param.format = param::ConvBias::Format::NCHW88;
    param.stride_h = stride;
    param.stride_w = stride;
    param.pad_h = pad;
    param.pad_w = pad;
    param.nonlineMode = noline_mode;
    auto src_tensor_shape = TensorShape{n, ic / 8, h, w, 8};
    if (ic == 3) {
        src_tensor_shape = TensorShape{n, ic, h, w};
    }

M
Megvii Engine Team 已提交
1892
    auto weight_tensor_shape = TensorShape{oc / 8, ic / 8, kernel_h, kernel_w, 8, 8};
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
    if (ic == 3) {
        weight_tensor_shape = TensorShape{oc / 8, kernel_h, kernel_w, ic, 8};
    }

    auto bias_tensor_shape = TensorShape{};

    if (bias_mode == megdnn::BiasMode::BROADCAST_CHANNEL_BIAS) {
        bias_tensor_shape = {1, oc / 8, 1, 1, 8};
    } else if (bias_mode == megdnn::BiasMode::BIAS) {
        TensorLayout dst_layout;
        auto ConvBiasOp = handle->create_operator<ConvBias>();
        ConvBiasOp->param() = param;
M
Megvii Engine Team 已提交
1905 1906 1907
        ConvBiasOp->deduce_layout(
                {src_tensor_shape, dtype::Float32()},
                {weight_tensor_shape, dtype::Float32()}, {}, {}, dst_layout);
1908 1909 1910 1911 1912 1913 1914
        bias_tensor_shape = dst_layout;
    }

    if (group == 1) {
        param.sparse = param::ConvBias::Sparse::DENSE;
    } else if (group > 1 && ic / group == 1 && oc / group == 1) {
        param.sparse = param::ConvBias::Sparse::GROUP;
M
Megvii Engine Team 已提交
1915 1916 1917 1918
        weight_tensor_shape = TensorShape{group / 8, 1, 1, kernel_h, kernel_w, 8};
    } else if (
            group > 1 && oc / group % 8 == 0 && oc / group > 0 && ic / group % 8 == 0 &&
            ic / group > 0) {
1919 1920
        param.sparse = param::ConvBias::Sparse::GROUP;
        weight_tensor_shape = TensorShape{
M
Megvii Engine Team 已提交
1921
                group, oc / group / 8, ic / group / 8, kernel_h, kernel_w, 8, 8};
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
    }
    checker.set_dtype(0, dtype::Float32())
            .set_dtype(1, dtype::Float32())
            .set_dtype(2, dtype::Float32())
            .set_dtype(4, dtype::Float32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng)
            .set_epsilon(1e-3)
            .set_param(param)
M
Megvii Engine Team 已提交
1932
            .execs({src_tensor_shape, weight_tensor_shape, bias_tensor_shape, {}, {}});
1933 1934 1935 1936 1937 1938 1939
}

static void x86_correctness_fp32_mkldnn(Handle* handle) {
    Checker<ConvBias> checker(handle);
    UniformIntRNG rng{-127, 127};

    checker.set_before_exec_callback(
M
Megvii Engine Team 已提交
1940
            conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_CONV_FP32"));
1941 1942 1943 1944

    for (auto bias_mode :
         {megdnn::BiasMode::NO_BIAS, megdnn::BiasMode::BROADCAST_CHANNEL_BIAS,
          megdnn::BiasMode::BIAS})
M
Megvii Engine Team 已提交
1945 1946 1947 1948
        for (auto noline_mode :
             {param::ConvBias::NonlineMode::IDENTITY,
              param::ConvBias::NonlineMode::SIGMOID,
              param::ConvBias::NonlineMode::H_SWISH})
1949 1950 1951 1952 1953 1954 1955 1956
            for (size_t n : {1, 2})
                for (size_t stride : {1, 2})
                    for (size_t kernel : {3, 5, 7})
                        for (size_t oc : {8, 16})
                            for (size_t ic : {3, 8, 16})
                                for (size_t h : {22, 33})
                                    for (size_t w : {22, 33}) {
                                        for (size_t group = 1;
M
Megvii Engine Team 已提交
1957
                                             group <= std::min(oc, ic); ++group) {
1958
                                            x86_correctness_fp32_mkldnn_run(
M
Megvii Engine Team 已提交
1959 1960 1961
                                                    checker, rng, handle, bias_mode,
                                                    noline_mode, n, stride, kernel, oc,
                                                    ic, h, w, group);
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
                                        }
                                    }
}

TEST_F(X86, CONV_BIAS_DIRECT_MKLDNN_C8) {
    x86_correctness_fp32_mkldnn(handle());
}
TEST_F(X86_MULTI_THREADS, CONV_BIAS_DIRECT_MKLDNN_C8) {
    x86_correctness_fp32_mkldnn(handle());
}

TEST_F(X86, CONV_BIAS_MKL_DNN_MATMUL_INT8) {
    using namespace conv_bias;

    std::vector<TestArg> args;
M
Megvii Engine Team 已提交
1977 1978
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
1989 1990 1991
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
1992 1993 1994 1995 1996 1997 1998
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t ic : {1, 2, 3, 4})
            for (size_t oc : {1, 2, 4})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 21, 22, 23, 24})
M
Megvii Engine Team 已提交
1999
                        for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
2000 2001 2002 2003 2004
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    Checker<ConvBias> checker(handle());
    checker.set_before_exec_callback(
M
Megvii Engine Team 已提交
2005
            conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_MATMUL_INT8"));
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
    checker.set_epsilon(1);
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Int8())
            .set_dtype(1, dtype::Int8())
            .set_dtype(2, dtype::Int32())
            .set_dtype(4, dtype::Int32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng);

    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
2017
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
2018 2019 2020 2021 2022 2023 2024
    }
}

TEST_F(X86, CONV_BIAS_MKL_DNN_INT8) {
    using namespace conv_bias;

    std::vector<TestArg> args;
M
Megvii Engine Team 已提交
2025 2026
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
2037 2038 2039
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
2040 2041 2042 2043 2044 2045 2046
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t ic : {1, 2, 3, 4})
            for (size_t oc : {1, 2, 4})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 22, 24})
M
Megvii Engine Team 已提交
2047
                        for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    Checker<ConvBias> checker(handle());
    checker.set_before_exec_callback(
            conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
    checker.set_epsilon(1);
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Int8())
            .set_dtype(1, dtype::Int8())
            .set_dtype(2, dtype::Int32())
            .set_dtype(4, dtype::Int32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng);

    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
2065
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
2066 2067 2068 2069 2070 2071 2072
    }
}

TEST_F(X86_MULTI_THREADS, CONV_BIAS_MKL_DNN_INT8) {
    using namespace conv_bias;

    std::vector<TestArg> args;
M
Megvii Engine Team 已提交
2073 2074
    auto run = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p,
                   NonlineMode nonline_mode) {
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;
        param::ConvBias param;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;
        param.nonlineMode = nonline_mode;

        //! no bias
M
Megvii Engine Team 已提交
2085 2086 2087
        args.emplace_back(
                param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
                TensorShape{});
2088 2089 2090 2091 2092 2093 2094
    };

    for (size_t kernel : {2, 3, 5, 7})
        for (size_t ic : {1, 2, 3, 4})
            for (size_t oc : {1, 2, 4})
                for (size_t p : {0, 2})
                    for (size_t size : {20, 22, 24})
M
Megvii Engine Team 已提交
2095
                        for (NonlineMode nonline_mode : {NonlineMode::IDENTITY}) {
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
                            run(oc, ic, size, size, kernel, p, nonline_mode);
                        }

    Checker<ConvBias> checker(handle());
    checker.set_before_exec_callback(
            conv_bias::ConvBiasAlgoChecker<ConvBiasForward>("MKLDNN_INT8"));
    checker.set_epsilon(1);
    UniformIntRNG rng{-50, 50};
    checker.set_dtype(0, dtype::Int8())
            .set_dtype(1, dtype::Int8())
            .set_dtype(2, dtype::Int32())
            .set_dtype(4, dtype::Int32())
            .set_rng(0, &rng)
            .set_rng(1, &rng)
            .set_rng(2, &rng);

    for (auto&& arg : args) {
M
Megvii Engine Team 已提交
2113
        checker.set_param(arg.param).exec({arg.src, arg.filter, arg.bias, {}, {}});
2114 2115 2116 2117 2118 2119
    }
}
#endif

#if MEGDNN_WITH_BENCHMARK
namespace {
M
Megvii Engine Team 已提交
2120 2121 2122 2123 2124 2125 2126 2127
void benchmark_impl(
        const param::ConvBias param,
        std::vector<std::pair<SmallVector<TensorShape>, float>>& shapes_and_computation,
        const std::string algo_name, size_t RUNS,
        TaskExecutorConfig&& multi_thread_config,
        TaskExecutorConfig&& single_thread_config, std::vector<DType> dtype_v) {
    std::vector<DType> data_type = {
            dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
2128 2129 2130

    std::vector<float> multi_thread_times, single_thread_times;
    {
M
Megvii Engine Team 已提交
2131
        auto multi_thread_hanle = create_cpu_handle(0, true, &multi_thread_config);
2132 2133 2134 2135 2136 2137 2138 2139 2140
        auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
        benchmarker.set_times(RUNS)
                .set_display(false)
                .set_dtype(0, dtype_v[0])
                .set_dtype(1, dtype_v[1])
                .set_dtype(2, dtype_v[2])
                .set_dtype(4, dtype_v[3])
                .set_param(param)
                .set_before_exec_callback(
M
Megvii Engine Team 已提交
2141
                        conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name.c_str()));
2142 2143 2144 2145 2146
        for (auto shape : shapes_and_computation) {
            multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
        }
    }
    {
M
Megvii Engine Team 已提交
2147
        auto single_thread_handle = create_cpu_handle(0, true, &single_thread_config);
2148 2149 2150 2151 2152 2153 2154 2155 2156
        auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
        benchmarker.set_times(RUNS)
                .set_display(false)
                .set_dtype(0, dtype_v[0])
                .set_dtype(1, dtype_v[1])
                .set_dtype(2, dtype_v[2])
                .set_dtype(4, dtype_v[3])
                .set_param(param)
                .set_before_exec_callback(
M
Megvii Engine Team 已提交
2157
                        conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name.c_str()));
2158 2159 2160 2161 2162 2163 2164 2165 2166
        for (auto shape : shapes_and_computation) {
            single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
        }
    }
    printf("Benchmark : Multi threads  %zu, ", multi_thread_config.nr_thread);
    printf("core_ids:");
    for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
        printf("%zu ", multi_thread_config.affinity_core_set[i]);
    }
M
Megvii Engine Team 已提交
2167
    printf(", Single thread core_id %zu\n", single_thread_config.affinity_core_set[0]);
2168 2169 2170 2171 2172 2173 2174 2175 2176
    for (size_t i = 0; i < shapes_and_computation.size(); i++) {
        auto shapes = shapes_and_computation[i];
        printf("Bench case: ");
        for (auto&& shape : shapes.first) {
            printf("%s ", shape.to_string().c_str());
        }
        float computations = shapes.second;
        printf("%zu threads gflops: %f,\n single thread gflops: "
               "%f. spead up = %f, speedup/cores=%f\n",
M
Megvii Engine Team 已提交
2177
               multi_thread_config.nr_thread, computations / multi_thread_times[i],
2178 2179 2180 2181 2182 2183 2184
               computations / single_thread_times[i],
               single_thread_times[i] / multi_thread_times[i],
               single_thread_times[i] / multi_thread_times[i] /
                       multi_thread_config.nr_thread);
    }
}

2185 2186
void benchmark_impl_comp(
        const param::ConvBias param,
M
Megvii Engine Team 已提交
2187
        std::vector<std::pair<SmallVector<TensorShape>, float>>& shapes_and_computation,
2188 2189 2190
        const std::string algo_name, const std::string algo_name1, size_t RUNS,
        TaskExecutorConfig&& multi_thread_config,
        TaskExecutorConfig&& single_thread_config, std::vector<DType> dtype_v) {
M
Megvii Engine Team 已提交
2191 2192
    std::vector<DType> data_type = {
            dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
2193 2194 2195

    std::vector<float> multi_thread_times, single_thread_times;
    {
M
Megvii Engine Team 已提交
2196
        auto multi_thread_hanle = create_cpu_handle(0, true, &multi_thread_config);
2197 2198 2199
        auto benchmarker = Benchmarker<ConvBias>(multi_thread_hanle.get());
        benchmarker.set_times(RUNS)
                .set_display(false)
2200 2201 2202 2203
                .set_dtype(0, dtype_v[0])
                .set_dtype(1, dtype_v[1])
                .set_dtype(2, dtype_v[2])
                .set_dtype(4, dtype_v[3])
2204 2205
                .set_param(param)
                .set_before_exec_callback(
M
Megvii Engine Team 已提交
2206
                        conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name.c_str()));
2207 2208 2209 2210 2211
        for (auto shape : shapes_and_computation) {
            multi_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
        }
    }
    {
M
Megvii Engine Team 已提交
2212
        auto single_thread_handle = create_cpu_handle(0, true, &single_thread_config);
2213 2214 2215
        auto benchmarker = Benchmarker<ConvBias>(single_thread_handle.get());
        benchmarker.set_times(RUNS)
                .set_display(false)
2216 2217 2218 2219
                .set_dtype(0, dtype_v[0])
                .set_dtype(1, dtype_v[1])
                .set_dtype(2, dtype_v[2])
                .set_dtype(4, dtype_v[3])
2220 2221
                .set_param(param)
                .set_before_exec_callback(
M
Megvii Engine Team 已提交
2222
                        conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name1.c_str()));
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
        for (auto shape : shapes_and_computation) {
            single_thread_times.push_back(benchmarker.exec(shape.first) / RUNS);
        }
    }
    printf("Benchmark : Multi threads  %zu, ", multi_thread_config.nr_thread);
    printf("core_ids:");
    for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
        printf("%zu ", multi_thread_config.affinity_core_set[i]);
    }
    for (size_t i = 0; i < shapes_and_computation.size(); i++) {
        auto shapes = shapes_and_computation[i];
        printf("Bench case: ");
        for (auto&& shape : shapes.first) {
            printf("%s ", shape.to_string().c_str());
        }
        float computations = shapes.second;
        printf("algo:%s gflops: %f,\n algo:%s gflops: "
               "%f. spead up = %f\n",
               algo_name.c_str(), computations / multi_thread_times[i],
               algo_name1.c_str(), computations / single_thread_times[i],
               single_thread_times[i] / multi_thread_times[i]);
    }
}

}  // namespace
2248

M
Megvii Engine Team 已提交
2249
static void benchmark_convbias_chanwise_avx2_int8(uint32_t stride, const char* algo) {
2250 2251
    constexpr size_t RUNS = 50;
    param::ConvBias param;
2252 2253
    param.stride_h = stride;
    param.stride_w = stride;
2254 2255
    param.sparse = param::ConvBias::Sparse::GROUP;

M
Megvii Engine Team 已提交
2256 2257
    std::vector<DType> data_type = {
            dtype::Int8(), dtype::Int8(), dtype::Int32(), dtype::Int32()};
2258

M
Megvii Engine Team 已提交
2259
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
2260 2261 2262 2263
    auto bench_case = [&](size_t N, size_t IC, size_t H, size_t W, size_t FS) {
        param.pad_h = FS / 2;
        param.pad_w = FS / 2;

M
Megvii Engine Team 已提交
2264 2265 2266
        SmallVector<TensorShape> shapes{{N, IC, H, W}, {IC, 1, 1, FS, FS}, {}, {}, {}};
        TensorShape dst{
                N, IC, (H + 2 * param.pad_h - FS) + 1, (W + 2 * param.pad_w - FS) + 1};
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
        float computations = (FS * FS * dst.total_nr_elems() * 2) * 1e-6;
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 32, 112, 112, 7);
    bench_case(1, 144, 56, 56, 7);
    bench_case(1, 192, 28, 28, 7);
    bench_case(1, 384, 28, 28, 7);
    bench_case(1, 576, 14, 14, 7);
    bench_case(1, 960, 7, 7, 7);

    bench_case(1, 32, 112, 112, 5);
    bench_case(1, 144, 56, 56, 5);
    bench_case(1, 192, 28, 28, 5);
    bench_case(1, 384, 28, 28, 5);
    bench_case(1, 576, 14, 14, 5);
    bench_case(1, 960, 7, 7, 5);

    bench_case(1, 32, 112, 112, 3);
    bench_case(1, 144, 56, 56, 3);
    bench_case(1, 192, 28, 28, 3);
    bench_case(1, 384, 28, 28, 3);
    bench_case(1, 576, 14, 14, 3);
    bench_case(1, 960, 7, 7, 3);

    bench_case(1, 32, 112, 112, 2);
    bench_case(1, 144, 56, 56, 2);
    bench_case(1, 192, 28, 28, 2);
    bench_case(1, 384, 28, 28, 2);
    bench_case(1, 576, 14, 14, 2);
    bench_case(1, 960, 7, 7, 2);

2299 2300
    std::string algo_name = algo;
    printf("Benchmark %s\n", algo);
M
Megvii Engine Team 已提交
2301 2302 2303 2304 2305 2306
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
            data_type);
2307 2308
    shapes_and_computation.clear();
}
2309 2310 2311 2312 2313 2314 2315 2316 2317
TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S1) {
    benchmark_convbias_chanwise_avx2_int8(
            1, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE1");
}

TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_CHANWISE_AVX2_INT8_S2) {
    benchmark_convbias_chanwise_avx2_int8(
            2, "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE2");
}
2318

2319 2320 2321 2322 2323 2324 2325
TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8) {
    constexpr size_t RUNS = 50;
    param::ConvBias param;
    param.stride_h = 1;
    param.stride_w = 1;
    param.sparse = param::ConvBias::Sparse::DENSE;

M
Megvii Engine Team 已提交
2326 2327
    std::vector<DType> data_type = {
            dtype::Int8(), dtype::Int8(), dtype::Int32(), dtype::Int32()};
2328

M
Megvii Engine Team 已提交
2329
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
2330 2331 2332 2333 2334
    auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
                          size_t FS) {
        param.pad_h = FS / 2;
        param.pad_w = FS / 2;

M
Megvii Engine Team 已提交
2335 2336 2337
        SmallVector<TensorShape> shapes{{N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
        TensorShape dst{
                N, OC, (H + 2 * param.pad_h - FS) + 1, (W + 2 * param.pad_w - FS) + 1};
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
        float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 32, 32, 200, 200, 7);
    bench_case(1, 32, 64, 200, 200, 7);
    bench_case(1, 32, 32, 128, 128, 7);
    bench_case(1, 32, 64, 128, 128, 7);
    bench_case(1, 32, 32, 100, 100, 7);
    bench_case(1, 32, 64, 100, 100, 7);
    bench_case(1, 32, 32, 80, 80, 7);
    bench_case(1, 32, 64, 80, 80, 7);

    bench_case(1, 32, 32, 200, 200, 5);
    bench_case(1, 32, 64, 200, 200, 5);
    bench_case(1, 32, 32, 128, 128, 5);
    bench_case(1, 32, 64, 128, 128, 5);
    bench_case(1, 32, 32, 100, 100, 5);
    bench_case(1, 32, 64, 100, 100, 5);
    bench_case(1, 32, 32, 80, 80, 5);
    bench_case(1, 32, 64, 80, 80, 5);

    bench_case(1, 32, 32, 200, 200, 3);
    bench_case(1, 32, 64, 200, 200, 3);
    bench_case(1, 32, 32, 128, 128, 3);
    bench_case(1, 32, 64, 128, 128, 3);
    bench_case(1, 32, 32, 100, 100, 3);
    bench_case(1, 32, 64, 100, 100, 3);
    bench_case(1, 32, 32, 80, 80, 3);
    bench_case(1, 32, 64, 80, 80, 3);

    bench_case(1, 32, 32, 200, 200, 2);
    bench_case(1, 32, 64, 200, 200, 2);
    bench_case(1, 32, 32, 128, 128, 2);
    bench_case(1, 32, 64, 128, 128, 2);
    bench_case(1, 32, 32, 100, 100, 2);
    bench_case(1, 32, 64, 100, 100, 2);
    bench_case(1, 32, 32, 80, 80, 2);
    bench_case(1, 32, 64, 80, 80, 2);

    std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1";
    printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1 algo\n");
M
Megvii Engine Team 已提交
2380 2381 2382 2383 2384 2385 2386 2387 2388
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
            data_type);
2389 2390 2391
    shapes_and_computation.clear();
}

2392 2393 2394 2395 2396 2397 2398
TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_8816) {
    constexpr size_t RUNS = 30;
    param::ConvBias param;
    param.stride_h = 1;
    param.stride_w = 1;
    param.sparse = param::ConvBias::Sparse::DENSE;

M
Megvii Engine Team 已提交
2399 2400
    std::vector<DType> data_type = {
            dtype::Int8(), dtype::Int8(), dtype::Int16(), dtype::Int16()};
2401

M
Megvii Engine Team 已提交
2402
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
2403 2404 2405 2406 2407
    auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
                          size_t FS) {
        param.pad_h = FS / 2;
        param.pad_w = FS / 2;

M
Megvii Engine Team 已提交
2408 2409 2410 2411
        SmallVector<TensorShape> shapes{{N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
        TensorShape dst{
                N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
                (W + 2 * param.pad_w - FS) / param.stride_w + 1};
2412 2413 2414 2415 2416 2417 2418
        float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 48, 192, 15, 15, 1);

    std::string algo_name = "IM2COLMATMUL:X86_INT8X8X16_AVX2";
M
Megvii Engine Team 已提交
2419 2420 2421
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
            data_type);
2422 2423 2424
    shapes_and_computation.clear();
}

M
Megvii Engine Team 已提交
2425
TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECT_AVX2_INT8_STRIDE2) {
2426 2427 2428 2429 2430 2431
    constexpr size_t RUNS = 50;
    param::ConvBias param;
    param.stride_h = 2;
    param.stride_w = 2;
    param.sparse = param::ConvBias::Sparse::DENSE;

M
Megvii Engine Team 已提交
2432 2433
    std::vector<DType> data_type = {
            dtype::Int8(), dtype::Int8(), dtype::Int32(), dtype::Int32()};
2434

M
Megvii Engine Team 已提交
2435
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
2436 2437 2438 2439 2440
    auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W,
                          size_t FS) {
        param.pad_h = FS / 2;
        param.pad_w = FS / 2;

M
Megvii Engine Team 已提交
2441 2442 2443 2444
        SmallVector<TensorShape> shapes{{N, IC, H, W}, {OC, IC, FS, FS}, {}, {}, {}};
        TensorShape dst{
                N, OC, (H + 2 * param.pad_h - FS) / param.stride_h + 1,
                (W + 2 * param.pad_w - FS) / param.stride_w + 1};
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
        float computations = (IC * FS * FS * dst.total_nr_elems() * 2) * 1e-6;
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 32, 32, 200, 200, 7);
    bench_case(1, 32, 64, 200, 200, 7);
    bench_case(1, 32, 32, 128, 128, 7);
    bench_case(1, 32, 64, 128, 128, 7);
    bench_case(1, 32, 32, 100, 100, 7);
    bench_case(1, 32, 64, 100, 100, 7);
    bench_case(1, 32, 32, 80, 80, 7);
    bench_case(1, 32, 64, 80, 80, 7);

    bench_case(1, 32, 32, 200, 200, 5);
    bench_case(1, 32, 64, 200, 200, 5);
    bench_case(1, 32, 32, 128, 128, 5);
    bench_case(1, 32, 64, 128, 128, 5);
    bench_case(1, 32, 32, 100, 100, 5);
    bench_case(1, 32, 64, 100, 100, 5);
    bench_case(1, 32, 32, 80, 80, 5);
    bench_case(1, 32, 64, 80, 80, 5);

    bench_case(1, 32, 32, 200, 200, 3);
    bench_case(1, 32, 64, 200, 200, 3);
    bench_case(1, 32, 32, 128, 128, 3);
    bench_case(1, 32, 64, 128, 128, 3);
    bench_case(1, 32, 32, 100, 100, 3);
    bench_case(1, 32, 64, 100, 100, 3);
    bench_case(1, 32, 32, 80, 80, 3);
    bench_case(1, 32, 64, 80, 80, 3);

    bench_case(1, 32, 32, 200, 200, 2);
    bench_case(1, 32, 64, 200, 200, 2);
    bench_case(1, 32, 32, 128, 128, 2);
    bench_case(1, 32, 64, 128, 128, 2);
    bench_case(1, 32, 32, 100, 100, 2);
    bench_case(1, 32, 64, 100, 100, 2);
    bench_case(1, 32, 32, 80, 80, 2);
    bench_case(1, 32, 64, 80, 80, 2);

    std::string algo_name = "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2";
    printf("Benchmark X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2 algo\n");
M
Megvii Engine Team 已提交
2487 2488 2489 2490 2491 2492 2493 2494 2495
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
            data_type);
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
    shapes_and_computation.clear();
}

TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_DIRECTF32) {
    constexpr size_t RUNS = 50;

    param::ConvBias param;
    param.nonlineMode = param::ConvBias::NonlineMode::RELU;
    param.pad_h = 1;
    param.pad_w = 1;
    param.stride_h = 1;
    param.stride_w = 1;
    param.sparse = param::ConvBias::Sparse::GROUP;

M
Megvii Engine Team 已提交
2510 2511
    std::vector<DType> data_type = {
            dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
2512

M
Megvii Engine Team 已提交
2513 2514 2515 2516 2517 2518 2519 2520 2521
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
    auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
                          size_t group) {
        SmallVector<TensorShape> shapes{
                {N, IC, H, W},
                {group, OC / group, IC / group, FS, FS},
                {1, OC, 1, 1},
                {},
                {N, OC, H, W}};
2522
        TensorShape dst{N, OC, H, W};
M
Megvii Engine Team 已提交
2523 2524 2525
        float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
                              dst.total_nr_elems()) *
                             1e-6;
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 32, 32, 200, 200, 3, 4);
    bench_case(1, 32, 32, 200, 200, 3, 32);
    bench_case(1, 32, 32, 128, 128, 3, 4);
    bench_case(1, 32, 32, 128, 128, 3, 32);
    bench_case(1, 32, 32, 100, 100, 3, 4);
    bench_case(1, 32, 32, 100, 100, 3, 32);
    bench_case(1, 32, 32, 80, 80, 3, 4);
    bench_case(1, 32, 32, 80, 80, 3, 32);

    std::string algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
2539
    printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_GROUP algo\n");
M
Megvii Engine Team 已提交
2540 2541 2542 2543 2544 2545 2546 2547 2548
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
            data_type);
2549 2550
    shapes_and_computation.clear();

2551 2552
    algo_name = "X86_CONV_BIAS_DIRECT_STRIDE1_LARGE_GROUP";
    printf("Benchmark X86_CONV_BIAS_DIRECT_STRIDE1_DENSE algo\n");
2553 2554 2555 2556
    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);
M
Megvii Engine Team 已提交
2557 2558 2559 2560 2561 2562 2563 2564 2565
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
            data_type);
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
}

TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32) {
    constexpr size_t RUNS = 50;

    param::ConvBias param;
    param.nonlineMode = param::ConvBias::NonlineMode::RELU;
    param.pad_h = 1;
    param.pad_w = 1;
    param.stride_h = 1;
    param.stride_w = 1;

M
Megvii Engine Team 已提交
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
    std::vector<DType> data_type = {
            dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
    auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
                          size_t group) {
        SmallVector<TensorShape> shapes{
                {N, IC, H, W},
                {OC / group, IC / group, FS, FS},
                {1, OC, 1, 1},
                {},
                {N, OC, H, W}};
2589
        TensorShape dst{N, OC, H, W};
M
Megvii Engine Team 已提交
2590 2591 2592
        float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
                              dst.total_nr_elems()) *
                             1e-6;
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);

    bench_case(1, 64, 32, 7, 7, 3, 1);
    bench_case(1, 64, 64, 7, 7, 3, 1);
    bench_case(1, 64, 128, 7, 7, 3, 1);
    bench_case(1, 64, 256, 7, 7, 3, 1);
    bench_case(1, 64, 512, 7, 7, 3, 1);
    bench_case(1, 64, 1024, 7, 7, 3, 1);

    bench_case(1, 64, 32, 14, 14, 3, 1);
    bench_case(1, 64, 64, 14, 14, 3, 1);
    bench_case(1, 64, 128, 14, 14, 3, 1);
    bench_case(1, 64, 256, 14, 14, 3, 1);
    bench_case(1, 64, 512, 14, 14, 3, 1);

    bench_case(1, 64, 1024, 14, 14, 3, 1);
    bench_case(1, 128, 128, 14, 14, 3, 1);
    bench_case(1, 128, 256, 14, 14, 3, 1);
    bench_case(1, 512, 512, 14, 14, 3, 1);
    bench_case(1, 256, 512, 14, 14, 3, 1);
    bench_case(1, 512, 1024, 14, 14, 3, 1);
    bench_case(1, 1024, 1024, 14, 14, 3, 1);

    std::string algo_name = "IM2COLMATMUL:X86_F32_BLAS:192";
    printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
M
Megvii Engine Team 已提交
2628 2629 2630 2631 2632 2633 2634 2635 2636
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
            data_type);
2637 2638 2639
    shapes_and_computation.clear();
}

M
Megvii Engine Team 已提交
2640
TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32_single_thread) {
2641 2642 2643 2644 2645 2646 2647 2648 2649
    constexpr size_t RUNS = 50;

    param::ConvBias param;
    param.nonlineMode = param::ConvBias::NonlineMode::RELU;
    param.pad_h = 1;
    param.pad_w = 1;
    param.stride_h = 1;
    param.stride_w = 1;

M
Megvii Engine Team 已提交
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
    std::vector<DType> data_type = {
            dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
    auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
                          size_t group) {
        SmallVector<TensorShape> shapes{
                {N, IC, H, W},
                {OC / group, IC / group, FS, FS},
                {1, OC, 1, 1},
                {},
                {N, OC, H, W}};
2661
        TensorShape dst{N, OC, H, W};
M
Megvii Engine Team 已提交
2662 2663 2664
        float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
                              dst.total_nr_elems()) *
                             1e-6;
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);
2676

2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
    bench_case(1, 64, 32, 7, 7, 3, 1);
    bench_case(1, 64, 64, 7, 7, 3, 1);
    bench_case(1, 64, 128, 7, 7, 3, 1);
    bench_case(1, 64, 256, 7, 7, 3, 1);
    bench_case(1, 64, 512, 7, 7, 3, 1);
    bench_case(1, 64, 1024, 7, 7, 3, 1);

    bench_case(1, 64, 32, 14, 14, 3, 1);
    bench_case(1, 64, 64, 14, 14, 3, 1);
    bench_case(1, 64, 128, 14, 14, 3, 1);
    bench_case(1, 64, 256, 14, 14, 3, 1);
    bench_case(1, 64, 512, 14, 14, 3, 1);

    bench_case(1, 64, 1024, 14, 14, 3, 1);
    bench_case(1, 128, 128, 14, 14, 3, 1);
    bench_case(1, 128, 256, 14, 14, 3, 1);
    bench_case(1, 512, 512, 14, 14, 3, 1);
    bench_case(1, 256, 512, 14, 14, 3, 1);
    bench_case(1, 512, 1024, 14, 14, 3, 1);
    bench_case(1, 1024, 1024, 14, 14, 3, 1);

    std::string algo_name = "IM2COLMATMUL:X86_F32_MKL_PACKA:192";
    std::string algo_name1 = "IM2COLMATMUL:X86_F32_BLAS:192";
    printf("Benchmark IM2COLMATMUL:X86_F32_BLAS algo\n");
M
Megvii Engine Team 已提交
2701 2702 2703 2704 2705 2706
    benchmark_impl_comp(
            param, shapes_and_computation, algo_name, algo_name1, RUNS, {1, {4}},
            {1, {4}}, data_type);
    benchmark_impl_comp(
            param, shapes_and_computation, algo_name, algo_name1, RUNS, {1, {7}},
            {1, {7}}, data_type);
2707 2708 2709
    shapes_and_computation.clear();
}

2710
TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32_6x16) {
2711 2712 2713 2714 2715 2716 2717 2718 2719
    constexpr size_t RUNS = 50;

    param::ConvBias param;
    param.nonlineMode = param::ConvBias::NonlineMode::RELU;
    param.pad_h = 1;
    param.pad_w = 1;
    param.stride_h = 1;
    param.stride_w = 1;

2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
    std::vector<DType> data_type = {
            dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
    auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
                          size_t group) {
        SmallVector<TensorShape> shapes{
                {N, IC, H, W},
                {OC / group, IC / group, FS, FS},
                {1, OC, 1, 1},
                {},
                {N, OC, H, W}};
2731
        TensorShape dst{N, OC, H, W};
2732 2733 2734
        float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
                              dst.total_nr_elems()) *
                             1e-6;
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);
2746

2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
    bench_case(1, 64, 32, 7, 7, 3, 1);
    bench_case(1, 64, 64, 7, 7, 3, 1);
    bench_case(1, 64, 128, 7, 7, 3, 1);
    bench_case(1, 64, 256, 7, 7, 3, 1);
    bench_case(1, 64, 512, 7, 7, 3, 1);
    bench_case(1, 64, 1024, 7, 7, 3, 1);

    bench_case(1, 64, 32, 14, 14, 3, 1);
    bench_case(1, 64, 64, 14, 14, 3, 1);
    bench_case(1, 64, 128, 14, 14, 3, 1);
    bench_case(1, 64, 256, 14, 14, 3, 1);
    bench_case(1, 64, 512, 14, 14, 3, 1);

    bench_case(1, 64, 1024, 14, 14, 3, 1);
    bench_case(1, 128, 128, 14, 14, 3, 1);
    bench_case(1, 128, 256, 14, 14, 3, 1);
    bench_case(1, 512, 512, 14, 14, 3, 1);
    bench_case(1, 256, 512, 14, 14, 3, 1);
    bench_case(1, 512, 1024, 14, 14, 3, 1);
    bench_case(1, 1024, 1024, 14, 14, 3, 1);

2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
    std::string algo_name = "IM2COLMATMUL:X86_F32_6x16:192";
    printf("Benchmark IM2COLMATMUL:X86_F32_6x16 algo\n");
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
            data_type);
2779 2780 2781
    shapes_and_computation.clear();
}

2782
TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_F32_6X16_single_thread) {
2783 2784 2785 2786 2787 2788 2789 2790 2791
    constexpr size_t RUNS = 50;

    param::ConvBias param;
    param.nonlineMode = param::ConvBias::NonlineMode::RELU;
    param.pad_h = 1;
    param.pad_w = 1;
    param.stride_h = 1;
    param.stride_w = 1;

2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802
    std::vector<DType> data_type = {
            dtype::Float32(), dtype::Float32(), dtype::Float32(), dtype::Float32()};
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
    auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
                          size_t group) {
        SmallVector<TensorShape> shapes{
                {N, IC, H, W},
                {OC / group, IC / group, FS, FS},
                {1, OC, 1, 1},
                {},
                {N, OC, H, W}};
2803
        TensorShape dst{N, OC, H, W};
2804 2805 2806
        float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
                              dst.total_nr_elems()) *
                             1e-6;
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);

    bench_case(1, 64, 32, 7, 7, 3, 1);
    bench_case(1, 64, 64, 7, 7, 3, 1);
    bench_case(1, 64, 128, 7, 7, 3, 1);
    bench_case(1, 64, 256, 7, 7, 3, 1);
    bench_case(1, 64, 512, 7, 7, 3, 1);
    bench_case(1, 64, 1024, 7, 7, 3, 1);

    bench_case(1, 64, 32, 14, 14, 3, 1);
    bench_case(1, 64, 64, 14, 14, 3, 1);
    bench_case(1, 64, 128, 14, 14, 3, 1);
    bench_case(1, 64, 256, 14, 14, 3, 1);
    bench_case(1, 64, 512, 14, 14, 3, 1);

    bench_case(1, 64, 1024, 14, 14, 3, 1);
    bench_case(1, 128, 128, 14, 14, 3, 1);
    bench_case(1, 128, 256, 14, 14, 3, 1);
    bench_case(1, 512, 512, 14, 14, 3, 1);
    bench_case(1, 256, 512, 14, 14, 3, 1);
    bench_case(1, 512, 1024, 14, 14, 3, 1);
    bench_case(1, 1024, 1024, 14, 14, 3, 1);

    std::string algo_name = "IM2COLMATMUL:X86_F32_MKL_PACKA:192";
    std::string algo_name1 = "IM2COLMATMUL:X86_F32_6x16:192";
    printf("Benchmark IM2COLMATMUL:X86_F32_6x16 algo\n");
2843 2844 2845 2846 2847 2848
    benchmark_impl_comp(
            param, shapes_and_computation, algo_name, algo_name1, RUNS, {1, {4}},
            {1, {4}}, data_type);
    benchmark_impl_comp(
            param, shapes_and_computation, algo_name, algo_name1, RUNS, {1, {7}},
            {1, {7}}, data_type);
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
    shapes_and_computation.clear();
}

TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_CONVBIAS_IM2COL_INT8X8X32) {
    constexpr size_t RUNS = 50;

    param::ConvBias param;
    param.pad_h = 1;
    param.pad_w = 1;
    param.stride_h = 1;
    param.stride_w = 1;

M
Megvii Engine Team 已提交
2861 2862 2863 2864 2865 2866 2867 2868 2869
    std::vector<std::pair<SmallVector<TensorShape>, float>> shapes_and_computation;
    auto bench_case = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS,
                          size_t group) {
        SmallVector<TensorShape> shapes{
                {N, IC, H, W},
                {OC / group, IC / group, FS, FS},
                {1, OC, 1, 1},
                {},
                {N, OC, H, W}};
2870
        TensorShape dst{N, OC, H, W};
M
Megvii Engine Team 已提交
2871 2872 2873
        float computations = ((IC / group) * FS * FS * dst.total_nr_elems() * 2 +
                              dst.total_nr_elems()) *
                             1e-6;
2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
        shapes_and_computation.push_back(std::make_pair(shapes, computations));
    };

    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 200, 200, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 128, 128, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 100, 100, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);
    bench_case(1, 32, 32, 80, 80, 3, 1);

    bench_case(1, 64, 32, 7, 7, 3, 1);
    bench_case(1, 64, 64, 7, 7, 3, 1);
    bench_case(1, 64, 128, 7, 7, 3, 1);
    bench_case(1, 64, 256, 7, 7, 3, 1);
    bench_case(1, 64, 512, 7, 7, 3, 1);
    bench_case(1, 64, 1024, 7, 7, 3, 1);

    bench_case(1, 64, 32, 14, 14, 3, 1);
    bench_case(1, 64, 64, 14, 14, 3, 1);
    bench_case(1, 64, 128, 14, 14, 3, 1);
    bench_case(1, 64, 256, 14, 14, 3, 1);
    bench_case(1, 64, 512, 14, 14, 3, 1);

    bench_case(1, 64, 1024, 14, 14, 3, 1);
    bench_case(1, 128, 128, 14, 14, 3, 1);
    bench_case(1, 128, 256, 14, 14, 3, 1);
    bench_case(1, 512, 512, 14, 14, 3, 1);
    bench_case(1, 256, 512, 14, 14, 3, 1);
    bench_case(1, 512, 1024, 14, 14, 3, 1);
    bench_case(1, 1024, 1024, 14, 14, 3, 1);

M
Megvii Engine Team 已提交
2907 2908
    std::vector<DType> data_type = {
            dtype::Int8(), dtype::Int8(), dtype::Int32(), dtype::Int32()};
2909
    std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2:192";
2910 2911
    // std::string algo_name = "IM2COLMATMUL:X86_INT8X8X32_AVX2_2X4X16";
    // printf("Benchmark IM2COLMATMUL:X86_INT8X8X32_AVX2_4X16X2 algo\n");
M
Megvii Engine Team 已提交
2912 2913 2914 2915 2916 2917 2918 2919 2920
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {4}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {4, {4, 5, 6, 7}}, {1, {7}},
            data_type);
    benchmark_impl(
            param, shapes_and_computation, algo_name, RUNS, {2, {4, 5}}, {1, {4}},
            data_type);
2921 2922 2923
    shapes_and_computation.clear();
}

2924
namespace {
M
Megvii Engine Team 已提交
2925 2926
std::vector<conv_bias::TestArg> get_winograd_benchmark_args(
        size_t kernel, size_t pack_size) {
2927
    std::vector<conv_bias::TestArg> args;
M
Megvii Engine Team 已提交
2928
    auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel, size_t p) {
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
        if (ic % pack_size != 0 || oc % pack_size != 0)
            return;
        if (w + 2 * p < kernel || h + 2 * p < kernel)
            return;

        param::ConvBias param;
        param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
        param.format = param::ConvBias::Format::NCHW88;
        param.sparse = param::ConvBias::Sparse::DENSE;
        param.nonlineMode = param::ConvBias::NonlineMode::RELU;
        param.stride_h = 1;
        param.stride_w = 1;
        param.pad_h = p;
        param.pad_w = p;

2944 2945 2946 2947 2948
        args.push_back(conv_bias::TestArg{
                param,
                TensorShape{1, ic / 8, h, w, 8},
                TensorShape{oc / 8, ic / 8, kernel, kernel, 8, 8},
                {1, oc / 8, 1, 1, 8}});
2949 2950
    };
    for (size_t ic : {64, 128, 256}) {
2951
        for (size_t oc : {64, 128, 256}) {
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
            pack(oc, ic, 56, 56, kernel, kernel / 2);
            pack(oc, ic, 14, 14, kernel, kernel / 2);
            pack(oc, ic, 28, 28, kernel, kernel / 2);
        }
    }

    //! conv in vgg16
    pack(512, 512, 15, 15, kernel, kernel / 2);
    pack(512, 256, 15, 15, kernel, kernel / 2);
    pack(256, 256, 29, 29, kernel, kernel / 2);
    pack(256, 128, 29, 29, kernel, kernel / 2);
    pack(128, 128, 57, 57, kernel, kernel / 2);
    pack(128, 64, 57, 57, kernel, kernel / 2);
    pack(64, 64, 56, 56, kernel, kernel / 2);
    pack(128, 128, 28, 28, kernel, kernel / 2);
    pack(512, 512, 14, 14, kernel, kernel / 2);
    return args;
}

M
Megvii Engine Team 已提交
2971 2972
void benchmark_winograd(
        const char* algo_name, Handle* handle, size_t kernel, size_t pack_size) {
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
    auto&& args = get_winograd_benchmark_args(kernel, pack_size);
    using namespace conv_bias;
    constexpr size_t RUN = 10;
    Benchmarker<ConvBias> benchmark(handle);
    benchmark.set_display(false);
    benchmark.set_times(RUN);

    Benchmarker<ConvBias> benchmark_winograd(handle);
    benchmark_winograd.set_display(false);
    benchmark_winograd.set_times(RUN);

    for (auto&& arg : args) {
        TensorLayout dst_layout;
        auto opr = handle->create_operator<ConvBias>();
        opr->param() = arg.param;
M
Megvii Engine Team 已提交
2988 2989 2990
        opr->deduce_layout(
                {arg.src, dtype::Float32()}, {arg.filter, dtype::Float32()},
                {arg.bias, dtype::Float32()}, {}, dst_layout);
2991 2992 2993 2994 2995
        //! dst.nr_elems * IC * FH * FW * 2
        float computations = dst_layout.total_nr_elems() * arg.filter[1] *
                             arg.filter[2] * arg.filter[3] * 2.0 * 8.0 /
                             (1024 * 1024 * 1024) * 1e3;

M
Megvii Engine Team 已提交
2996 2997 2998
        auto used =
                benchmark.set_param(arg.param).exec({arg.src, arg.filter, {}, {}, {}}) /
                RUN;
2999 3000

        benchmark_winograd.set_param(arg.param);
M
Megvii Engine Team 已提交
3001 3002 3003 3004
        auto used_winograd = algo_benchmark<ConvBias>(
                                     benchmark_winograd,
                                     {arg.src, arg.filter, {}, {}, {}}, algo_name) /
                             RUN;
3005 3006 3007 3008

        printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
               "speedup: "
               "%f\n",
M
Megvii Engine Team 已提交
3009 3010 3011
               arg.src.to_string().c_str(), arg.filter.to_string().c_str(), used,
               computations / used, used_winograd, computations / used_winograd,
               used / used_winograd);
3012 3013
    }
}
3014
}  // namespace
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029

TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F63_8x8) {
    benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:6:8", handle(), 3, 8);
}

TEST_F(X86, BENCHMARK_CONVBIAS_WINOGRAD_F23_8x8) {
    benchmark_winograd("WINOGRAD:X86_F32MK8_8X8:8:2:8", handle(), 3, 8);
}

#endif

}  // namespace test
}  // namespace megdnn

// vim: syntax=cpp.doxygen