cudnn_frontend_ExecutionPlan.h
algorithm
array
functional
memory
sstream
utility
cudnn.h
cudnn_backend.h
cudnn_frontend_Engine.h
cudnn_frontend_utils.h
cudnn_frontend.h
cudnn_frontend_ExecutionPlanCache.h
functional
limits
cudnn_frontend_Engine.h
cudnn_frontend_MatMulDesc.h
cudnn.h
cudnn_frontend_PointWiseDesc.h
string
cudnn_frontend_Logging.h
cudnn_frontend_Tensor.h
exception
fstream
ostream
cudnn_frontend_Operation.h
cudnn_frontend_ExecutionPlan.h
vector
cudnn_frontend_ConvDesc.h
cudnn_frontend_OperationGraph.h
cudnn_backend.h
utility
array
sstream
cudnn_frontend_ReductionDesc.h
iostream
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_ExecutionPlan.h
cudnn_frontend.h
cudnn_frontend_find_plan.h
cudnn_frontend::ExecutionPlan_v8
cudnn_frontend::ExecutionPlanBuilder_v8
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.
    */

    #pragmaonce

    #include<algorithm>
    #include<array>
    #include<functional>
    #include<memory>
    #include<sstream>
    #include<utility>

    #include<cudnn.h>
    #include<cudnn_backend.h>

    #include"cudnn_frontend_Engine.h"
    #include"cudnn_frontend_utils.h"

    namespacecudnn_frontend{
    classExecutionPlan_v8:publicBackendDescriptor{
    public:
    friendclassExecutionPlanBuilder_v8;

    ExecutionPlan_v8(ExecutionPlan_v8&&from)=default;
    ExecutionPlan_v8&
    operator=(ExecutionPlan_v8&&)=default;

    ~ExecutionPlan_v8()=default;
    auto
    getWorkspaceSize(void)const->int64_t{
    returnworkSpaceSize;
    }

    std::string
    describe()constoverride{
    std::stringstreamss;
    ss<<"CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:";
    ss<<getTag()<<",";
    ss<<"numeric_notes:"<<"";
    for(autonote:numeric_notes)
    ss<<note<<"";
    #if(CUDNN_VERSION>=8200)
    ss<<"behavior_notes:"<<"";
    for(autonote:behavior_notes)
    ss<<note<<"";
    #endif
    ss<<"workSpaceSize:"<<workSpaceSize;
    returnss.str();
    }

    std::stringconst&
    getTag()const{
    returnplanTag;
    }

    void
    setExecutionTime(floattime_){
    execution_time_ms=time_;
    }

    float
    getExecutionTime()const{
    returnexecution_time_ms;
    }

    std::array<cudnnBackendNumericalNote_t,CUDNN_NUMERICAL_NOTE_TYPE_COUNT>const&
    getNumericNotes()const{
    returnnumeric_notes;
    }

    #if(CUDNN_VERSION>=8200)
    std::array<cudnnBackendBehaviorNote_t,CUDNN_BEHAVIOR_NOTE_TYPE_COUNT>const&
    getBehaviorNotes()const{
    returnbehavior_notes;
    }
    #endif

    ExecutionPlan_v8(ExecutionPlan_v8const&)=default;
    ExecutionPlan_v8&
    operator=(ExecutionPlan_v8const&)=default;
    private:
    void
    fetchNotes(ManagedOpaqueDescriptor&extractedEngine){
    autostatus=CUDNN_STATUS_SUCCESS;
    int64_telem_count=0;
    cudnnBackendDescriptor_textractedEngine_=extractedEngine->get_backend_descriptor();
    status=cudnnBackendGetAttribute(extractedEngine_,
    CUDNN_ATTR_ENGINE_NUMERICAL_NOTE,
    CUDNN_TYPE_NUMERICAL_NOTE,
    CUDNN_NUMERICAL_NOTE_TYPE_COUNT,
    &elem_count,
    numeric_notes.data());
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttribute"
    "CUDNN_ATTR_ENGINE_NUMERICAL_NOTEFailed");
    }
    #if(CUDNN_VERSION>=8200)
    status=cudnnBackendGetAttribute(extractedEngine_,
    CUDNN_ATTR_ENGINE_BEHAVIOR_NOTE,
    CUDNN_TYPE_BEHAVIOR_NOTE,
    CUDNN_BEHAVIOR_NOTE_TYPE_COUNT,
    &elem_count,
    behavior_notes.data());
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttribute"
    "CUDNN_ATTR_ENGINE_BEHAVIOR_NOTEFailed");
    }
    #endif
    }

    void
    buildTag(ManagedOpaqueDescriptor&extractedEngine){
    //Computeauniquetagforexecutionplan:
    autostatus=CUDNN_STATUS_SUCCESS;
    std::stringstreamtag{""};
    int64_telemCount=0,engineId=0,numKnobs=0;

    std::array<ManagedOpaqueDescriptor,CUDNN_KNOB_TYPE_COUNTS>extractedKnobs{{nullptr}};
    for(auto&knob:extractedKnobs){
    knob=make_shared_backend_pointer(CUDNN_BACKEND_KNOB_CHOICE_DESCRIPTOR);
    status=knob->get_status();
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    this,status,"CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:cudnnCreateFailedwhencomputetag");
    }
    }

    cudnnBackendDescriptor_textractedEngine_=extractedEngine->get_backend_descriptor();
    std::array<cudnnBackendDescriptor_t,CUDNN_KNOB_TYPE_COUNTS>extractedKnobs_{{nullptr}};
    for(std::uint32_ti=0;i<extractedKnobs.size();i++){
    extractedKnobs_[i]=extractedKnobs[i]->get_backend_descriptor();
    }

    status=cudnnBackendGetAttribute(
    extractedEngine_,CUDNN_ATTR_ENGINE_GLOBAL_INDEX,CUDNN_TYPE_INT64,1,&elemCount,&engineId);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttribute"
    "CUDNN_ATTR_ENGINE_GLOBAL_INDEXFailed");
    }
    tag<<"eng"<<engineId;

    status=cudnnBackendGetAttribute(engine_config->get_backend_descriptor(),
    CUDNN_ATTR_ENGINECFG_KNOB_CHOICES,
    CUDNN_TYPE_BACKEND_DESCRIPTOR,
    CUDNN_KNOB_TYPE_COUNTS,
    &numKnobs,
    &(extractedKnobs_[0]));
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttribute"
    "CUDNN_ATTR_ENGINECFG_KNOB_CHOICESFailed");
    }
    if(numKnobs>CUDNN_KNOB_TYPE_COUNTS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttribute"
    "numKnobsexceedtheCUDNN_KNOB_TYPE_COUNTS");
    }
    for(int64_tidx=0;idx<numKnobs;++idx){
    constcudnnBackendDescriptor_t&knob=extractedKnobs_[idx];
    cudnnBackendKnobType_ttype=CUDNN_KNOB_TYPE_COUNTS;
    int64_tchoice=-2;
    status=cudnnBackendGetAttribute(
    knob,CUDNN_ATTR_KNOB_CHOICE_KNOB_TYPE,CUDNN_TYPE_KNOB_TYPE,1,nullptr,&type);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "computeTagCUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttribute"
    "CUDNN_ATTR_KNOB_CHOICE_KNOB_TYPEFailed");
    }
    status=cudnnBackendGetAttribute(
    knob,CUDNN_ATTR_KNOB_CHOICE_KNOB_VALUE,CUDNN_TYPE_INT64,1,nullptr,&choice);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttribute"
    "CUDNN_ATTR_KNOB_CHOICE_KNOB_VALUEFailed");
    }
    tag<<"_k"<<type<<"="<<choice;
    }
    planTag+=tag.str();

    }

    void
    computeWorkSpaceSize(){
    autostatus=cudnnBackendGetAttribute(pointer->get_backend_descriptor(),
    CUDNN_ATTR_EXECUTION_PLAN_WORKSPACE_SIZE,
    CUDNN_TYPE_INT64,
    1,
    NULL,
    &workSpaceSize);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttribute"
    "CUDNN_ATTR_EXECUTION_PLAN_WORKSPACE_SIZEFailed");
    }
    if(workSpaceSize<0){
    set_error_and_throw_exception(
    this,status,"CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttributeWorkspaceSizeInvalid");
    }
    }

    ExecutionPlan_v8()=default;
    ManagedOpaqueDescriptorengine_config=nullptr;
    cudnnHandle_thandle=nullptr;
    std::stringplanTag;

    std::int64_tworkSpaceSize=0;
    std::array<cudnnBackendNumericalNote_t,CUDNN_NUMERICAL_NOTE_TYPE_COUNT>numeric_notes;
    #if(CUDNN_VERSION>=8200)
    std::array<cudnnBackendBehaviorNote_t,CUDNN_BEHAVIOR_NOTE_TYPE_COUNT>behavior_notes;
    #endif

    floatexecution_time_ms=0.0f;

    };

    classExecutionPlanBuilder_v8{
    public:
    auto
    setHandle(cudnnHandle_thandle_)->ExecutionPlanBuilder_v8&{
    m_execution_plan.handle=handle_;
    return*this;
    }
    auto
    setEngineConfig(EngineConfig_v8const&engine_config_)->ExecutionPlanBuilder_v8&{
    m_execution_plan.engine_config=engine_config_.get_desc();
    m_execution_plan.planTag=engine_config_.getTag();
    return*this;
    }

    auto
    setEngineConfig(ManagedOpaqueDescriptor&desc,std::stringconst&opGraphTag_="")->ExecutionPlanBuilder_v8&{
    m_execution_plan.engine_config=desc;
    m_execution_plan.planTag=opGraphTag_;
    return*this;
    }
    ExecutionPlan_v8&&
    build(){
    if(m_execution_plan.handle==nullptr){
    set_error_and_throw_exception(
    &m_execution_plan,
    CUDNN_STATUS_BAD_PARAM,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:CheckandSettheCUDNN_ATTR_EXECUTION_PLAN_HANDLE");
    returnstd::move(m_execution_plan);
    };
    if(m_execution_plan.engine_config==nullptr){
    set_error_and_throw_exception(
    &m_execution_plan,
    CUDNN_STATUS_BAD_PARAM,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:CheckandSettheCUDNN_ATTR_EXECUTION_PLAN_ENGINE_CONFIG");
    returnstd::move(m_execution_plan);
    };

    //Createadescriptor.Memoryallocationhappenshere.
    autostatus=m_execution_plan.initialize_managed_backend_pointer(CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_execution_plan,status,"CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:cudnnCreateFailed");
    returnstd::move(m_execution_plan);
    }

    status=cudnnBackendSetAttribute(m_execution_plan.pointer->get_backend_descriptor(),
    CUDNN_ATTR_EXECUTION_PLAN_ENGINE_CONFIG,
    CUDNN_TYPE_BACKEND_DESCRIPTOR,
    1,
    &(m_execution_plan.engine_config->get_backend_descriptor()));
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_execution_plan,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:SetAttributeCUDNN_ATTR_EXECUTION_PLAN_ENGINE_CONFIGFailed");
    returnstd::move(m_execution_plan);
    }
    status=cudnnBackendSetAttribute(m_execution_plan.pointer->get_backend_descriptor(),
    CUDNN_ATTR_EXECUTION_PLAN_HANDLE,
    CUDNN_TYPE_HANDLE,
    1,
    &m_execution_plan.handle);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_execution_plan,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:SetAttributeCUDNN_ATTR_EXECUTION_PLAN_HANDLEFailed");
    returnstd::move(m_execution_plan);
    }
    //Finalizingthedescriptor
    status=cudnnBackendFinalize(m_execution_plan.pointer->get_backend_descriptor());
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_execution_plan,status,"CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:cudnnFinalizeDescriptorFailed");
    returnstd::move(m_execution_plan);
    }

    ManagedOpaqueDescriptorextractedEngine=make_shared_backend_pointer(CUDNN_BACKEND_ENGINE_DESCRIPTOR);
    status=extractedEngine->get_status();
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_execution_plan,status,"CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:cudnnCreateofCUDNN_BACKEND_ENGINE_DESCRIPTORfailedwhencomputetag");
    returnstd::move(m_execution_plan);
    }
    cudnnBackendDescriptor_textractedEngine_=extractedEngine->get_backend_descriptor();
    int64_telemCount=0;
    status=cudnnBackendGetAttribute(m_execution_plan.engine_config->get_backend_descriptor(),
    CUDNN_ATTR_ENGINECFG_ENGINE,
    CUDNN_TYPE_BACKEND_DESCRIPTOR,
    1,
    &elemCount,
    &extractedEngine_);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(&m_execution_plan,
    status,
    "CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR:GetAttribute"
    "CUDNN_ATTR_ENGINECFG_ENGINEFailed");
    returnstd::move(m_execution_plan);
    }

    m_execution_plan.buildTag(extractedEngine);
    m_execution_plan.fetchNotes(extractedEngine);
    m_execution_plan.computeWorkSpaceSize();

    getLogger()<<"[cudnn_frontend]"<<m_execution_plan<<std::endl;
    returnstd::move(m_execution_plan);
    }

    explicitExecutionPlanBuilder_v8()=default;
    ~ExecutionPlanBuilder_v8()=default;
    ExecutionPlanBuilder_v8(ExecutionPlanBuilder_v8&&)=delete;
    ExecutionPlanBuilder_v8(ExecutionPlanBuilder_v8const&)=delete;
    ExecutionPlanBuilder_v8&
    operator=(ExecutionPlanBuilder_v8const&)=delete;

    private:
    ExecutionPlan_v8m_execution_plan;
    };

    usingExecutionPlan=ExecutionPlan_v8;
    usingExecutionPlanBuilder=ExecutionPlanBuilder_v8;
    }