opr_impl.cpp 3.1 KB
Newer Older
1 2 3 4
/**
 * \file dnn/src/x86/matrix_mul/opr_impl.cpp
 * 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 16 17 18 19 20 21
 */

#include "src/x86/matrix_mul/opr_impl.h"
#include "src/common/metahelper.h"
#include "src/x86/matrix_mul/algos.h"
using namespace megdnn;
using namespace x86;

class MatrixMulImpl::AlgoPack : NonCopyableObj {
    AlgoF32Blas f32blas;

22
#if MEGDNN_X86_WITH_MKL && SUPPORT_MKL_PACKED_GEMM
23 24 25 26 27
    AlgoF32MKLPackA f32mkl_packa;
#endif
#if MEGDNN_X86_WITH_VNNI
    AlgoInt8x8x32Vnni algoint8x8x32vnni;
#endif
28
#if MEGDNN_X86_WITH_MKL_DNN
29 30 31 32 33
    AlgoInt8x8x32Mkldnn algoint8x8x32mkldnn;
#endif
    AlgoInt8x8x32AVX2M4N16K2 algoint8x8x32avx2_m4n16k2;
    AlgoInt8x8x32AVX2M2N4K16 algoint8x8x32avx2_m2n4k16;
    AlgoInt8x8x32SSEM4N8K2 algoint8x8x32sse_m4n8k2;
34
    AlgoInt8x8x16AVX2 algoint8x8x16avx2_m4n16k2;
35
    AlgoInt8x8x16SSE algoint8x8x16sse_m4n8k2;
36
    AlgoF32MK8_8x8 algof32mk8_8x8;
37
    AlgoFloatAVX2M6N16 algof32_6x16;
38

39 40 41
    SmallVector<fallback::MatrixMulImpl::AlgoBase*> m_all_algos;
    fallback::MatrixMulImpl::AlgoBase::Mapper m_all_algos_map;

42 43 44 45
public:
    AlgoPack() {
        if (is_supported(SIMDType::VNNI)) {
#if MEGDNN_X86_WITH_VNNI
46
            m_all_algos.emplace_back(&algoint8x8x32vnni);
47 48
#endif
        }
49 50 51 52 53 54
        m_all_algos.emplace_back(&algoint8x8x32avx2_m4n16k2);
        m_all_algos.emplace_back(&algoint8x8x16avx2_m4n16k2);
        m_all_algos.emplace_back(&algoint8x8x32avx2_m2n4k16);
        m_all_algos.emplace_back(&algoint8x8x32sse_m4n8k2);
        m_all_algos.emplace_back(&algoint8x8x16sse_m4n8k2);
        m_all_algos.emplace_back(&algof32mk8_8x8);
55
        m_all_algos.emplace_back(&algof32_6x16);
56
#if MEGDNN_X86_WITH_MKL_DNN
57
        m_all_algos.emplace_back(&algoint8x8x32mkldnn);
58
#endif
59
        m_all_algos.emplace_back(&f32blas);
60
#if MEGDNN_X86_WITH_MKL && SUPPORT_MKL_PACKED_GEMM
61
        m_all_algos.emplace_back(&f32mkl_packa);
62
#endif
63 64 65 66 67 68 69 70

        for (auto&& algo : m_all_algos) {
            m_all_algos_map.emplace(algo->info().desc, algo);
        }
    }

    const SmallVector<fallback::MatrixMulImpl::AlgoBase*>& all_algos() const {
        return m_all_algos;
71
    }
72
    const AlgoBase::Mapper& all_algos_map() const { return m_all_algos_map; }
73 74
};

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
const MatrixMulImpl::AlgoPack& MatrixMulImpl::algo_pack() {
    static AlgoPack algo_pack;
    return algo_pack;
}

fallback::MatrixMulImpl::AlgoBase* MatrixMulImpl::get_algo_from_desc(
        const AlgorithmDesc& desc) {
    megdnn_assert(algo_pack().all_algos_map().find(desc) !=
                  algo_pack().all_algos_map().end());
    return algo_pack().all_algos_map().at(desc);
}

SmallVector<fallback::MatrixMulImpl::AlgoBase*>
MatrixMulImpl::get_all_packed_algo() {
    auto&& algos = fallback::MatrixMulImpl::get_all_packed_algo();
    algos.insert(algos.begin(), algo_pack().all_algos().begin(),
                 algo_pack().all_algos().end());
92 93 94 95
    return std::move(algos);
}

// vim: syntax=cpp.doxygen