CUDNN Frontend API 8.3.0

cudnn_frontend_ReductionDesc.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #pragma once
24
25 #include <algorithm>
26 #include <array>
27 #include <functional>
28 #include <memory>
29 #include <sstream>
30 #include <utility>
31
32 #include <cudnn.h>
33 #include <cudnn_backend.h>
34
35 #include "cudnn_frontend_utils.h"
36
37 namespace cudnn_frontend {
48 class ReductionDesc_v8 : public BackendDescriptor {
49 public:
50 friend class ReductionDescBuilder_v8;
51 std::string
52 describe() const override {
53 std::stringstream ss;
54 ss << "CUDNN_BACKEND_REDUCTION_DESCRIPTOR :"
55 << " Math precision " << (math_precision) << "Reduction operator " << (reduction_op);
56 return ss.str();
57 }
58
59 ReductionDesc_v8(ReductionDesc_v8 &&from) = default;
60 ReductionDesc_v8 &
61 operator= (ReductionDesc_v8 &&from) = default;
62
63 ~ReductionDesc_v8() = default;
64
65 private:
66 ReductionDesc_v8() = default;
67 ReductionDesc_v8(ReductionDesc_v8 const &) = delete;
68 ReductionDesc_v8 &
69 operator=(ReductionDesc_v8 const &) = delete;
70
71 cudnnDataType_t math_precision = CUDNN_DATA_FLOAT;
72 cudnnReduceTensorOp_t reduction_op = CUDNN_REDUCE_TENSOR_ADD;
73 };
74
78 class ReductionDescBuilder_v8 {
79 public:
84 auto
86 setMathPrecision(cudnnDataType_t data_type_) -> ReductionDescBuilder_v8 & {
87 m_reductionDesc.math_precision = data_type_;
88 return *this;
89 }
91 auto
92 setReductionOp(cudnnReduceTensorOp_t op_) -> ReductionDescBuilder_v8 & {
93 m_reductionDesc.reduction_op = op_;
94 return *this;
95 }
98 ReductionDesc_v8 &&
101 build() {
102 // Create a descriptor. Memory allocation happens here.
103 auto status = m_reductionDesc.initialize_managed_backend_pointer(CUDNN_BACKEND_REDUCTION_DESCRIPTOR);
104 if (status != CUDNN_STATUS_SUCCESS) {
105 set_error_and_throw_exception(
106 &m_reductionDesc, status, "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: cudnnCreate Failed");
107 return std::move(m_reductionDesc);
108 }
109
110 // Once Created lets set the descriptor parameters.
111 status = cudnnBackendSetAttribute(m_reductionDesc.pointer->get_backend_descriptor(),
112 CUDNN_ATTR_REDUCTION_COMP_TYPE,
113 CUDNN_TYPE_DATA_TYPE,
114 1,
115 &m_reductionDesc.math_precision);
116 if (status != CUDNN_STATUS_SUCCESS) {
117 set_error_and_throw_exception(
118 &m_reductionDesc,
119 status,
120 "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_REDUCTION_COMP_TYPE Failed");
121 return std::move(m_reductionDesc);
122 }
123
124 status = cudnnBackendSetAttribute(m_reductionDesc.pointer->get_backend_descriptor(),
125 CUDNN_ATTR_REDUCTION_OPERATOR,
126 CUDNN_TYPE_REDUCTION_OPERATOR_TYPE,
127 1,
128 &m_reductionDesc.reduction_op);
129 if (status != CUDNN_STATUS_SUCCESS) {
130 set_error_and_throw_exception(
131 &m_reductionDesc,
132 status,
133 "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_REDUCTION_OPERATOR Failed");
134 return std::move(m_reductionDesc);
135 }
136
137 // Finalizing the descriptor
138 status = cudnnBackendFinalize(m_reductionDesc.pointer->get_backend_descriptor());
139 if (status != CUDNN_STATUS_SUCCESS) {
140 set_error_and_throw_exception(
141 &m_reductionDesc, status, "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: cudnnFinalize Failed");
142 return std::move(m_reductionDesc);
143 }
144
145 getLogger() << "[cudnn_frontend] " << m_reductionDesc << std::endl;
146 return std::move(m_reductionDesc);
147 }
148
149 explicit ReductionDescBuilder_v8() = default;
150 ~ReductionDescBuilder_v8() = default;
151 ReductionDescBuilder_v8(ReductionDescBuilder_v8 &&) = delete;
152 ReductionDescBuilder_v8(ReductionDescBuilder_v8 const &) = delete;
153 ReductionDescBuilder_v8 &
154 operator=(ReductionDescBuilder_v8 const &) = delete;
155
156 private:
157 ReductionDesc_v8 m_reductionDesc;
158 };
159 }
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