algo.h 4.2 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/cuda/local_share/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 9 10 11 12 13 14 15
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#pragma once

#include "megdnn/oprs.h"

16 17
#include "src/common/algo_base.h"
#include "src/common/metahelper.h"
18 19 20 21
#include "src/common/utils.h"
#include "src/cuda/handle.h"
#include "src/cuda/local_share/opr_impl.h"

22 23
#include <unordered_map>

24 25 26 27 28 29 30 31
namespace megdnn {
namespace cuda {

class LocalShareBackwardDataImpl::AlgoBase : public Algorithm {
protected:
    ~AlgoBase() = default;

public:
32 33 34 35 36 37
    enum class AlgoType : uint32_t {
        CUDA_IMPLICIT_GEMM,
        CUDA_BATCHED_MATMUL,
    };
    using Mapper = std::unordered_map<AlgorithmDesc, AlgoBase*>;

38
    AlgoBase() : Algorithm() { m_handle_type = Handle::HandleType::CUDA; }
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    struct SizeArgs {
        LocalShareBackwardDataImpl* opr;
        TensorLayout filter_layout, diff_layout, grad_layout;

        std::string to_string() const;
        SizeArgs(LocalShareBackwardDataImpl* opr, const TensorLayout& filter,
                 const TensorLayout& diff, const TensorLayout& grad);
    };
    struct ExecArgs : public SizeArgs {
        const TensorND *filter_tensor, *diff_tensor, *grad_tensor;
        Workspace workspace;

        ExecArgs(LocalShareBackwardDataImpl* 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;
    }
62 63 64
    bool is_available_attribute(
            const SizeArgs& args,
            const AlgoAttribute& attr = AlgoAttribute::REPRODUCIBLE,
65
            size_t limit = std::numeric_limits<size_t>::max()) {
66
        return contain_attribute(attr) && is_available_wk(args, limit);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    }
    AlgoBase& check_workspace(const SizeArgs& args,
                              const Workspace& workspace) {
        auto req = get_workspace_in_bytes(args);
        megdnn_assert(req <= workspace.size,
                      "local share conv fwd algo %s: required workspace %zu "
                      "bytes, got %zu",
                      name(), req, workspace.size);
        return *this;
    }
};

class LocalShareBackwardDataImpl::AlgoImplicitGemm 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;

85 86 87
    AlgoAttribute attribute() const override {
        return AlgoAttribute::REPRODUCIBLE;
    }
88 89 90 91

    const char* name() const override {
        return "LOCAL_SHARE_IMPLICIT_GEMM";
    }
92
    MEGDNN_DECL_ALGO_TYPE(CUDA_IMPLICIT_GEMM)
93 94 95 96 97 98 99 100 101 102 103
};

class LocalShareBackwardDataImpl::AlgoBatchedMatMul final
        : public AlgoBase {
public:
    bool is_available(const SizeArgs& args) const override;
    size_t get_workspace_in_bytes(const SizeArgs& args) const override;
    WorkspaceBundle get_workspace_bundle(dt_byte* raw_ptr,
                                         const SizeArgs& args) const;
    void exec(const ExecArgs& args) const override;

104 105 106
    AlgoAttribute attribute() const override {
        return AlgoAttribute::REPRODUCIBLE;
    }
107 108 109 110

    const char* name() const override {
        return "LOCAL_SHARE_BATCHED_MATMUL";
    }
111
    MEGDNN_DECL_ALGO_TYPE(CUDA_BATCHED_MATMUL)
112 113
};

114 115
class LocalShareBackwardDataImpl::AlgoPack : NonCopyableObj {
    AlgoBase::Mapper m_all_algos_map;
116 117 118 119 120 121 122 123

public:
    AlgoPack();

    AlgoImplicitGemm implicit_gemm;
    AlgoBatchedMatMul batched_matmul;

    std::vector<AlgoBase*> all_algos;
124
    const AlgoBase::Mapper& all_algos_map() const { return m_all_algos_map; }
125 126 127 128 129 130
};

}  // namespace cuda
}  // namespace megdnn

// vim: syntax=cpp.doxygen