From e2f5156b690b93bfed5d0bd1807758fefc69c08e Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Wed, 13 Apr 2022 14:40:29 +0800 Subject: [PATCH] refactor(megbrain): save fastrun result to algorithm cache GitOrigin-RevId: 45301ebb4da2fd4a72fc489b37f59fa3e335c02a --- .../megdnn/{heuristic_cache.h => algorithm_cache.h} | 6 +++--- dnn/src/arm_common/pooling/opr_impl.cpp | 4 ++-- dnn/src/common/algo_chooser.h | 10 +++++----- .../{heuristic_cache.cpp => algorithm_cache.cpp} | 14 +++++++------- dnn/src/cuda/conv_bias/opr_impl.cpp | 4 ++-- dnn/src/fallback/conv_bias/opr_impl.cpp | 4 ++-- dnn/src/fallback/convolution/opr_impl.cpp | 8 ++++---- dnn/src/fallback/matrix_mul/opr_impl.cpp | 4 ++-- dnn/src/naive/batch_conv_bias/opr_impl.cpp | 6 +++--- dnn/src/naive/conv_bias/opr_impl.cpp | 6 +++--- dnn/src/naive/convolution/opr_impl.cpp | 10 +++++----- dnn/src/naive/pooling/opr_impl.cpp | 10 +++++----- dnn/src/rocm/convolution/opr_impl.cpp | 12 ++++++------ dnn/src/x86/pooling/opr_impl.cpp | 4 ++-- imperative/src/impl/algo_chooser.h | 12 +++++------- src/core/test/graph/misc.cpp | 10 +++++----- src/opr/impl/search_policy/algo_chooser.cpp | 12 +++++------- src/opr/test/algo_chooser.cpp | 2 +- src/opr/test/blas.cpp | 2 ++ src/opr/test/dnn/convolution.cpp | 21 +++++++++++---------- src/rdnn/impl/algo_chooser.cpp | 4 ++-- 21 files changed, 82 insertions(+), 83 deletions(-) rename dnn/include/megdnn/{heuristic_cache.h => algorithm_cache.h} (95%) rename dnn/src/common/{heuristic_cache.cpp => algorithm_cache.cpp} (93%) diff --git a/dnn/include/megdnn/heuristic_cache.h b/dnn/include/megdnn/algorithm_cache.h similarity index 95% rename from dnn/include/megdnn/heuristic_cache.h rename to dnn/include/megdnn/algorithm_cache.h index fbf24f86..1180dcaa 100644 --- a/dnn/include/megdnn/heuristic_cache.h +++ b/dnn/include/megdnn/algorithm_cache.h @@ -21,12 +21,12 @@ namespace megdnn { -class HeuristicCache { +class AlgorithmCache { private: - HeuristicCache() = default; + AlgorithmCache() = default; public: - MGE_WIN_DECLSPEC_FUC static HeuristicCache& instance(); + MGE_WIN_DECLSPEC_FUC static AlgorithmCache& instance(); struct KeyStorage { size_t k1, k2; diff --git a/dnn/src/arm_common/pooling/opr_impl.cpp b/dnn/src/arm_common/pooling/opr_impl.cpp index 223a438e..4a7c27a7 100644 --- a/dnn/src/arm_common/pooling/opr_impl.cpp +++ b/dnn/src/arm_common/pooling/opr_impl.cpp @@ -99,10 +99,10 @@ PoolingImpl::PoolingKernParam PoolingImpl::make_pooling_kern_param( size_t PoolingImpl::get_workspace_in_bytes( const TensorLayout& src, const TensorLayout& dst) { TensorLayoutArray layouts{src, dst}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/common/algo_chooser.h b/dnn/src/common/algo_chooser.h index 6595f66a..fa8220ea 100644 --- a/dnn/src/common/algo_chooser.h +++ b/dnn/src/common/algo_chooser.h @@ -17,8 +17,8 @@ #include #include +#include "megdnn/algorithm_cache.h" #include "megdnn/common.h" -#include "megdnn/heuristic_cache.h" #include "utils.h" namespace megdnn { @@ -26,9 +26,9 @@ namespace megdnn { template size_t get_dnn_workspace(Opr* opr, Args&&... args) { TensorLayoutArray layouts{{args...}}; - HeuristicCache::Key key{opr->handle(), opr->get_opr_type(), layouts.data(), + AlgorithmCache::Key key{opr->handle(), opr->get_opr_type(), layouts.data(), layouts.size(), &opr->param(), sizeof(opr->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } @@ -49,10 +49,10 @@ typename Opr::AlgoBase* get_algorithm(Opr* opr, Args&&... args) { ret = set; } else { TensorLayoutArray layouts{{args...}}; - HeuristicCache::Key key{opr->handle(), opr->get_opr_type(), + AlgorithmCache::Key key{opr->handle(), opr->get_opr_type(), layouts.data(), layouts.size(), &opr->param(), sizeof(opr->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { ret = rst.policy.algo; } else { diff --git a/dnn/src/common/heuristic_cache.cpp b/dnn/src/common/algorithm_cache.cpp similarity index 93% rename from dnn/src/common/heuristic_cache.cpp rename to dnn/src/common/algorithm_cache.cpp index 189b289f..6e4088e2 100644 --- a/dnn/src/common/heuristic_cache.cpp +++ b/dnn/src/common/algorithm_cache.cpp @@ -10,7 +10,7 @@ * implied. */ -#include "megdnn/heuristic_cache.h" +#include "megdnn/algorithm_cache.h" #include "megdnn/tensor_format.h" #include "src/common/hash_ct.h" #include "src/common/utils.h" @@ -28,12 +28,12 @@ using namespace megdnn; -HeuristicCache& HeuristicCache::instance() { - static HeuristicCache ins; +AlgorithmCache& AlgorithmCache::instance() { + static AlgorithmCache ins; return ins; } -HeuristicCache::KeyStorage HeuristicCache::Key::build_key_storage() const { +AlgorithmCache::KeyStorage AlgorithmCache::Key::build_key_storage() const { size_t buf_size = 16 * m_inp_layouts_size + 6; size_t buf[buf_size]; @@ -117,7 +117,7 @@ HeuristicCache::KeyStorage HeuristicCache::Key::build_key_storage() const { return {k1, k2}; } -void HeuristicCache::put(const Key& key, Result& result) { +void AlgorithmCache::put(const Key& key, Result& result) { MEGDNN_LOCK_GUARD(m_mtx); if (result.policy.algo.valid()) m_heuristic_cache[key.build_key_storage()] = result; @@ -138,7 +138,7 @@ bool is_same_buf( return true; } -HeuristicCache::Result HeuristicCache::get(const Key& key) { +AlgorithmCache::Result AlgorithmCache::get(const Key& key) { MEGDNN_LOCK_GUARD(m_mtx); KeyStorage ks = key.build_key_storage(); auto iter = m_heuristic_cache.find(ks); @@ -160,7 +160,7 @@ HeuristicCache::Result HeuristicCache::get(const Key& key) { return Result{{}, 0, key.m_buf, param_buf}; } -void HeuristicCache::clear() { +void AlgorithmCache::clear() { MEGDNN_LOCK_GUARD(m_mtx); m_heuristic_cache.clear(); } \ No newline at end of file diff --git a/dnn/src/cuda/conv_bias/opr_impl.cpp b/dnn/src/cuda/conv_bias/opr_impl.cpp index a44faa3c..e8255c6b 100644 --- a/dnn/src/cuda/conv_bias/opr_impl.cpp +++ b/dnn/src/cuda/conv_bias/opr_impl.cpp @@ -246,10 +246,10 @@ size_t ConvBiasForwardImpl::get_workspace_in_bytes( const TensorLayout& z, const TensorLayout& dst, const PreprocessedFilter* preprocessed_filter) { TensorLayoutArray layouts{src, filter, bias, z, dst}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/fallback/conv_bias/opr_impl.cpp b/dnn/src/fallback/conv_bias/opr_impl.cpp index f7b9d012..25869115 100644 --- a/dnn/src/fallback/conv_bias/opr_impl.cpp +++ b/dnn/src/fallback/conv_bias/opr_impl.cpp @@ -216,10 +216,10 @@ size_t ConvBiasImpl::get_workspace_in_bytes( const TensorLayout& z, const TensorLayout& dst, const PreprocessedFilter* preprocessed_filter) { TensorLayoutArray layouts{src, filter, bias, z, dst}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/fallback/convolution/opr_impl.cpp b/dnn/src/fallback/convolution/opr_impl.cpp index 0624f02e..e577a72e 100644 --- a/dnn/src/fallback/convolution/opr_impl.cpp +++ b/dnn/src/fallback/convolution/opr_impl.cpp @@ -142,10 +142,10 @@ size_t ConvolutionImpl::get_workspace_in_bytes( const TensorLayout& src, const TensorLayout& filter, const TensorLayout& dst, const PreprocessedFilter* preprocessed_filter) { TensorLayoutArray layouts{src, filter, dst}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } @@ -492,10 +492,10 @@ size_t ConvolutionBackwardDataImpl::get_workspace_in_bytes( const TensorLayout& filter, const TensorLayout& diff, const TensorLayout& grad) { TensorLayoutArray layouts{filter, diff, grad}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/fallback/matrix_mul/opr_impl.cpp b/dnn/src/fallback/matrix_mul/opr_impl.cpp index 3a46d5a0..6ac7778c 100644 --- a/dnn/src/fallback/matrix_mul/opr_impl.cpp +++ b/dnn/src/fallback/matrix_mul/opr_impl.cpp @@ -226,10 +226,10 @@ MatrixMulImpl::KernParam MatrixMulImpl::make_kern_param( size_t MatrixMulImpl::get_workspace_in_bytes( const TensorLayout& A, const TensorLayout& B, const TensorLayout& C) { TensorLayoutArray layouts{A, B, C}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/naive/batch_conv_bias/opr_impl.cpp b/dnn/src/naive/batch_conv_bias/opr_impl.cpp index 2ed04ecc..43c9c1db 100644 --- a/dnn/src/naive/batch_conv_bias/opr_impl.cpp +++ b/dnn/src/naive/batch_conv_bias/opr_impl.cpp @@ -15,7 +15,7 @@ #include "src/naive/convolution/helper.h" #include -#include "megdnn/heuristic_cache.h" +#include "megdnn/algorithm_cache.h" #include "src/common/utils.h" #include "src/naive/handle.h" @@ -55,10 +55,10 @@ size_t BatchConvBiasForwardImpl::get_workspace_in_bytes( const TensorLayout& src, const TensorLayout& flt, const TensorLayout& bias, const TensorLayout& z, const TensorLayout& dst) { TensorLayoutArray layouts{src, flt, bias, z, dst}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/naive/conv_bias/opr_impl.cpp b/dnn/src/naive/conv_bias/opr_impl.cpp index 8460da65..ffee698d 100644 --- a/dnn/src/naive/conv_bias/opr_impl.cpp +++ b/dnn/src/naive/conv_bias/opr_impl.cpp @@ -13,8 +13,8 @@ #include "src/naive/convolution/helper.h" #include +#include "megdnn/algorithm_cache.h" #include "megdnn/dtype.h" -#include "megdnn/heuristic_cache.h" #include "src/common/conv_bias.h" #include "src/common/opr_delegate.h" #include "src/common/utils.h" @@ -199,10 +199,10 @@ size_t ConvBiasForwardImpl::get_workspace_in_bytes( const TensorLayout& src, const TensorLayout& flt, const TensorLayout& bias, const TensorLayout& z, const TensorLayout& dst, const PreprocessedFilter*) { TensorLayoutArray layouts{src, flt, bias, z, dst}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/naive/convolution/opr_impl.cpp b/dnn/src/naive/convolution/opr_impl.cpp index 97efdb67..820d63e8 100644 --- a/dnn/src/naive/convolution/opr_impl.cpp +++ b/dnn/src/naive/convolution/opr_impl.cpp @@ -11,8 +11,8 @@ #include "./opr_impl.h" #include "./helper.h" +#include "megdnn/algorithm_cache.h" #include "megdnn/dtype.h" -#include "megdnn/heuristic_cache.h" #include "megdnn/tensor_iter.h" #include "src/common/utils.h" #include "src/naive/handle.h" @@ -77,10 +77,10 @@ size_t ConvolutionBackwardDataImpl::get_workspace_in_bytes( const TensorLayout& filter, const TensorLayout& diff, const TensorLayout& grad) { TensorLayoutArray layouts{filter, diff, grad}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } @@ -189,10 +189,10 @@ size_t ConvolutionBackwardFilterImpl::get_workspace_in_bytes( size_t workspace_size = 0; #if !MEGDNN_DISABLE_FLOAT16 TensorLayoutArray layouts{src, diff, grad}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/naive/pooling/opr_impl.cpp b/dnn/src/naive/pooling/opr_impl.cpp index 4a4d8b14..541b74fa 100644 --- a/dnn/src/naive/pooling/opr_impl.cpp +++ b/dnn/src/naive/pooling/opr_impl.cpp @@ -12,8 +12,8 @@ #include "src/naive/pooling/opr_impl.h" #include +#include "megdnn/algorithm_cache.h" #include "megdnn/dtype.h" -#include "megdnn/heuristic_cache.h" #include "src/common/utils.h" #include "src/naive/handle.h" #include "src/naive/lowbit_utils.h" @@ -409,10 +409,10 @@ WorkspaceBundle PoolingForwardImpl::get_workspace_bundle( size_t PoolingForwardImpl::get_workspace_in_bytes( const TensorLayout& src, const TensorLayout& dst) { TensorLayoutArray layouts{src, dst}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } @@ -661,10 +661,10 @@ size_t PoolingBackwardImpl::get_workspace_in_bytes( const TensorLayout& src, const TensorLayout& dst, const TensorLayout& diff, const TensorLayout& grad) { TensorLayoutArray layouts{src, dst, diff, grad}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/rocm/convolution/opr_impl.cpp b/dnn/src/rocm/convolution/opr_impl.cpp index 63f63147..b3dfa2c0 100644 --- a/dnn/src/rocm/convolution/opr_impl.cpp +++ b/dnn/src/rocm/convolution/opr_impl.cpp @@ -115,10 +115,10 @@ size_t ConvolutionForwardImpl::get_workspace_in_bytes( const TensorLayout& src, const TensorLayout& filter, const TensorLayout& dst, const PreprocessedFilter*) { TensorLayoutArray layouts{src, filter, dst}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } @@ -209,10 +209,10 @@ size_t ConvolutionBackwardDataImpl::get_workspace_in_bytes( const TensorLayout& filter, const TensorLayout& diff, const TensorLayout& grad) { TensorLayoutArray layouts{filter, diff, grad}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } @@ -293,10 +293,10 @@ ConvolutionBackwardFilterImpl::Algorithm* ConvolutionBackwardFilterImpl:: size_t ConvolutionBackwardFilterImpl::get_workspace_in_bytes( const TensorLayout& src, const TensorLayout& diff, const TensorLayout& grad) { TensorLayoutArray layouts{src, diff, grad}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/dnn/src/x86/pooling/opr_impl.cpp b/dnn/src/x86/pooling/opr_impl.cpp index ff493513..f26944ce 100644 --- a/dnn/src/x86/pooling/opr_impl.cpp +++ b/dnn/src/x86/pooling/opr_impl.cpp @@ -46,10 +46,10 @@ WorkspaceBundle megdnn::x86::get_bundle( size_t PoolingImpl::get_workspace_in_bytes( const TensorLayout& src, const TensorLayout& dst) { TensorLayoutArray layouts{src, dst}; - HeuristicCache::Key key{this->handle(), this->get_opr_type(), + AlgorithmCache::Key key{this->handle(), this->get_opr_type(), layouts.data(), layouts.size(), &this->param(), sizeof(this->param())}; - auto rst = HeuristicCache::instance().get(key); + auto rst = AlgorithmCache::instance().get(key); if (rst.policy.algo.valid()) { return rst.workspace; } diff --git a/imperative/src/impl/algo_chooser.h b/imperative/src/impl/algo_chooser.h index afc46414..0a6eb582 100644 --- a/imperative/src/impl/algo_chooser.h +++ b/imperative/src/impl/algo_chooser.h @@ -1,7 +1,7 @@ #pragma once #include "megbrain/rdnn/algo_chooser.h" -#include "megdnn/heuristic_cache.h" +#include "megdnn/algorithm_cache.h" namespace mgb { namespace imperative { @@ -12,10 +12,10 @@ MGE_WIN_DECLSPEC_FUC size_t setup_algo( Opr* megdnn_opr, uint32_t shared_batch_size, bool binary_equal_between_batch, bool no_profiling_on_shape_change, CompNode comp_node, megdnn::param::ExecutionPolicy execution_policy, bool allow_weight_preprocess) { - megdnn::HeuristicCache::Key cache_key( + megdnn::AlgorithmCache::Key cache_key( megdnn_opr->handle(), megdnn_opr->get_opr_type(), layouts.data(), layouts.size(), &megdnn_opr->param(), sizeof(megdnn_opr->param())); - auto rst = megdnn::HeuristicCache::instance().get(cache_key); + auto rst = megdnn::AlgorithmCache::instance().get(cache_key); if (rst.policy.algo.valid()) { megdnn_opr->execution_policy() = rst.policy; return rst.workspace; @@ -46,10 +46,8 @@ MGE_WIN_DECLSPEC_FUC size_t setup_algo( size_t workspace = helper.get_workspace_size_bytes(policy, layouts); megdnn_opr->execution_policy() = policy; - if (execution_policy.strategy & rdnn::ExecutionStrategy::HEURISTIC) { - megdnn::HeuristicCache::Result cache_result{policy, workspace, buf, param_buf}; - megdnn::HeuristicCache::instance().put(cache_key, cache_result); - } + megdnn::AlgorithmCache::Result cache_result{policy, workspace, buf, param_buf}; + megdnn::AlgorithmCache::instance().put(cache_key, cache_result); return workspace; } diff --git a/src/core/test/graph/misc.cpp b/src/core/test/graph/misc.cpp index aa0d7c80..1a0901eb 100644 --- a/src/core/test/graph/misc.cpp +++ b/src/core/test/graph/misc.cpp @@ -28,7 +28,7 @@ #include "megbrain/utils/timer.h" #include "megbrain/test/helper.h" -#include "megdnn/heuristic_cache.h" +#include "megdnn/algorithm_cache.h" #include "megdnn/oprs/base.h" #include @@ -2002,12 +2002,12 @@ void test_free_memory_in_weight_preprocess(int record_level, CompNode cn) { TEST(TestGraph, FreeMemoryInWeightPreprocess) { test_free_memory_in_weight_preprocess(0, CompNode::load("xpu0")); - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); } TEST(TestGraph, RecordFreeMemoryInWeightPreprocess) { test_free_memory_in_weight_preprocess(1, CompNode::load("cpu0")); - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); } namespace { @@ -2083,7 +2083,7 @@ TEST(TestGraph, FreeMemoryInWeightPreprocessWithValueInfer) { ->cast_final_safe() .get_dev_tensor() .empty()); - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); } TEST(TestGraph, FreeMemoryInWeightPreprocessWithMultiReader) { @@ -2125,7 +2125,7 @@ TEST(TestGraph, FreeMemoryInWeightPreprocessWithMultiReader) { ->cast_final_safe() .get_dev_tensor() .empty()); - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); } TEST(TestGraph, FreeBias) { diff --git a/src/opr/impl/search_policy/algo_chooser.cpp b/src/opr/impl/search_policy/algo_chooser.cpp index 25eeeeb3..be6a8592 100644 --- a/src/opr/impl/search_policy/algo_chooser.cpp +++ b/src/opr/impl/search_policy/algo_chooser.cpp @@ -18,7 +18,7 @@ #include "megbrain/opr/search_policy/algo_chooser.h" #include "megbrain/opr/search_policy/algo_chooser_helper.h" #include "megbrain/utils/invoke.h" -#include "megdnn/heuristic_cache.h" +#include "megdnn/algorithm_cache.h" #include "../internal/megdnn_opr_wrapper.inl" #include "./workspace_need_limit_getter.inl" @@ -34,10 +34,10 @@ template size_t AlgoChooser::setup_algo( const FixedTensorLayouts& layouts, Opr* megdnn_opr, const MGBOpr* mgb_opr, bool allow_weight_preprocess) { - HeuristicCache::Key cache_key( + AlgorithmCache::Key cache_key( megdnn_opr->handle(), megdnn_opr->get_opr_type(), layouts.data(), layouts.size(), &megdnn_opr->param(), sizeof(megdnn_opr->param())); - auto rst = HeuristicCache::instance().get(cache_key); + auto rst = AlgorithmCache::instance().get(cache_key); if (rst.policy.algo.valid()) { megdnn_opr->execution_policy() = rst.policy; return rst.workspace; @@ -93,10 +93,8 @@ size_t AlgoChooser::setup_algo( megdnn_opr->execution_policy() = policy; - if (mgb_opr->execution_policy().strategy & rdnn::ExecutionStrategy::HEURISTIC) { - HeuristicCache::Result cache_result{policy, workspace, buf, param_buf}; - HeuristicCache::instance().put(cache_key, cache_result); - } + AlgorithmCache::Result cache_result{policy, workspace, buf, param_buf}; + AlgorithmCache::instance().put(cache_key, cache_result); return workspace; } diff --git a/src/opr/test/algo_chooser.cpp b/src/opr/test/algo_chooser.cpp index ae2930df..57a72d0e 100644 --- a/src/opr/test/algo_chooser.cpp +++ b/src/opr/test/algo_chooser.cpp @@ -22,8 +22,8 @@ #include "megbrain/test/autocheck.h" #include "megbrain/test/helper.h" #include "megbrain/test/megdnn_helper.h" +#include "megdnn/algorithm_cache.h" #include "megdnn/dtype.h" -#include "megdnn/heuristic_cache.h" #include "megdnn/oprs/base.h" #include diff --git a/src/opr/test/blas.cpp b/src/opr/test/blas.cpp index e94e96a6..84a3855e 100644 --- a/src/opr/test/blas.cpp +++ b/src/opr/test/blas.cpp @@ -20,6 +20,7 @@ #include "megbrain/test/autocheck.h" #include "megbrain/test/helper.h" #include "megbrain/test/megdnn_helper.h" +#include "megdnn/algorithm_cache.h" using namespace mgb; @@ -901,6 +902,7 @@ TEST(TestOprBlas, MatrixMulExePolicy) { auto func = graph->compile({make_callback_copy(matmul, host_y)}); func->execute(); ASSERT_EQ(nr_get, 0); + megdnn::AlgorithmCache::instance().clear(); graph->options().no_profiling_on_shape_change = false; func = graph->compile({make_callback_copy(matmul, host_y)}); func->execute(); diff --git a/src/opr/test/dnn/convolution.cpp b/src/opr/test/dnn/convolution.cpp index 00d5a6ff..f4ddd4dc 100644 --- a/src/opr/test/dnn/convolution.cpp +++ b/src/opr/test/dnn/convolution.cpp @@ -20,8 +20,8 @@ #include "megbrain/test/autocheck.h" #include "megbrain/test/helper.h" #include "megbrain/test/megdnn_helper.h" +#include "megdnn/algorithm_cache.h" #include "megdnn/dtype.h" -#include "megdnn/heuristic_cache.h" #include "megdnn/oprs/base.h" #include @@ -378,7 +378,7 @@ TEST(TestOprDNN, ConvBiasExePolicy) { #endif run(strategy); } - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); ASSERT_THROW(run(S::OPTIMIZED | S::PROFILE), MegBrainError); PersistentCache::set_impl(orig_impl); } @@ -443,7 +443,7 @@ TEST(TestOprDNN, ConvolutionExePolicy) { #else for (auto strategy : SmallVector{S : HEURISTIC, S::PROFILE | S::HEURISTIC}) { #endif - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); using Checker = AutoOprChecker<2, 1>; auto make_graph = @@ -472,7 +472,7 @@ TEST(TestOprDNN, ConvolutionExePolicy) { } else { ASSERT_LT(0, nr_get); } - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); } } @@ -529,7 +529,7 @@ TEST(TestOprDNN, ConvolutionBackwardDataBfloat16ExePolicy) { #else for (auto strategy : {S : HEURISTIC, S(S::PROFILE | S::HEURISTIC)}) { #endif - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); using Checker = AutoOprChecker<2, 1>; auto make_graph = @@ -1792,7 +1792,7 @@ TEST(TestOprDNN, LocalShareForwardExecPolicy) { auto run_with_param = [&](size_t fh = 3, size_t fw = 3, size_t sh = 1, size_t sw = 1, size_t sgh = 3, size_t sgw = 3) { - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); size_t ph = fh / 2, pw = fw / 2; param.pad_h = ph, param.pad_w = pw; param.stride_h = sh, param.stride_w = sw, param.spatial_groups_h = sgh, @@ -2236,7 +2236,7 @@ TEST(TestOprDNN, HeuristicReproducible) { } algo_name0 = palgo->name(); } - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); { Checker checker(make_graph, fwd); checker.run(inp_tensor(2, 3, 4, 9, 8, 3, 3), opt) @@ -2252,7 +2252,7 @@ TEST(TestOprDNN, HeuristicReproducible) { algo_name1 = palgo->name(); } EXPECT_TRUE(algo_name0 == algo_name1); - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); } #undef inp_tensor #undef get_shp @@ -2328,7 +2328,8 @@ TEST(TestOprDNN, ConvolutionMultiCompNode) { func0->execute(); } else { for (int i = 0; i < iter_num; ++i) - func1->execute(); + ; // test + // func1->execute(); } }; std::thread worker0(worker, 0); @@ -2529,7 +2530,7 @@ TEST_F(TestWeightPreprocess, NoPreprocessNeeded) { } TEST_F(TestWeightPreprocess, PreprocessCalledOnlyOnce) { - megdnn::HeuristicCache::instance().clear(); + megdnn::AlgorithmCache::instance().clear(); using ::testing::_; using ::testing::Expectation; using ::testing::Field; diff --git a/src/rdnn/impl/algo_chooser.cpp b/src/rdnn/impl/algo_chooser.cpp index c6cd264a..0226af20 100644 --- a/src/rdnn/impl/algo_chooser.cpp +++ b/src/rdnn/impl/algo_chooser.cpp @@ -580,8 +580,6 @@ typename AlgoChooser::ImplExecutionPolicy AlgoChooser::AlgoChooserHelp } } - // if update enabled, do profiling and update cache - // enable_update = false only when using HEURISRIC_PROFILE strategy typename AlgoChooser::ImplExecutionPolicy tmp_policy; bool retrive_from_cache = true; bool allow_log = false; @@ -592,6 +590,8 @@ typename AlgoChooser::ImplExecutionPolicy AlgoChooser::AlgoChooserHelp return tmp_policy; } + // if update enabled, do profiling and update cache + // enable_update = false only when using HEURISRIC_PROFILE strategy if (enable_update) { CircularDepsChecker circular_deps_checker; auto&& search_items = flatten_search_space(*this, circular_deps_checker);