cudnn_frontend_Engine.h
algorithm
array
functional
memory
sstream
utility
vector
cudnn.h
cudnn_backend.h
cudnn_frontend_OperationGraph.h
cudnn_frontend_utils.h
cudnn_frontend_EngineConfig.h
cudnn_frontend.h
cudnn_frontend_ExecutionPlan.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
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_Engine.h
cudnn_frontend_EngineConfig.h
cudnn_frontend_Heuristics.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::Engine_v8
cudnn_frontend::Engine_v8::Knob
cudnn_frontend::EngineBuilder_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<vector>

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

    #include"cudnn_frontend_OperationGraph.h"
    #include"cudnn_frontend_utils.h"

    namespacecudnn_frontend{

    classEngine_v8:publicBackendDescriptor{
    private:
    Engine_v8()=default;
    Engine_v8(Engine_v8const&)=delete;
    Engine_v8&
    operator=(Engine_v8const&)=delete;

    classKnob{
    public:
    Knob(cudnnBackendKnobType_ttype_,int64_tmax,int64_tmin,int64_tstride_)
    :knobType(type_),maxValue(max),minValue(min),stride(stride_){}

    std::string
    describe()const{
    std::stringstreamss;
    ss<<"Knob:"<<knobType;
    ss<<"Min:"<<minValue;
    ss<<"Max:"<<maxValue;
    ss<<"Stride:"<<stride;
    returnss.str();
    }

    void
    setChoice(uint64_tval_){
    choice=val_;
    }

    int64_t
    getChoice()const{
    returnchoice;
    }

    cudnnBackendKnobType_t
    getKnobType()const{
    returnknobType;
    }

    int64_t
    getMinValue()const{
    returnminValue;
    }

    int64_t
    getMaxValue()const{
    returnminValue;
    }

    int64_t
    getStride()const{
    returnstride;
    }

    private:
    cudnnBackendKnobType_tknobType=CUDNN_KNOB_TYPE_COUNTS;
    int64_tmaxValue=0,minValue=0,stride=0;
    int64_tchoice=-1;
    };

    ManagedOpaqueDescriptoropGraph=nullptr;
    int64_tidx=-1;
    int64_tnumKnobs=0;
    std::array<ManagedOpaqueDescriptor,CUDNN_KNOB_TYPE_COUNTS>bKnobs={};
    std::vector<Knob>knobs;
    std::stringopGraphTag;

    void
    buildKnobs(){
    cudnnStatus_tstatus;
    for(autoi=0;i<numKnobs;i++){
    autobKnob=bKnobs[i]->get_backend_descriptor();
    cudnnBackendKnobType_ttype;
    int64_tmaxValue,minValue,stride,elemCount;
    status=
    cudnnBackendGetAttribute(bKnob,CUDNN_ATTR_KNOB_INFO_TYPE,CUDNN_TYPE_KNOB_TYPE,1,&elemCount,&type);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_ENGINE_DESCRIPTOR:CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR"
    "GetAttributeCUDNN_ATTR_KNOB_INFO_TYPEfailed");
    }
    status=cudnnBackendGetAttribute(
    bKnob,CUDNN_ATTR_KNOB_INFO_MAXIMUM_VALUE,CUDNN_TYPE_INT64,1,&elemCount,&maxValue);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_ENGINE_DESCRIPTOR:CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR"
    "GetAttributeCUDNN_ATTR_KNOB_INFO_MAXIMUM_VALUEFailed");
    }
    status=cudnnBackendGetAttribute(
    bKnob,CUDNN_ATTR_KNOB_INFO_MINIMUM_VALUE,CUDNN_TYPE_INT64,1,&elemCount,&minValue);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_ENGINE_DESCRIPTOR:CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR"
    "GetAttributeCUDNN_ATTR_KNOB_INFO_MINIMUM_VALUEFailed");
    }
    status=
    cudnnBackendGetAttribute(bKnob,CUDNN_ATTR_KNOB_INFO_STRIDE,CUDNN_TYPE_INT64,1,&elemCount,&stride);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(this,
    status,
    "CUDNN_BACKEND_ENGINE_DESCRIPTOR:CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR"
    "GetAttributeCUDNN_ATTR_KNOB_INFO_STRIDEFailed");
    }
    knobs.emplace_back(Knob(type,maxValue,minValue,stride));
    }
    }

    public:
    friendclassEngineBuilder_v8;
    std::string
    describe()constoverride{
    std::stringstreamss;
    ss<<"CUDNN_BACKEND_ENGINE_DESCRIPTOR:";
    ss<<"ID:"<<idx;
    ss<<"Has"<<numKnobs<<"knobs";
    returnss.str();
    }
    Engine_v8(Engine_v8&&from)
    :BackendDescriptor(from.get_desc(),from.get_status(),from.get_error()),
    opGraph(from.opGraph),
    idx(from.idx),
    opGraphTag(from.opGraphTag){
    cudnnStatus_tstatus;
    for(uint64_ti=0;i<bKnobs.size();i++){
    bKnobs[i]=make_shared_backend_pointer(CUDNN_BACKEND_KNOB_INFO_DESCRIPTOR);
    if(bKnobs[i]->is_good()==false){
    status=bKnobs[i]->get_status();
    set_error_and_throw_exception(
    this,
    status,
    "CUDNN_BACKEND_ENGINE_DESCRIPTOR:CUDNN_BACKEND_KNOB_INFO_DESCRIPTORcudnnCreateFailed");
    }
    }

    std::array<cudnnBackendDescriptor_t,CUDNN_KNOB_TYPE_COUNTS>bKnobs_=
    {};
    for(std::uint32_ti=0;i<bKnobs.size();i++){
    bKnobs_[i]=bKnobs[i]->get_backend_descriptor();
    }
    status=cudnnBackendGetAttribute(pointer->get_backend_descriptor(),
    CUDNN_ATTR_ENGINE_KNOB_INFO,
    CUDNN_TYPE_BACKEND_DESCRIPTOR,
    CUDNN_KNOB_TYPE_COUNTS,
    &numKnobs,
    bKnobs_.data());
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    this,status,"CUDNN_BACKEND_ENGINE_DESCRIPTOR:GetAttributeCUDNN_ATTR_ENGINE_KNOB_INFOQueryFailed");
    }
    buildKnobs();
    }
    Engine_v8&
    operator=(Engine_v8&&)=default;
    ~Engine_v8()=default;

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

    std::vector<Knob>&
    getSupportedKnobs(){
    returnknobs;
    }

    std::vector<Knob>const&
    getFinalizedKnobs()const{
    returnknobs;
    }

    boolknobs_set()const{
    boolis_knob_set=false;
    for(autoi=0;i<numKnobs;i++){
    if(knobs[i].getChoice()!=-1){
    is_knob_set=true;
    break;
    }
    }
    returnis_knob_set;
    }
    };

    classEngineBuilder_v8{
    public:
    auto
    setOperationGraph(OperationGraph_v8const&opGraph_)->EngineBuilder_v8&{
    m_engine.opGraph=opGraph_.get_desc();
    m_engine.opGraphTag=opGraph_.getTag();
    return*this;
    }

    auto
    setOperationGraph(ManagedOpaqueDescriptordesc_)->EngineBuilder_v8&{
    m_engine.opGraph=desc_;
    return*this;
    }
    auto
    setGlobalEngineIdx(int64_tidx_)->EngineBuilder_v8&{
    m_engine.idx=idx_;
    return*this;
    }
    Engine_v8&&
    build(){
    if(m_engine.idx<0){
    set_error_and_throw_exception(
    &m_engine,
    CUDNN_STATUS_BAD_PARAM,
    "CUDNN_BACKEND_ENGINE_DESCRIPTOR:CheckandSettheCUDNN_ATTR_ENGINE_GLOBAL_INDEXtovalidvalue");
    returnstd::move(m_engine);
    }
    if(m_engine.opGraph==nullptr){
    set_error_and_throw_exception(
    &m_engine,
    CUDNN_STATUS_BAD_PARAM,
    "CUDNN_BACKEND_ENGINE_DESCRIPTOR:CheckandSetCUDNN_ATTR_ENGINE_OPERATION_GRAPHtovalidvalue");
    returnstd::move(m_engine);
    }

    //Createadescriptor.Memoryallocationhappenshere.
    autostatus=m_engine.initialize_managed_backend_pointer(CUDNN_BACKEND_ENGINE_DESCRIPTOR);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_engine,status,"CUDNN_BACKEND_ENGINE_DESCRIPTOR:cudnnCreateDescriptorFailed");
    returnstd::move(m_engine);
    }

    status=cudnnBackendSetAttribute(m_engine.pointer->get_backend_descriptor(),
    CUDNN_ATTR_ENGINE_OPERATION_GRAPH,
    CUDNN_TYPE_BACKEND_DESCRIPTOR,
    1,
    &(m_engine.opGraph->get_backend_descriptor()));
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_engine,
    status,
    "CUDNN_BACKEND_ENGINE_DESCRIPTOR:SetAttributeCUDNN_ATTR_ENGINE_OPERATION_GRAPHFailed");
    returnstd::move(m_engine);
    }

    status=cudnnBackendSetAttribute(m_engine.pointer->get_backend_descriptor(),
    CUDNN_ATTR_ENGINE_GLOBAL_INDEX,
    CUDNN_TYPE_INT64,
    1,
    &m_engine.idx);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_engine,
    status,
    "CUDNN_BACKEND_ENGINE_DESCRIPTOR:SetAttributeCUDNN_ATTR_ENGINE_GLOBAL_INDEXFailed");
    returnstd::move(m_engine);
    }

    //Finalizingthedescriptor
    status=cudnnBackendFinalize(m_engine.pointer->get_backend_descriptor());
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(&m_engine,status,"CUDNN_BACKEND_ENGINE_DESCRIPTOR:cudnnFinalizeFailed");
    returnstd::move(m_engine);
    }

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

    explicitEngineBuilder_v8()=default;
    ~EngineBuilder_v8()=default;
    EngineBuilder_v8(EngineBuilder_v8&&)=delete;
    EngineBuilder_v8(EngineBuilder_v8const&)=delete;
    EngineBuilder_v8&
    operator=(EngineBuilder_v8const&)=delete;

    private:
    Engine_v8m_engine;
    };
    }