f32_direct_nchw_nchw44_kern_common.h 42.3 KB
Newer Older
1 2 3 4 5
#pragma once
#include "megdnn/arch.h"
#include "src/common/unroll_macro.h"
#include "src/common/utils.h"
#include "src/fallback/conv_bias/common.h"
6 7 8 9
#include "src/fallback/conv_bias/gi/fp32/f32_direct_nchw_nchw44_kern.h"
#include "src/fallback/conv_bias/gi/intrinsic_helper.h"
#include "src/fallback/conv_bias/opr_impl.h"
#include "src/fallback/elemwise_helper/elemwise_op.h"
10 11 12 13
#if MEGDNN_ARMV7
#include "src/armv7/matrix_mul/asm/common.h"
#endif

14
using namespace megdnn;
15
using namespace fallback;
16 17 18 19 20 21 22 23 24 25

namespace {
/**
 *\brief ShiftCalHelper is core calculate code
 *\tparam src_idx is offset for src regs
 *\tparam weight_idx is offset for weight regs
 *\tparam T is type of output regs
 *\tparam T2 is type of src regs
 *\tparam T3 is type of weight regs
 */
M
Megvii Engine Team 已提交
26 27 28
template <
        int src_idx, int weight_idx, int c_dim, int stride, int remain_w, typename T,
        typename T2, typename T3>
29 30 31 32
struct ShiftCalHelper {
    static MEGDNN_ALWAYS_INLINE void impl(T& c, T2& src, T3& weight);
};

M
Megvii Engine Team 已提交
33 34 35
template <
        int src_idx, int weight_idx, int c_dim, int stride, typename T, typename T2,
        typename T3>
36 37 38 39
struct ShiftCalHelper<src_idx, weight_idx, c_dim, stride, 0, T, T2, T3> {
    static MEGDNN_ALWAYS_INLINE void impl(T&, T2&, T3&) {}
};

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#define cb(step)                                                            \
    c[0][step] = GiFloat32Type2FixLenType(GiSimdFmaLane(                    \
            GiFixLenType2GiFloat32Type(c[0][step]),                         \
            GiFixLenType2GiFloat32Type(weight[0][weight_idx]),              \
            GiFixLenType2GiFloat32Type(src[(step * stride + src_idx) / 4]), \
            (step * stride + src_idx) % 4));                                \
    c[1][step] = GiFloat32Type2FixLenType(GiSimdFmaLane(                    \
            GiFixLenType2GiFloat32Type(c[1][step]),                         \
            GiFixLenType2GiFloat32Type(weight[1][weight_idx]),              \
            GiFixLenType2GiFloat32Type(src[(step * stride + src_idx) / 4]), \
            (step * stride + src_idx) % 4));

#define cb2(step)                                                           \
    c[0][step] = GiFloat32Type2FixLenType(GiSimdFmaLane(                    \
            GiFixLenType2GiFloat32Type(c[0][step]),                         \
            GiFixLenType2GiFloat32Type(weight[0][weight_idx]),              \
            GiFixLenType2GiFloat32Type(src[(step * stride + src_idx) / 4]), \
            (step * stride + src_idx) % 4));
M
Megvii Engine Team 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

#define SHIFT_CAL_HELPER(ow_remain)                                               \
    template <                                                                    \
            int src_idx, int weight_idx, int stride, typename T, typename T2,     \
            typename T3>                                                          \
    struct ShiftCalHelper<src_idx, weight_idx, 2, stride, ow_remain, T, T2, T3> { \
        static MEGDNN_ALWAYS_INLINE void impl(T& c, T2& src, T3& weight) {        \
            UNROLL_CALL_RAW(ow_remain, cb);                                       \
        }                                                                         \
    };                                                                            \
    template <                                                                    \
            int src_idx, int weight_idx, int stride, typename T, typename T2,     \
            typename T3>                                                          \
    struct ShiftCalHelper<src_idx, weight_idx, 1, stride, ow_remain, T, T2, T3> { \
        static MEGDNN_ALWAYS_INLINE void impl(T& c, T2& src, T3& weight) {        \
            UNROLL_CALL_RAW(ow_remain, cb2);                                      \
        }                                                                         \
75 76 77 78 79 80 81 82 83 84 85 86
    };

SHIFT_CAL_HELPER(1)
SHIFT_CAL_HELPER(2)
SHIFT_CAL_HELPER(3)
SHIFT_CAL_HELPER(4)
SHIFT_CAL_HELPER(5)
SHIFT_CAL_HELPER(6)
SHIFT_CAL_HELPER(7)
SHIFT_CAL_HELPER(8)

#undef SHIFT_CAL_HELPER
87
#undef cb
88
#undef cb2
89

M
Megvii Engine Team 已提交
90 91 92
template <
        int src_idx, int weight_idx, int c_dim, int stride, int remain_w, typename T,
        typename T2, typename T3>
93
MEGDNN_ALWAYS_INLINE void cal_helper(T& c, T2& src, T3& weight) {
M
Megvii Engine Team 已提交
94 95
    ShiftCalHelper<src_idx, weight_idx, c_dim, stride, remain_w, T, T2, T3>::impl(
            c, src, weight);
96
};
97 98 99 100
enum CpuTag {
    DEFAULT_CPU_TAG = 0,
    A7_TAG,
};
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
template <int oc>
struct OCHelper {
public:
    static const int val = -1;
};

template <>
struct OCHelper<4> {
public:
    static const int val = 1;
};

template <>
struct OCHelper<8> {
public:
    static const int val = 2;
};
/**
 *  oc8_ow8(m = 8, n = 8) and oc4_ow8(m = 4, n = 8) gemm like kernel
 **/
M
Megvii Engine Team 已提交
121 122 123
template <
        BiasMode bias_mode, typename Op, int remain_w, int filter_size, int oc_block,
        int stride, int ow_block, int tag = CpuTag::DEFAULT_CPU_TAG>
124
struct KerGiXXs2NchwNchw44FP32 {
M
Megvii Engine Team 已提交
125 126 127 128
    static void impl(
            const float32_t* src_ptr, const float32_t* weight_ptr,
            const float32_t* bias_ptr, float32_t* dst_ptr, int ic, int ih, int iw,
            int ld_dst_oc, const Op& op);
129
};
M
Megvii Engine Team 已提交
130 131 132
template <
        BiasMode bias_mode, typename Op, int remain_w, int oc_block, int stride,
        int ow_block>
133
struct KerGiXXs2NchwNchw44FP32<bias_mode, Op, remain_w, 7, oc_block, stride, ow_block> {
M
Megvii Engine Team 已提交
134 135 136 137
    static void impl(
            const float32_t* src_ptr, const float32_t* weight_ptr,
            const float32_t* bias_ptr, float32_t* dst_ptr, int ic, int ih, int iw,
            int ld_dst_oc, const Op& op) {
138 139 140 141 142
        constexpr int loop_ic_step = 1;
        constexpr int filter_size = 7;
        constexpr int oc_step = 4;
        constexpr int simd_len = 4;
        constexpr int src_reg_size =
M
Megvii Engine Team 已提交
143
                (ow_block * stride + filter_size - stride + simd_len - 1) / simd_len;
144 145 146 147 148 149

        constexpr int ld_weight_fw = oc_step * filter_size;
        const int ld_weight_oc = oc_step * filter_size * filter_size * ic;
        const int ld_weight_ic = oc_step * filter_size * filter_size;
        const int ld_src_ic = ih * iw;
        constexpr int c_dim = OCHelper<oc_block>::val;
150
        GI_FLOAT32_FIXLEN_t c[c_dim][8];
151
        init_ocx_ow8<c_dim, bias_mode, remain_w>(c, bias_ptr, oc_step);
152 153

        for (int ic_idx = 0; ic_idx < ic; ic_idx += loop_ic_step) {
154 155
            GI_FLOAT32_FIXLEN_t src[src_reg_size];
            GI_FLOAT32_FIXLEN_t weight[c_dim][filter_size];
156

M
Megvii Engine Team 已提交
157
#define KERNEL_CB(step)                                                                \
158 159
    load_helper<src_reg_size, 0, simd_len, 0, Vld1qF32S>(src, src_ptr + step * iw, 0); \
    load_helper<filter_size, 0, oc_step, c_dim, Vld1qF32S>(                            \
M
Megvii Engine Team 已提交
160 161 162 163 164 165 166
            weight, weight_ptr + step * ld_weight_fw, ld_weight_oc);                   \
    cal_helper<0, 0, c_dim, stride, remain_w>(c, src, weight);                         \
    cal_helper<1, 1, c_dim, stride, remain_w>(c, src, weight);                         \
    cal_helper<2, 2, c_dim, stride, remain_w>(c, src, weight);                         \
    cal_helper<3, 3, c_dim, stride, remain_w>(c, src, weight);                         \
    cal_helper<4, 4, c_dim, stride, remain_w>(c, src, weight);                         \
    cal_helper<5, 5, c_dim, stride, remain_w>(c, src, weight);                         \
167
    cal_helper<6, 6, c_dim, stride, remain_w>(c, src, weight);
168 169 170 171 172 173 174

            UNROLL_CALL_RAW(7, KERNEL_CB)
#undef KERNEL_CB

            src_ptr += ld_src_ic;
            weight_ptr += ld_weight_ic;
        }
M
Megvii Engine Team 已提交
175
        store_ocx_ow8_remain_static<c_dim, remain_w, Op>(c, op, dst_ptr, ld_dst_oc);
176 177 178
    }
};

M
Megvii Engine Team 已提交
179 180 181
template <
        BiasMode bias_mode, typename Op, int remain_w, int oc_block, int stride,
        int ow_block>
182
struct KerGiXXs2NchwNchw44FP32<bias_mode, Op, remain_w, 5, oc_block, stride, ow_block> {
M
Megvii Engine Team 已提交
183 184 185 186
    static void impl(
            const float32_t* src_ptr, const float32_t* weight_ptr,
            const float32_t* bias_ptr, float32_t* dst_ptr, int ic, int ih, int iw,
            int ld_dst_oc, const Op& op) {
187 188 189 190 191
        constexpr int loop_ic_step = 1;
        constexpr int filter_size = 5;
        constexpr int oc_step = 4;
        constexpr int simd_len = 4;
        constexpr int src_reg_size =
M
Megvii Engine Team 已提交
192
                (ow_block * stride + filter_size - stride + simd_len - 1) / simd_len;
193 194 195 196 197 198

        constexpr int ld_weight_fw = oc_step * filter_size;
        const int ld_weight_oc = oc_step * filter_size * filter_size * ic;
        const int ld_weight_ic = oc_step * filter_size * filter_size;
        const int ld_src_ic = ih * iw;
        constexpr int c_dim = OCHelper<oc_block>::val;
199
        GI_FLOAT32_FIXLEN_t c[c_dim][8];
200
        init_ocx_ow8<c_dim, bias_mode, remain_w>(c, bias_ptr, oc_step);
201 202

        for (int ic_idx = 0; ic_idx < ic; ic_idx += loop_ic_step) {
203 204
            GI_FLOAT32_FIXLEN_t src[src_reg_size];
            GI_FLOAT32_FIXLEN_t weight[c_dim][filter_size];
205

M
Megvii Engine Team 已提交
206
#define KERNEL_CB(step)                                                                \
207 208
    load_helper<src_reg_size, 0, simd_len, 0, Vld1qF32S>(src, src_ptr + step * iw, 0); \
    load_helper<filter_size, 0, oc_step, c_dim, Vld1qF32S>(                            \
M
Megvii Engine Team 已提交
209 210 211 212 213
            weight, weight_ptr + step * ld_weight_fw, ld_weight_oc);                   \
    cal_helper<0, 0, c_dim, stride, remain_w>(c, src, weight);                         \
    cal_helper<1, 1, c_dim, stride, remain_w>(c, src, weight);                         \
    cal_helper<2, 2, c_dim, stride, remain_w>(c, src, weight);                         \
    cal_helper<3, 3, c_dim, stride, remain_w>(c, src, weight);                         \
214
    cal_helper<4, 4, c_dim, stride, remain_w>(c, src, weight);
215 216 217 218 219 220
            UNROLL_CALL_RAW(5, KERNEL_CB)
#undef KERNEL_CB

            src_ptr += ld_src_ic;
            weight_ptr += ld_weight_ic;
        }
M
Megvii Engine Team 已提交
221
        store_ocx_ow8_remain_static<c_dim, remain_w, Op>(c, op, dst_ptr, ld_dst_oc);
222 223 224
    }
};

M
Megvii Engine Team 已提交
225 226 227
template <
        BiasMode bias_mode, typename Op, int remain_w, int oc_block, int stride,
        int ow_block>
228
struct KerGiXXs2NchwNchw44FP32<bias_mode, Op, remain_w, 3, oc_block, stride, ow_block> {
M
Megvii Engine Team 已提交
229 230 231 232
    static void impl(
            const float32_t* src_ptr, const float32_t* weight_ptr,
            const float32_t* bias_ptr, float32_t* dst_ptr, int ic, int ih, int iw,
            int ld_dst_oc, const Op& op) {
233 234 235 236 237
        constexpr int loop_ic_step = 1;
        constexpr int filter_size = 3;
        constexpr int oc_step = 4;
        constexpr int simd_len = 4;
        constexpr int src_reg_size =
M
Megvii Engine Team 已提交
238
                (ow_block * stride + filter_size - stride + simd_len - 1) / simd_len;
239 240 241 242 243 244

        constexpr int ld_weight_fw = oc_step * filter_size;
        const int ld_weight_oc = oc_step * filter_size * filter_size * ic;
        const int ld_weight_ic = oc_step * filter_size * filter_size;
        const int ld_src_ic = ih * iw;
        constexpr int c_dim = OCHelper<oc_block>::val;
245
        GI_FLOAT32_FIXLEN_t c[c_dim][8];
246
        init_ocx_ow8<c_dim, bias_mode, remain_w>(c, bias_ptr, oc_step);
247 248

        for (int ic_idx = 0; ic_idx < ic; ic_idx += loop_ic_step) {
249 250
            GI_FLOAT32_FIXLEN_t src[src_reg_size];
            GI_FLOAT32_FIXLEN_t weight[c_dim][filter_size];
251
            // row 0
252 253
            load_helper<src_reg_size, 0, simd_len, 0, Vld1qF32S>(src, src_ptr, 0);
            load_helper<filter_size, 0, oc_step, c_dim, Vld1qF32S>(
254
                    weight, weight_ptr, ld_weight_oc);
255 256 257
            cal_helper<0, 0, c_dim, stride, remain_w>(c, src, weight);
            cal_helper<1, 1, c_dim, stride, remain_w>(c, src, weight);
            cal_helper<2, 2, c_dim, stride, remain_w>(c, src, weight);
258 259

            // row 1
260 261
            load_helper<src_reg_size, 0, simd_len, 0, Vld1qF32S>(src, src_ptr + iw, 0);
            load_helper<filter_size, 0, oc_step, c_dim, Vld1qF32S>(
262
                    weight, weight_ptr + 1 * ld_weight_fw, ld_weight_oc);
263 264 265
            cal_helper<0, 0, c_dim, stride, remain_w>(c, src, weight);
            cal_helper<1, 1, c_dim, stride, remain_w>(c, src, weight);
            cal_helper<2, 2, c_dim, stride, remain_w>(c, src, weight);
266 267

            // row 2
268
            load_helper<src_reg_size, 0, simd_len, 0, Vld1qF32S>(
269
                    src, src_ptr + 2 * iw, 0);
270
            load_helper<filter_size, 0, oc_step, c_dim, Vld1qF32S>(
271
                    weight, weight_ptr + 2 * ld_weight_fw, ld_weight_oc);
272 273 274
            cal_helper<0, 0, c_dim, stride, remain_w>(c, src, weight);
            cal_helper<1, 1, c_dim, stride, remain_w>(c, src, weight);
            cal_helper<2, 2, c_dim, stride, remain_w>(c, src, weight);
275 276 277 278

            src_ptr += ld_src_ic;
            weight_ptr += ld_weight_ic;
        }
M
Megvii Engine Team 已提交
279
        store_ocx_ow8_remain_static<c_dim, remain_w, Op>(c, op, dst_ptr, ld_dst_oc);
280 281 282
    }
};

283 284 285
#if MEGDNN_ARMV7

template <BiasMode bias_mode, typename Op>
286
struct KerGiXXs2NchwNchw44FP32<bias_mode, Op, 8, 3, 4, 2, 8, CpuTag::A7_TAG> {
M
Megvii Engine Team 已提交
287 288 289 290
    static void impl(
            const float32_t* src_ptr, const float32_t* weight_ptr,
            const float32_t* bias_ptr, float32_t* dst_ptr, int ic, int ih, int iw,
            int ld_dst_oc, const Op& op) {
291 292 293 294 295 296 297 298 299
        constexpr int oc_block = 4;
        constexpr int stride = 2;
        constexpr int remain_w = 8;
        constexpr int ow_block = 8;
        constexpr int loop_ic_step = 1;
        constexpr int filter_size = 3;
        constexpr int oc_step = 4;
        constexpr int src_line_block = ow_block * stride + filter_size - stride;

M
Megvii Engine Team 已提交
300
        const int iw_skip_bytes = (iw - round_up(src_line_block, 2)) * sizeof(float);
301 302 303
        const int ld_src_ic_skip_bytes =
                iw * (ih - filter_size) * sizeof(float) + iw_skip_bytes;
        constexpr int c_dim = OCHelper<oc_block>::val;
304
        GI_FLOAT32_FIXLEN_t c[1][8];
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
        init_ocx_ow8<c_dim, bias_mode, 8>(c, bias_ptr, oc_step);
        const int img_stride = ih * iw;
        constexpr int filter_stride = filter_size * filter_size * oc_step;
        megdnn::armv7::prefetch_2x(src_ptr);
        megdnn::armv7::prefetch_2x(src_ptr + iw);
        megdnn::armv7::prefetch_2x(src_ptr + 2 * iw);
        megdnn::armv7::prefetch_2x(weight_ptr);

        /**
         * c q8-q15
         * src q0-q4
         * weight q5-q7
         * optimized for A7
         *
         */
        for (int ic_idx = 0; ic_idx < ic; ic_idx += loop_ic_step) {
            megdnn::armv7::prefetch_2x(src_ptr + img_stride);
            megdnn::armv7::prefetch_2x(src_ptr + img_stride + iw);
            megdnn::armv7::prefetch_2x(src_ptr + img_stride + 2 * iw);
            megdnn::armv7::prefetch_2x(weight_ptr + filter_stride);
            asm volatile(

                    "2:\n"
                    //! row 0
                    "vld1.32 {d10, d11}, [%[weight_ptr]]!\n"
                    "vld1.32 {d0, d1}, [%[src_ptr]]!\n"
                    "vld1.32 {d2, d3}, [%[src_ptr]]!\n"
                    "vld1.32 {d4, d5}, [%[src_ptr]]!\n"
                    "vld1.32 {d6, d7}, [%[src_ptr]]!\n"
                    "vld1.32 {d8}, [%[src_ptr]]!\n"
                    "vld1.32 {d12, d13}, [%[weight_ptr]]!\n"
                    "vld1.32 {d14, d15}, [%[weight_ptr]]!\n"
                    "add %[src_ptr], %[src_ptr], %[iw_skip_bytes]\n"

                    "vmla.f32 %q[c0], q5, d0[0]\n"
                    "vmla.f32 %q[c1], q5, d1[0]\n"
                    "vmla.f32 %q[c2], q5, d2[0]\n"
                    "vmla.f32 %q[c3], q5, d3[0]\n"
                    "vmla.f32 %q[c4], q5, d4[0]\n"
                    "vmla.f32 %q[c5], q5, d5[0]\n"
                    "vmla.f32 %q[c6], q5, d6[0]\n"
                    "vmla.f32 %q[c7], q5, d7[0]\n"

                    "vmla.f32 %q[c0], q6, d0[1]\n"
                    "vmla.f32 %q[c1], q6, d1[1]\n"
                    "vmla.f32 %q[c2], q6, d2[1]\n"
                    "vmla.f32 %q[c3], q6, d3[1]\n"
                    "vmla.f32 %q[c4], q6, d4[1]\n"
                    "vmla.f32 %q[c5], q6, d5[1]\n"
                    "vmla.f32 %q[c6], q6, d6[1]\n"
                    "vmla.f32 %q[c7], q6, d7[1]\n"

                    "vmla.f32 %q[c0], q7, d1[0]\n"
                    "vmla.f32 %q[c1], q7, d2[0]\n"
                    "vmla.f32 %q[c2], q7, d3[0]\n"
                    "vmla.f32 %q[c3], q7, d4[0]\n"
                    "vmla.f32 %q[c4], q7, d5[0]\n"
                    "vmla.f32 %q[c5], q7, d6[0]\n"
                    "vmla.f32 %q[c6], q7, d7[0]\n"
                    "vmla.f32 %q[c7], q7, d8[0]\n"

                    //! row 1
                    "vld1.32 {d10, d11}, [%[weight_ptr]]!\n"
                    "vld1.32 {d0, d1}, [%[src_ptr]]!\n"
                    "vld1.32 {d2, d3}, [%[src_ptr]]!\n"
                    "vld1.32 {d4, d5}, [%[src_ptr]]!\n"
                    "vld1.32 {d6, d7}, [%[src_ptr]]!\n"
                    "vld1.32 {d8}, [%[src_ptr]]!\n"
                    "vld1.32 {d12, d13}, [%[weight_ptr]]!\n"
                    "vld1.32 {d14, d15}, [%[weight_ptr]]!\n"
                    "add %[src_ptr], %[src_ptr], %[iw_skip_bytes]\n"

                    "vmla.f32 %q[c0], q5, d0[0]\n"
                    "vmla.f32 %q[c1], q5, d1[0]\n"
                    "vmla.f32 %q[c2], q5, d2[0]\n"
                    "vmla.f32 %q[c3], q5, d3[0]\n"
                    "vmla.f32 %q[c4], q5, d4[0]\n"
                    "vmla.f32 %q[c5], q5, d5[0]\n"
                    "vmla.f32 %q[c6], q5, d6[0]\n"
                    "vmla.f32 %q[c7], q5, d7[0]\n"

                    "vmla.f32 %q[c0], q6, d0[1]\n"
                    "vmla.f32 %q[c1], q6, d1[1]\n"
                    "vmla.f32 %q[c2], q6, d2[1]\n"
                    "vmla.f32 %q[c3], q6, d3[1]\n"
                    "vmla.f32 %q[c4], q6, d4[1]\n"
                    "vmla.f32 %q[c5], q6, d5[1]\n"
                    "vmla.f32 %q[c6], q6, d6[1]\n"
                    "vmla.f32 %q[c7], q6, d7[1]\n"

                    "vmla.f32 %q[c0], q7, d1[0]\n"
                    "vmla.f32 %q[c1], q7, d2[0]\n"
                    "vmla.f32 %q[c2], q7, d3[0]\n"
                    "vmla.f32 %q[c3], q7, d4[0]\n"
                    "vmla.f32 %q[c4], q7, d5[0]\n"
                    "vmla.f32 %q[c5], q7, d6[0]\n"
                    "vmla.f32 %q[c6], q7, d7[0]\n"
                    "vmla.f32 %q[c7], q7, d8[0]\n"

                    //! row 2
                    "vld1.32 {d10, d11}, [%[weight_ptr]]!\n"
                    "vld1.32 {d0, d1}, [%[src_ptr]]!\n"
                    "vld1.32 {d2, d3}, [%[src_ptr]]!\n"
                    "vld1.32 {d4, d5}, [%[src_ptr]]!\n"
                    "vld1.32 {d6, d7}, [%[src_ptr]]!\n"
                    "vld1.32 {d8}, [%[src_ptr]]!\n"
                    "vld1.32 {d12, d13}, [%[weight_ptr]]!\n"
                    "vld1.32 {d14, d15}, [%[weight_ptr]]!\n"
                    "add %[src_ptr], %[src_ptr], %[ld_src_ic_skip_bytes]\n"

                    "vmla.f32 %q[c0], q5, d0[0]\n"
                    "vmla.f32 %q[c1], q5, d1[0]\n"
                    "vmla.f32 %q[c2], q5, d2[0]\n"
                    "vmla.f32 %q[c3], q5, d3[0]\n"
                    "vmla.f32 %q[c4], q5, d4[0]\n"
                    "vmla.f32 %q[c5], q5, d5[0]\n"
                    "vmla.f32 %q[c6], q5, d6[0]\n"
                    "vmla.f32 %q[c7], q5, d7[0]\n"

                    "vmla.f32 %q[c0], q6, d0[1]\n"
                    "vmla.f32 %q[c1], q6, d1[1]\n"
                    "vmla.f32 %q[c2], q6, d2[1]\n"
                    "vmla.f32 %q[c3], q6, d3[1]\n"
                    "vmla.f32 %q[c4], q6, d4[1]\n"
                    "vmla.f32 %q[c5], q6, d5[1]\n"
                    "vmla.f32 %q[c6], q6, d6[1]\n"
                    "vmla.f32 %q[c7], q6, d7[1]\n"

                    "vmla.f32 %q[c0], q7, d1[0]\n"
                    "vmla.f32 %q[c1], q7, d2[0]\n"
                    "vmla.f32 %q[c2], q7, d3[0]\n"
                    "vmla.f32 %q[c3], q7, d4[0]\n"
                    "vmla.f32 %q[c4], q7, d5[0]\n"
                    "vmla.f32 %q[c5], q7, d6[0]\n"
                    "vmla.f32 %q[c6], q7, d7[0]\n"
                    "vmla.f32 %q[c7], q7, d8[0]\n"

                    "6:\n"
M
Megvii Engine Team 已提交
443 444 445 446
                    : [c0] "+w"(c[0][0]), [c1] "+w"(c[0][1]), [c2] "+w"(c[0][2]),
                      [c3] "+w"(c[0][3]), [c4] "+w"(c[0][4]), [c5] "+w"(c[0][5]),
                      [c6] "+w"(c[0][6]), [c7] "+w"(c[0][7]), [src_ptr] "+r"(src_ptr),
                      [weight_ptr] "+r"(weight_ptr)
447 448 449

                    : [ld_src_ic_skip_bytes] "r"(ld_src_ic_skip_bytes),
                      [iw_skip_bytes] "r"(iw_skip_bytes)
M
Megvii Engine Team 已提交
450 451
                    : "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10",
                      "d11", "d12", "d13", "d14", "d15", "r1", "r2", "cc", "memory");
452
        }
M
Megvii Engine Team 已提交
453
        store_ocx_ow8_remain_static<c_dim, remain_w, Op>(c, op, dst_ptr, ld_dst_oc);
454 455 456 457
    }
};

template <BiasMode bias_mode, typename Op>
458
struct KerGiXXs2NchwNchw44FP32<bias_mode, Op, 8, 3, 4, 2, 8, CpuTag::DEFAULT_CPU_TAG> {
M
Megvii Engine Team 已提交
459 460 461 462
    static void impl(
            const float32_t* src_ptr, const float32_t* weight_ptr,
            const float32_t* bias_ptr, float32_t* dst_ptr, int ic, int ih, int iw,
            int ld_dst_oc, const Op& op) {
463 464 465 466 467 468 469 470 471
        constexpr int oc_block = 4;
        constexpr int stride = 2;
        constexpr int remain_w = 8;
        constexpr int ow_block = 8;
        constexpr int loop_ic_step = 1;
        constexpr int filter_size = 3;
        constexpr int oc_step = 4;
        constexpr int src_line_block = ow_block * stride + filter_size - stride;

M
Megvii Engine Team 已提交
472
        const int iw_skip_bytes = (iw - round_up(src_line_block, 2)) * sizeof(float);
473 474 475
        const int ld_src_ic_skip_bytes =
                iw * (ih - filter_size) * sizeof(float) + iw_skip_bytes;
        constexpr int c_dim = OCHelper<oc_block>::val;
476
        GI_FLOAT32_FIXLEN_t c[1][8];
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
        init_ocx_ow8<c_dim, bias_mode, 8>(c, bias_ptr, oc_step);
        /**
         * c q8-q15
         * src q0-q4
         * weight q5-q7
         * optimized for big core
         *
         */
        for (int ic_idx = 0; ic_idx < ic; ic_idx += loop_ic_step) {
            asm volatile(

                    "2:\n"
                    //! row 0
                    "vld1.32 {d10, d11}, [%[weight_ptr]]!\n"
                    "vld1.32 {d0, d1}, [%[src_ptr]]!\n"
                    "vld1.32 {d2, d3}, [%[src_ptr]]!\n"
                    "vld1.32 {d4, d5}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c0], q5, d0[0]\n"
                    "vld1.32 {d6, d7}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c1], q5, d1[0]\n"
                    "vmla.f32 %q[c2], q5, d2[0]\n"
                    "vld1.32 {d12, d13}, [%[weight_ptr]]!\n"
                    "vmla.f32 %q[c3], q5, d3[0]\n"
                    "vld1.32 {d14, d15}, [%[weight_ptr]]!\n"
                    "vmla.f32 %q[c4], q5, d4[0]\n"
                    "vld1.32 {d8}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c5], q5, d5[0]\n"
                    "add %[src_ptr], %[src_ptr], %[iw_skip_bytes]\n"
                    "vmla.f32 %q[c6], q5, d6[0]\n"
                    "vmla.f32 %q[c7], q5, d7[0]\n"
                    "vld1.32 {d10, d11}, [%[weight_ptr]]!\n"

                    "vmla.f32 %q[c0], q6, d0[1]\n"
                    "vmla.f32 %q[c1], q6, d1[1]\n"
                    "vmla.f32 %q[c2], q6, d2[1]\n"
                    "vmla.f32 %q[c3], q6, d3[1]\n"
                    "vmla.f32 %q[c4], q6, d4[1]\n"
                    "vmla.f32 %q[c5], q6, d5[1]\n"
                    "vmla.f32 %q[c6], q6, d6[1]\n"
                    "vmla.f32 %q[c7], q6, d7[1]\n"
                    "vld1.32 {d12, d13}, [%[weight_ptr]]!\n"

                    "vmla.f32 %q[c0], q7, d1[0]\n"
                    "vld1.32 {d0, d1}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c1], q7, d2[0]\n"
                    "vmla.f32 %q[c2], q7, d3[0]\n"
                    "vld1.32 {d2, d3}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c3], q7, d4[0]\n"
                    "vmla.f32 %q[c4], q7, d5[0]\n"
                    "vld1.32 {d4, d5}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c5], q7, d6[0]\n"
                    "vmla.f32 %q[c6], q7, d7[0]\n"
                    "vld1.32 {d6, d7}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c7], q7, d8[0]\n"
                    "vld1.32 {d14, d15}, [%[weight_ptr]]!\n"
                    //! row 1

                    "vmla.f32 %q[c0], q5, d0[0]\n"
                    "vld1.32 {d8}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c1], q5, d1[0]\n"
                    "add %[src_ptr], %[src_ptr], %[iw_skip_bytes]\n"
                    "vmla.f32 %q[c2], q5, d2[0]\n"
                    "vmla.f32 %q[c3], q5, d3[0]\n"
                    "vmla.f32 %q[c4], q5, d4[0]\n"
                    "vmla.f32 %q[c5], q5, d5[0]\n"
                    "vmla.f32 %q[c6], q5, d6[0]\n"
                    "vmla.f32 %q[c7], q5, d7[0]\n"
                    "vld1.32 {d10, d11}, [%[weight_ptr]]!\n"

                    "vmla.f32 %q[c0], q6, d0[1]\n"
                    "vmla.f32 %q[c1], q6, d1[1]\n"
                    "vmla.f32 %q[c2], q6, d2[1]\n"
                    "vmla.f32 %q[c3], q6, d3[1]\n"
                    "vmla.f32 %q[c4], q6, d4[1]\n"
                    "vmla.f32 %q[c5], q6, d5[1]\n"
                    "vmla.f32 %q[c6], q6, d6[1]\n"
                    "vmla.f32 %q[c7], q6, d7[1]\n"
                    "vld1.32 {d12, d13}, [%[weight_ptr]]!\n"

                    "vmla.f32 %q[c0], q7, d1[0]\n"
                    "vld1.32 {d0, d1}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c1], q7, d2[0]\n"
                    "vmla.f32 %q[c2], q7, d3[0]\n"
                    "vld1.32 {d2, d3}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c3], q7, d4[0]\n"
                    "vmla.f32 %q[c4], q7, d5[0]\n"
                    "vld1.32 {d4, d5}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c5], q7, d6[0]\n"
                    "vmla.f32 %q[c6], q7, d7[0]\n"
                    "vld1.32 {d6, d7}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c7], q7, d8[0]\n"
                    "vld1.32 {d14, d15}, [%[weight_ptr]]!\n"
                    //! row 2

                    "vmla.f32 %q[c0], q5, d0[0]\n"
                    "vld1.32 {d8}, [%[src_ptr]]!\n"
                    "vmla.f32 %q[c1], q5, d1[0]\n"
                    "add %[src_ptr], %[src_ptr], %[ld_src_ic_skip_bytes]\n"
                    "vmla.f32 %q[c2], q5, d2[0]\n"
                    "vmla.f32 %q[c3], q5, d3[0]\n"
                    "vmla.f32 %q[c4], q5, d4[0]\n"
                    "vmla.f32 %q[c5], q5, d5[0]\n"
                    "vmla.f32 %q[c6], q5, d6[0]\n"
                    "vmla.f32 %q[c7], q5, d7[0]\n"

                    "vmla.f32 %q[c0], q6, d0[1]\n"
                    "vmla.f32 %q[c1], q6, d1[1]\n"
                    "vmla.f32 %q[c2], q6, d2[1]\n"
                    "vmla.f32 %q[c3], q6, d3[1]\n"
                    "vmla.f32 %q[c4], q6, d4[1]\n"
                    "vmla.f32 %q[c5], q6, d5[1]\n"
                    "vmla.f32 %q[c6], q6, d6[1]\n"
                    "vmla.f32 %q[c7], q6, d7[1]\n"

                    "vmla.f32 %q[c0], q7, d1[0]\n"
                    "vmla.f32 %q[c1], q7, d2[0]\n"
                    "vmla.f32 %q[c2], q7, d3[0]\n"
                    "vmla.f32 %q[c3], q7, d4[0]\n"
                    "vmla.f32 %q[c4], q7, d5[0]\n"
                    "vmla.f32 %q[c5], q7, d6[0]\n"
                    "vmla.f32 %q[c6], q7, d7[0]\n"
                    "vmla.f32 %q[c7], q7, d8[0]\n"

                    "6:\n"
M
Megvii Engine Team 已提交
601 602 603 604
                    : [c0] "+w"(c[0][0]), [c1] "+w"(c[0][1]), [c2] "+w"(c[0][2]),
                      [c3] "+w"(c[0][3]), [c4] "+w"(c[0][4]), [c5] "+w"(c[0][5]),
                      [c6] "+w"(c[0][6]), [c7] "+w"(c[0][7]), [src_ptr] "+r"(src_ptr),
                      [weight_ptr] "+r"(weight_ptr)
605 606 607

                    : [ld_src_ic_skip_bytes] "r"(ld_src_ic_skip_bytes),
                      [iw_skip_bytes] "r"(iw_skip_bytes)
M
Megvii Engine Team 已提交
608 609
                    : "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10",
                      "d11", "d12", "d13", "d14", "d15", "r1", "r2", "cc", "memory");
610
        }
M
Megvii Engine Team 已提交
611
        store_ocx_ow8_remain_static<c_dim, remain_w, Op>(c, op, dst_ptr, ld_dst_oc);
612 613 614 615
    }
};

#endif
M
Megvii Engine Team 已提交
616 617 618
template <
        BiasMode bias_mode, typename Op, int remain_w, int oc_block, int stride,
        int ow_block>
619
struct KerGiXXs2NchwNchw44FP32<bias_mode, Op, remain_w, 2, oc_block, stride, ow_block> {
M
Megvii Engine Team 已提交
620 621 622 623
    static void impl(
            const float32_t* src_ptr, const float32_t* weight_ptr,
            const float32_t* bias_ptr, float32_t* dst_ptr, int ic, int ih, int iw,
            int ld_dst_oc, const Op& op) {
624 625 626 627 628
        constexpr int loop_ic_step = 1;
        constexpr int filter_size = 2;
        constexpr int oc_step = 4;
        constexpr int simd_len = 4;
        constexpr int src_reg_size =
M
Megvii Engine Team 已提交
629
                (ow_block * stride + filter_size - stride + simd_len - 1) / simd_len;
630 631 632 633 634 635

        constexpr int ld_weight_fw = oc_step * filter_size;
        const int ld_weight_oc = oc_step * filter_size * filter_size * ic;
        const int ld_weight_ic = oc_step * filter_size * filter_size;
        const int ld_src_ic = ih * iw;
        constexpr int c_dim = OCHelper<oc_block>::val;
636
        GI_FLOAT32_FIXLEN_t c[c_dim][8];
637
        init_ocx_ow8<c_dim, bias_mode, remain_w>(c, bias_ptr, oc_step);
638 639

        for (int ic_idx = 0; ic_idx < ic; ic_idx += loop_ic_step) {
640 641
            GI_FLOAT32_FIXLEN_t src[src_reg_size];
            GI_FLOAT32_FIXLEN_t weight[c_dim][filter_size];
642
            // row 0
643 644
            load_helper<src_reg_size, 0, simd_len, 0, Vld1qF32S>(src, src_ptr, 0);
            load_helper<filter_size, 0, oc_step, c_dim, Vld1qF32S>(
645
                    weight, weight_ptr, ld_weight_oc);
646 647
            cal_helper<0, 0, c_dim, stride, remain_w>(c, src, weight);
            cal_helper<1, 1, c_dim, stride, remain_w>(c, src, weight);
648 649

            // row 1
650 651
            load_helper<src_reg_size, 0, simd_len, 0, Vld1qF32S>(src, src_ptr + iw, 0);
            load_helper<filter_size, 0, oc_step, c_dim, Vld1qF32S>(
652
                    weight, weight_ptr + 1 * ld_weight_fw, ld_weight_oc);
653 654
            cal_helper<0, 0, c_dim, stride, remain_w>(c, src, weight);
            cal_helper<1, 1, c_dim, stride, remain_w>(c, src, weight);
655 656 657 658

            src_ptr += ld_src_ic;
            weight_ptr += ld_weight_ic;
        }
M
Megvii Engine Team 已提交
659
        store_ocx_ow8_remain_static<c_dim, remain_w, Op>(c, op, dst_ptr, ld_dst_oc);
660 661 662 663
    }
};

template <BiasMode bias_mode, typename Op, int filter_size, int stride>
664 665
struct ConvDirectFp32NchwNchw44 {
    static MEGDNN_ALWAYS_INLINE void impl(
M
Megvii Engine Team 已提交
666 667 668 669
            const float32_t* src, const float32_t* filter, const float32_t* bias,
            float32_t*, float32_t* dst, const int oc, const int ic, const int ih,
            const int iw, const int oh, const int oh_block, const int ow,
            const Op& op) {
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
        constexpr int fh = filter_size;
        constexpr int fw = filter_size;
        constexpr int ic_step = 1;
#if MEGDNN_ARMV7
        constexpr int big_oc_step = 4;
#else
        constexpr int big_oc_step = 8;
#endif
        constexpr int oc_step = 4;
        constexpr int ih_step = 1;
        constexpr int oh_step = 1;
        constexpr int ow_step = 8;
        constexpr int stride_h = stride;
        constexpr int stride_w = stride;
        constexpr int pack_iw_len = 1;

        const int img_stride = oh * ow;
        const int ow_end = ow / ow_step * ow_step;
        const int ow_remain = ow - ow_end;
        const int oc_end = oc / big_oc_step * big_oc_step;
        const int oc_remain = oc - oc_end;
        const int ld_dst_oc = oc_step * img_stride;

        using remain_fun = std::function<void(
                const float32_t* src_ptr, const float32_t* weight_ptr,
M
Megvii Engine Team 已提交
695 696
                const float32_t* bias_ptr, float32_t* dst_ptr, int ic, int ih, int iw,
                int ld_dst_oc, const Op& op)>;
697 698 699 700
        remain_fun kern_big_oc_remain = nullptr;
        remain_fun kern_small_oc_remain = nullptr;

        switch (ow_remain) {
M
Megvii Engine Team 已提交
701 702
#define cb(step)                                                                       \
    case step:                                                                         \
703
        kern_big_oc_remain = KerGiXXs2NchwNchw44FP32<                                  \
M
Megvii Engine Team 已提交
704
                bias_mode, Op, step, filter_size, big_oc_step, stride, ow_step>::impl; \
705
        kern_small_oc_remain = KerGiXXs2NchwNchw44FP32<                                \
M
Megvii Engine Team 已提交
706
                bias_mode, Op, step, filter_size, oc_step, stride, ow_step>::impl;     \
707 708
        break;

709 710 711 712 713 714 715 716 717
            UNROLL_CALL_RAW(8, cb);
            default:
                megdnn_assert(0, "no remain %d for kern", ow_remain);
        }
#undef cb
        for (int oc_idx = 0; oc_idx < oc_end; oc_idx += big_oc_step) {
            const int weight_offset = oc_idx * ic * fh * fw;
            for (int oh_idx = 0; oh_idx < oh_block; oh_idx += oh_step) {
                for (int ow_idx = 0; ow_idx < ow_end; ow_idx += ow_step) {
M
Megvii Engine Team 已提交
718 719 720 721 722
                    const int src_offset =
                            (oh_idx * stride_h * iw + ow_idx * stride_w * ih_step) *
                            ic_step * pack_iw_len;
                    const int dst_offset =
                            oc_idx * img_stride + (oh_idx * ow + ow_idx) * oc_step;
723
                    KerGiXXs2NchwNchw44FP32<
M
Megvii Engine Team 已提交
724 725 726 727 728
                            bias_mode, Op, ow_step, filter_size, big_oc_step, stride,
                            ow_step>::
                            impl(src + src_offset, filter + weight_offset,
                                 bias + oc_idx, dst + dst_offset, ic, ih, iw, ld_dst_oc,
                                 op);
729 730
                }
                if (ow_remain > 0) {
M
Megvii Engine Team 已提交
731 732 733 734 735 736 737 738
                    const int src_offset =
                            (oh_idx * stride_h * iw + ow_end * stride_w * ih_step) *
                            ic_step * pack_iw_len;
                    const int dst_offset =
                            oc_idx * img_stride + (oh_idx * ow + ow_end) * oc_step;
                    kern_big_oc_remain(
                            src + src_offset, filter + weight_offset, bias + oc_idx,
                            dst + dst_offset, ic, ih, iw, ld_dst_oc, op);
739
                }
740
            }
741 742 743 744 745 746
        }
        if (oc_remain > 0) {
            int oc_idx = oc_end;
            const int weight_offset = oc_idx * ic * fh * fw;
            for (int oh_idx = 0; oh_idx < oh_block; oh_idx += oh_step) {
                for (int ow_idx = 0; ow_idx < ow_end; ow_idx += ow_step) {
M
Megvii Engine Team 已提交
747 748 749 750 751
                    const int src_offset =
                            (oh_idx * stride_h * iw + ow_idx * stride_w * ih_step) *
                            ic_step * pack_iw_len;
                    const int dst_offset =
                            oc_idx * img_stride + (oh_idx * ow + ow_idx) * oc_step;
752
                    KerGiXXs2NchwNchw44FP32<
M
Megvii Engine Team 已提交
753 754 755 756 757
                            bias_mode, Op, ow_step, filter_size, oc_step, stride,
                            ow_step>::
                            impl(src + src_offset, filter + weight_offset,
                                 bias + oc_idx, dst + dst_offset, ic, ih, iw, ld_dst_oc,
                                 op);
758 759
                }
                if (ow_remain > 0) {
M
Megvii Engine Team 已提交
760 761 762 763 764 765 766 767
                    const int src_offset =
                            (oh_idx * stride_h * iw + ow_end * stride_w * ih_step) *
                            ic_step * pack_iw_len;
                    const int dst_offset =
                            oc_idx * img_stride + (oh_idx * ow + ow_end) * oc_step;
                    kern_small_oc_remain(
                            src + src_offset, filter + weight_offset, bias + oc_idx,
                            dst + dst_offset, ic, ih, iw, ld_dst_oc, op);
768
                }
769 770 771
            }
        }
    }
772 773 774 775 776 777
};

#if MEGDNN_ARMV7
template <BiasMode bias_mode, typename Op>
struct ConvDirectFp32NchwNchw44<bias_mode, Op, 3, 2> {
    static MEGDNN_ALWAYS_INLINE void impl(
M
Megvii Engine Team 已提交
778 779 780 781
            const float32_t* src, const float32_t* filter, const float32_t* bias,
            float32_t*, float32_t* dst, const int oc, const int ic, const int ih,
            const int iw, const int oh, const int oh_block, const int ow,
            const Op& op) {
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
        constexpr int filter_size = 3;
        constexpr int stride = 2;
        constexpr int fh = filter_size;
        constexpr int fw = filter_size;
        constexpr int ic_step = 1;
        constexpr int oc_step = 4;
        constexpr int big_oc_step = oc_step;
        constexpr int ih_step = 1;
        constexpr int oh_step = 1;
        constexpr int ow_step = 8;
        constexpr int stride_h = stride;
        constexpr int stride_w = stride;
        constexpr int pack_iw_len = 1;

        const int img_stride = oh * ow;
        const int ow_end = ow / ow_step * ow_step;
        const int ow_remain = ow - ow_end;
        const int oc_end = oc / big_oc_step * big_oc_step;
        const int ld_dst_oc = oc_step * img_stride;

        using remain_fun = std::function<void(
                const float32_t* src_ptr, const float32_t* weight_ptr,
M
Megvii Engine Team 已提交
804 805
                const float32_t* bias_ptr, float32_t* dst_ptr, int ic, int ih, int iw,
                int ld_dst_oc, const Op& op)>;
806 807 808
        remain_fun kern_big_oc_remain = nullptr;

        switch (ow_remain) {
M
Megvii Engine Team 已提交
809 810
#define cb(step)                                                                       \
    case step:                                                                         \
811
        kern_big_oc_remain = KerGiXXs2NchwNchw44FP32<                                  \
M
Megvii Engine Team 已提交
812
                bias_mode, Op, step, filter_size, big_oc_step, stride, ow_step>::impl; \
813 814 815 816 817 818 819 820
        break;

            UNROLL_CALL_RAW(8, cb);
            default:
                megdnn_assert(0, "no remain %d for kern", ow_remain);
        }
#undef cb
#if MGB_ENABLE_CPUINFO
M
Megvii Engine Team 已提交
821 822 823
        auto arch_tag = cpuinfo_get_current_core()->uarch == cpuinfo_uarch_cortex_a7
                              ? CpuTag::A7_TAG
                              : CpuTag::DEFAULT_CPU_TAG;
824 825 826
#else
        auto arch_tag = CpuTag::A7_TAG;
#endif
M
Megvii Engine Team 已提交
827 828 829 830

#ifdef __IN_TEE_ENV__
        arch_tag = CpuTag::DEFAULT_CPU_TAG;
#endif
831 832 833 834 835
        if (arch_tag == CpuTag::A7_TAG) {
            for (int oc_idx = 0; oc_idx < oc_end; oc_idx += big_oc_step) {
                const int weight_offset = oc_idx * ic * fh * fw;
                for (int oh_idx = 0; oh_idx < oh_block; oh_idx += oh_step) {
                    for (int ow_idx = 0; ow_idx < ow_end; ow_idx += ow_step) {
M
Megvii Engine Team 已提交
836 837 838 839 840
                        const int src_offset =
                                (oh_idx * stride_h * iw + ow_idx * stride_w * ih_step) *
                                ic_step * pack_iw_len;
                        const int dst_offset =
                                oc_idx * img_stride + (oh_idx * ow + ow_idx) * oc_step;
841
                        KerGiXXs2NchwNchw44FP32<
M
Megvii Engine Team 已提交
842 843 844 845 846
                                bias_mode, Op, ow_step, filter_size, big_oc_step,
                                stride, ow_step, CpuTag::A7_TAG>::
                                impl(src + src_offset, filter + weight_offset,
                                     bias + oc_idx, dst + dst_offset, ic, ih, iw,
                                     ld_dst_oc, op);
847 848
                    }
                    if (ow_remain > 0) {
M
Megvii Engine Team 已提交
849 850 851 852 853 854 855 856
                        const int src_offset =
                                (oh_idx * stride_h * iw + ow_end * stride_w * ih_step) *
                                ic_step * pack_iw_len;
                        const int dst_offset =
                                oc_idx * img_stride + (oh_idx * ow + ow_end) * oc_step;
                        kern_big_oc_remain(
                                src + src_offset, filter + weight_offset, bias + oc_idx,
                                dst + dst_offset, ic, ih, iw, ld_dst_oc, op);
857 858
                    }
                }
859
            }
860 861 862 863 864
        } else {
            for (int oc_idx = 0; oc_idx < oc_end; oc_idx += big_oc_step) {
                const int weight_offset = oc_idx * ic * fh * fw;
                for (int oh_idx = 0; oh_idx < oh_block; oh_idx += oh_step) {
                    for (int ow_idx = 0; ow_idx < ow_end; ow_idx += ow_step) {
M
Megvii Engine Team 已提交
865 866 867 868 869
                        const int src_offset =
                                (oh_idx * stride_h * iw + ow_idx * stride_w * ih_step) *
                                ic_step * pack_iw_len;
                        const int dst_offset =
                                oc_idx * img_stride + (oh_idx * ow + ow_idx) * oc_step;
870
                        KerGiXXs2NchwNchw44FP32<
M
Megvii Engine Team 已提交
871 872 873 874 875
                                bias_mode, Op, ow_step, filter_size, big_oc_step,
                                stride, ow_step>::
                                impl(src + src_offset, filter + weight_offset,
                                     bias + oc_idx, dst + dst_offset, ic, ih, iw,
                                     ld_dst_oc, op);
876 877
                    }
                    if (ow_remain > 0) {
M
Megvii Engine Team 已提交
878 879 880 881 882 883 884 885
                        const int src_offset =
                                (oh_idx * stride_h * iw + ow_end * stride_w * ih_step) *
                                ic_step * pack_iw_len;
                        const int dst_offset =
                                oc_idx * img_stride + (oh_idx * ow + ow_end) * oc_step;
                        kern_big_oc_remain(
                                src + src_offset, filter + weight_offset, bias + oc_idx,
                                dst + dst_offset, ic, ih, iw, ld_dst_oc, op);
886 887
                    }
                }
888 889 890
            }
        }
    }
891 892 893 894 895 896 897 898 899 900
};

#endif

}  // namespace

template <BiasMode bias_mode, typename Op, int filter_size, int stride>
void fp32_direct_nchw_nchw44::conv_direct_fp32_nchw_nchw44(
        const float32_t* src, const float32_t* filter, const float32_t* bias,
        float32_t*, float32_t* dst, const int oc, const int ic, const int ih,
M
Megvii Engine Team 已提交
901 902
        const int iw, const int oh, const int oh_block, const int ow, const Op& op,
        const int, const int) {
903
    ConvDirectFp32NchwNchw44<bias_mode, Op, filter_size, stride>::impl(
M
Megvii Engine Team 已提交
904
            src, filter, bias, nullptr, dst, oc, ic, ih, iw, oh, oh_block, ow, op);
905 906
}

M
Megvii Engine Team 已提交
907 908 909 910 911 912 913
#define INSTANTIATION(stride, filter_size, bias_mode, Op)                         \
    template void fp32_direct_nchw_nchw44::conv_direct_fp32_nchw_nchw44<          \
            bias_mode, Op, filter_size, stride>(                                  \
            const float32_t* src, const float32_t* filter, const float32_t* bias, \
            float32_t*, float32_t* dst, const int oc, const int ic, const int ih, \
            const int iw, const int oh, const int oh_block, const int ow,         \
            const Op& op, const int, const int);
914 915 916 917 918 919

#define FOR_OP(stride, filter, bias)                        \
    INSTANTIATION(stride, filter, bias, NoneOp<dt_float32>) \
    INSTANTIATION(stride, filter, bias, ReluOp<dt_float32>) \
    INSTANTIATION(stride, filter, bias, HSwishOp<dt_float32>)

920 921 922 923 924 925
#define INSTANCE_CONV_NO_BIAS(filter, stride) FOR_OP(stride, filter, BiasMode::NO_BIAS)

#define INSTANCE_CONV_BROADCAST_CHANNEL_BIAS(filter, stride) \
    FOR_OP(stride, filter, BiasMode::BROADCAST_CHANNEL_BIAS)

#define INSTANCE_CONV_BIAS(filter, stride) FOR_OP(stride, filter, BiasMode::BIAS)
926 927

// vim: syntax=cpp.doxygen