CUDNN Frontend API  8.2.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 {
49  public:
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 
60  : BackendDescriptor(from.get_desc(), from.get_status(), from.get_error()),
62  reduction_op(from.reduction_op) {}
63 
64  ~ReductionDesc_v8() = default;
65 
66  private:
67  ReductionDesc_v8() = default;
68  ReductionDesc_v8(ReductionDesc_v8 const &) = delete;
70  operator=(ReductionDesc_v8 const &) = delete;
71 
72  cudnnDataType_t math_precision = CUDNN_DATA_FLOAT;
73  cudnnReduceTensorOp_t reduction_op = CUDNN_REDUCE_TENSOR_ADD;
74 };
75 
80  public:
85  auto
87  setMathPrecision(cudnnDataType_t data_type_) -> ReductionDescBuilder_v8 & {
88  m_reductionDesc.math_precision = data_type_;
89  return *this;
90  }
92  auto
93  setReductionOp(cudnnReduceTensorOp_t op_) -> ReductionDescBuilder_v8 & {
94  m_reductionDesc.reduction_op = op_;
95  return *this;
96  }
102  build() {
103  // Create a descriptor. Memory allocation happens here.
104  auto status = m_reductionDesc.initialize_managed_backend_pointer(CUDNN_BACKEND_REDUCTION_DESCRIPTOR);
105  if (status != CUDNN_STATUS_SUCCESS) {
107  &m_reductionDesc, status, "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: cudnnCreate Failed");
108  return std::move(m_reductionDesc);
109  }
110 
111  // Once Created lets set the descriptor parameters.
112  status = cudnnBackendSetAttribute(m_reductionDesc.pointer->get_backend_descriptor(),
113  CUDNN_ATTR_REDUCTION_COMP_TYPE,
114  CUDNN_TYPE_DATA_TYPE,
115  1,
116  &m_reductionDesc.math_precision);
117  if (status != CUDNN_STATUS_SUCCESS) {
119  &m_reductionDesc,
120  status,
121  "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_REDUCTION_COMP_TYPE Failed");
122  return std::move(m_reductionDesc);
123  }
124 
125  status = cudnnBackendSetAttribute(m_reductionDesc.pointer->get_backend_descriptor(),
126  CUDNN_ATTR_REDUCTION_OPERATOR,
127  CUDNN_TYPE_REDUCTION_OPERATOR_TYPE,
128  1,
129  &m_reductionDesc.reduction_op);
130  if (status != CUDNN_STATUS_SUCCESS) {
132  &m_reductionDesc,
133  status,
134  "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: SetAttribute CUDNN_ATTR_REDUCTION_OPERATOR Failed");
135  return std::move(m_reductionDesc);
136  }
137 
138  // Finalizing the descriptor
139  status = cudnnBackendFinalize(m_reductionDesc.pointer->get_backend_descriptor());
140  if (status != CUDNN_STATUS_SUCCESS) {
142  &m_reductionDesc, status, "CUDNN_BACKEND_REDUCTION_DESCRIPTOR: cudnnFinalize Failed");
143  return std::move(m_reductionDesc);
144  }
145 
146  return std::move(m_reductionDesc);
147  }
148 
149  explicit ReductionDescBuilder_v8() = default;
150  ~ReductionDescBuilder_v8() = default;
154  operator=(ReductionDescBuilder_v8 const &) = delete;
155 
156  private:
158 };
159 }
static void set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message)
auto setMathPrecision(cudnnDataType_t data_type_) -> ReductionDescBuilder_v8 &
Set Math Precision Data Type for the Reduction Operation.
ManagedOpaqueDescriptor get_desc() const
Returns a copy of underlying managed descriptor.
ReductionDesc_v8 & operator=(ReductionDesc_v8 const &)=delete
cudnnStatus_t get_status() const
Current status of the descriptor.
const char * get_error() const
Diagonistic error message if any.
std::string describe() const override
Return a string describing the backend Descriptor.
auto setReductionOp(cudnnReduceTensorOp_t op_) -> ReductionDescBuilder_v8 &
Set redution operator for the Reduction Operation.
cudnnStatus_t status
Shared pointer of the OpaqueBackendPointer.