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 {
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 
59  ReductionDesc_v8(ReductionDesc_v8 &&from) = default;
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;
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 
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  }
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) {
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) {
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) {
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) {
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;
154  operator=(ReductionDescBuilder_v8 const &) = delete;
155 
156  private:
158 };
159 }
ConditionalStreamer & getLogger()
static void set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message)
ReductionDesc_v8 & operator=(ReductionDesc_v8 &&from)=default
auto setMathPrecision(cudnnDataType_t data_type_) -> ReductionDescBuilder_v8 &
Set Math Precision Data Type for the Reduction Operation.
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.