cudnn_frontend_ExecutionPlanCache.h
Go to the documentation of this file.
/*
* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <tuple>
#include <unordered_map>
#include <map>
#include <memory>
#include <mutex>
#include <cudnn_frontend_OperationGraph.h>
#include <cudnn_frontend_ExecutionPlan.h>
/***
* device_id_0 Operation_Graph0 (conv_fprop)
* -------------------------------------------------------------------------------
* | Feature_vector_type0_val0 | Plan A0 |
* | Feature_vector_type0_val1 | Plan B0 |
* ===============================================================================
*
* device_id_0 Operation_Graph1 (dgrad)
* -------------------------------------------------------------------------------
* | Feature_vector_type1_val0 | Plan A1 |
* | Feature_vector_type1_val1 | Plan B1 |
* ===============================================================================
*
* device_id_0 Operation_Graph2 (wgrad)
* -------------------------------------------------------------------------------
* | Feature_vector_type2_val0 | Plan B2 |
* ===============================================================================
*
* device_id_1 Operation_Graph0 (conv_fprop)
* -------------------------------------------------------------------------------
* | Feature_vector_type0_val0 | Plan A0 |
* | Feature_vector_type0_val1 | Plan B0 |
* ===============================================================================
*
* device_id_1 Operation_Graph1 (dgrad)
* -------------------------------------------------------------------------------
* | Feature_vector_type1_val0 | Plan A1 |
* | Feature_vector_type1_val1 | Plan B1 |
* ===============================================================================
*
* device_id_1 Operation_Graph2 (wgrad)
* -------------------------------------------------------------------------------
* | Feature_vector_type2_val0 | Plan B2 |
* ===============================================================================
*/
namespace cudnn_frontend {
class ExecutionPlanCache_v1 {
protected:
struct compare {
bool operator ()(const feature_vector_t & fv1, const feature_vector_t &fv2) const {
for (uint32_t i = 0u; i < fv1.size(); i++) {
if (fv1[i] < fv2[i]) {
return true;
}
}
}
};
std::string name = "plan_cache_[unnamed]";
using FeatureVectorToPlanMap = std::map<cudnn_frontend::feature_vector_t, cudnn_frontend::ExecutionPlan, cudnn_frontend::ExecutionPlanCache_v1::compare>;
FeatureVectorToPlanMap cache;
mutable std::mutex cache_mutex;
public:
virtual bool
is_fastest_plan_stable(const cudnn_frontend::OperationGraph &op_graph,
const std::string & tag) {
return true;
}
void add_plan_to_cache(const cudnn_frontend::OperationGraph &op_graph,
const cudnn_frontend::ExecutionPlan &plan) {
std::lock_guard<std::mutex> guard(cache_mutex);
cache.insert(std::make_pair(op_graph.getFeatureVector(),plan));
getLogger() << "[cudnn_frontend] Added to " << name << " " << op_graph.getTag() << std::endl;
}
ExecutionPlanCache_v1(const char * name_) {
name = name_;
}
const std::string & get_name() const {
return name;
}
// Plan is the output here.
bool get_plan_from_cache(const cudnn_frontend::OperationGraph &op_graph,
const cudnn_frontend::ExecutionPlan *&plan) const {
{
std::lock_guard<std::mutex> guard(cache_mutex);
auto it = cache.find(op_graph.getFeatureVector());
if (it == cache.end()) {
getLogger() << "[cudnn_frontend] Cached Plan Not Found in " << name << std::endl;
return false;
}
plan = &(it->second);
}
getLogger() << "[cudnn_frontend] Cached Plan Found in " << name << std::endl;
return true;
}
};
class ExecutionPlanCache_v2 : public ExecutionPlanCache_v1 {
using SaturationTracker = std::map<std::pair<cudnn_frontend::feature_vector_t, std::string>, int32_t>;
SaturationTracker tracker;
int32_t saturationCount = 1;
public:
virtual bool
is_fastest_plan_stable(const cudnn_frontend::OperationGraph &op_graph,
const std::string & tag) {
if (saturationCount == 1) {return true;} // Special case. Always add to the cache.
// If plan cache is already created for the op_graph no need to update.
// Ideally, one will auto-tune only if the plan cache has no plan for the op_graph.
cudnn_frontend::ExecutionPlan const *plan = nullptr;
if (get_plan_from_cache(op_graph, plan)) {
getLogger() << "[cudnn_frontend] SaturationTracker " << name << " " << op_graph.getTag() << " " << tag << " plan already in cache." << std::endl;
return false;
}
// Lock the cache and increase the count till we saturate
std::lock_guard<std::mutex> guard(cache_mutex);
auto cnt = tracker[std::make_pair(op_graph.getFeatureVector(),tag)] += 1;
getLogger() << "[cudnn_frontend] SaturationTracker " << name << " " << op_graph.getTag() << " " << tag << " " << cnt << std::endl;
return cnt >= saturationCount;
}
void set_saturation_count(int32_t count) {
saturationCount = count;
}
ExecutionPlanCache_v2(const char * name_) : ExecutionPlanCache_v1(name_) {
}
};
using ExecutionPlanCache = ExecutionPlanCache_v2;
}
cudnn_frontend::feature_vector_t
std::vector< int64_t > feature_vector_t
Detailed feature_vector. Generally the Tensor and Operation properties.
Definition: cudnn_frontend_utils.h:34
cudnn_frontend::getLogger
ConditionalStreamer & getLogger()
Definition: cudnn_frontend_Logging.h:72
cudnn_frontend::ExecutionPlanCache_v1::ExecutionPlanCache_v1
ExecutionPlanCache_v1(const char *name_)
Definition: cudnn_frontend_ExecutionPlanCache.h:115
cudnn_frontend::ExecutionPlan_v8
Definition: cudnn_frontend_ExecutionPlan.h:51
cudnn_frontend::ExecutionPlanCache_v1
Plan Cache structure for the above table.
Definition: cudnn_frontend_ExecutionPlanCache.h:79
cudnn_frontend_OperationGraph.h
cudnn_frontend::ExecutionPlanCache_v1::is_fastest_plan_stable
virtual bool is_fastest_plan_stable(const cudnn_frontend::OperationGraph &op_graph, const std::string &tag)
Definition: cudnn_frontend_ExecutionPlanCache.h:103
cudnn_frontend::ExecutionPlanCache_v1::compare::operator()
bool operator()(const feature_vector_t &fv1, const feature_vector_t &fv2) const
Definition: cudnn_frontend_ExecutionPlanCache.h:83
cudnn_frontend::ExecutionPlanCache_v1::cache
FeatureVectorToPlanMap cache
Definition: cudnn_frontend_ExecutionPlanCache.h:97
cudnn_frontend::ExecutionPlanCache_v2::SaturationTracker
std::map< std::pair< cudnn_frontend::feature_vector_t, std::string >, int32_t > SaturationTracker
Definition: cudnn_frontend_ExecutionPlanCache.h:143
cudnn_frontend::OperationGraph_v8::getTag
std::string const & getTag() const
Definition: cudnn_frontend_OperationGraph.h:98
cudnn_frontend::OperationGraph_v8::getFeatureVector
feature_vector_t getFeatureVector() const
Definition: cudnn_frontend_OperationGraph.h:103
cudnn_frontend::ExecutionPlanCache_v1::cache_mutex
std::mutex cache_mutex
Definition: cudnn_frontend_ExecutionPlanCache.h:99
cudnn_frontend::ExecutionPlanCache_v1::add_plan_to_cache
void add_plan_to_cache(const cudnn_frontend::OperationGraph &op_graph, const cudnn_frontend::ExecutionPlan &plan)
Definition: cudnn_frontend_ExecutionPlanCache.h:108
cudnn_frontend::ExecutionPlanCache_v2::is_fastest_plan_stable
virtual bool is_fastest_plan_stable(const cudnn_frontend::OperationGraph &op_graph, const std::string &tag)
Definition: cudnn_frontend_ExecutionPlanCache.h:150
cudnn_frontend::ExecutionPlanCache_v1::get_name
const std::string & get_name() const
Definition: cudnn_frontend_ExecutionPlanCache.h:119
cudnn_frontend
Definition: cudnn_backend_base.h:29
cudnn_frontend::ExecutionPlanCache_v2::tracker
SaturationTracker tracker
Definition: cudnn_frontend_ExecutionPlanCache.h:144
cudnn_frontend::ExecutionPlanCache_v1::get_plan_from_cache
bool get_plan_from_cache(const cudnn_frontend::OperationGraph &op_graph, const cudnn_frontend::ExecutionPlan *&plan) const
Definition: cudnn_frontend_ExecutionPlanCache.h:124
cudnn_frontend_ExecutionPlan.h
cudnn_frontend::ExecutionPlanCache_v1::name
std::string name
Definition: cudnn_frontend_ExecutionPlanCache.h:92
cudnn_frontend::ExecutionPlanCache_v2::set_saturation_count
void set_saturation_count(int32_t count)
Definition: cudnn_frontend_ExecutionPlanCache.h:169
cudnn_frontend::ExecutionPlanCache_v2::ExecutionPlanCache_v2
ExecutionPlanCache_v2(const char *name_)
Definition: cudnn_frontend_ExecutionPlanCache.h:173
cudnn_frontend::ExecutionPlanCache_v1::compare
Definition: cudnn_frontend_ExecutionPlanCache.h:82
cudnn_frontend::OperationGraph_v8
Definition: cudnn_frontend_OperationGraph.h:52
cudnn_frontend::ExecutionPlanCache_v2
Definition: cudnn_frontend_ExecutionPlanCache.h:141
cudnn_frontend::ExecutionPlanCache_v1::FeatureVectorToPlanMap
std::map< cudnn_frontend::feature_vector_t, cudnn_frontend::ExecutionPlan, cudnn_frontend::ExecutionPlanCache_v1::compare > FeatureVectorToPlanMap
Definition: cudnn_frontend_ExecutionPlanCache.h:96