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

namespace megdnn {
namespace x86 {
17 18 19 20 21 22 23 24 25 26 27

/* ===================== avx2 stride1 chanwise algo ===================== */
class ConvBiasImpl::AlgoChanWiseAvx2Stride1Qint8 final : public AlgoBase {
    SmallVector<NCBKern> get_kimpls(const NCBKernSizeParam& param) const;
    static WorkspaceBundle get_bundle(const NCBKernSizeParam& param);

public:
    bool is_reproducible() const override { return true; }
    const char* name() const override {
        return "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE1";
    }
28
    bool usable(const NCBKernSizeParam& param,
29
                AlgoSelectionStrategy algo_selection_strategy) const override;
30
    size_t get_workspace(const NCBKernSizeParam& param) const override;
31 32 33 34 35
    virtual SmallVector<NCBKern> dispatch_kerns(
            const NCBKernSizeParam& param) const override {
        return get_kimpls(param);
    }
    void* type() const override;
36
    bool is_preferred(const NCBKernSizeParam& param) const override;
37 38
};

39 40 41 42 43 44 45 46 47 48
/* ===================== avx2 stride2 chanwise algo ===================== */
class ConvBiasImpl::AlgoChanWiseAvx2Stride2Qint8 final : public AlgoBase {
    SmallVector<NCBKern> get_kimpls(const NCBKernSizeParam& param) const;
    static WorkspaceBundle get_bundle(const NCBKernSizeParam& param);

public:
    bool is_reproducible() const override { return true; }
    const char* name() const override {
        return "X86_CONV_BIAS_CHANWISE_AVX2_INT8_STRIDE2";
    }
49
    bool usable(const NCBKernSizeParam& param,
50
                AlgoSelectionStrategy algo_selection_strategy) const override;
51
    size_t get_workspace(const NCBKernSizeParam& param) const override;
52 53 54 55 56
    virtual SmallVector<NCBKern> dispatch_kerns(
            const NCBKernSizeParam& param) const override {
        return get_kimpls(param);
    }
    void* type() const override;
57
    bool is_preferred(const NCBKernSizeParam& param) const override;
58 59
};

60 61 62 63 64 65 66 67 68 69
/* ===================== avx2 stride1 direct algo ===================== */
class ConvBiasImpl::AlgoDirectAvx2Stride1Int8 final : public AlgoBase {
    SmallVector<NCBKern> get_kimpls(const NCBKernSizeParam& param) const;
    static WorkspaceBundle get_bundle(const NCBKernSizeParam& param);

public:
    bool is_reproducible() const override { return true; }
    const char* name() const override {
        return "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE1";
    }
70
    bool usable(const NCBKernSizeParam& param,
71
                AlgoSelectionStrategy algo_selection_strategy) const override;
72
    size_t get_workspace(const NCBKernSizeParam& param) const override;
73 74 75 76 77
    virtual SmallVector<NCBKern> dispatch_kerns(
            const NCBKernSizeParam& param) const override {
        return get_kimpls(param);
    }
    void* type() const override;
78
    bool is_preferred(const NCBKernSizeParam& param) const override;
79 80 81 82 83 84 85 86 87 88 89 90
};

/* ================== avx2 int8 direct conv stride2 algo ================== */
class ConvBiasImpl::AlgoAVX2DirectConvStride2 final : public AlgoBase {
    SmallVector<NCBKern> get_kimpls(const NCBKernSizeParam& param) const;
    static WorkspaceBundle get_bundle(const NCBKernSizeParam& param);

public:
    bool is_reproducible() const override { return true; }
    const char* name() const override {
        return "X86_CONV_BIAS_DIRECT_AVX2_INT8_STRIDE2";
    }
91
    bool usable(const NCBKernSizeParam& param,
92
                AlgoSelectionStrategy algo_selection_strategy) const override;
93
    size_t get_workspace(const NCBKernSizeParam& param) const override;
94 95 96 97 98
    SmallVector<NCBKern> dispatch_kerns(
            const NCBKernSizeParam& param) const override {
        return get_kimpls(param);
    }
    void* type() const override;
99
    bool is_preferred(const NCBKernSizeParam& param) const override;
100 101
};

102
#if MEGDNN_X86_WITH_MKL_DNN
103 104 105 106 107 108 109 110 111 112
/* ===================== mkldnn qint8 algo ===================== */
class ConvBiasImpl::AlgoMkldnnQint8 final : public AlgoBase {
    static void kern_mkldnn_s8x8x32(const NCBKernParam& param,
                                    const NCBKernIndex&);
    static WorkspaceBundle get_bundle(const NCBKernSizeParam& param);

public:
    AlgoMkldnnQint8() {}
    bool is_reproducible() const override { return true; }
    const char* name() const override { return "MKLDNN_INT8"; }
113
    bool usable(const NCBKernSizeParam& param,
114 115
                AlgoSelectionStrategy) const override;

116
    size_t get_workspace(const NCBKernSizeParam& param) const override {
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        size_t nr_threads = param.nr_threads;
        return get_bundle(param).total_size_in_bytes() * nr_threads;
    }
    SmallVector<NCBKern> dispatch_kerns(
            const NCBKernSizeParam& param) const override {
        size_t group = param.filter_meta.group;
        size_t n = param.n;
        auto workspace_per_thread = get_bundle(param).total_size_in_bytes();
        auto kern = [workspace_per_thread](const NCBKernParam& param,
                                           const NCBKernIndex& ncb_index) {
            auto thread_param = param;
            thread_param.workspace_ptr = reinterpret_cast<void*>(
                    reinterpret_cast<ptrdiff_t>(param.workspace_ptr) +
                    ncb_index.thread_id * workspace_per_thread);
            kern_mkldnn_s8x8x32(thread_param, std::move(ncb_index));
        };
        return {{kern, {group, n, 1_z}}};
    }
    void* type() const override;
136
    bool is_preferred(const NCBKernSizeParam& param) const override;
137 138 139 140 141 142 143 144 145 146 147
};
/* ===================== mkldnn qint8 matmul algo ===================== */
class ConvBiasImpl::AlgoMkldnnMatmulQint8 final : public AlgoBase {
    static MatrixMul* get_matmul_opr();
    static void kern_mkldnn_matmul_s8x8x32(const NCBKernParam& param,
                                           const NCBKernIndex&);
    static WorkspaceBundle get_bundle(const NCBKernSizeParam& param);

public:
    bool is_reproducible() const override { return true; }
    const char* name() const override { return "MKLDNN_MATMUL_INT8"; }
148
    bool usable(const NCBKernSizeParam& param,
149 150
                AlgoSelectionStrategy) const override;

151
    size_t get_workspace(const NCBKernSizeParam& param) const override {
152 153 154 155 156 157 158 159
        return get_bundle(param).total_size_in_bytes();
    }
    SmallVector<NCBKern> dispatch_kerns(
            const NCBKernSizeParam& param) const override {
        size_t group = param.filter_meta.group;
        return {{kern_mkldnn_matmul_s8x8x32, {group, 1_z, 1_z}}};
    }
    //! select matmul to the highest preference
160
    bool is_preferred(const NCBKernSizeParam& param) const override;
161 162 163 164 165 166 167 168 169

    void* type() const override;
};
#endif

}  // namespace x86
}  // namespace megdnn

// vim: syntax=cpp.doxygen