cudnn_frontend_ReductionDesc.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 ReductionDesc_v8 : public BackendDescriptor {
public:
friend class ReductionDescBuilder_v8;
std::string
describe() const override {
std::stringstream ss;
ss << "CUDNN_BACKEND_REDUCTION_DESCRIPTOR :"
<< " Math precision " << (math_precision) << "Reduction operator " << (reduction_op);
return ss.str();
}
ReductionDesc_v8(ReductionDesc_v8 &&from) = default;
ReductionDesc_v8 &
operator= (ReductionDesc_v8 &&from) = default;
~ReductionDesc_v8() = default;
private:
ReductionDesc_v8() = default;
ReductionDesc_v8(ReductionDesc_v8 const &) = delete;
ReductionDesc_v8 &
operator=(ReductionDesc_v8 const &) = delete;
cudnnDataType_t math_precision = CUDNN_DATA_FLOAT;
cudnnReduceTensorOp_t reduction_op = CUDNN_REDUCE_TENSOR_ADD;
};
class ReductionDescBuilder_v8 {
public:
auto
setMathPrecision(cudnnDataType_t data_type_) -> ReductionDescBuilder_v8 & {
m_reductionDesc.math_precision = data_type_;
return *this;
}
auto
setReductionOp(cudnnReduceTensorOp_t op_) -> ReductionDescBuilder_v8 & {
m_reductionDesc.reduction_op = op_;
return *this;
}
ReductionDesc_v8 &&
build() {
// Create a descriptor. Memory allocation happens here.
auto status = m_reductionDesc.initialize_managed_backend_pointer(CUDNN_BACKEND_REDUCTION_DESCRIPTOR);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_reductionDesc, status, "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: cudnnCreate Failed");
return std::move(m_reductionDesc);
}
// Once Created lets set the descriptor parameters.
status = cudnnBackendSetAttribute(m_reductionDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_REDUCTION_COMP_TYPE,
CUDNN_TYPE_DATA_TYPE,
1,
&m_reductionDesc.math_precision);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_reductionDesc,
status,
"CUDNN_BACKEND_REDUCTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_REDUCTION_COMP_TYPE Failed");
return std::move(m_reductionDesc);
}
status = cudnnBackendSetAttribute(m_reductionDesc.pointer->get_backend_descriptor(),
CUDNN_ATTR_REDUCTION_OPERATOR,
CUDNN_TYPE_REDUCTION_OPERATOR_TYPE,
1,
&m_reductionDesc.reduction_op);
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_reductionDesc,
status,
"CUDNN_BACKEND_REDUCTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_REDUCTION_OPERATOR Failed");
return std::move(m_reductionDesc);
}
// Finalizing the descriptor
status = cudnnBackendFinalize(m_reductionDesc.pointer->get_backend_descriptor());
if (status != CUDNN_STATUS_SUCCESS) {
set_error_and_throw_exception(
&m_reductionDesc, status, "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: cudnnFinalize Failed");
return std::move(m_reductionDesc);
}
getLogger() << "[cudnn_frontend] " << m_reductionDesc << std::endl;
return std::move(m_reductionDesc);
}
explicit ReductionDescBuilder_v8() = default;
~ReductionDescBuilder_v8() = default;
ReductionDescBuilder_v8(ReductionDescBuilder_v8 &&) = delete;
ReductionDescBuilder_v8(ReductionDescBuilder_v8 const &) = delete;
ReductionDescBuilder_v8 &
operator=(ReductionDescBuilder_v8 const &) = delete;
private:
ReductionDesc_v8 m_reductionDesc;
};
}
cudnn_frontend::getLogger
ConditionalStreamer & getLogger()
Definition: cudnn_frontend_Logging.h:72
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
cudnn_frontend::ReductionDesc_v8::ReductionDesc_v8
ReductionDesc_v8()=default
cudnn_frontend::ReductionDesc_v8::operator=
ReductionDesc_v8 & operator=(ReductionDesc_v8 &&from)=default
cudnn_frontend::ReductionDesc_v8::~ReductionDesc_v8
~ReductionDesc_v8()=default
cudnn_frontend::ReductionDesc_v8::ReductionDescBuilder_v8
friend class ReductionDescBuilder_v8
Definition: cudnn_frontend_ReductionDesc.h:50
cudnn_frontend::BackendDescriptor
Definition: cudnn_backend_base.h:99
cudnn_frontend::ReductionDescBuilder_v8::setMathPrecision
auto setMathPrecision(cudnnDataType_t data_type_) -> ReductionDescBuilder_v8 &
Set Math Precision Data Type for the Reduction Operation.
Definition: cudnn_frontend_ReductionDesc.h:86
cudnn_frontend::ReductionDesc_v8::math_precision
cudnnDataType_t math_precision
Definition: cudnn_frontend_ReductionDesc.h:71
cudnn_frontend_utils.h
cudnn_frontend::ReductionDesc_v8
Definition: cudnn_frontend_ReductionDesc.h:48
cudnn_frontend::ReductionDesc_v8::reduction_op
cudnnReduceTensorOp_t reduction_op
Definition: cudnn_frontend_ReductionDesc.h:72
cudnn_frontend::ReductionDescBuilder_v8
Definition: cudnn_frontend_ReductionDesc.h:78
cudnn_frontend::ReductionDescBuilder_v8::build
ReductionDesc_v8 && build()
Definition: cudnn_frontend_ReductionDesc.h:101
cudnn_frontend
Definition: cudnn_backend_base.h:29
cudnn_frontend::ReductionDesc_v8::describe
std::string describe() const override
Return a string describing the backend Descriptor.
Definition: cudnn_frontend_ReductionDesc.h:52
cudnn_frontend::ReductionDescBuilder_v8::setReductionOp
auto setReductionOp(cudnnReduceTensorOp_t op_) -> ReductionDescBuilder_v8 &
Set redution operator for the Reduction Operation.
Definition: cudnn_frontend_ReductionDesc.h:92
cudnn_frontend::ReductionDescBuilder_v8::m_reductionDesc
ReductionDesc_v8 m_reductionDesc
Definition: cudnn_frontend_ReductionDesc.h:157
cudnn_frontend::BackendDescriptor::status
cudnnStatus_t status
Shared pointer of the OpaqueBackendPointer.
Definition: cudnn_backend_base.h:160