opr_algo_proxy.h 8.2 KB
Newer Older
1 2 3 4
/**
 * \file dnn/test/common/opr_algo_proxy.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/basic_types.h"
15
#include "src/common/opr_trait.h"
16 17 18 19 20 21 22 23
#include "test/common/utils.h"

namespace megdnn {
namespace test {

template <typename Opr, size_t Arity>
struct AlgoProxy;

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#define DEF_ALGO_PROXY(arity)                                                 \
    template <typename Opr>                                                   \
    struct AlgoProxy<Opr, arity> {                                            \
        static std::vector<typename Opr::AlgorithmInfo>                       \
        get_all_algorithms_info(Opr* opr, const TensorLayoutArray& layouts) { \
            megdnn_assert(layouts.size() == arity);                           \
            return opr->get_all_algorithms_info(LAYOUTS);                     \
        }                                                                     \
        static typename Opr::AlgorithmInfo get_algorithm_info_heuristic(      \
                Opr* opr, const TensorLayoutArray& layouts) {                 \
            megdnn_assert(layouts.size() == arity);                           \
            return opr->get_algorithm_info_heuristic(LAYOUTS);                \
        }                                                                     \
        static size_t get_workspace_in_bytes(                                 \
                Opr* opr, const TensorLayoutArray& layouts) {                 \
            megdnn_assert(layouts.size() == arity);                           \
            return opr->get_workspace_in_bytes(LAYOUTS);                      \
        }                                                                     \
        static void exec(Opr* opr, const TensorNDArray& tensors,              \
                         Workspace workspace) {                               \
            megdnn_assert(tensors.size() == arity);                           \
            return opr->exec(TENSORS, workspace);                             \
        }                                                                     \
47 48
    }

49 50 51 52 53 54
#define LAYOUTS layouts[0], layouts[1]
#define TENSORS tensors[0], tensors[1]
DEF_ALGO_PROXY(2);
#undef LAYOUTS
#undef TENSORS

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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#define LAYOUTS layouts[0], layouts[1], layouts[2]
#define TENSORS tensors[0], tensors[1], tensors[2]
DEF_ALGO_PROXY(3);
#undef LAYOUTS
#undef TENSORS

#define LAYOUTS layouts[0], layouts[1], layouts[2], layouts[3], layouts[4]
#define TENSORS tensors[0], tensors[1], tensors[2], tensors[3], tensors[4]
DEF_ALGO_PROXY(5);
#undef LAYOUTS
#undef TENSORS

#define LAYOUTS                                                             \
    layouts[0], layouts[1], layouts[2], layouts[3], layouts[4], layouts[5], \
            layouts[6], layouts[7]
#define TENSORS                                                             \
    tensors[0], tensors[1], tensors[2], tensors[3], tensors[4], tensors[5], \
            tensors[6], tensors[7]
DEF_ALGO_PROXY(8);
#undef LAYOUTS
#undef TENSORS

#undef DEF_ALGO_PROXY

#define DEF_ALGO_PROXY(Opr, arity)                                             \
    template <>                                                                \
    struct AlgoProxy<Opr, arity> {                                             \
        static std::vector<typename Opr::AlgorithmInfo>                        \
        get_all_algorithms_info(Opr* opr, const TensorLayoutArray& layouts) {  \
            megdnn_assert(layouts.size() == arity);                            \
            return opr->get_all_algorithms_info(LAYOUTS);                      \
        }                                                                      \
        static typename Opr::AlgorithmInfo get_algorithm_info_heuristic(       \
                Opr* opr, const TensorLayoutArray& layouts) {                  \
            megdnn_assert(layouts.size() == arity);                            \
            return opr->get_algorithm_info_heuristic(LAYOUTS);                 \
        }                                                                      \
        static size_t get_workspace_in_bytes(                                  \
                Opr* opr, const TensorLayoutArray& layouts,                    \
                const typename Opr::PreprocessedFilter* preprocessed_filter =  \
                        nullptr) {                                             \
            megdnn_assert(layouts.size() == arity);                            \
            return opr->get_workspace_in_bytes(LAYOUTS, preprocessed_filter);  \
        }                                                                      \
        static void exec(                                                      \
                Opr* opr, const TensorNDArray& tensors,                        \
                const typename Opr::PreprocessedFilter* preprocessed_filter,   \
                Workspace workspace) {                                         \
            megdnn_assert(tensors.size() == arity);                            \
            return opr->exec(TENSORS, preprocessed_filter, workspace);         \
        }                                                                      \
        static void exec(Opr* opr, const TensorNDArray& tensors,               \
                         Workspace workspace) {                                \
            megdnn_assert(tensors.size() == arity);                            \
            return opr->exec(TENSORS, nullptr, workspace);                     \
        }                                                                      \
        static size_t get_preprocess_workspace_in_bytes(                       \
                Opr* opr, const TensorLayoutArray& layouts) {                  \
            megdnn_assert(layouts.size() == arity);                            \
            return opr->get_preprocess_workspace_in_bytes(LAYOUTS);            \
        }                                                                      \
        static SmallVector<TensorLayout> deduce_preprocessed_filter_layout(    \
                Opr* opr, const TensorLayoutArray& layouts) {                  \
            megdnn_assert(layouts.size() == arity);                            \
            return opr->deduce_preprocessed_filter_layout(LAYOUTS);            \
        }                                                                      \
        static void exec_preprocess(                                           \
                Opr* opr, const TensorNDArray& tensors,                        \
                const TensorLayoutArray& layouts,                              \
                Opr::PreprocessedFilter* preprocessed_filter,                  \
                _megdnn_workspace workspace) {                                 \
            megdnn_assert(layouts.size() == arity && tensors.size() == arity); \
            return opr->exec_preprocess(PREPROCESS_ARGS, preprocessed_filter,  \
                                        workspace);                            \
        }                                                                      \
    };

#define LAYOUTS layouts[0], layouts[1], layouts[2]
#define TENSORS tensors[0], tensors[1], tensors[2]
#define PREPROCESS_ARGS layouts[0], tensors[1], layouts[2]
DEF_ALGO_PROXY(ConvolutionForward, 3);
#undef PREPROCESS_ARGS
#undef LAYOUTS
#undef TENSORS

#define LAYOUTS layouts[0], layouts[1], layouts[2], layouts[3], layouts[4]
#define TENSORS tensors[0], tensors[1], tensors[2], tensors[3], tensors[4]
#define PREPROCESS_ARGS \
    layouts[0], tensors[1], tensors[2], layouts[3], layouts[4]
DEF_ALGO_PROXY(ConvBias, 5);
#undef PREPROCESS_ARGS
#undef LAYOUTS
#undef TENSORS

#undef DEF_ALGO_PROXY
150 151 152 153 154 155 156 157 158 159 160

template <typename Opr, size_t arity = OprTrait<Opr>::arity>
struct OprAlgoProxyDefaultImpl : public AlgoProxy<Opr, arity> {};

template <typename Opr>
struct OprAlgoProxy : public OprAlgoProxyDefaultImpl<Opr> {};

}  // namespace test
}  // namespace megdnn

// vim: syntax=cpp.doxygen