strategy.cpp 13.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
/**
 * \file dnn/src/aarch64/conv_bias/quint8/strategy.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/aarch64/conv_bias/quint8/strategy.h"
#include "src/arm_common/simd_macro/marm_neon.h"
#include "src/common/utils.h"
#include "src/fallback/conv_bias/common.h"

#include "src/aarch64/matrix_mul/quint8_dot/kernel_8x8x4.h"
#include "src/aarch64/matrix_mul/quint8/kernel_8x8x8.h"
#include "src/arm_common/conv_bias/matmul_postprocess.h"

using namespace megdnn;
using namespace aarch64;
using namespace aarch64::matmul;

namespace impl {
template <BiasMode bmode, typename Op, int block_m, int block_n>
struct KernCaller;

#if __ARM_FEATURE_DOTPROD
template <BiasMode bmode, typename Op>
struct KernCaller<bmode, Op, 8, 8> {
    static void run(const dt_uint8* packA, const dt_uint8* packB, size_t M,
                    size_t N, size_t K, dt_uint8* C, size_t LDC,
                    bool is_first_k, Op op, const dt_int32* bias,
                    dt_int32* workspace, uint8_t zp_A, uint8_t zp_B) {
        megdnn_assert(is_first_k);
        constexpr size_t A_INTERLEAVE = 8;
        constexpr size_t B_INTERLEAVE = 8;
        const uint32_t zAB =
                static_cast<uint32_t>(zp_A) * static_cast<uint32_t>(zp_B) * K;
        //! K is packed to times of 4
        K = round_up<size_t>(K, 4);
        const int K8 = (K << 3);
        const int K4 = K * 4;

        size_t m = 0;
        for (; m + A_INTERLEAVE - 1 < M; m += A_INTERLEAVE) {
            uint8_t* output = C + (m * LDC);

            size_t n = 0;
            const dt_uint8* cur_packB = packB;
            for (; n + B_INTERLEAVE - 1 < N; n += B_INTERLEAVE) {
                matmul_8x8x4::kern_8x8(packA, cur_packB, K, workspace, 8,
                                       is_first_k, zp_A, zp_B, zAB);

                arm_common::ConvBiasMatmul<bmode, Op, dt_uint8, 8, 8, 8,
                                           8>::postprocess(bias, workspace,
                                                           output, LDC, op);
                output += B_INTERLEAVE;
                cur_packB += K8;
            }

            for (; n < N; n += 4) {
                matmul_8x8x4::kern_8x4(packA, cur_packB, K, workspace, 4,
                                       is_first_k, std::min<size_t>(N - n, 4),
                                       zp_A, zp_B, zAB);
#define cb(m, n)                                                              \
    arm_common::ConvBiasMatmul<bmode, Op, dt_uint8, 8, 4, 8, n>::postprocess( \
            bias, workspace, output, LDC, op);
                DISPATCH_N(cb, 8, std::min<size_t>(N - n, 4));
#undef cb

                output += 4;
                cur_packB += K4;
            }
            packA += K8;
            if (bmode == BiasMode::BROADCAST_CHANNEL_BIAS) {
                bias += A_INTERLEAVE;
            }
        }

        for (; m < M; m += 4) {
            uint8_t* output = C + (m * LDC);
            const dt_uint8* cur_packB = packB;
            size_t n = 0;
            for (; n + B_INTERLEAVE - 1 < N; n += B_INTERLEAVE) {
                matmul_8x8x4::kern_4x8(packA, cur_packB, K, workspace, 8,
                                       is_first_k, std::min<size_t>(M - m, 4),
                                       zp_A, zp_B, zAB);
#define cb(m, n)                                                              \
    arm_common::ConvBiasMatmul<bmode, Op, dt_uint8, 4, 8, m, n>::postprocess( \
            bias, workspace, output, LDC, op);
                DISPATCH_M_N(cb, std::min<size_t>(M - m, 4), 8);
#undef cb

                output += B_INTERLEAVE;
                cur_packB += K8;
            }

            for (; n < N; n += 4) {
                matmul_8x8x4::kern_4x4(packA, cur_packB, K, workspace, 4,
                                       is_first_k, std::min<size_t>(M - m, 4),
                                       std::min<size_t>(N - n, 4), zp_A, zp_B,
                                       zAB);
#define cb(m, n)                                                              \
    arm_common::ConvBiasMatmul<bmode, Op, dt_uint8, 4, 4, m, n>::postprocess( \
            bias, workspace, output, LDC, op);
                DISPATCH_M(cb, std::min<size_t>(M - m, 4),
                           std::min<size_t>(N - n, 4));
#undef cb

                output += 4;
                cur_packB += K4;
            }
            packA += K4;
            if (bmode == BiasMode::BROADCAST_CHANNEL_BIAS) {
                bias += 4;
            }
        }
    }
};

#else

template <BiasMode bmode, typename Op>
struct KernCaller<bmode, Op, 8, 8> {
    static void run(const dt_uint8* packA, const dt_uint8* packB, size_t M,
                    size_t N, size_t K, dt_uint8* C, size_t LDC,
                    bool is_first_k, Op op, const dt_int32* bias,
                    dt_int32* workspace, uint8_t zp_A, uint8_t zp_B) {
        megdnn_assert(is_first_k);

        constexpr size_t A_INTERLEAVE = 8;
        constexpr size_t B_INTERLEAVE = 8;
        //! K is packed to times of 8
        K = round_up<size_t>(K, 8);
        const int K8 = K * 8;
        const int K4 = K * 4;

        size_t m = 0;
        for (; m + A_INTERLEAVE - 1 < M; m += A_INTERLEAVE) {
            uint8_t* output = C + (m * LDC);

            size_t n = 0;
            const dt_uint8* cur_packB = packB;
            for (; n + B_INTERLEAVE - 1 < N; n += B_INTERLEAVE) {
                matmul_8x8x8::kern_8x8(packA, cur_packB, K, workspace, 8,
                                       is_first_k, zp_A, zp_B);

                arm_common::ConvBiasMatmul<bmode, Op, dt_uint8, 8, 8, 8,
                                           8>::postprocess(bias, workspace,
                                                           output, LDC, op);
                output += B_INTERLEAVE;
                cur_packB += K8;
            }

            for (; n < N; n += 4) {
                matmul_8x8x8::kern_8x4(packA, cur_packB, K, workspace, 4,
                                       is_first_k, std::min<size_t>(N - n, 4),
                                       zp_A, zp_B);
#define cb(m, n)                                                              \
    arm_common::ConvBiasMatmul<bmode, Op, dt_uint8, 8, 4, 8, n>::postprocess( \
            bias, workspace, output, LDC, op);
                DISPATCH_N(cb, 8, std::min<size_t>(N - n, 4));
#undef cb


                output += 4;
                cur_packB += K4;
            }
            packA += K8;
            if (bmode == BiasMode::BROADCAST_CHANNEL_BIAS) {
                bias += A_INTERLEAVE;
            }
        }

        for (; m < M; m += 4) {
            uint8_t* output = C + (m * LDC);
            const dt_uint8* cur_packB = packB;
            size_t n = 0;
            for (; n + B_INTERLEAVE - 1 < N; n += B_INTERLEAVE) {
                matmul_8x8x8::kern_4x8(packA, cur_packB, K, workspace, 8,
                                       is_first_k, std::min<size_t>(M - m, 4),
                                       zp_A, zp_B);
#define cb(m, n)                                                              \
    arm_common::ConvBiasMatmul<bmode, Op, dt_uint8, 4, 8, m, n>::postprocess( \
            bias, workspace, output, LDC, op);
                DISPATCH_M_N(cb, std::min<size_t>(M - m, 4), 8);
#undef cb

                output += B_INTERLEAVE;
                cur_packB += K8;
            }

            for (; n < N; n += 4) {
                matmul_8x8x8::kern_4x4(packA, cur_packB, K, workspace, 4,
                                       is_first_k, std::min<size_t>(M - m, 4),
                                       std::min<size_t>(N - n, 4), zp_A, zp_B);
#define cb(m, n)                                                              \
    arm_common::ConvBiasMatmul<bmode, Op, dt_uint8, 4, 4, m, n>::postprocess( \
            bias, workspace, output, LDC, op);
                DISPATCH_M(cb, std::min<size_t>(M - m, 4),
                           std::min<size_t>(N - n, 4));
#undef cb


                output += 4;
                cur_packB += K4;
            }
            packA += K4;
            if (bmode == BiasMode::BROADCAST_CHANNEL_BIAS) {
                bias += 4;
            }
        }
    }
};

#endif

}  // namespace impl
#if __ARM_FEATURE_DOTPROD
MEGDNN_REG_GEMM_STRATEGY_IMPL(gemm_u8_8x8_nobias_identity)

void gemm_u8_8x8_nobias_identity::pack_A(uint8_t* outptr, const uint8_t* inptr,
                                         int ldin, int y0, int ymax, int k0,
                                         int kmax, bool transpose) const {
    if (transpose) {
        matmul_8x8x4::gemm_u8_8x8_transpose_pack_helper(outptr, inptr, ldin, y0,
                                                        ymax, k0, kmax);
    } else {
        matmul_8x8x4::gemm_u8_8x8_interleave_pack_helper(outptr, inptr, ldin,
                                                         y0, ymax, k0, kmax);
    }
}

void gemm_u8_8x8_nobias_identity::pack_B(uint8_t* out, const uint8_t* in,
                                         int ldin, int x0, int xmax, int k0,
                                         int kmax, bool transpose) const {
    if (transpose) {
        matmul_8x8x4::gemm_u8_8x8_interleave_pack_helper(out, in, ldin, x0,
                                                         xmax, k0, kmax);
    } else {
        matmul_8x8x4::gemm_u8_8x8_transpose_pack_helper(out, in, ldin, x0, xmax,
                                                        k0, kmax);
    }
}

#else

MEGDNN_REG_GEMM_STRATEGY_IMPL(gemm_u8_8x8_nobias_identity)
void gemm_u8_8x8_nobias_identity::pack_A(dt_uint8* outptr,
                                         const dt_uint8* inptr, int ldin,
                                         int y0, int ymax, int k0, int kmax,
                                         bool transpose) const {
    uint8_t zA = A_dtype.param<dtype::Quantized8Asymm>().zero_point;
    if (transpose) {
        matmul_8x8x8::gemm_u8_8x8_transpose_pack_A_n(outptr, inptr, ldin, y0,
                                                     ymax, k0, kmax, zA);
    } else {
        matmul_8x8x8::gemm_u8_8x8_pack_A_n(outptr, inptr, ldin, y0, ymax, k0,
                                           kmax, zA);
    }
}

void gemm_u8_8x8_nobias_identity::pack_B(dt_uint8* out, const dt_uint8* in,
                                         int ldin, int x0, int xmax, int k0,
                                         int kmax, bool transpose) const {
    uint8_t zB = B_dtype.param<dtype::Quantized8Asymm>().zero_point;
    if (transpose) {
        matmul_8x8x8::gemm_u8_8x8_transpose_pack_B_n(out, in, ldin, x0, xmax,
                                                     k0, kmax, zB);
    } else {
        matmul_8x8x8::gemm_u8_8x8_pack_B_n(out, in, ldin, x0, xmax, k0, kmax,
                                           zB);
    }
}

#endif
size_t gemm_u8_8x8_nobias_identity::get_workspace_size() const {
    return 8 * 8 * sizeof(dt_int32);
}

#define KERN(_block_m, _block_n, _bias, _BIAS, _nonline, _OP)                 \
    void gemm_u8_##_block_m##x##_block_n##_##_bias##_##_nonline::kern(        \
            const dt_uint8* packA, const dt_uint8* packB, size_t M, size_t N, \
            size_t K, dt_uint8* C, size_t LDC, bool is_first_k,               \
            const dt_int32* bias, dt_int32* workspace) const {                \
        float scale_A = A_dtype.param<dtype::Quantized8Asymm>().scale;        \
        uint8_t zp_A = A_dtype.param<dtype::Quantized8Asymm>().zero_point;    \
        float scale_B = B_dtype.param<dtype::Quantized8Asymm>().scale;        \
        uint8_t zp_B = B_dtype.param<dtype::Quantized8Asymm>().zero_point;    \
        float scale_C = C_dtype.param<dtype::Quantized8Asymm>().scale;        \
        uint8_t zp_C = C_dtype.param<dtype::Quantized8Asymm>().zero_point;    \
        DEFINE_OP(_OP);                                                       \
        impl::KernCaller<_BIAS, decltype(op), _block_m, _block_n>::run(       \
                packA, packB, M, N, K, C, LDC, is_first_k, op, bias,          \
                workspace, zp_A, zp_B);                                       \
    }

#define DEFINE_OP(_Op) \
    arm_common::_Op<dt_qint32, dt_quint8> op(scale_A* scale_B, scale_C, zp_C);

KERN(8, 8, nobias, BiasMode::NO_BIAS, identity, TypeCvtOp)
KERN(8, 8, nobias, BiasMode::NO_BIAS, relu, ReluOp)
KERN(8, 8, nobias, BiasMode::NO_BIAS, hswish, HSwishOp)
#undef DEFINE_OP

#define DEFINE_OP(_Op)                                         \
    arm_common::_Op<dt_qint32, dt_quint8> op(scale_A* scale_B, \
                                             scale_A* scale_B, scale_C, zp_C);
KERN(8, 8, bias_channel, BiasMode::BROADCAST_CHANNEL_BIAS, identity, AddOp)
KERN(8, 8, bias_channel, BiasMode::BROADCAST_CHANNEL_BIAS, relu, FuseAddReluOp)
KERN(8, 8, bias_channel, BiasMode::BROADCAST_CHANNEL_BIAS, hswish,
     FuseAddHSwishOp)
#undef DEFINE_OP

#undef KERN

// vim: syntax=cpp.doxygen