algo.h 6.9 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/cuda/convolution3d/backward_data/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
 */

#pragma once

#include <unordered_map>
16 17 18
#include "src/cuda/convolution3d/helper.h"
#include "src/common/algo_base.h"
#include "src/common/metahelper.h"
19 20 21 22 23 24 25 26 27 28

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.
 */
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
class Convolution3DBackwardDataImpl::AlgoBase : public Algorithm {
protected:
    ~AlgoBase() = default;

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

    AlgoBase() : Algorithm() { m_handle_type = Handle::HandleType::CUDA; }
    struct SizeArgs {
        HandleImpl* handle;
        CanonizedFilterMeta filter_meta;
        const TensorLayout *diff_layout, *grad_layout;
        Convolution3DBackwardDataImpl* opr;

        std::string to_string() const;
        void init_desc(convolution3d::CUDNNBwdDataDescs& desc) const {
            desc.set(filter_meta, *diff_layout, *grad_layout, opr->param());
51
        }
52 53 54 55 56 57 58 59 60
        SizeArgs(Convolution3DBackwardDataImpl* opr, const TensorLayout& filter,
                 const TensorLayout& diff, const TensorLayout& grad);
        SizeArgs(Convolution3DBackwardDataImpl* opr,
                 const CanonizedFilterMeta& filter, const TensorLayout& diff,
                 const TensorLayout& grad);

        convolution3d::ForwardSizeArgs as_fwd_args() const {
            return {handle, grad_layout, filter_meta, diff_layout,
                    opr->param().data_type};
61
        }
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    };
    struct ExecArgs : public SizeArgs {
        const TensorND *filter_tensor, *diff_tensor, *grad_tensor;
        Workspace workspace;

        ExecArgs(Convolution3DBackwardDataImpl* opr, _megdnn_tensor_in filter,
                 _megdnn_tensor_in diff, _megdnn_tensor_out grad,
                 _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()) {
81 82
        return (!reproducible ||
                contain_attribute(AlgoAttribute::REPRODUCIBLE)) &&
83 84 85 86 87 88 89 90 91 92 93 94 95
               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,
                      "conv bwd data algo %s: "
                      "required workspace %zu bytes, got %zu",
                      name(), req, workspace.size);
        return *this;
    }

    virtual bool is_cudnn() const { return false; }
96 97 98 99
};

class Convolution3DBackwardDataImpl::AlgoCUDNN final : public AlgoBase {
    cudnnConvolutionBwdDataAlgo_t m_cudnn_enum;
100
    CudnnAlgoPack::Attr m_attr;
101

102 103 104 105 106 107 108
public:
    AlgoCUDNN(cudnnConvolutionBwdDataAlgo_t cudnn_enum)
            : m_cudnn_enum(cudnn_enum) {
        megdnn_assert(CudnnAlgoPack::conv3d_bwd_data_algos().find(cudnn_enum) !=
                      CudnnAlgoPack::conv3d_bwd_data_algos().end());
        m_attr = CudnnAlgoPack::conv3d_bwd_data_algos().at(cudnn_enum);
    }
109

110 111 112
    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;
113

114
    const char* name() const override { return m_attr.name.c_str(); }
115 116 117 118 119 120 121
    AlgoAttribute attribute() const override {
        auto ret = static_cast<AlgoAttribute>(0);
        if (m_attr.is_reproducible) {
            ret |= AlgoAttribute::REPRODUCIBLE;
        }
        return ret;
    }
122

123
    cudnnConvolutionBwdDataAlgo_t cudnn_enum() const { return m_cudnn_enum; }
124

125
    bool is_cudnn() const override { return true; }
126

127 128 129 130 131 132 133
    MEGDNN_DECL_ALGO_TYPE(CUDA_CUDNN)

    std::string param() const override {
        std::string ret;
        serialize_write_pod(m_cudnn_enum, ret);
        return ret;
    }
134 135
};

136 137 138 139 140
class Convolution3DBackwardDataImpl::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;
141

142 143
    const char* name() const override { return "CHANNEL_WISE"; }
    MEGDNN_DECL_ALGO_TYPE(CUDA_CHANWISE)
144 145 146
    AlgoAttribute attribute() const override {
        return AlgoAttribute::REPRODUCIBLE;
    }
147 148 149
};

//! implement group conv by another algo
150 151 152
class Convolution3DBackwardDataImpl::AlgoGroupConvGeneral final
        : public AlgoBase {
    AlgoBase* m_impl;
153 154
    std::string m_name;

155 156
public:
    AlgoGroupConvGeneral(AlgoBase* impl);
157

158 159 160
    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;
161

162
    const char* name() const override { return m_name.c_str(); }
163

164 165
    static void modify_size_args(SizeArgs& args, TensorLayout& diff_pg,
                                 TensorLayout& grad_pg);
166 167 168 169 170 171 172
    AlgoAttribute attribute() const override {
        auto ret = static_cast<AlgoAttribute>(0);
        if (m_impl->contain_attribute(AlgoAttribute::REPRODUCIBLE)) {
            ret |= AlgoAttribute::REPRODUCIBLE;
        }
        return ret;
    }
173

174 175 176
    MEGDNN_DECL_ALGO_TYPE(CUDA_GROUP_CONV_GENERAL)
    std::string param() const override {
        std::string ret;
177
        serialize_write_pod(m_impl->name(), ret);
178 179
        return ret;
    }
180 181
};

182 183

class Convolution3DBackwardDataImpl::AlgoPack : NonCopyableObj {
184 185 186
    // defined in cudnn.cpp
    void fill_cudnn_algos();

187
    AlgoBase::Mapper m_all_algos_map;
188

189 190
public:
    AlgoPack();
191

192 193 194 195
    std::vector<AlgoCUDNN> cudnn;
    AlgoChanwise chanwise;
    std::vector<AlgoGroupConvGeneral> gconv;
    std::unordered_map<AlgoBase*, AlgoGroupConvGeneral*> algo2gconv;
196

197
    std::vector<AlgoBase*>
198 199 200 201 202
            //! all algorithms
            all_algos,
            //! non-cudnn algos, used for heuristic if cudnn is not supported
            non_cudnn_algos;

203 204 205
    AlgoCUDNN* cudnn_from_enum(cudnnConvolutionBwdDataAlgo_t algo);

    const AlgoBase::Mapper& all_algos_map() const { return m_all_algos_map; }
206 207
};

208 209
}  // namespace cuda
}  // namespace megdnn
210 211

// vim: syntax=cpp.doxygen