algos.h 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 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
/**
 * \file dnn/src/fallback/conv_bias/algos.h
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * 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 "src/fallback/conv_bias/opr_impl.h"
#include "src/fallback/matrix_mul/opr_impl.h"
#include "megdnn/thin/small_vector.h"

namespace megdnn {
namespace fallback {

class ConvBiasImpl::AlgoNaive final : public AlgoBase {
public:
    bool is_reproducible() const override { return true; }
    const char* name() const override { return "FALLBACK_NAIVE"; }
    bool usable(ConvBiasImpl* opr, const NCBKernSizeParam& param,
                AlgoSelectionStrategy algo_selection_strategy) const override;
    size_t get_workspace(ConvBiasImpl*,
                         const NCBKernSizeParam& param) const override;
    SmallVector<NCBKern> dispatch_kerns(ConvBiasImpl*,
                                        const NCBKernSizeParam&) const override;
};

class ConvBiasImpl::AlgoWinogradF32 final : public AlgoBase {
public:
    AlgoWinogradF32(MatrixMulImpl::AlgoBase* matmul_algo)
            : m_matmul_algo{matmul_algo} {}
    bool is_reproducible() const override { return true; }
    const char* name() const override {
        if (m_name.empty()) {
            m_name = ConvBiasImpl::algo_name<ConvBias::WinogradParam>(
                    ssprintf("FALLBACK_WINOGRAD_F32-%s", m_matmul_algo->name()),
                    {1, 2, UNIT_TILE_SIZE});
        }
        return m_name.c_str();
    }
    bool usable(ConvBiasImpl* opr, const NCBKernSizeParam& param,
                AlgoSelectionStrategy algo_selection_strategy) const override;
    size_t get_workspace(ConvBiasImpl*,
                         const NCBKernSizeParam& param) const override;
    SmallVector<NCBKern> dispatch_kerns(ConvBiasImpl*,
                                        const NCBKernSizeParam&) const override;

private:
    MatrixMulImpl::AlgoBase* m_matmul_algo;
    mutable std::string m_name;
    constexpr size_t static UNIT_TILE_SIZE = 32;
};

class ConvBiasImpl::AlgoWinogradF32_4x4 final : public AlgoBase {
public:
    AlgoWinogradF32_4x4(MatrixMulImpl::AlgoBase* matmul_algo)
            : m_matmul_algo{matmul_algo} {}
    bool is_reproducible() const override { return true; }
    const char* name() const override {
        if (m_name.empty()) {
            m_name = ConvBiasImpl::algo_name<ConvBias::WinogradParam>(
                    ssprintf("FALLBACK_WINOGRAD_F32-%s", m_matmul_algo->name()),
                    {4, 2, UNIT_TILE_SIZE});
        }
        return m_name.c_str();
    }
    bool usable(ConvBiasImpl* opr, const NCBKernSizeParam& param,
                AlgoSelectionStrategy algo_selection_strategy) const override;
    size_t get_workspace(ConvBiasImpl*,
                         const NCBKernSizeParam& param) const override;
    SmallVector<NCBKern> dispatch_kerns(ConvBiasImpl*,
                                        const NCBKernSizeParam&) const override;

private:
    MatrixMulImpl::AlgoBase* m_matmul_algo;
    mutable std::string m_name;
    constexpr size_t static UNIT_TILE_SIZE = 32;
};

class ConvBiasImpl::AlgoWinogradQS8 final : public AlgoBase {
public:
    AlgoWinogradQS8(MatrixMulImpl::AlgoBase* matmul_algo)
            : m_matmul_algo{matmul_algo} {}
    bool is_reproducible() const override { return true; }
    const char* name() const override {
        if (m_name.empty()) {
            m_name = ConvBiasImpl::algo_name<ConvBias::WinogradParam>(
                    ssprintf("FALLBACK_WINOGRAD_QS8-%s", m_matmul_algo->name()),
                    {1, 2, UNIT_TILE_SIZE});
        }
        return m_name.c_str();
    }
    bool usable(ConvBiasImpl* opr, const NCBKernSizeParam& param,
                AlgoSelectionStrategy algo_selection_strategy) const override;
    size_t get_workspace(ConvBiasImpl*,
                         const NCBKernSizeParam& param) const override;
    SmallVector<NCBKern> dispatch_kerns(ConvBiasImpl*,
                                        const NCBKernSizeParam&) const override;

private:
    MatrixMulImpl::AlgoBase* m_matmul_algo;
    mutable std::string m_name;
    constexpr size_t static UNIT_TILE_SIZE = 32;
};

class ConvBiasImpl::AlgoWinogradQS8_8x8 final : public AlgoBase {
public:
    AlgoWinogradQS8_8x8(MatrixMulImpl::AlgoBase* matmul_algo)
            : m_matmul_algo{matmul_algo} {}
    bool is_reproducible() const override { return true; }
    const char* name() const override {
        if (m_name.empty()) {
            m_name = ConvBiasImpl::algo_name<ConvBias::WinogradParam>(
                    ssprintf("FALLBACK_WINOGRAD_QS8-%s", m_matmul_algo->name()),
                    {8, 2, UNIT_TILE_SIZE});
        }
        return m_name.c_str();
    }
    bool usable(ConvBiasImpl* opr, const NCBKernSizeParam& param,
                AlgoSelectionStrategy algo_selection_strategy) const override;
    size_t get_workspace(ConvBiasImpl*,
                         const NCBKernSizeParam& param) const override;
    SmallVector<NCBKern> dispatch_kerns(ConvBiasImpl*,
                                        const NCBKernSizeParam&) const override;

private:
    MatrixMulImpl::AlgoBase* m_matmul_algo;
    mutable std::string m_name;
    constexpr size_t static UNIT_TILE_SIZE = 32;
};

}  // namespace fallback
}  // namespace megdnn

// vim: syntax=cpp.doxygen