CUDNN API  8
cudnn_frontend_PointWiseDesc.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 {
52  public:
54  std::string
55  describe() const override {
56  std::stringstream ss;
57  char sep = ' ';
58  ss << "CUDNN_BACKEND_POINTWISE_DESCRIPTOR :"
59  << " Mode: " << (mode) << " Math precision " << (math_precision);
60  return ss.str();
61  }
62 
63  int64_t
64  getPortCount() const {
65  switch (mode) {
66  case CUDNN_POINTWISE_ADD:
67  case CUDNN_POINTWISE_MUL:
68  case CUDNN_POINTWISE_MIN:
69  case CUDNN_POINTWISE_MAX:
70  return 3;
71  case CUDNN_POINTWISE_SQRT:
72  case CUDNN_POINTWISE_RELU_FWD:
73  case CUDNN_POINTWISE_TANH_FWD:
74  case CUDNN_POINTWISE_SIGMOID_FWD:
75  case CUDNN_POINTWISE_ELU_FWD:
76  return 2;
77  default:
78  return -1;
79  }
80  }
81 
82  cudnnPointwiseMode_t
83  getPointWiseMode() const {
84  return mode;
85  }
86 
88  : BackendDescriptor(from.get_desc(), from.get_status(), from.get_error()),
90  mode(from.mode),
92  upper_clip(from.upper_clip),
93  lower_clip(from.lower_clip) {}
94 
95  ~PointWiseDesc_v8() = default;
96 
97  private:
98  PointWiseDesc_v8() = default;
99  PointWiseDesc_v8(PointWiseDesc_v8 const &) = delete;
101  operator=(PointWiseDesc_v8 const &) = delete;
102 
103  cudnnDataType_t math_precision = CUDNN_DATA_FLOAT;
104  cudnnPointwiseMode_t mode = CUDNN_POINTWISE_ADD;
105  cudnnNanPropagation_t nan_propagation = CUDNN_NOT_PROPAGATE_NAN;
106  double upper_clip = std::numeric_limits<double>::max();
107  double lower_clip = std::numeric_limits<double>::min();
108 };
109 
114  public:
119  auto
121  setMathPrecision(cudnnDataType_t data_type_) -> PointWiseDescBuilder_v8 & {
122  m_pointWiseDesc.math_precision = data_type_;
123  return *this;
124  }
126  auto
127  setClipping(double l, double u) -> PointWiseDescBuilder_v8 & {
128  m_pointWiseDesc.upper_clip = u;
129  m_pointWiseDesc.lower_clip = l;
130  return *this;
131  }
133  auto
134  setMode(cudnnPointwiseMode_t mode_) -> PointWiseDescBuilder_v8 & {
135  m_pointWiseDesc.mode = mode_;
136  return *this;
137  }
139  auto
140  setMode(cudnnNanPropagation_t nan_mode_) -> PointWiseDescBuilder_v8 & {
141  m_pointWiseDesc.nan_propagation = nan_mode_;
142  return *this;
143  }
149  build() {
150  // Create a descriptor. Memory allocation happens here.
151  auto status = m_pointWiseDesc.initialize_managed_backend_pointer(CUDNN_BACKEND_POINTWISE_DESCRIPTOR);
152  if (status != CUDNN_STATUS_SUCCESS) {
154  &m_pointWiseDesc, status, "CUDNN_BACKEND_POINTWISE_DESCRIPTOR: cudnnCreate Failed");
155  return std::move(m_pointWiseDesc);
156  }
157 
158  // Once Created lets set the descriptor parameters.
159  status = cudnnBackendSetAttribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
160  CUDNN_ATTR_POINTWISE_MODE,
161  CUDNN_TYPE_POINTWISE_MODE,
162  1,
163  &m_pointWiseDesc.mode);
164  if (status != CUDNN_STATUS_SUCCESS) {
166  &m_pointWiseDesc,
167  status,
168  "CUDNN_BACKEND_POINTWISE_DESCRIPTOR: CUDNN_TYPE_POINTWISE_MODE SetAttribute Failed");
169  return std::move(m_pointWiseDesc);
170  }
171 
172  status = cudnnBackendSetAttribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
173  CUDNN_ATTR_POINTWISE_MATH_PREC,
174  CUDNN_TYPE_DATA_TYPE,
175  1,
176  &m_pointWiseDesc.math_precision);
177  if (status != CUDNN_STATUS_SUCCESS) {
179  &m_pointWiseDesc,
180  status,
181  "CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_MATH_PREC Failed");
182  return std::move(m_pointWiseDesc);
183  }
184 
185  if (m_pointWiseDesc.mode == CUDNN_POINTWISE_RELU_FWD) {
186  status = cudnnBackendSetAttribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
187  CUDNN_ATTR_POINTWISE_NAN_PROPAGATION,
188  CUDNN_TYPE_NAN_PROPOGATION,
189  1,
190  &m_pointWiseDesc.nan_propagation);
191  if (status != CUDNN_STATUS_SUCCESS) {
193  &m_pointWiseDesc,
194  status,
195  "CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_NAN_PROPAGATION Failed");
196  return std::move(m_pointWiseDesc);
197  }
198 
199  status = cudnnBackendSetAttribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
200  CUDNN_ATTR_POINTWISE_RELU_LOWER_CLIP,
201  CUDNN_TYPE_DOUBLE,
202  1,
203  &m_pointWiseDesc.lower_clip);
204  if (status != CUDNN_STATUS_SUCCESS) {
206  &m_pointWiseDesc,
207  status,
208  "CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_RELU_LOWER_CLIP, Failed");
209  return std::move(m_pointWiseDesc);
210  }
211 
212  status = cudnnBackendSetAttribute(m_pointWiseDesc.pointer->get_backend_descriptor(),
213  CUDNN_ATTR_POINTWISE_RELU_UPPER_CLIP,
214  CUDNN_TYPE_DOUBLE,
215  1,
216  &m_pointWiseDesc.upper_clip);
217  if (status != CUDNN_STATUS_SUCCESS) {
219  &m_pointWiseDesc,
220  status,
221  "CUDNN_BACKEND_POINTWISE_DESCRIPTOR: SetAttribute CUDNN_ATTR_POINTWISE_RELU_UPPER_CLIP, Failed");
222  return std::move(m_pointWiseDesc);
223  }
224  }
225 
226  // Finalizing the descriptor
227  status = cudnnBackendFinalize(m_pointWiseDesc.pointer->get_backend_descriptor());
228  if (status != CUDNN_STATUS_SUCCESS) {
230  &m_pointWiseDesc, status, "CUDNN_BACKEND_POINTWISE_DESCRIPTOR: cudnnFinalize Failed");
231  return std::move(m_pointWiseDesc);
232  }
233 
234  return std::move(m_pointWiseDesc);
235  }
236 
237  explicit PointWiseDescBuilder_v8() = default;
238  ~PointWiseDescBuilder_v8() = default;
242  operator=(PointWiseDescBuilder_v8 const &) = delete;
243 
244  private:
246 };
247 }
static void set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message)
auto setClipping(double l, double u) -> PointWiseDescBuilder_v8 &
Set upper and lower limits for the RELU activation.
PointWiseDesc_v8 & operator=(PointWiseDesc_v8 const &)=delete
auto setMode(cudnnNanPropagation_t nan_mode_) -> PointWiseDescBuilder_v8 &
Set NaN propagation mode.
cudnnPointwiseMode_t getPointWiseMode() const
ManagedOpaqueDescriptor get_desc() const
Returns a copy of underlying managed descriptor.
std::string describe() const override
Return a string describing the backend Descriptor.
cudnnStatus_t get_status() const
Current status of the descriptor.
const char * get_error() const
Diagonistic error message if any.
auto setMathPrecision(cudnnDataType_t data_type_) -> PointWiseDescBuilder_v8 &
Set Math Precision Data Type for the Convolution Operation.
auto setMode(cudnnPointwiseMode_t mode_) -> PointWiseDescBuilder_v8 &
Set upper and lower limits for the RELU activation.
cudnnStatus_t status
Shared pointer of the OpaqueBackendPointer.