algo.h 7.8 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/cuda/convolution3d/forward/algo.h
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6 7 8
 *
 * 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 "megdnn/oprs.h"

17
#include "src/common/utils.h"
18 19
#include "src/cuda/convolution3d/helper.h"
#include "src/cuda/convolution3d/opr_impl.h"
20 21 22
#include "src/cuda/handle.h"
#include "src/common/algo_base.h"
#include "src/common/metahelper.h"
23 24 25 26 27 28 29 30 31 32 33 34

#include <unordered_map>

namespace megdnn {
namespace cuda {

/*!
 * \brief base class for convolution3d algos
 *
 * All the algo impls should try to support non-contiguous batch dim, for group
 * conv execution.
 */
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
class Convolution3DForwardImpl::AlgoBase : public Algorithm {
protected:
    ~AlgoBase() = default;

public:
    enum class AlgoType : uint32_t {
        CUDA_1X1X1,
        CUDA_GROUP_CONV_GENERAL,
        CUDA_CUDNN,
        CUDA_INPLACE_MATMUL,
        CUDA_CHANWISE,
    };
    using Mapper = std::unordered_map<AlgorithmDesc, AlgoBase*>;

    AlgoBase() : Algorithm() { m_handle_type = Handle::HandleType::CUDA; }
    struct SizeArgs : public convolution3d::ForwardSizeArgs {
        Convolution3DForwardImpl* opr;

        std::string to_string() const;
        void init_desc(convolution3d::CUDNNForwardDescs& desc) const {
            desc.set(*src_layout, filter_meta, *dst_layout, opr->param());
        }
        SizeArgs(Convolution3DForwardImpl* opr, const TensorLayout& src,
                 const TensorLayout& filter, const TensorLayout& dst);
        SizeArgs(Convolution3DForwardImpl* opr, const TensorLayout& src,
                 const CanonizedFilterMeta& filter, const TensorLayout& dst);
    };
    struct ExecArgs : public SizeArgs {
        const TensorND *src_tensor, *filter_tensor, *dst_tensor;
        Workspace workspace;

        ExecArgs(Convolution3DForwardImpl* opr, _megdnn_tensor_in src,
                 _megdnn_tensor_in filter, _megdnn_tensor_out dst,
                 _megdnn_workspace workspace);
    };
    virtual bool is_available(const SizeArgs& args) const = 0;
    virtual size_t get_workspace_in_bytes(const SizeArgs& args) const = 0;
    virtual void exec(const ExecArgs& args) const = 0;

    bool is_available_wk(const SizeArgs& args, size_t limit) {
        return is_available(args) && get_workspace_in_bytes(args) <= limit;
    }
    bool is_available_reproducible(
            const SizeArgs& args, bool reproducible = true,
            size_t limit = std::numeric_limits<size_t>::max()) {
80 81
        return (!reproducible ||
                contain_attribute(AlgoAttribute::REPRODUCIBLE)) &&
82 83 84 85 86 87 88 89 90 91 92 93 94
               is_available_wk(args, limit);
    }
    AlgoBase& check_workspace(const SizeArgs& args,
                              const Workspace& workspace) {
        auto req = get_workspace_in_bytes(args);
        megdnn_assert(
                req <= workspace.size,
                "conv3d fwd algo %s: required workspace %zu bytes, got %zu",
                name(), req, workspace.size);
        return *this;
    }

    virtual bool is_cudnn() const { return false; }
95
};
96 97 98 99 100 101 102 103 104 105
class Convolution3DForwardImpl::Algo1x1x1 final : public AlgoBase {
    static void extract_matmul_layouts(const SizeArgs& args, TensorLayout& A,
                                       TensorLayout& B, TensorLayout& C);

public:
    bool is_available(const SizeArgs& args) const override;
    size_t get_workspace_in_bytes(const SizeArgs& args) const override;
    void exec(const ExecArgs& args) const override;

    const char* name() const override { return "1x1x1"; }
106 107 108
    AlgoAttribute attribute() const override {
        return AlgoAttribute::REPRODUCIBLE;
    }
109
    MEGDNN_DECL_ALGO_TYPE(CUDA_1X1X1)
110 111 112
};

//! implement group conv by another algo
113 114
class Convolution3DForwardImpl::AlgoGroupConvGeneral final : public AlgoBase {
    AlgoBase* m_impl;
115 116
    std::string m_name;

117 118
public:
    AlgoGroupConvGeneral(AlgoBase* impl);
119

120 121 122
    bool is_available(const SizeArgs& args) const override;
    size_t get_workspace_in_bytes(const SizeArgs& args) const override;
    void exec(const ExecArgs& args) const override;
123

124
    const char* name() const override { return m_name.c_str(); }
125

126 127 128 129 130 131 132
    AlgoAttribute attribute() const override {
        auto ret = static_cast<AlgoAttribute>(0);
        if (m_impl->contain_attribute(AlgoAttribute::REPRODUCIBLE)) {
            ret |= AlgoAttribute::REPRODUCIBLE;
        }
        return ret;
    }
133 134 135 136 137 138 139 140
    static void modify_size_args(SizeArgs& args, TensorLayout& src_pg,
                                 TensorLayout& dst_pg);
    MEGDNN_DECL_ALGO_TYPE(CUDA_GROUP_CONV_GENERAL)
    std::string param() const override {
        std::string ret;
        serialize_write_pod(m_impl, ret);
        return ret;
    }
141 142 143 144
};

class Convolution3DForwardImpl::AlgoCUDNN final : public AlgoBase {
    cudnnConvolutionFwdAlgo_t m_cudnn_enum;
145
    CudnnAlgoPack::Attr m_attr;
146

147 148 149 150 151 152
public:
    AlgoCUDNN(cudnnConvolutionFwdAlgo_t cudnn_enum) : m_cudnn_enum(cudnn_enum) {
        megdnn_assert(CudnnAlgoPack::conv3d_fwd_algos().find(cudnn_enum) !=
                      CudnnAlgoPack::conv3d_fwd_algos().end());
        m_attr = CudnnAlgoPack::conv3d_fwd_algos().at(cudnn_enum);
    }
153

154 155 156
    bool is_available(const SizeArgs& args) const override;
    size_t get_workspace_in_bytes(const SizeArgs& args) const override;
    void exec(const ExecArgs& args) const override;
157

158 159 160 161 162 163 164
    AlgoAttribute attribute() const override {
        auto ret = static_cast<AlgoAttribute>(0);
        if (m_attr.is_reproducible) {
            ret |= AlgoAttribute::REPRODUCIBLE;
        }
        return ret;
    }
165

166
    const char* name() const override { return m_attr.name.c_str(); }
167

168
    cudnnConvolutionFwdAlgo_t cudnn_enum() const { return m_cudnn_enum; }
169

170
    bool is_cudnn() const override { return true; }
171

172
    MEGDNN_DECL_ALGO_TYPE(CUDA_CUDNN)
173

174 175 176 177 178
    std::string param() const override {
        std::string ret;
        serialize_write_pod(m_cudnn_enum, ret);
        return ret;
    }
179 180 181

};

182 183 184 185 186
class Convolution3DForwardImpl::AlgoInplaceMatmul final : public AlgoBase {
public:
    bool is_available(const SizeArgs& args) const override;
    size_t get_workspace_in_bytes(const SizeArgs& args) const override;
    void exec(const ExecArgs& args) const override;
187

188
    const char* name() const override { return "INPLACE_MATMUL"; }
189 190 191
    AlgoAttribute attribute() const override {
        return AlgoAttribute::REPRODUCIBLE;
    }
192 193
    MEGDNN_DECL_ALGO_TYPE(CUDA_INPLACE_MATMUL)
};
194

195 196 197 198 199 200 201
class Convolution3DForwardImpl::AlgoChanwise final : public AlgoBase {
public:
    bool is_available(const SizeArgs& args) const override;
    size_t get_workspace_in_bytes(const SizeArgs& args) const override;
    void exec(const ExecArgs& args) const override;

    const char* name() const override { return "CHANNEL_WISE"; }
202 203 204
    AlgoAttribute attribute() const override {
        return AlgoAttribute::REPRODUCIBLE;
    }
205
    MEGDNN_DECL_ALGO_TYPE(CUDA_CHANWISE)
206 207
};

208
class Convolution3DForwardImpl::AlgoPack : NonCopyableObj {
209 210 211
    // defined in cudnn.cpp
    void fill_cudnn_algos();

212
    AlgoBase::Mapper m_all_algos_map;
213

214 215
public:
    AlgoPack();
216

217 218 219 220 221 222
    std::vector<AlgoCUDNN> cudnn;
    Algo1x1x1 a1x1x1;
    AlgoInplaceMatmul inplace_matmul;
    AlgoChanwise chanwise;
    std::vector<AlgoGroupConvGeneral> gconv;
    std::unordered_map<AlgoBase*, AlgoGroupConvGeneral*> algo2gconv;
223

224
    std::vector<AlgoBase*>
225 226 227 228 229
            //! all algorithms
            all_algos,
            //! non-cudnn algos, used for heuristic if cudnn is not supported
            non_cudnn_algos;

230 231 232
    AlgoCUDNN* cudnn_from_enum(cudnnConvolutionFwdAlgo_t algo);

    const AlgoBase::Mapper& all_algos_map() const { return m_all_algos_map; }
233 234
};

235 236
}  // namespace cuda
}  // namespace megdnn
237 238

// vim: syntax=cpp.doxygen
239