CUDNN Frontend API  8.3.0
cudnn_frontend_MatMulDesc.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  public:
49  friend class MatMulDescBuilder_v8;
50  std::string
51  describe() const override {
52  std::stringstream ss;
53  ss << "CUDNN_BACKEND_MATMUL_DESCRIPTOR :"
54  << " Math precision " << (math_precision);
55  return ss.str();
56  }
57 
58  MatMulDesc_v8(MatMulDesc_v8 &&from) = default;
60  operator= (MatMulDesc_v8 &&from) = default;
61 
62  ~MatMulDesc_v8() = default;
63 
64  private:
65  MatMulDesc_v8() = default;
66  MatMulDesc_v8(MatMulDesc_v8 const &) = delete;
68  operator=(MatMulDesc_v8 const &) = delete;
69 
70  cudnnDataType_t math_precision = CUDNN_DATA_FLOAT;
71 };
72 
77  public:
82  auto
84  setMathPrecision(cudnnDataType_t data_type_) -> MatMulDescBuilder_v8 & {
85  m_matMulDesc.math_precision = data_type_;
86  return *this;
87  }
90  MatMulDesc_v8 &&
93  build() {
94  // Create a descriptor. Memory allocation happens here.
95  auto status = m_matMulDesc.initialize_managed_backend_pointer(CUDNN_BACKEND_MATMUL_DESCRIPTOR);
96  if (status != CUDNN_STATUS_SUCCESS) {
97  set_error_and_throw_exception(&m_matMulDesc, status, "CUDNN_BACKEND_MATMUL_DESCRIPTOR: cudnnCreate Failed");
98  return std::move(m_matMulDesc);
99  }
100 
101  // Once Created lets set the descriptor parameters.
102  status = cudnnBackendSetAttribute(m_matMulDesc.pointer->get_backend_descriptor(),
103  CUDNN_ATTR_MATMUL_COMP_TYPE,
104  CUDNN_TYPE_DATA_TYPE,
105  1,
106  &m_matMulDesc.math_precision);
107  if (status != CUDNN_STATUS_SUCCESS) {
109  &m_matMulDesc,
110  status,
111  "CUDNN_BACKEND_MATMUL_DESCRIPTOR: SetAttribute CUDNN_ATTR_MATMUL_COMP_TYPE Failed");
112  return std::move(m_matMulDesc);
113  }
114 
115  // Finalizing the descriptor
116  status = cudnnBackendFinalize(m_matMulDesc.pointer->get_backend_descriptor());
117  if (status != CUDNN_STATUS_SUCCESS) {
118  set_error_and_throw_exception(&m_matMulDesc, status, "CUDNN_BACKEND_MATMUL_DESCRIPTOR: cudnnFinalize Failed");
119  return std::move(m_matMulDesc);
120  }
121 
122  getLogger() << "[cudnn_frontend] " << m_matMulDesc << std::endl;
123  return std::move(m_matMulDesc);
124  }
125 
126  explicit MatMulDescBuilder_v8() = default;
127  ~MatMulDescBuilder_v8() = default;
129  MatMulDescBuilder_v8(MatMulDescBuilder_v8 const &) = delete;
131  operator=(MatMulDescBuilder_v8 const &) = delete;
132 
133  private:
135 };
136 }
ConditionalStreamer & getLogger()
static void set_error_and_throw_exception(BackendDescriptor const *desc, cudnnStatus_t status, const char *message)
std::string describe() const override
Return a string describing the backend Descriptor.
auto setMathPrecision(cudnnDataType_t data_type_) -> MatMulDescBuilder_v8 &
Set Math Precision Data Type for the Matmul Operation.
MatMulDesc_v8 & operator=(MatMulDesc_v8 &&from)=default
cudnnStatus_t status
Shared pointer of the OpaqueBackendPointer.