handle_impl.h 7.0 KB
Newer Older
1 2 3 4 5 6 7 8 9
#pragma once

#include "megdnn/handle.h"
#include "megdnn/oprs.h"

#include "src/common/utils.h"

#include <mutex>

10 11 12 13
#include "midout.h"

MIDOUT_DECL(dnn_src_common_handle_impl)

14 15 16 17 18 19 20
namespace megdnn {

class HandleImplHelper : public Handle {
public:
    using Handle::Handle;

    //! global matmul opr
M
Megvii Engine Team 已提交
21
    virtual MatrixMul* matmul_opr() { megdnn_throw("Unimplement matmul opr.\n"); }
22 23

    //! global matmul opr with first operand transposed
M
Megvii Engine Team 已提交
24
    virtual MatrixMul* matmul_aT_opr() { megdnn_throw("Unimplement matmul_aT opr.\n"); }
25 26

    //! global matmul opr with second operand transposed
M
Megvii Engine Team 已提交
27
    virtual MatrixMul* matmul_bT_opr() { megdnn_throw("Unimplement matmul_bT opr.\n"); }
28 29 30 31 32 33 34

    //! global matmul opr with both operand transposed
    virtual MatrixMul* matmul_aT_bT_opr() {
        megdnn_throw("Unimplement matmul_aT_bT opr.\n");
    }

    //! global relayout opr
M
Megvii Engine Team 已提交
35
    virtual Relayout* relayout_opr() { megdnn_throw("Unimplement Relayout opr.\n"); }
36

M
Megvii Engine Team 已提交
37
    virtual Checksum* checksum_opr() { megdnn_throw("Unimplement Checksum opr.\n"); }
38 39 40 41 42 43 44 45 46

    virtual MaxTensorDiff* max_tensor_diff_opr() {
        megdnn_throw("Unimplement MaxTensorDiff opr.\n");
    }

protected:
    static constexpr size_t NR_HELPER_OPRS = 7;

    template <class Opr, size_t idx, class Self>
M
Megvii Engine Team 已提交
47
    static Opr* get_helper_opr(Self self, const typename Opr::Param& param = {}) {
48 49
        MIDOUT_BEGIN(dnn_src_common_handle_impl, Opr, idx) {
            static_assert(idx < NR_HELPER_OPRS, "invalid idx");
50
            if (!self->m_helper_oprs[idx]) {
51
                MEGDNN_LOCK_GUARD(self->m_helper_oprs_mtx);
52
                if (!self->m_helper_oprs[idx]) {
M
Megvii Engine Team 已提交
53 54
                    self->m_helper_oprs[idx] = self->template create_operator<Opr>();
                    auto ret = static_cast<Opr*>(self->m_helper_oprs[idx].get());
55 56 57 58
                    ret->param() = param;
                    megdnn_assert(ret->is_thread_safe());
                    return ret;
                }
59
            }
60
            return static_cast<Opr*>(self->m_helper_oprs[idx].get());
61
        }
62
        MIDOUT_END();
63 64 65 66
    }

private:
    std::array<std::unique_ptr<OperatorBase>, NR_HELPER_OPRS> m_helper_oprs;
67
    DNN_MUTEX m_helper_oprs_mtx;
68 69 70
};

}  // namespace megdnn
71 72 73 74 75
   /*!
    * \brief iterate though each operator class name; useful for explicit
    *      instantialization of create_operator<> templates
    */
   // clang-format off
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
#define MEGDNN_FOREACH_OPR_CLASS(cb) \
    cb(ConvolutionForward) \
    cb(ConvolutionBackwardData) \
    cb(ConvolutionBackwardFilter) \
    cb(ConvPoolingForward) \
    cb(ConvBiasForward) \
    cb(Images2NeibsForward) \
    cb(Images2NeibsBackward) \
    cb(SlidingWindowTransposeForward) \
    cb(SlidingWindowTransposeBackward) \
    cb(ElemwiseForward) \
    cb(ElemwiseMultiType) \
    cb(AddUpdateForward) \
    cb(RelayoutForward) \
    cb(PoolingForward) \
    cb(PoolingBackward) \
    cb(LocalForward) \
    cb(LocalBackwardData) \
    cb(LocalBackwardFilter) \
    cb(LRNForward) \
    cb(LRNBackward) \
    cb(ROIPoolingForward) \
    cb(ROIPoolingBackward) \
    cb(WarpPerspectiveForward) \
    cb(WarpPerspectiveBackwardData) \
    cb(WarpPerspectiveBackwardMat) \
    cb(DotForward) \
    cb(MatrixInverse) \
    cb(MatrixMulForward) \
    cb(BatchedMatrixMulForward) \
    cb(SVDForward) \
    cb(ReduceForward) \
    cb(CondTake) \
    cb(CumsumForward) \
    cb(ArgmaxForward) \
    cb(ArgminForward) \
    cb(TransposeForward) \
    cb(ConcatForward) \
    cb(SplitForward) \
    cb(TileForward) \
    cb(TileBackward) \
    cb(RepeatForward) \
    cb(RepeatBackward) \
    cb(ArgsortForward) \
    cb(ArgsortBackward) \
    cb(TypeCvt) \
    cb(IndexingRemapForward) \
    cb(IndexingRemapBackward) \
    cb(ChecksumForward) \
    cb(IndexingOneHotForward) \
    cb(IndexingSetOneHotForward) \
    cb(IndexingMultiAxisVec) \
    cb(IndexingSetMultiAxisVec) \
    cb(IndexingIncrMultiAxisVec) \
    cb(MeshIndexing) \
    cb(IncrMeshIndexing) \
    cb(SetMeshIndexing) \
    cb(BatchedMeshIndexing) \
    cb(BatchedIncrMeshIndexing) \
    cb(BatchedSetMeshIndexing) \
    cb(Linspace) \
    cb(Eye) \
138
    cb(Diag) \
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    cb(SleepForward) \
    cb(UniformRNG) \
    cb(GaussianRNG) \
    cb(GammaRNG) \
    cb(BetaRNG) \
    cb(PoissonRNG) \
    cb(PermutationRNG) \
    cb(ShuffleRNGForward) \
    cb(ShuffleRNGBackward) \
    cb(SeparableConvForward) \
    cb(SeparableFilterForward) \
    cb(BNForward) \
    cb(BNBackward) \
    cb(GroupLocalForward) \
    cb(GroupLocalBackwardData) \
    cb(GroupLocalBackwardFilter) \
    cb(Flip) \
    cb(Rotate) \
    cb(ROICopy) \
    cb(CvtColor) \
    cb(WarpAffine) \
    cb(GaussianBlur) \
    cb(Resize) \
    cb(ResizeBackward) \
163
    cb(Resize3D) \
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
    cb(ParamPackConcat) \
    cb(MaxTensorDiff) \
    cb(MaskConvForward) \
    cb(MaskPropagate) \
    cb(Convolution3DForward) \
    cb(Convolution3DBackwardData) \
    cb(Convolution3DBackwardFilter) \
    cb(DeformableConvForward) \
    cb(DeformableConvBackwardFilter) \
    cb(DeformableConvBackwardData) \
    cb(DeformablePSROIPoolingForward) \
    cb(DeformablePSROIPoolingBackward) \
    cb(RelayoutFormat) \
    cb(TopK) \
    cb(PowC) \
    cb(LocalShareForward) \
    cb(LocalShareBackwardData) \
    cb(LocalShareBackwardFilter) \
    cb(ROIAlignForward) \
    cb(ROIAlignBackward) \
    cb(CorrelationForward) \
    cb(CorrelationBackwardData1) \
    cb(CorrelationBackwardData2) \
    cb(BatchConvBiasForward) \
    cb(Remap) \
    cb(RemapBackwardData) \
    cb(RemapBackwardMat) \
    cb(AdaptivePoolingForward) \
    cb(AdaptivePoolingBackward) \
    cb(DctChannelSelectForward) \
    cb(FakeQuantForward) \
    cb(FakeQuantBackward) \
    cb(TQTForward) \
    cb(TQTBackward) \
    cb(CheckNonFinite) \
    cb(LSQForward) \
    cb(LSQBackward) \
    cb(Fill) \
    cb(PaddingForward) \
203 204
    cb(PaddingBackward) \
    cb(LayerNormForward) \
205
    cb(LayerNormBackward) \
206 207
    cb(GeneralNormForward) \
    cb(GeneralNormBackward) \
208
    cb(DropoutForward) \
209
    cb(DropoutBackward) \
210 211 212 213 214
    cb(RNNCell) \
    cb(LSTMCell) \
    cb(RNN) \
    cb(RNNBackward) \
    cb(LSTM) \
215
    cb(LAMBUpdate) \
216 217
    cb(LSTMBackward) \
    cb(SoftmaxForward) \
218
    cb(SoftmaxBackward) \
219 220 221
    cb(NormForward) \
    cb(RegionRestrictedConvolutionForward) \
    cb(RegionRestrictedConvolutionBackwardData) \
222 223
    cb(RegionRestrictedConvolutionBackwardFilter) \
    cb(GroupNormForward) \
M
Megvii Engine Team 已提交
224
    cb(GroupNormBackward) \
225 226 227
    cb(MaskedFill) \
    cb(MultiHeadAttnForward)\
    cb(MultiHeadAttnBackward)
228
// clang-format on
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

/*!
 * \brief specialize HandleImpl::create_operator for a single opr type;
 *      implemented by <opr>Impl class
 */
#define MEGDNN_SPECIALIZE_CREATE_OPERATOR(opr)                   \
    template <>                                                  \
    std::unique_ptr<megdnn::opr> HandleImpl::create_operator() { \
        return megdnn::make_unique<opr##Impl>(this);             \
    }

/*!
 * \brief for explicit instantiation for HandleImpl::create_operator methods
 */
#define MEGDNN_INST_CREATE_OPERATOR(opr) \
    template std::unique_ptr<megdnn::opr> HandleImpl::create_operator();

// vim: syntax=cpp.doxygen