algos.cpp 31.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * \file dnn/src/fallback/conv_bias/im2col/algos.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#include "src/fallback/conv_bias/im2col/algos.h"
13
#include "src/fallback/conv_bias/im2col/factory.h"
14 15 16 17 18 19
#include "megdnn/opr_param_defs.h"
#include "src/common/opr_delegate.h"
#include "src/fallback/conv_bias/common.h"
#include "src/fallback/conv_bias/opr_impl.h"
#include "src/fallback/conv_bias/winograd/strategy.h"
#include "src/naive/convolution/helper.h"
20

21
#include "midout.h"
22

23 24 25 26
MIDOUT_DECL(megdnn_fallback_im2col)

using namespace megdnn;
using namespace fallback;
27
using namespace im2col;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

/*======================== AlgoIm2col=======================*/
/*!
 *  *\brief The index of all parts workspace in im2col workspace bundel
 *  *Through witch can convenient get the needed ptr
 */
struct Im2colBundelIndex {
    static constexpr size_t BUNDLE_PADDING_INDEX = 0_z;
    static constexpr size_t BUNDLE_PACKA_INDEX = 1_z;
    static constexpr size_t BUNDLE_THREAD_INDEX = 2_z;
};

using Pack_Mode=fallback::MatrixMulImpl::AlgoBase::PackMode;

//! Process one input channel copy padding
static void copy_padding_kern(WorkspaceBundle bundle,
                              const ConvBiasImpl::NCBKernParam& param,
45
                              const ConvBiasImpl::NCBKernIndex& ncb_index,
46 47
                              StrategyBase* im2colstrategy, size_t pack_oc_size) {
    im2colstrategy->copy_padding_kern(bundle, param, ncb_index, pack_oc_size);
48
}
49

50 51 52 53 54 55
//! packA_kern
static void packA_kern(WorkspaceBundle bundle,
                       const fallback::ConvBiasImpl::NCBKernParam& param,
                       fallback::MatrixMulImpl::KernSizeParam matmulparam,
                       fallback::MatrixMulImpl::AlgoBase* matmul_algo,
                       const fallback::ConvBiasImpl::NCBKernIndex& ncb_index,
56
                       StrategyBase* im2colstrategy, size_t pack_oc_size) {
57
    im2colstrategy->packA_kern(bundle, param, matmulparam, matmul_algo,
58
                               ncb_index, pack_oc_size);
59
}
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

/*!
 * *\brief Im2colKerns collects all the im2col kerns in it
 */

template <Pack_Mode packmode>
class Im2colKerns;

template <>
class Im2colKerns<Pack_Mode::DEFAULT> {
public:
    //! conv kernel
    static void kerns(
            WorkspaceBundle bundle, WorkspaceBundle bundle_thread,
            const ConvBiasImpl::NCBKernParam& param,
            fallback::MatrixMulImpl::KernSizeParam matmul_kernsize_param,
            fallback::MatrixMulImpl::AlgoBase* matmul_algo,
77
            StrategyParam strategyparam,
78
            fallback::ConvBiasImpl::NCBKernIndex ncb_index,
79 80
            size_t ohw_tile_size, StrategyBase* im2colstrategy) {
        size_t OC = param.filter_meta.ocpg;
81
        size_t output_block_size = std::min(
82 83
                ohw_tile_size,
                strategyparam.ohw - ncb_index.ndrange_id[2] * ohw_tile_size);
84
        size_t output_block_oc_size = std::min(
85 86 87 88 89 90 91 92 93 94 95 96 97 98
                strategyparam.oc_tile_size,
                OC - ncb_index.ndrange_id[3] * strategyparam.oc_tile_size);

        strategyparam.batch_id = ncb_index.ndrange_id[0];
        strategyparam.group_id = ncb_index.ndrange_id[1];
        strategyparam.oc_cur_index =
                ncb_index.ndrange_id[3] *
                strategyparam.oc_tile_size;
        strategyparam.oc_end_index = strategyparam.oc_cur_index +
                                     output_block_oc_size;
        strategyparam.ohw_cur_index =
                ncb_index.ndrange_id[2] * ohw_tile_size;
        strategyparam.output_block_oc_size = output_block_oc_size;
        strategyparam.output_block_size = output_block_size;
99 100

        bundle.set(param.workspace_ptr);
101 102 103 104
        bundle_thread.set(
                static_cast<int8_t*>(
                        bundle.get(Im2colBundelIndex::BUNDLE_THREAD_INDEX)) +
                bundle_thread.total_size_in_bytes() * ncb_index.thread_id);
105 106 107 108
        fallback::MatrixMulImpl::KernParam matmul_param;
        static_cast<fallback::MatrixMulImpl::KernSizeParam&>(matmul_param) =
                matmul_kernsize_param;

109 110 111
        //! 1.Im2col
        im2colstrategy->exec_im2col(bundle, bundle_thread, strategyparam, param,
                                    matmul_param, matmul_algo);
112

113 114 115
        //! 2.packb and matmul compute
        im2colstrategy->exec_matmul(param, strategyparam, bundle, bundle_thread,
                                    matmul_param, matmul_algo, ncb_index);
116

117 118 119
        //! 3.postprocess and copy dst if need
        im2colstrategy->exec_postprocess(param, strategyparam, bundle_thread);
    }
120

121 122 123 124 125 126 127
    WorkspaceBundle get_thread_bundle(
            const fallback::ConvBiasImpl::NCBKernSizeParam& param,
            fallback::MatrixMulImpl::KernSizeParam im2col_kern_param,
            MatrixMulImpl::AlgoBase* matmul_algo, size_t ohw_tile_size,
            size_t oc_tile_size) {
        size_t IC = param.filter_meta.icpg, FH = param.filter_meta.spatial[0],
               FW = param.filter_meta.spatial[1];
128
        size_t pack_oc_size = 1;
129 130 131
        size_t im2col = 0, packb = 0, bias_temp = 0;
        bool default_pack = matmul_algo->packmode() == Pack_Mode::DEFAULT;
        megdnn_assert(default_pack, "only support default packa");
132 133 134
        if (param.filter_meta.format == param::ConvBias::Format::NCHW44) {
            pack_oc_size = 4;
        }
135 136
        size_t im2col_dst_size =
                IC * FH * FW * ohw_tile_size * sizeof(param.src_type);
137 138
        size_t matmul_dst_size = pack_oc_size * oc_tile_size * ohw_tile_size *
                                 sizeof(param.bias_type);
139 140 141 142 143 144 145 146
        //! matmul_dst and im2col_dst use the same memory
        WorkspaceBundle wb = matmul_algo->get_bundle(im2col_kern_param);
        packb = wb.get_size(1);
        im2col = std::max(im2col_dst_size, matmul_dst_size);
        if (param.bias_mode == megdnn::BiasMode::BIAS) {
            bias_temp = oc_tile_size * ohw_tile_size * sizeof(param.bias_type);
        }
        return {nullptr, {packb, im2col, bias_temp}};
147 148 149 150 151 152 153 154 155 156 157 158
    }
};

template <>
class Im2colKerns<Pack_Mode::ONLY_PACKA> {
public:
    //! conv kernel
    static void kerns(
            WorkspaceBundle bundle, WorkspaceBundle bundle_thread,
            const ConvBiasImpl::NCBKernParam& param,
            fallback::MatrixMulImpl::KernSizeParam matmul_kernsize_param,
            fallback::MatrixMulImpl::AlgoBase* matmul_algo,
159
            StrategyParam strategyparam,
160
            fallback::ConvBiasImpl::NCBKernIndex ncb_index,
161 162
            size_t ohw_tile_size, StrategyBase* im2colstrategy) {
        size_t OC = param.filter_meta.ocpg;
163
        size_t output_block_size = std::min(
164 165
                ohw_tile_size,
                strategyparam.ohw - ncb_index.ndrange_id[2] * ohw_tile_size);
166
        size_t output_block_oc_size = std::min(
167 168
                strategyparam.oc_tile_size,
                OC - ncb_index.ndrange_id[3] * strategyparam.oc_tile_size);
169 170

        bundle.set(param.workspace_ptr);
171 172 173 174
        bundle_thread.set(
                static_cast<int8_t*>(
                        bundle.get(Im2colBundelIndex::BUNDLE_THREAD_INDEX)) +
                bundle_thread.total_size_in_bytes() * ncb_index.thread_id);
175 176 177 178 179

        fallback::MatrixMulImpl::KernParam matmul_param;
        static_cast<fallback::MatrixMulImpl::KernSizeParam&>(matmul_param) =
                matmul_kernsize_param;

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        strategyparam.batch_id = ncb_index.ndrange_id[0];
        strategyparam.group_id = ncb_index.ndrange_id[1];
        strategyparam.oc_cur_index =
                ncb_index.ndrange_id[3] *
                strategyparam.oc_tile_size;
        strategyparam.oc_end_index = strategyparam.oc_cur_index +
                                     output_block_oc_size;
        strategyparam.ohw_cur_index =
                ncb_index.ndrange_id[2] * ohw_tile_size;
        strategyparam.output_block_oc_size = output_block_oc_size;
        strategyparam.output_block_size = output_block_size;

        //! 1.Im2col
        im2colstrategy->exec_im2col(bundle, bundle_thread, strategyparam, param,
                                    matmul_param, matmul_algo);

        //! 2.packb and matmul compute
        im2colstrategy->exec_matmul(param, strategyparam, bundle, bundle_thread,
                                    matmul_param, matmul_algo, ncb_index);

        //! 3.postprocess and copy dst if need
        im2colstrategy->exec_postprocess(param, strategyparam, bundle_thread);
    }
    WorkspaceBundle get_thread_bundle(
            const fallback::ConvBiasImpl::NCBKernSizeParam& param,
            fallback::MatrixMulImpl::KernSizeParam im2col_kern_param,
            MatrixMulImpl::AlgoBase* matmul_algo, size_t ohw_tile_size,
            size_t oc_tile_size) {
        size_t IC = param.filter_meta.icpg, FH = param.filter_meta.spatial[0],
               FW = param.filter_meta.spatial[1];

        size_t im2col = 0, packb = 0, matmul_dst = 0, bias_temp = 0;
        bool only_packA = matmul_algo->packmode() == Pack_Mode::ONLY_PACKA;
        megdnn_assert(only_packA, "onlysupport onlypackA mode");
        size_t im2col_dst_size =
                IC * FH * FW * ohw_tile_size * sizeof(param.src_type);
        size_t matmul_dst_size =
                oc_tile_size * ohw_tile_size * sizeof(param.bias_type);
        //! matmul_dst and im2col_dst use the same memory
        WorkspaceBundle wb = matmul_algo->get_bundle(im2col_kern_param);
        packb = wb.get_size(1);
        im2col = im2col_dst_size;
        matmul_dst = matmul_dst_size;
        if (param.bias_mode == megdnn::BiasMode::BIAS) {
            bias_temp = oc_tile_size * ohw_tile_size * sizeof(param.bias_type);
        }
226

227
        return {nullptr, {packb, im2col, matmul_dst, bias_temp}};
228 229 230 231 232 233 234 235 236 237 238 239
    }
};

template <>
class Im2colKerns<Pack_Mode::NO_PACK> {
public:
    //! conv kernel
    static void kerns(
            WorkspaceBundle bundle, WorkspaceBundle bundle_thread,
            const ConvBiasImpl::NCBKernParam& param,
            fallback::MatrixMulImpl::KernSizeParam matmul_kernsize_param,
            fallback::MatrixMulImpl::AlgoBase* matmul_algo,
240
            StrategyParam strategyparam,
241
            fallback::ConvBiasImpl::NCBKernIndex ncb_index,
242 243
            size_t ohw_tile_size, StrategyBase* im2colstrategy) {
        size_t OC = param.filter_meta.ocpg;
244
        size_t output_block_size = std::min(
245 246
                ohw_tile_size,
                strategyparam.ohw - ncb_index.ndrange_id[2] * ohw_tile_size);
247
        size_t output_block_oc_size = std::min(
248 249 250 251 252 253 254 255 256 257 258 259 260 261
                strategyparam.oc_tile_size,
                OC - ncb_index.ndrange_id[3] * strategyparam.oc_tile_size);

        strategyparam.batch_id = ncb_index.ndrange_id[0];
        strategyparam.group_id = ncb_index.ndrange_id[1];
        strategyparam.oc_cur_index =
                ncb_index.ndrange_id[3] *
                strategyparam.oc_tile_size;
        strategyparam.oc_end_index = strategyparam.oc_cur_index +
                                     output_block_oc_size;
        strategyparam.ohw_cur_index =
                ncb_index.ndrange_id[2] * ohw_tile_size;
        strategyparam.output_block_oc_size = output_block_oc_size;
        strategyparam.output_block_size = output_block_size;
262 263

        bundle.set(param.workspace_ptr);
264 265 266 267
        bundle_thread.set(
                static_cast<int8_t*>(
                        bundle.get(Im2colBundelIndex::BUNDLE_THREAD_INDEX)) +
                bundle_thread.total_size_in_bytes() * ncb_index.thread_id);
268 269 270 271 272

        fallback::MatrixMulImpl::KernParam matmul_param;
        static_cast<fallback::MatrixMulImpl::KernSizeParam&>(matmul_param) =
                matmul_kernsize_param;

273 274 275
        //! 1.Im2col
        im2colstrategy->exec_im2col(bundle, bundle_thread, strategyparam, param,
                                    matmul_param, matmul_algo);
276

277 278 279
        //! 2.packb and matmul compute
        im2colstrategy->exec_matmul(param, strategyparam, bundle, bundle_thread,
                                    matmul_param, matmul_algo, ncb_index);
280

281 282 283 284 285 286 287 288 289 290 291
        //! 3.postprocess and copy dst if need
        im2colstrategy->exec_postprocess(param, strategyparam, bundle_thread);
    }
    WorkspaceBundle get_thread_bundle(
            const fallback::ConvBiasImpl::NCBKernSizeParam& param,
            fallback::MatrixMulImpl::KernSizeParam im2col_kern_param,
            MatrixMulImpl::AlgoBase* matmul_algo, size_t ohw_tile_size,
            size_t oc_tile_size) {
        size_t IC = param.filter_meta.icpg, FH = param.filter_meta.spatial[0],
               FW = param.filter_meta.spatial[1];
        size_t ohw = param.osz[0] * param.osz[1];
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
        size_t im2col = 0, matmul_dst = 0, bias_temp = 0, matmul_compute = 0;
        bool no_pack = matmul_algo->packmode() == Pack_Mode::NO_PACK;
        megdnn_assert(no_pack, "only support no pack");
        bool is_dst_8bit =
                (param.src_type.enumv() == DTypeEnum::QuantizedS8 &&
                 param.dst_type.enumv() == DTypeEnum::QuantizedS8) ||
                (param.src_type.enumv() == DTypeEnum::Quantized8Asymm &&
                 param.dst_type.enumv() == DTypeEnum::Quantized8Asymm);
        size_t im2col_dst_size =
                IC * FH * FW * ohw_tile_size * sizeof(param.src_type);
        size_t matmul_dst_size =
                oc_tile_size * ohw_tile_size * sizeof(param.bias_type);
        im2col = im2col_dst_size;
        if (is_dst_8bit) {
            matmul_dst = matmul_dst_size;
        } else {
            matmul_dst = ohw_tile_size >= ohw ? 0 : matmul_dst_size;
        }
        matmul_compute = matmul_algo->get_workspace(im2col_kern_param);
        if (param.bias_mode == megdnn::BiasMode::BIAS) {
            bias_temp = oc_tile_size * ohw_tile_size * sizeof(param.bias_type);
        }
315

316
        return {nullptr, {im2col, matmul_dst, bias_temp, matmul_compute}};
317 318 319 320 321 322 323
    }
};

fallback::MatrixMulImpl::KernSizeParam
ConvBiasImpl::AlgoIm2col ::get_matmul_kern_param(const NCBKernSizeParam& param,
                                                 size_t ohw_tile_size,
                                                 size_t oc_tile_size) const {
324 325
    bool is_nchw44 =
            param.filter_meta.format == param::ConvBias::Format::NCHW44;
326 327 328 329
    size_t M = oc_tile_size;
    size_t N = ohw_tile_size;
    size_t K = param.filter_meta.icpg * param.filter_meta.spatial[0] *
               param.filter_meta.spatial[1];
330 331
    size_t pack_oc_size = is_nchw44 ? 4 : 1;
    size_t LDA = pack_oc_size * K, LDB = pack_oc_size * N, LDC = N;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    bool is_dst_8bit = (param.src_type.enumv() == DTypeEnum::QuantizedS8 &&
                        param.dst_type.enumv() == DTypeEnum::QuantizedS8) ||
                       (param.src_type.enumv() == DTypeEnum::Quantized8Asymm &&
                        param.dst_type.enumv() == DTypeEnum::Quantized8Asymm);
    return {param.filter_type,
            param.src_type,
            is_dst_8bit ? param.bias_type : param.dst_type,
            M,
            N,
            K,
            LDA,
            LDB,
            LDC,
            false,
            false,
            param::MatrixMul::ComputeMode::DEFAULT,
348 349
            is_nchw44 ? param::MatrixMul::Format::MK4
                      : param::MatrixMul::Format::DEFAULT};
350 351 352
}

void ConvBiasImpl::AlgoIm2col::choice_ohw_oc_block(
353 354
        const NCBKernSizeParam& param, size_t& oc_tile_size,
        size_t& ohw_tile_size, size_t block_m, size_t block_n,
355 356 357 358 359 360 361 362 363
        bool need_pack) const {
    size_t nr_threads = param.nr_threads;
    size_t OC = param.filter_meta.ocpg;
    size_t ohw = param.osz[0] * param.osz[1];
    //! pay attention please, should not change the 2 line code,
    //! the opr use the same im2col algo, via choice_ohw_oc_block may change the
    //! m_ohw_tile_size and m_oc_tile_size, if the two value changed, the
    //! workspace size may change, will ocur workspace not match problem, so
    //! should use the original data init them to avoid the problem
364 365
    oc_tile_size = DEFAULT_OC_TILE_SIZE;
    ohw_tile_size = m_ohw_tile_size;
366

367 368
    oc_tile_size = std::min(oc_tile_size, OC);
    ohw_tile_size = std::min(ohw_tile_size, ohw);
369 370

    if (nr_threads > 1) {
371 372 373 374 375 376 377 378 379
        if (ohw / ohw_tile_size < nr_threads) {
            ohw_tile_size = round_up(div_ceil(ohw, nr_threads), block_n);
            if (ohw_tile_size < DEFAULT_OHW_MIN_TILE_SIZE) {
                ohw_tile_size = ohw;
                oc_tile_size = round_up(div_ceil(OC, nr_threads), block_m);
                if (oc_tile_size > DEFAULT_OC_MAX_TILE_SIZE) {
                    oc_tile_size = DEFAULT_OC_MAX_TILE_SIZE;
                } else if (oc_tile_size < DEFAULT_OC_MIN_TILE_SIZE) {
                    oc_tile_size = DEFAULT_OC_MIN_TILE_SIZE;
380 381 382 383 384
                }
            }
        }
    } else {
        if (!need_pack) {  //! no pack ,usually in x86 save memroy
385 386
            ohw_tile_size = ohw;
            oc_tile_size = OC;
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        }
    }
}

WorkspaceBundle ConvBiasImpl::AlgoIm2col::get_bundle(
        const NCBKernSizeParam& param) const {
    UNPACK_CONV_F32_NCB_KERN_SIZES(param);
    MEGDNN_MARK_USED_VAR(OC);
    MEGDNN_MARK_USED_VAR(OH);
    MEGDNN_MARK_USED_VAR(OW);
    MEGDNN_MARK_USED_VAR(FH);
    MEGDNN_MARK_USED_VAR(FW);
    MEGDNN_MARK_USED_VAR(SW);
    MEGDNN_MARK_USED_VAR(SH);

    auto IW2 = IH + 2 * PH;
    auto IH2 = IW + 2 * PW;
    bool no_need_pading = (PH == 0 && PW == 0);
    size_t padding = 0, packa_size = 0, packa_group_size = 0;
    size_t nr_threads = param.nr_threads;
    size_t GROUP = param.filter_meta.group;
    bool need_pack = m_matmul_algo->packmode() == Pack_Mode::DEFAULT;
    bool only_packA = m_matmul_algo->packmode() == Pack_Mode::ONLY_PACKA;
410
    size_t oc_tile_size = 0, ohw_tile_size = 0;
411 412
    if (need_pack || only_packA) {
        auto inner_block = m_matmul_algo->get_inner_block_size();
413 414
        choice_ohw_oc_block(param, oc_tile_size, ohw_tile_size, inner_block.m,
                            inner_block.n, need_pack);
415
        auto im2col_kern_param = get_matmul_kern_param(
416 417
                param, ohw_tile_size, only_packA ? oc_tile_size : OC);
        size_t oc_parallel_times = div_ceil<size_t>(OC, oc_tile_size);
418 419 420 421 422 423
        WorkspaceBundle wb = m_matmul_algo->get_bundle(im2col_kern_param);
        packa_group_size = only_packA ? oc_parallel_times * wb.get_size(0)
                                      : wb.get_size(0);
    } else {  //! not support pack,not need pack
        size_t nopack_default_blockm = 8;
        size_t nopack_default_blockn = 16;
424 425
        choice_ohw_oc_block(param, oc_tile_size, ohw_tile_size,
                            nopack_default_blockm, nopack_default_blockn,
426 427 428
                            need_pack);
        packa_group_size = 0;
    }
429

430 431 432 433 434 435
    if (no_need_pading) {
        padding = 0;  //! not need  padding
    } else {
        padding = (GROUP * N * IC * IH2 * IW2) *
                  sizeof(param.src_type);  //! for padding
    }
436

437
    packa_size = GROUP * packa_group_size;  //! for packA  size = GROUP * a_size
438
    WorkspaceBundle ws = {nullptr, {}};
439
    auto im2col_kern_param =
440
            get_matmul_kern_param(param, ohw_tile_size, oc_tile_size);
441

442 443 444
    if (m_matmul_algo->packmode() == Pack_Mode::DEFAULT) {
        Im2colKerns<Pack_Mode::DEFAULT> defaultkern;
        ws = defaultkern.get_thread_bundle(param, im2col_kern_param,
445 446
                                           m_matmul_algo, ohw_tile_size,
                                           oc_tile_size);
447 448 449
    } else if (m_matmul_algo->packmode() == Pack_Mode::ONLY_PACKA) {
        Im2colKerns<Pack_Mode::ONLY_PACKA> onlypackakern;
        ws = onlypackakern.get_thread_bundle(param, im2col_kern_param,
450 451
                                             m_matmul_algo, ohw_tile_size,
                                             oc_tile_size);
452
    } else {
453 454
        Im2colKerns<Pack_Mode::NO_PACK> nopackkern;
        ws = nopackkern.get_thread_bundle(param, im2col_kern_param,
455 456
                                          m_matmul_algo, ohw_tile_size,
                                          oc_tile_size);
457
    }
458

459 460
    return {nullptr,
            {padding, packa_size, ws.total_size_in_bytes() * nr_threads}};
461 462 463 464 465 466 467 468 469 470 471 472
}

size_t ConvBiasImpl::AlgoIm2col::get_workspace(
        ConvBiasImpl*, const NCBKernSizeParam& p) const {
    MIDOUT_BEGIN(megdnn_fallback_im2col, 0, 0) {
        return get_bundle(p).total_size_in_bytes();
    }
    MIDOUT_END();
    return 0;
}

SmallVector<ConvBiasImpl::NCBKern> ConvBiasImpl::AlgoIm2col::dispatch_kerns(
473
        ConvBiasImpl*, const NCBKernSizeParam& param) const {
474
    MIDOUT_BEGIN(megdnn_fallback_im2col, 0, 1) {
475 476 477 478 479 480 481
        UNPACK_CONV_F32_NCB_KERN_SIZES(param);
        MEGDNN_MARK_USED_VAR(SH);
        MEGDNN_MARK_USED_VAR(SW);
        MEGDNN_MARK_USED_VAR(IH);
        MEGDNN_MARK_USED_VAR(IW);
        MEGDNN_MARK_USED_VAR(FH);
        MEGDNN_MARK_USED_VAR(FW);
482
        size_t oc_tile_size = 0, ohw_tile_size = 0;
483
        size_t ohw = OH * OW;
484 485
        size_t GROUP = param.filter_meta.group;
        WorkspaceBundle bundle = get_bundle(param);
486
        WorkspaceBundle bundle_thread = {nullptr, {}};
487
        bool need_padding = (PH != 0 || PW != 0);
488 489 490 491
        Pack_Mode packmode = m_matmul_algo->packmode();
        bool default_pack = packmode == Pack_Mode::DEFAULT;
        bool no_pack = packmode == Pack_Mode::NO_PACK;
        bool only_packA = packmode == Pack_Mode::ONLY_PACKA;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506

        if (default_pack || only_packA) {
            auto inner_block = m_matmul_algo->get_inner_block_size();
            choice_ohw_oc_block(param, oc_tile_size, ohw_tile_size,
                                inner_block.m, inner_block.n, default_pack);
        } else {  //! not support pack,not need pack
            size_t nopack_default_blockm = 8;
            size_t nopack_default_blockn = 16;
            choice_ohw_oc_block(param, oc_tile_size, ohw_tile_size,
                                nopack_default_blockm, nopack_default_blockn,
                                no_pack);
        }

        size_t ohw_parallel_times = div_ceil(ohw, ohw_tile_size);
        size_t oc_parallel_times = div_ceil<size_t>(OC, oc_tile_size);
507
        size_t packa_parallel_times = 0;
508 509 510
        size_t pack_oc_size =
                (param.filter_meta.format == param::ConvBias::Format::NCHW ? 1
                                                                           : 4);
511
        if (only_packA) {
512
            packa_parallel_times = div_ceil<size_t>(OC, oc_tile_size);
513
        } else if (default_pack) {
514
            packa_parallel_times = div_ceil<size_t>(
515
                    OC, m_matmul_algo->get_inner_block_size().m * pack_oc_size);
516 517 518
        }

        auto matmul_param = get_matmul_kern_param(
519
                param, ohw_tile_size, only_packA ? oc_tile_size : OC);
520 521 522
        if (m_matmul_algo->packmode() == Pack_Mode::DEFAULT) {
            Im2colKerns<Pack_Mode::DEFAULT> defaultkern;
            bundle_thread = defaultkern.get_thread_bundle(
523 524
                    param, matmul_param, m_matmul_algo, ohw_tile_size,
                    oc_tile_size);
525 526 527
        } else if (m_matmul_algo->packmode() == Pack_Mode::ONLY_PACKA) {
            Im2colKerns<Pack_Mode::ONLY_PACKA> onlypackakern;
            bundle_thread = onlypackakern.get_thread_bundle(
528 529
                    param, matmul_param, m_matmul_algo, ohw_tile_size,
                    oc_tile_size);
530 531 532
        } else {
            Im2colKerns<Pack_Mode::NO_PACK> nopackkern;
            bundle_thread = nopackkern.get_thread_bundle(
533 534
                    param, matmul_param, m_matmul_algo, ohw_tile_size,
                    oc_tile_size);
535
        }
536

537 538 539 540 541 542 543
        StrategyParam strategyparam;
        strategyparam.ohw = ohw;
        strategyparam.is_dst_8bit =
                (param.src_type.enumv() == DTypeEnum::QuantizedS8 &&
                 param.dst_type.enumv() == DTypeEnum::QuantizedS8) ||
                (param.src_type.enumv() == DTypeEnum::Quantized8Asymm &&
                 param.dst_type.enumv() == DTypeEnum::Quantized8Asymm);
544
        strategyparam.is_ohw_size_bigger = (ohw_tile_size >= ohw);
545 546
        strategyparam.skip_copy_dst =
                strategyparam.is_ohw_size_bigger && !strategyparam.is_dst_8bit;
547
        strategyparam.oc_tile_size = oc_tile_size;
548
        strategyparam.pack_oc_size = pack_oc_size;
549

550 551 552 553
        SmallVector<ConvBiasImpl::NCBKern> ret_kern;
        MIDOUT_BEGIN(
                megdnn_fallback_im2col,
                midout_iv("ConvBiasImpl::AlgoIm2col::dispatch_kerns"_hash)) {
554 555 556 557
            StrategyBase* im2colstrategy =
                    Factory::get_im2col_strategy(param, m_matmul_algo);
            auto kern_padding = [bundle, im2colstrategy,
                                 pack_oc_size = pack_oc_size](
558 559
                                        const NCBKernParam& param,
                                        const NCBKernIndex& ncb_index) {
560 561
                copy_padding_kern(bundle, param, ncb_index, im2colstrategy,
                                  pack_oc_size);
562 563 564
            };

            auto kern_packA = [bundle, matmul_algo = m_matmul_algo,
565 566 567 568
                               matmul_param, im2colstrategy,
                               pack_oc_size = pack_oc_size](
                                      const NCBKernParam& param,
                                      const NCBKernIndex& ncb_index) {
569
                packA_kern(bundle, param, matmul_param, matmul_algo, ncb_index,
570
                           im2colstrategy, pack_oc_size);
571 572 573 574 575
            };
            if (default_pack) {
                auto kern_compute_default =
                        [bundle, bundle_thread, matmul_param,
                         matmul_algo = m_matmul_algo,
576
                         ohw_tile_size = ohw_tile_size,
577 578 579 580 581 582 583 584 585 586 587
                         strategyparam = strategyparam,
                         im2colstrategy](const NCBKernParam& param,
                                         const NCBKernIndex& ncb_index) {
                            Im2colKerns<Pack_Mode::DEFAULT>::kerns(
                                    bundle, bundle_thread, param, matmul_param,
                                    matmul_algo, strategyparam, ncb_index,
                                    ohw_tile_size, im2colstrategy);
                        };
                ret_kern.push_back({kern_packA, {GROUP, packa_parallel_times}});

                if (need_padding) {
588 589
                    ret_kern.push_back({kern_padding,
                                        {param.n, GROUP, IC / pack_oc_size}});
590 591 592 593 594 595 596 597 598
                }
                ret_kern.push_back(
                        {kern_compute_default,
                         {N, GROUP, ohw_parallel_times, oc_parallel_times}});
            } else if (only_packA) {
                auto kern_compute_onlypackA =
                        [bundle, bundle_thread, matmul_param,
                         matmul_algo = m_matmul_algo,
                         strategyparam = strategyparam,
599
                         ohw_tile_size = ohw_tile_size,
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
                         im2colstrategy](const NCBKernParam& param,
                                         const NCBKernIndex& ncb_index) {
                            Im2colKerns<Pack_Mode::ONLY_PACKA>::kerns(
                                    bundle, bundle_thread, param, matmul_param,
                                    matmul_algo, strategyparam, ncb_index,
                                    ohw_tile_size, im2colstrategy);
                        };
                ret_kern.push_back({kern_packA, {GROUP, packa_parallel_times}});
                if (need_padding) {
                    ret_kern.push_back({kern_padding, {param.n, GROUP, IC}});
                }
                ret_kern.push_back(
                        {kern_compute_onlypackA,
                         {N, GROUP, ohw_parallel_times, oc_parallel_times}});
            } else if (no_pack) {
                auto kern_compute_nopack =
                        [bundle, bundle_thread, matmul_param,
                         matmul_algo = m_matmul_algo,
                         strategyparam = strategyparam,
619
                         ohw_tile_size = ohw_tile_size,
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
                         im2colstrategy](const NCBKernParam& param,
                                         const NCBKernIndex& ncb_index) {
                            Im2colKerns<Pack_Mode::NO_PACK>::kerns(
                                    bundle, bundle_thread, param, matmul_param,
                                    matmul_algo, strategyparam, ncb_index,
                                    ohw_tile_size, im2colstrategy);
                        };

                if (need_padding) {
                    ret_kern.push_back({kern_padding, {param.n, GROUP, IC}});
                }
                ret_kern.push_back(
                        {kern_compute_nopack,
                         {N, GROUP, ohw_parallel_times, oc_parallel_times}});
            }
            return ret_kern;
        }
        MIDOUT_END();
        return {};
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
    }
    MIDOUT_END();
    return {};
}

bool ConvBiasImpl::AlgoIm2col::usable(
        ConvBiasImpl* opr, const NCBKernSizeParam& param,
        AlgoSelectionStrategy /*algo_selection_strategy*/) const {
    MIDOUT_BEGIN(megdnn_fallback_im2col, 0, 2) {
        //! make sure 8x8x16 and 8x8x32 biasmode is  nobias and nonlineMode is
        //! identity otherwise return false mean that 8x8x32 and 8x8x16 not support
        //! PostProcess
        if (param.src_type.enumv() == param.filter_type.enumv() &&
            ((param.src_type.enumv() == DTypeEnum::Int8 &&
              (param.dst_type.enumv() == DTypeEnum::Int16 ||
               param.dst_type.enumv() == DTypeEnum::Int32)) ||
             ((param.src_type.enumv() == DTypeEnum::QuantizedS8 ||
               param.src_type.enumv() == DTypeEnum::Quantized8Asymm) &&
              param.dst_type.enumv() == DTypeEnum::QuantizedS32)) &&
            param.bias_mode != megdnn::BiasMode::NO_BIAS &&
            param.nonlineMode != megdnn::NonlineMode::IDENTITY) {
            return false;
        }
662 663 664 665 666 667 668 669
        //! current now im2col only support int8 quantized s8 nchw44
        if (opr->param().format == param::ConvBias::Format::NCHW44 &&
            (param.src_type.enumv() == param.filter_type.enumv() &&
             (param.src_type.enumv() != DTypeEnum::Int8) &&
             (param.src_type.enumv() != DTypeEnum::QuantizedS8))) {
            return false;
        }

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
        size_t oc_tile_size = 0, ohw_tile_size = 0;
        Pack_Mode packmode = m_matmul_algo->packmode();
        bool default_pack = packmode == Pack_Mode::DEFAULT;
        bool no_pack = packmode == Pack_Mode::NO_PACK;
        bool only_packA = packmode == Pack_Mode::ONLY_PACKA;

        if (default_pack || only_packA) {
            auto inner_block = m_matmul_algo->get_inner_block_size();
            choice_ohw_oc_block(param, oc_tile_size, ohw_tile_size,
                                inner_block.m, inner_block.n, default_pack);
        } else {  //! not support pack,not need pack
            size_t nopack_default_blockm = 8;
            size_t nopack_default_blockn = 16;
            choice_ohw_oc_block(param, oc_tile_size, ohw_tile_size,
                                nopack_default_blockm, nopack_default_blockn,
                                no_pack);
        }
687
        fallback::MatrixMulImpl::KernSizeParam matmul_param =
688
                get_matmul_kern_param(param, ohw_tile_size, oc_tile_size);
689 690
        bool matmulusable = m_matmul_algo->usable(matmul_param);
        return matmulusable &&
691 692 693 694 695 696 697
               (opr->param().format == param::ConvBias::Format::NCHW ||
                opr->param().format == param::ConvBias::Format::NCHW44) &&
               (!(param.filter_meta.spatial[0] ==
                          param.filter_meta.spatial[1] &&
                  (param.filter_meta.spatial[0] == 1) &&
                  param.filter_meta.stride[0] == param.filter_meta.stride[1] &&
                  param.filter_meta.stride[0] == 1)) &&
698 699 700 701 702 703 704 705 706 707
               (param.filter_meta.dilation[0] ==
                        param.filter_meta.dilation[1] &&
                param.filter_meta.dilation[0] == 1) &&
               param.compute_mode == param::ConvBias::ComputeMode::DEFAULT;
    }
    MIDOUT_END();
    return false;
}

// vim: syntax=cpp.doxygen