cudnn_frontend_Tensor.h
algorithm
array
functional
memory
sstream
utility
cudnn_frontend_utils.h
cudnn_frontend_Operation.h
cudnn_frontend.h
cudnn_frontend_Reorder_Tensor.h
functional
cudnn.h
string
cudnn_frontend_Logging.h
cudnn_frontend_Tensor.h
exception
fstream
ostream
vector
utility
array
sstream
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_Tensor.h
cudnn_frontend_EngineConfigGenerator.h
cudnn_frontend_ExecutionPlanCache.h
cudnn_frontend_Operation.h
cudnn_frontend_Reorder_Tensor.h
cudnn_frontend_ExecutionPlan.h
cudnn_frontend_OperationGraph.h
cudnn_frontend.h
cudnn_frontend_find_plan.h
cudnn_frontend::Tensor_v8
cudnn_frontend::TensorBuilder_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_frontend_utils.h"

    namespacecudnn_frontend{

    classTensor_v8:publicBackendDescriptor{
    public:
    friendclassTensorBuilder_v8;
    std::string
    describe()constoverride{
    std::stringstreamss;
    charsep='';
    ss<<"CUDNN_BACKEND_TENSOR_DESCRIPTOR:"
    <<"Datatype:"<<to_string(data_type)<<"Id:"<<std::to_string(id)<<"Alignment:"<<alignment
    <<"nDims"<<nDims<<"VectorCount:"<<vectorCount<<"vectorDimension"<<vectorDimension;
    ss<<"Dim[";
    for(autoi=0;i<nDims;i++){
    ss<<sep<<btensor_dimA[i];
    sep=',';
    }
    ss<<"]Str[";
    for(autoi=0;i<nDims;i++){
    ss<<sep<<btensor_strA[i];
    sep=',';
    }
    ss<<"]";
    ss<<"isVirtual:"<<std::to_string(isVirtual)<<"isByValue:"<<std::to_string(isByValue);
    #if(CUDNN_VERSION>=8300)
    ss<<"reorder_type:"<<reorder_type;
    #endif
    returnss.str();
    }

    int64_t
    getPackedElementCount()const{
    int64_tcount=vectorCount;
    for(autoi=0;i<nDims;i++){
    count=count*btensor_dimA[i];
    }
    returncount;
    };

    int64_t
    getDimensionCount()const{
    returnnDims;
    }

    int64_tconst*
    getDimArray()const{
    returnbtensor_dimA;
    }

    int64_tconst*
    getStrideArray()const{
    returnbtensor_strA;
    }

    int64_t
    getDataType()const{
    returnstatic_cast<int64_t>(data_type);
    }

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

    ~Tensor_v8()=default;

    private:
    Tensor_v8()=default;
    Tensor_v8(Tensor_v8const&)=delete;
    Tensor_v8&
    operator=(Tensor_v8const&)=delete;

    cudnnDataType_tdata_type=CUDNN_DATA_FLOAT;
    int64_tbtensor_dimA[CUDNN_DIM_MAX+1]={-1};
    int64_tbtensor_strA[CUDNN_DIM_MAX+1]={-1};
    int64_tid=-1;
    int64_talignment=-1;
    int64_tnDims=-1;
    int64_tvectorDimension=-1;
    int64_tvectorCount=1;
    boolisVirtual=false;
    boolisByValue=false;
    #if(CUDNN_VERSION>=8300)
    cudnnBackendTensorReordering_treorder_type=CUDNN_TENSOR_REORDERING_NONE;
    #endif
    };

    classTensorBuilder_v8{
    public:
    auto
    setDataType(cudnnDataType_tdata_type_)->TensorBuilder_v8&{
    m_tensor.data_type=data_type_;
    return*this;
    }
    auto
    setDim(int64_tndim,int64_tconst*dim)->TensorBuilder_v8&{
    std::copy((dim),dim+ndim,m_tensor.btensor_dimA);
    m_tensor.nDims=ndim;
    return*this;
    }
    auto
    setStrides(int64_tndim,int64_tconst*strides)->TensorBuilder_v8&{
    std::copy(strides,strides+ndim,m_tensor.btensor_strA);
    return*this;
    }
    auto
    setId(int64_tid_)->TensorBuilder_v8&{
    m_tensor.id=id_;
    return*this;
    }
    auto
    setAlignment(int64_talignment_)->TensorBuilder_v8&{
    m_tensor.alignment=alignment_;
    return*this;
    }
    auto
    setVirtual(boolvirtual_=true)->TensorBuilder_v8&{
    m_tensor.isVirtual=virtual_;
    return*this;
    }
    auto
    setByValue(boolisByValue_=true)->TensorBuilder_v8&{
    m_tensor.isByValue=isByValue_;
    return*this;
    }
    auto
    setVectorCountAndDimension(int64_tvectorCount_,int64_tvectorDimension_)->TensorBuilder_v8&{
    m_tensor.vectorCount=vectorCount_;
    m_tensor.vectorDimension=vectorDimension_;
    return*this;
    }

    #if(CUDNN_VERSION>=8300)
    auto
    setReorderType(cudnnBackendTensorReordering_ttype_)->TensorBuilder_v8&{
    m_tensor.reorder_type=type_;
    return*this;
    }
    #endif

    Tensor_v8&&
    build(){
    //Sanitycheckifnon-defaultfieldshavebeensetcorrectly.
    if(m_tensor.alignment<=0){
    set_error_and_throw_exception(
    &m_tensor,
    CUDNN_STATUS_BAD_PARAM,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:CheckandSettheCUDNN_ATTR_TENSOR_BYTE_ALIGNMENTfield");
    returnstd::move(m_tensor);
    }
    if(m_tensor.id<=0){
    set_error_and_throw_exception(
    &m_tensor,
    CUDNN_STATUS_BAD_PARAM,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:CheckandSettheCUDNN_ATTR_TENSOR_UNIQUE_IDasavalidvalue");
    returnstd::move(m_tensor);
    }
    if(m_tensor.btensor_strA[0]<=0){
    set_error_and_throw_exception(
    &m_tensor,
    CUDNN_STATUS_BAD_PARAM,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:CheckandSettheCUDNN_ATTR_TENSOR_STRIDESCorrectly");
    returnstd::move(m_tensor);
    }
    if(m_tensor.btensor_dimA[0]<=0){
    set_error_and_throw_exception(
    &m_tensor,
    CUDNN_STATUS_BAD_PARAM,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:CheckandSettheCUDNN_ATTR_TENSOR_DIMENSIONSCorrectly");
    returnstd::move(m_tensor);
    }
    if(m_tensor.pointer!=nullptr){
    set_error_and_throw_exception(&m_tensor,
    CUDNN_STATUS_BAD_PARAM,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:Badtensorcreated.Thetensoralready"
    "seemstobepointingtosomething");
    returnstd::move(m_tensor);
    }

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

    //OnceCreatedletssetthedescriptorparameters.
    status=cudnnBackendSetAttribute(m_tensor.pointer->get_backend_descriptor(),
    CUDNN_ATTR_TENSOR_DATA_TYPE,
    CUDNN_TYPE_DATA_TYPE,
    1,
    &m_tensor.data_type);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,status,"CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_DATA_TYPEFailed");
    returnstd::move(m_tensor);
    }
    status=cudnnBackendSetAttribute(m_tensor.pointer->get_backend_descriptor(),
    CUDNN_ATTR_TENSOR_DIMENSIONS,
    CUDNN_TYPE_INT64,
    m_tensor.nDims,
    m_tensor.btensor_dimA);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,status,"CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_DIMENSIONSFailed");
    returnstd::move(m_tensor);
    }
    status=cudnnBackendSetAttribute(m_tensor.pointer->get_backend_descriptor(),
    CUDNN_ATTR_TENSOR_STRIDES,
    CUDNN_TYPE_INT64,
    m_tensor.nDims,
    m_tensor.btensor_strA);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,status,"CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_STRIDESFailed");
    returnstd::move(m_tensor);
    }
    status=cudnnBackendSetAttribute(
    m_tensor.pointer->get_backend_descriptor(),CUDNN_ATTR_TENSOR_UNIQUE_ID,CUDNN_TYPE_INT64,1,&m_tensor.id);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,status,"CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_UNIQUE_IDFailed");
    returnstd::move(m_tensor);
    }
    cudnnBackendSetAttribute(m_tensor.pointer->get_backend_descriptor(),
    CUDNN_ATTR_TENSOR_BYTE_ALIGNMENT,
    CUDNN_TYPE_INT64,
    1,
    &m_tensor.alignment);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,
    status,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_BYTE_ALIGNMENTFailed");
    returnstd::move(m_tensor);
    }
    if(m_tensor.isVirtual){
    cudnnBackendSetAttribute(m_tensor.pointer->get_backend_descriptor(),
    CUDNN_ATTR_TENSOR_IS_VIRTUAL,
    CUDNN_TYPE_BOOLEAN,
    1,
    &m_tensor.isVirtual);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,
    status,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_BYTE_ALIGNMENTFailed");
    returnstd::move(m_tensor);
    }
    }
    if(m_tensor.isByValue){
    cudnnBackendSetAttribute(m_tensor.pointer->get_backend_descriptor(),
    CUDNN_ATTR_TENSOR_IS_BY_VALUE,
    CUDNN_TYPE_BOOLEAN,
    1,
    &m_tensor.isByValue);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,
    status,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_IS_BY_VALUEFailed");
    returnstd::move(m_tensor);
    }
    }

    if(m_tensor.vectorCount>1){
    cudnnBackendSetAttribute(m_tensor.pointer->get_backend_descriptor(),
    CUDNN_ATTR_TENSOR_VECTOR_COUNT,
    CUDNN_TYPE_INT64,
    1,
    &m_tensor.vectorCount);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,
    status,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_VECTOR_COUNTFailed");
    returnstd::move(m_tensor);
    }
    }
    if(m_tensor.vectorDimension>=0){
    cudnnBackendSetAttribute(m_tensor.pointer->get_backend_descriptor(),
    CUDNN_ATTR_TENSOR_VECTORIZED_DIMENSION,
    CUDNN_TYPE_INT64,
    1,
    &m_tensor.vectorDimension);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,
    status,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_VECTORIZED_DIMENSIONFailed");
    returnstd::move(m_tensor);
    }
    }

    //Setthereorder_type
    #if(CUDNN_VERSION>=8300)
    if(m_tensor.reorder_type!=CUDNN_TENSOR_REORDERING_NONE){
    cudnnBackendSetAttribute(m_tensor.pointer->get_backend_descriptor(),
    CUDNN_ATTR_TENSOR_REORDERING_MODE,
    CUDNN_TYPE_TENSOR_REORDERING_MODE,
    1,
    &m_tensor.reorder_type);
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(
    &m_tensor,
    status,
    "CUDNN_BACKEND_TENSOR_DESCRIPTOR:SetAttributeCUDNN_ATTR_TENSOR_REORDERING_MODEFailed");
    returnstd::move(m_tensor);
    }
    }
    #endif
    //Finalizingthedescriptor
    status=cudnnBackendFinalize(m_tensor.pointer->get_backend_descriptor());
    if(status!=CUDNN_STATUS_SUCCESS){
    set_error_and_throw_exception(&m_tensor,status,"CUDNN_BACKEND_TENSOR_DESCRIPTORcudnnFinalizefailed");
    returnstd::move(m_tensor);
    }
    getLogger()<<"[cudnn_frontend]"<<m_tensor<<std::endl;
    returnstd::move(m_tensor);
    }

    explicitTensorBuilder_v8()=default;
    ~TensorBuilder_v8()=default;
    TensorBuilder_v8(TensorBuilder_v8&&)=delete;
    TensorBuilder_v8(TensorBuilder_v8const&)=delete;
    TensorBuilder_v8&
    operator=(TensorBuilder_v8const&)=delete;

    private:
    Tensor_v8m_tensor;
    };

    }