opr_impl.h 6.6 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/cuda/convolution/opr_impl.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
 */
#pragma once

#include "megdnn/oprs/nn.h"
15
#include "src/common/utils.h"
16 17 18 19

namespace megdnn {
namespace cuda {

20 21 22
class ConvolutionForwardImpl : public ConvolutionForward {
public:
    using ConvolutionForward::ConvolutionForward;
M
Megvii Engine Team 已提交
23 24 25 26
    void exec(
            _megdnn_tensor_in src, _megdnn_tensor_in filter, _megdnn_tensor_out dst,
            const PreprocessedFilter* preprocessed_filter,
            _megdnn_workspace workspace) override;
27 28 29 30 31 32 33 34

    size_t get_workspace_in_bytes(
            const TensorLayout& src, const TensorLayout& filter,
            const TensorLayout& dst,
            const PreprocessedFilter* preprocessed_filter) override;
    const char* get_algorithm_set_name() const override;

    SmallVector<TensorLayout> deduce_preprocessed_filter_layout(
M
Megvii Engine Team 已提交
35
            const TensorLayout&, const TensorLayout&, const TensorLayout&) override {
36 37
        return {};
    }
M
Megvii Engine Team 已提交
38 39
    size_t get_preprocess_workspace_in_bytes(
            const TensorLayout&, const TensorLayout&, const TensorLayout&) override {
40 41
        return 0;
    }
M
Megvii Engine Team 已提交
42 43 44
    void exec_preprocess(
            const TensorLayout&, _megdnn_tensor_in, const TensorLayout&,
            PreprocessedFilter*, _megdnn_workspace) override {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        megdnn_throw("cuda exec_preprocess has not implemeted yet");
    }

    Algorithm* get_algorithm_from_desc(const AlgorithmDesc& desc) override;

    class AlgoBase;
    class AlgoDefault;
    class AlgoPack;

    static const AlgoPack& algo_pack() { return sm_algo_pack; }

protected:
    std::vector<Algorithm*> get_all_algorithms(
            const TensorLayout& src, const TensorLayout& filter,
            const TensorLayout& dst) override;
60 61 62 63

    std::vector<Algorithm*> get_all_algorithms_safe(
            const TensorLayout& src, const TensorLayout& filter,
            const TensorLayout& dst) override;
64 65 66 67 68
    Algorithm* get_algorithm_heuristic(
            const TensorLayout& src, const TensorLayout& filter,
            const TensorLayout& dst, size_t workspace_limit_in_bytes,
            const AlgoAttribute& positive_attr,
            const AlgoAttribute& negative_attr) override;
69 70 71

private:
    static AlgoPack sm_algo_pack;
72 73
};

74 75 76
class ConvolutionBackwardDataImpl : public ConvolutionBackwardData {
public:
    using ConvolutionBackwardData::ConvolutionBackwardData;
M
Megvii Engine Team 已提交
77 78 79
    void exec(
            _megdnn_tensor_in filter, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
            _megdnn_workspace workspace) override;
80 81 82
    AlgorithmInfo get_algorithm_info_heuristic(
            const TensorLayout& filter, const TensorLayout& diff,
            const TensorLayout& grad, size_t workspace_limit_in_bytes,
M
Megvii Engine Team 已提交
83 84 85 86
            const AlgoAttribute& positive_attr, const AlgoAttribute& negative_attr) {
        return get_algorithm_heuristic(
                       filter, diff, grad, workspace_limit_in_bytes, positive_attr,
                       negative_attr)
87 88 89
                ->info();
    }

M
Megvii Engine Team 已提交
90 91 92
    size_t get_workspace_in_bytes(
            const TensorLayout& filter, const TensorLayout& diff,
            const TensorLayout& grad) override;
93 94 95 96 97 98 99 100 101
    const char* get_algorithm_set_name() const override;

    class AlgoBase;
    class AlgoCUDNN;
    class AlgoMatmul;
    class AlgoChanwise;
    class AlgoChanwiseSmall;
    class AlgoGroupConvGeneral;
    class AlgoBFloat16;
102
    class AlgoInt8NCHW4DotProdImplicitGemm;
103
    class AlgoInt8NCHWDotProdImplicitGemm;
104
    class AlgoInt8NHWCIMMAImplicitGemm;
105 106
    class AlgoFloat32NCHWFMAImplicitBatchedGemm;
    class AlgoFloat16NCHWHMMAImplicitBatchedGemm;
107 108 109 110 111

    class AlgoPack;

    static const AlgoPack& algo_pack() { return sm_algo_pack; }

112
    Algorithm* get_algorithm_from_desc(const AlgorithmDesc& desc) override;
113 114 115 116 117

protected:
    std::vector<Algorithm*> get_all_algorithms(
            const TensorLayout& filter, const TensorLayout& diff,
            const TensorLayout& grad) override;
118 119 120 121

    std::vector<Algorithm*> get_all_algorithms_safe(
            const TensorLayout& filter, const TensorLayout& diff,
            const TensorLayout& grad) override;
122 123 124 125 126
    Algorithm* get_algorithm_heuristic(
            const TensorLayout& filter, const TensorLayout& diff,
            const TensorLayout& grad, size_t workspace_limit_in_bytes,
            const AlgoAttribute& positive_attr,
            const AlgoAttribute& negative_attr) override;
127

128 129 130
private:
    static AlgoPack sm_algo_pack;
};
131

132 133 134
class ConvolutionBackwardFilterImpl : public ConvolutionBackwardFilter {
public:
    using ConvolutionBackwardFilter::ConvolutionBackwardFilter;
M
Megvii Engine Team 已提交
135 136 137 138 139 140
    void exec(
            _megdnn_tensor_in src, _megdnn_tensor_in diff, _megdnn_tensor_out grad,
            _megdnn_workspace workspace) override;
    size_t get_workspace_in_bytes(
            const TensorLayout& src, const TensorLayout& diff,
            const TensorLayout& grad) override;
141 142 143
    AlgorithmInfo get_algorithm_info_heuristic(
            const TensorLayout& filter, const TensorLayout& diff,
            const TensorLayout& grad, size_t workspace_limit_in_bytes,
M
Megvii Engine Team 已提交
144 145 146 147
            const AlgoAttribute& positive_attr, const AlgoAttribute& negative_attr) {
        return get_algorithm_heuristic(
                       filter, diff, grad, workspace_limit_in_bytes, positive_attr,
                       negative_attr)
148 149 150
                ->info();
    }

151 152 153 154 155 156 157 158 159 160 161 162 163
    const char* get_algorithm_set_name() const override;

    class AlgoBase;
    class AlgoCUDNN;
    class AlgoMatmul;
    class AlgoChanwise;
    class AlgoGroupConvGeneral;
    class AlgoBFloat16;

    class AlgoPack;

    static const AlgoPack& algo_pack() { return sm_algo_pack; }

164
    Algorithm* get_algorithm_from_desc(const AlgorithmDesc& desc) override;
165 166 167 168 169

protected:
    std::vector<Algorithm*> get_all_algorithms(
            const TensorLayout& src, const TensorLayout& diff,
            const TensorLayout& grad) override;
170 171 172 173

    std::vector<Algorithm*> get_all_algorithms_safe(
            const TensorLayout& src, const TensorLayout& diff,
            const TensorLayout& grad) override;
174
    Algorithm* get_algorithm_heuristic(
M
Megvii Engine Team 已提交
175 176
            const TensorLayout& src, const TensorLayout& diff, const TensorLayout& grad,
            size_t workspace_limit_in_bytes, const AlgoAttribute& positive_attr,
177
            const AlgoAttribute& negative_attr) override;
178

179 180
private:
    static AlgoPack sm_algo_pack;
181 182
};

183 184
}  // namespace cuda
}  // namespace megdnn
185 186

// vim: syntax=cpp.doxygen