cudnn_frontend_ExecutionPlanCache.h
tuple
unordered_map
map
memory
mutex
cudnn_frontend_OperationGraph.h
cudnn_frontend_ExecutionPlan.h
cudnn_frontend.h
functional
limits
cudnn_frontend_Engine.h
cudnn_frontend_MatMulDesc.h
cudnn.h
cudnn_frontend_PointWiseDesc.h
string
map
cudnn_frontend_Logging.h
cudnn_frontend_Tensor.h
exception
fstream
ostream
cudnn_frontend_ExecutionPlanCache.h
cudnn_frontend_Operation.h
cudnn_frontend_ExecutionPlan.h
tuple
vector
cudnn_frontend_ConvDesc.h
cudnn_frontend_OperationGraph.h
cudnn_backend.h
utility
array
sstream
cudnn_frontend_ReductionDesc.h
iostream
unordered_map
mutex
algorithm
cstring
cudnn_frontend_utils.h
memory
cudnn_backend_base.h
cudnn_frontend_get_plan.h
cudnn_frontend_EngineConfigGenerator.h
cudnn_frontend_ExecutionPlanCache.h
cudnn_frontend.h
cudnn_frontend_find_plan.h
cudnn_frontend::ExecutionPlanCache_v1
cudnn_frontend::ExecutionPlanCache_v1::compare
cudnn_frontend::ExecutionPlanCache_v2
cudnn_frontend

    /*
    *Copyright(c)2021,NVIDIACORPORATION.Allrightsreserved.
    *
    *Permissionisherebygranted,freeofcharge,toanypersonobtaininga
    *copyofthissoftwareandassociateddocumentationfiles(the"Software"),
    *todealintheSoftwarewithoutrestriction,includingwithoutlimitation
    *therightstouse,copy,modify,merge,publish,distribute,sublicense,
    *and/orsellcopiesoftheSoftware,andtopermitpersonstowhomthe
    *Softwareisfurnishedtodoso,subjecttothefollowingconditions:
    *
    *Theabovecopyrightnoticeandthispermissionnoticeshallbeincludedin
    *allcopiesorsubstantialportionsoftheSoftware.
    *
    *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND,EXPRESSOR
    *IMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY,
    *FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALL
    *THEAUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOROTHER
    *LIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,ARISING
    *FROM,OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOROTHER
    *DEALINGSINTHESOFTWARE.
    */

    #include<tuple>
    #include<unordered_map>
    #include<map>
    #include<memory>
    #include<mutex>

    #include<cudnn_frontend_OperationGraph.h>
    #include<cudnn_frontend_ExecutionPlan.h>

    /***
    *device_id_0Operation_Graph0(conv_fprop)
    *-------------------------------------------------------------------------------
    *|Feature_vector_type0_val0|PlanA0|
    *|Feature_vector_type0_val1|PlanB0|
    *===============================================================================
    *
    *device_id_0Operation_Graph1(dgrad)
    *-------------------------------------------------------------------------------
    *|Feature_vector_type1_val0|PlanA1|
    *|Feature_vector_type1_val1|PlanB1|
    *===============================================================================
    *
    *device_id_0Operation_Graph2(wgrad)
    *-------------------------------------------------------------------------------
    *|Feature_vector_type2_val0|PlanB2|
    *===============================================================================
    *
    *device_id_1Operation_Graph0(conv_fprop)
    *-------------------------------------------------------------------------------
    *|Feature_vector_type0_val0|PlanA0|
    *|Feature_vector_type0_val1|PlanB0|
    *===============================================================================
    *
    *device_id_1Operation_Graph1(dgrad)
    *-------------------------------------------------------------------------------
    *|Feature_vector_type1_val0|PlanA1|
    *|Feature_vector_type1_val1|PlanB1|
    *===============================================================================
    *
    *device_id_1Operation_Graph2(wgrad)
    *-------------------------------------------------------------------------------
    *|Feature_vector_type2_val0|PlanB2|
    *===============================================================================
    */

    namespacecudnn_frontend{

    classExecutionPlanCache_v1{

    protected:
    structcompare{
    booloperator ()(constfeature_vector_t&fv1,constfeature_vector_t&fv2)const{
    for(uint32_ti=0u;i<fv1.size();i++){
    if(fv1[i]<fv2[i]){
    returntrue;
    }
    }
    }
    };

    std::stringname="plan_cache_[unnamed]";

    usingFeatureVectorToPlanMap=std::map<cudnn_frontend::feature_vector_t,cudnn_frontend::ExecutionPlan,cudnn_frontend::ExecutionPlanCache_v1::compare>;
    FeatureVectorToPlanMapcache;

    mutablestd::mutexcache_mutex;

    public:
    virtualbool
    is_fastest_plan_stable(constcudnn_frontend::OperationGraph&op_graph,
    conststd::string&tag){
    returntrue;
    }

    voidadd_plan_to_cache(constcudnn_frontend::OperationGraph&op_graph,
    constcudnn_frontend::ExecutionPlan&plan){
    std::lock_guard<std::mutex>guard(cache_mutex);
    cache.insert(std::make_pair(op_graph.getFeatureVector(),plan));
    getLogger()<<"[cudnn_frontend]Addedto"<<name<<""<<op_graph.getTag()<<std::endl;
    }

    ExecutionPlanCache_v1(constchar*name_){
    name=name_;
    }

    conststd::string&get_name()const{
    returnname;
    }

    //Planistheoutputhere.
    boolget_plan_from_cache(constcudnn_frontend::OperationGraph&op_graph,
    constcudnn_frontend::ExecutionPlan*&plan)const{
    {
    std::lock_guard<std::mutex>guard(cache_mutex);
    autoit=cache.find(op_graph.getFeatureVector());

    if(it==cache.end()){
    getLogger()<<"[cudnn_frontend]CachedPlanNotFoundin"<<name<<std::endl;
    returnfalse;
    }
    plan=&(it->second);
    }
    getLogger()<<"[cudnn_frontend]CachedPlanFoundin"<<name<<std::endl;
    returntrue;
    }
    };

    classExecutionPlanCache_v2:publicExecutionPlanCache_v1{

    usingSaturationTracker=std::map<std::pair<cudnn_frontend::feature_vector_t,std::string>,int32_t>;
    SaturationTrackertracker;

    int32_tsaturationCount=1;

    public:
    virtualbool
    is_fastest_plan_stable(constcudnn_frontend::OperationGraph&op_graph,
    conststd::string&tag){
    if(saturationCount==1){returntrue;}//Specialcase.Alwaysaddtothecache.

    //Ifplancacheisalreadycreatedfortheop_graphnoneedtoupdate.
    //Ideally,onewillauto-tuneonlyiftheplancachehasnoplanfortheop_graph.
    cudnn_frontend::ExecutionPlanconst*plan=nullptr;
    if(get_plan_from_cache(op_graph,plan)){
    getLogger()<<"[cudnn_frontend]SaturationTracker"<<name<<""<<op_graph.getTag()<<""<<tag<<"planalreadyincache."<<std::endl;
    returnfalse;
    }

    //Lockthecacheandincreasethecounttillwesaturate
    std::lock_guard<std::mutex>guard(cache_mutex);
    autocnt=tracker[std::make_pair(op_graph.getFeatureVector(),tag)]+=1;
    getLogger()<<"[cudnn_frontend]SaturationTracker"<<name<<""<<op_graph.getTag()<<""<<tag<<""<<cnt<<std::endl;
    returncnt>=saturationCount;
    }

    voidset_saturation_count(int32_tcount){
    saturationCount=count;
    }

    ExecutionPlanCache_v2(constchar*name_):ExecutionPlanCache_v1(name_){
    }

    };

    usingExecutionPlanCache=ExecutionPlanCache_v2;

    }