algos.h 7.1 KB
Newer Older
1 2 3 4 5 6 7 8
/**
 * \file dnn/src/fallback/convolution/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 17 18 19 20 21 22 23
 */

#pragma once

#include "src/fallback/conv_bias/algos.h"
#include "src/fallback/convolution/opr_impl.h"
#include "src/naive/convolution/helper.h"

namespace megdnn {
namespace fallback {

template <typename ST, typename DT, typename CT>
void kern_naive_forward(const ConvolutionImpl::NCBKernParam& p,
24 25 26
                        const ConvolutionImpl::NCBKernIndex& ncb_index) {
    size_t batch_id = ncb_index.ndrange_id[1];
    size_t group_id = ncb_index.ndrange_id[0];
27 28
    auto IC = p.filter_meta.icpg, IH = p.isz[0], IW = p.isz[1],
         OC = p.filter_meta.ocpg, OH = p.osz[0], OW = p.osz[1];
29 30 31 32 33
    ptrdiff_t fstrd = p.filter_meta.icpg * p.filter_meta.ocpg *
                      p.filter_meta.spatial[0] * p.filter_meta.spatial[1] *
                      p.filter_type.size();
    ptrdiff_t istrd = p.filter_meta.icpg * p.src_type.size();
    ptrdiff_t ostrd = p.filter_meta.ocpg * p.dst_type.size();
34 35 36 37 38
    TensorND src, dst;

    src.layout.dtype = p.src_type;
    dst.layout.dtype = p.dst_type;
    if (p.filter_meta.format == param::Convolution::Format::NCHW) {
39 40 41 42
        istrd *= p.isz[0] * p.isz[1];
        ostrd *= p.osz[0] * p.osz[1];
        src.layout.init_contiguous_stride({1, IC, IH, IW});
        dst.layout.init_contiguous_stride({1, OC, OH, OW});
43 44 45 46 47 48 49 50 51
    } else {
        // Must be NHWC
        megdnn_assert(
                p.filter_meta.format == param::Convolution::Format::NHWC,
                "AlgoNaive only support NCHW and NHWC, not support format %d",
                static_cast<int>(p.filter_meta.format));
        src.layout.init_contiguous_stride({1, IH, IW, IC});
        dst.layout.init_contiguous_stride({1, OH, OW, OC});
    }
52 53 54 55 56 57 58 59
    src.raw_ptr = reinterpret_cast<void*>(
            reinterpret_cast<uintptr_t>(p.src_ptr) +
            batch_id * p.inp_bs * p.src_type.size() + group_id * istrd);
    dst.raw_ptr = reinterpret_cast<void*>(
            reinterpret_cast<uintptr_t>(p.dst_ptr) +
            batch_id * p.out_bs * p.dst_type.size() + group_id * ostrd);
    ST* filter = reinterpret_cast<ST*>(
            reinterpret_cast<uintptr_t>(p.filter_ptr) + group_id * fstrd);
60 61
    std::copy(p.inp_s, p.inp_s + 4, src.layout.stride);
    std::copy(p.out_s, p.out_s + 4, dst.layout.stride);
62
    naive::convolution::forward<ST, ST, DT, CT>(src, filter, dst,
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
                                                p.filter_meta);
}

template <typename ftype, typename dtype, typename gtype>
void kern_naive(const ConvolutionBackwardDataImpl::NCBKernParam& p) {
    TensorND diff(const_cast<void*>(p.diff_ptr), p.diff_layout),
            filter(const_cast<void*>(p.filter_ptr), p.filter_layout),
            grad(p.grad_ptr, p.grad_layout);
    naive::convolution::backward_data<ftype, dtype, gtype>(filter, diff, grad,
                                                           p.filter_meta);
}

class ConvolutionImpl::AlgoFallback final : public AlgoBase {
public:
    bool is_reproducible() const override { return true; }
    const char* name() const override { return "FALLBACK_ALGO"; }
79
    bool usable(const NCBKernSizeParam& param,
80 81
                AlgoSelectionStrategy algo_selection_strategy) const override;

82
    size_t get_workspace(const NCBKernSizeParam& param) const override;
83 84 85 86 87 88 89 90 91

    SmallVector<NCBKern> dispatch_kern(
            const NCBKernSizeParam& /*param*/) const override;
};

class ConvolutionImpl::AlgoNaive final : public AlgoBase {
public:
    bool is_reproducible() const override { return true; }
    const char* name() const override { return "NAIVE_ALGO"; }
92
    bool usable(const NCBKernSizeParam& /*param*/,
93 94
                AlgoSelectionStrategy algo_selection_strategy) const override;

95
    size_t get_workspace(const NCBKernSizeParam&) const override { return 0; };
96 97 98 99 100 101

    SmallVector<NCBKern> dispatch_kern(
            const NCBKernSizeParam& /*param*/) const override;
};

class ConvolutionImpl::AlgoDefault final : public AlgoBase {
102 103
    static ConvBiasImpl::NCBKernSizeParam init_conv_bias_param(
            const NCBKernSizeParam& param);
104
    WorkspaceBundle get_bundle(const NCBKernSizeParam& param) const;
105
    static SmallVector<NCBKern> get_kimpl(ConvBiasImpl::AlgoBase* algo,
106
                                          const NCBKernSizeParam& param);
107
    static SmallVector<NCBKern> get_preprocess_kimpl(
108
            ConvBiasImpl::AlgoBase* algo, const NCBKernSizeParam& param);
109 110

public:
111
    AlgoDefault(ConvBiasImpl::AlgoBase*);
112 113
    bool is_reproducible() const override { return true; }
    const char* name() const override { return m_name.c_str(); }
114
    bool usable(const NCBKernSizeParam& param,
115 116
                AlgoSelectionStrategy algo_selection_strategy) const override;

117
    size_t get_workspace(const NCBKernSizeParam& param) const override;
118

119
    size_t get_preprocess_workspace(const NCBKernSizeParam&) const override;
120 121

    SmallVector<TensorLayout> deduce_preprocessed_filter_layout(
122
            const NCBKernSizeParam&) const override;
123 124

    SmallVector<NCBKern> dispatch_preprocess_kern(
125 126
            const NCBKernSizeParam& param) const override {
        return get_preprocess_kimpl(m_algorithm, param);
127 128
    }

129 130
    SmallVector<NCBKern> dispatch_kern(
            const NCBKernSizeParam& param) const override {
131
        return get_kimpl(m_algorithm, param);
132 133 134 135 136
    }

    void* type() const override { return sm_fallback_conv_algo_type; }

    //! select matmul to the highest preference
137
    bool is_preferred(const NCBKernSizeParam& param) const override;
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

private:
    std::string m_name;
    ConvBiasImpl::AlgoBase* m_algorithm;
};

class ConvolutionBackwardDataImpl::AlgoDirect final : public AlgoBase {
public:
    bool is_reproducible() const override { return true; }
    const char* name() const override { return "DeconvDirect"; }
    bool usable(ConvolutionBackwardDataImpl* opr,
                const NCBKernSizeParam& param) const override;
    size_t get_workspace(ConvolutionBackwardDataImpl*,
                         const NCBKernSizeParam& param) const override;
    ncb_kern_t dispatch_kern(ConvolutionBackwardDataImpl*,
                             const NCBKernSizeParam&) const override;
    void* type() const override { return sm_fallback_deconv_algo_type; }
};

class ConvolutionBackwardDataImpl::AlgoMatrixMul final : public AlgoBase {
public:
    bool is_reproducible() const override { return true; }
    const char* name() const override { return "DeconvMatmul"; }
    bool usable(ConvolutionBackwardDataImpl* opr,
                const NCBKernSizeParam& param) const override;
    size_t get_workspace(ConvolutionBackwardDataImpl*,
                         const NCBKernSizeParam& param) const override;
    ncb_kern_t dispatch_kern(ConvolutionBackwardDataImpl*,
                             const NCBKernSizeParam&) const override;
    void* type() const override { return sm_fallback_deconv_algo_type; }
};

}  // namespace fallback
}  // namespace megdnn

// vim: syntax=cpp.doxygen