cudnn_frontend_ConvDesc.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.
*/
#pragma once
#include <algorithm>
#include <array>
#include <functional>
#include <memory>
#include <sstream>
#include <utility>
#include <cudnn.h>
#include <cudnn_backend.h>
#include "cudnn_frontend_utils.h"
namespace cudnn_frontend {
class ConvDesc_v8 : public BackendDescriptor {
public:
friend class ConvDescBuilder_v8;
std::string
describe() const override {
std::stringstream ss;
char sep = ' ';
ss << "CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR :"
<< " Datatype: " << to_string(compute_precision) << " Mode: " << std::to_string(mode)
<< " Num Dimensions: " << nDims;
ss << " PadLower [";
for (auto i = 0; i < nDims; i++) {
ss << sep << padLower[i];
sep = ',';
}
ss << " ] PadUpper [";
for (auto i = 0; i < nDims; i++) {
ss << sep << padUpper[i];
sep = ',';
}
ss << " ] Dilation [";
for (auto i = 0; i < nDims; i++) {
ss << sep << dilation[i];
sep = ',';
}
ss << " ] Stride [";
for (auto i = 0; i < nDims; i++) {
ss << sep << stride[i];
sep = ',';
}
ss << "]";
return ss.str();
}
ConvDesc_v8(ConvDesc_v8 &&from) = default;
ConvDesc_v8 &
operator=(ConvDesc_v8 &&) = default;
~ConvDesc_v8() = default;
cudnnDataType_t
getComputePrecision() const {
return compute_precision;
}
int64_t
getDimensionCount() const {
return nDims;
}
int64_t const *
getPadding() const {
return padLower;
}
int64_t const *
getStride() const {
return stride;
}
int64_t const *
getDilation() const {
return dilation;
}
cudnnConvolutionMode_t
getMathMode() const {
return mode;
}
private:
ConvDesc_v8() = default;
ConvDesc_v8(ConvDesc_v8 const &) = delete;
ConvDesc_v8 &
operator=(ConvDesc_v8 const &) = delete;
cudnnDataType_t compute_precision = CUDNN_DATA_FLOAT;
cudnnConvolutionMode_t mode = CUDNN_CONVOLUTION;
int64_t nDims = -1;
int64_t padLower[CUDNN_DIM_MAX + 1] = {0};
int64_t padUpper[CUDNN_DIM_MAX + 1] = {0};
int64_t dilation[CUDNN_DIM_MAX + 1] = {0};
int64_t stride[CUDNN_DIM_MAX + 1] = {-1};
};
class ConvDescBuilder_v8 {
public:
auto
setDataType(cudnnDataType_t data_type_) -> ConvDescBuilder_v8 & {
return setComputePrecision(data_type_);
}
auto
setComputePrecision(cudnnDataType_t data_type_) -> ConvDescBuilder_v8 & {
m_convDesc.compute_precision = data_type_;
return *this;
}
auto
setPrePadding(int64_t ndims, int64_t const *padding) -> ConvDescBuilder_v8 & {
std::copy(padding, padding + ndims, m_convDesc.padLower);
return *this;
}
auto
setPostPadding(int64_t ndims, int64_t const *padding) -> ConvDescBuilder_v8 & {
std::copy(padding, padding + ndims, m_convDesc.padUpper);
return *this;
}
auto
setDilation(int64_t ndims, int64_t const *dilation) -> ConvDescBuilder_v8 & {
std::copy(dilation, dilation + ndims, m_convDesc.dilation);
return *this;
}
auto
setStrides(int64_t ndims, int64_t const *strides) -> ConvDescBuilder_v8 & {
std::copy(strides, strides + ndims, m_convDesc.stride);
return *this;
}
auto
setNDims(int64_t nDims_) -> ConvDescBuilder_v8 & {
m_convDesc.nDims = nDims_;
return *this;
}
auto
setMathMode(cudnnConvolutionMode_t mode_) -> ConvDescBuilder_v8 & {
m_convDesc.mode = mode_;
return *this;
}
ConvDesc_v8 &&
build() {
// Sanity check if non-default fields have been set correctly.
if (m_convDesc.nDims <= 0) {
set_error_and_throw_exception(
&m_convDesc,
CUDNN_STATUS_BAD_PARAM,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: Check and Set the CUDNN_ATTR_CONVOLUTION_SPATIAL_DIMS field");
return std::move(m_convDesc);
};
if (m_convDesc.stride[0] <= 0) {
set_error_and_throw_exception(
&m_convDesc,
CUDNN_STATUS_BAD_PARAM,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: Check and Set the CUDNN_ATTR_CONVOLUTION_FILTER_STRIDES field");
return std::move(m_convDesc);
}
// Create a descriptor. Memory allocation happens here.
auto status = m_convDesc.initialize_managed_backend_pointer(CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc, status, "CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: Bad descriptor created");
return std::move(m_convDesc);
}
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc, status, "CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: cudnnCreate Failed");
return std::move(m_convDesc);
}
// Once Created lets set the descriptor parameters.
status = cudnnBackendSetAttribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_COMP_TYPE,
CUDNN_TYPE_DATA_TYPE,
1,
&m_convDesc.compute_precision);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_COMP_TYPE Failed");
return std::move(m_convDesc);
}
status = cudnnBackendSetAttribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_CONV_MODE,
CUDNN_TYPE_CONVOLUTION_MODE,
1,
&m_convDesc.mode);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_CONV_MODE Failed");
return std::move(m_convDesc);
}
status = cudnnBackendSetAttribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_SPATIAL_DIMS,
CUDNN_TYPE_INT64,
1,
&m_convDesc.nDims);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_SPATIAL_DIMS Failed");
return std::move(m_convDesc);
}
status = cudnnBackendSetAttribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_PRE_PADDINGS,
CUDNN_TYPE_INT64,
m_convDesc.nDims,
m_convDesc.padLower);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_PRE_PADDINGS Failed");
return std::move(m_convDesc);
}
status = cudnnBackendSetAttribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_POST_PADDINGS,
CUDNN_TYPE_INT64,
m_convDesc.nDims,
m_convDesc.padUpper);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_POST_PADDINGS Failed");
return std::move(m_convDesc);
}
status = cudnnBackendSetAttribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_DILATIONS,
CUDNN_TYPE_INT64,
m_convDesc.nDims,
m_convDesc.dilation);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_DILATIONS Failed");
return std::move(m_convDesc);
}
status = cudnnBackendSetAttribute(m_convDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_CONVOLUTION_FILTER_STRIDES,
CUDNN_TYPE_INT64,
m_convDesc.nDims,
m_convDesc.stride);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc,
status,
"CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_CONVOLUTION_FILTER_STRIDES Failed");
return std::move(m_convDesc);
}
// Finalizing the descriptor
status = cudnnBackendFinalize(m_convDesc.pointer->get_backend_descriptor());
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_convDesc, status, "CUDNN_BACKEND_CONVOLUTION_DESCRIPTOR: cudnnFinalize Failed");
return std::move(m_convDesc);
}
getLogger() << "[cudnn_frontend] " << m_convDesc << std::endl;
return std::move(m_convDesc);
}
explicit ConvDescBuilder_v8() = default;
~ConvDescBuilder_v8() = default;
ConvDescBuilder_v8(ConvDescBuilder_v8 &&) = delete;
ConvDescBuilder_v8(ConvDescBuilder_v8 const &) = delete;
ConvDescBuilder_v8 &
operator=(ConvDescBuilder_v8 const &) = delete;
private:
ConvDesc_v8 m_convDesc;
};
}
cudnn_frontend::ConvDesc_v8::padUpper
int64_t padUpper[CUDNN_DIM_MAX+1]
d, h, w
Definition: cudnn_frontend_ConvDesc.h:133
cudnn_frontend::ConvDesc_v8::getStride
int64_t const * getStride() const
Definition: cudnn_frontend_ConvDesc.h:109
cudnn_frontend::getLogger
ConditionalStreamer & getLogger()
Definition: cudnn_frontend_Logging.h:72
cudnn_frontend::ConvDesc_v8::compute_precision
cudnnDataType_t compute_precision
Definition: cudnn_frontend_ConvDesc.h:129
cudnn_frontend::set_error_and_throw_exception
static void set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message)
Definition: cudnn_frontend_utils.h:138
nlohmann::to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:25855
cudnn_frontend::ConvDesc_v8::getPadding
int64_t const * getPadding() const
Definition: cudnn_frontend_ConvDesc.h:105
cudnn_frontend::ConvDesc_v8::getComputePrecision
cudnnDataType_t getComputePrecision() const
Definition: cudnn_frontend_ConvDesc.h:95
cudnn_frontend::ConvDesc_v8::~ConvDesc_v8
~ConvDesc_v8()=default
cudnn_frontend::BackendDescriptor
Definition: cudnn_backend_base.h:99
cudnn_frontend::ConvDesc_v8::ConvDesc_v8
ConvDesc_v8()=default
cudnn_frontend::ConvDesc_v8::dilation
int64_t dilation[CUDNN_DIM_MAX+1]
d, h, w
Definition: cudnn_frontend_ConvDesc.h:134
cudnn_frontend::ConvDesc_v8::nDims
int64_t nDims
Convolution vs cross correlation.
Definition: cudnn_frontend_ConvDesc.h:131
cudnn_frontend_utils.h
cudnn_frontend::ConvDescBuilder_v8::setNDims
auto setNDims(int64_t nDims_) -> ConvDescBuilder_v8 &
Set Num Spatial Dimensions of the convolution Operation.
Definition: cudnn_frontend_ConvDesc.h:183
cudnn_frontend::ConvDescBuilder_v8::setPrePadding
auto setPrePadding(int64_t ndims, int64_t const *padding) -> ConvDescBuilder_v8 &
Set Padding Lower of the convDesc.
Definition: cudnn_frontend_ConvDesc.h:159
cudnn_frontend::ConvDescBuilder_v8
Definition: cudnn_frontend_ConvDesc.h:141
cudnn_frontend::ConvDescBuilder_v8::setDilation
auto setDilation(int64_t ndims, int64_t const *dilation) -> ConvDescBuilder_v8 &
Set Dilation of the convDesc.
Definition: cudnn_frontend_ConvDesc.h:171
cudnn_frontend::ConvDescBuilder_v8::setMathMode
auto setMathMode(cudnnConvolutionMode_t mode_) -> ConvDescBuilder_v8 &
Set Convolution Mode of the convolution Operation.
Definition: cudnn_frontend_ConvDesc.h:189
cudnn_frontend::ConvDesc_v8::stride
int64_t stride[CUDNN_DIM_MAX+1]
d, h, w
Definition: cudnn_frontend_ConvDesc.h:135
cudnn_frontend::ConvDescBuilder_v8::build
ConvDesc_v8 && build()
Definition: cudnn_frontend_ConvDesc.h:198
cudnn_frontend::ConvDesc_v8
Definition: cudnn_frontend_ConvDesc.h:54
cudnn_frontend
Definition: cudnn_backend_base.h:29
cudnn_frontend::ConvDesc_v8::getMathMode
cudnnConvolutionMode_t getMathMode() const
Definition: cudnn_frontend_ConvDesc.h:118
cudnn_frontend::ConvDesc_v8::getDimensionCount
int64_t getDimensionCount() const
Definition: cudnn_frontend_ConvDesc.h:100
cudnn_frontend::ConvDescBuilder_v8::setStrides
auto setStrides(int64_t ndims, int64_t const *strides) -> ConvDescBuilder_v8 &
Set Strides of the convDesc.
Definition: cudnn_frontend_ConvDesc.h:177
cudnn_frontend::ConvDescBuilder_v8::m_convDesc
ConvDesc_v8 m_convDesc
Definition: cudnn_frontend_ConvDesc.h:341
cudnn_frontend::to_string
static std::string to_string(cudnnDataType_t type)
Definition: cudnn_frontend_utils.h:72
cudnn_frontend::ConvDescBuilder_v8::setPostPadding
auto setPostPadding(int64_t ndims, int64_t const *padding) -> ConvDescBuilder_v8 &
Set Padding Upper of the convDesc.
Definition: cudnn_frontend_ConvDesc.h:165
cudnn_frontend::ConvDesc_v8::padLower
int64_t padLower[CUDNN_DIM_MAX+1]
number of dimensions
Definition: cudnn_frontend_ConvDesc.h:132
cudnn_frontend::ConvDesc_v8::describe
std::string describe() const override
Return a string describing the backend Descriptor.
Definition: cudnn_frontend_ConvDesc.h:58
cudnn_frontend::ConvDescBuilder_v8::setDataType
auto setDataType(cudnnDataType_t data_type_) -> ConvDescBuilder_v8 &
Set Datatype for the Convolution Operation.
Definition: cudnn_frontend_ConvDesc.h:149
cudnn_frontend::ConvDesc_v8::getDilation
int64_t const * getDilation() const
Definition: cudnn_frontend_ConvDesc.h:113
cudnn_frontend::ConvDesc_v8::mode
cudnnConvolutionMode_t mode
Convolution operation data type.
Definition: cudnn_frontend_ConvDesc.h:130
cudnn_frontend::ConvDesc_v8::operator=
ConvDesc_v8 & operator=(ConvDesc_v8 &&)=default
cudnn_frontend::ConvDescBuilder_v8::setComputePrecision
auto setComputePrecision(cudnnDataType_t data_type_) -> ConvDescBuilder_v8 &
Definition: cudnn_frontend_ConvDesc.h:153
cudnn_frontend::BackendDescriptor::status
cudnnStatus_t status
Shared pointer of the OpaqueBackendPointer.
Definition: cudnn_backend_base.h:160
cudnn_frontend::ConvDesc_v8::ConvDescBuilder_v8
friend class ConvDescBuilder_v8
Definition: cudnn_frontend_ConvDesc.h:56