LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
miopen.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_UTILS_DNN_LIB_MIOPEN_HPP
28 #define LBANN_UTILS_DNN_LIB_MIOPEN_HPP
29 
31 
32 #ifdef LBANN_HAS_MIOPEN
33 
34 #include <miopen/miopen.h>
35 
36 // Error utility macros
37 #define CHECK_MIOPEN_NODEBUG(miopen_call) \
38  do { \
39  const miopenStatus_t status_CHECK_MIOPEN = (miopen_call); \
40  if (status_CHECK_MIOPEN != miopenStatusSuccess) { \
41  LBANN_ERROR("MIOpen error (", \
42  miopenGetErrorString(status_CHECK_MIOPEN), \
43  ")"); \
44  } \
45  } while (0)
46 #define CHECK_MIOPEN_DEBUG(miopen_call) \
47  do { \
48  LBANN_ROCM_CHECK_LAST_ERROR(true); \
49  CHECK_MIOPEN_NODEBUG(miopen_call); \
50  } while (0)
51 #ifdef LBANN_DEBUG
52 #define CHECK_MIOPEN(miopen_call) CHECK_MIOPEN_DEBUG(miopen_call)
53 #else
54 #define CHECK_MIOPEN(miopen_call) CHECK_MIOPEN_NODEBUG(miopen_call)
55 #endif // #ifdef LBANN_DEBUG
56 
57 #define CHECK_MIOPEN_DTOR(miopen_call) \
58  try { \
59  CHECK_MIOPEN(miopen_call); \
60  } \
61  catch (std::exception const& e) { \
62  std::cerr << "Caught exception:\n\n what(): " << e.what() \
63  << "\n\nCalling std::terminate() now." << std::endl; \
64  std::terminate(); \
65  } \
66  catch (...) { \
67  std::cerr << "Caught something that isn't an std::exception.\n\n" \
68  << "Calling std::terminate() now." << std::endl; \
69  std::terminate(); \
70  }
71 
72 namespace lbann {
73 
74 // Forward declaration
75 class Layer;
76 
77 namespace miopen {
78 
79 using dnnHandle_t = miopenHandle_t;
80 using dnnDataType_t = miopenDataType_t;
81 using dnnTensorDescriptor_t = miopenTensorDescriptor_t;
82 using dnnFilterDescriptor_t = miopenTensorDescriptor_t;
83 using dnnTensorFormat_t = miopenTensorLayout_t;
84 using dnnDropoutDescriptor_t = miopenDropoutDescriptor_t;
85 using dnnRNGType_t = miopenRNGType_t;
86 using dnnRNNDescriptor_t = miopenRNNDescriptor_t;
87 using dnnRNNAlgo_t = miopenRNNAlgo_t;
88 using dnnRNNMode_t = miopenRNNMode_t;
89 using dnnRNNBiasMode_t = miopenRNNBiasMode_t;
90 using dnnDirectionMode_t = miopenRNNDirectionMode_t;
91 using dnnRNNInputMode_t = miopenRNNInputMode_t;
92 using dnnMathType_t = int;
93 using dnnRNNDataDescriptor_t = int;
94 using dnnRNNDataLayout_t = int;
95 using dnnConvolutionDescriptor_t = miopenConvolutionDescriptor_t;
96 using dnnConvolutionMode_t = miopenConvolutionMode_t;
97 using dnnActivationDescriptor_t = miopenActivationDescriptor_t;
98 using dnnActivationMode_t = miopenActivationMode_t;
99 using dnnNanPropagation_t = miopenNanPropagation_t;
100 using dnnPoolingDescriptor_t = miopenPoolingDescriptor_t;
101 using dnnPoolingMode_t = miopenPoolingMode_t;
102 using dnnLRNDescriptor_t = miopenLRNDescriptor_t;
103 using dnnLRNMode_t = miopenLRNMode_t;
104 using dnnConvolutionFwdAlgo_t = miopenConvFwdAlgorithm_t;
105 using dnnConvolutionBwdDataAlgo_t = miopenConvBwdDataAlgorithm_t;
106 using dnnConvolutionBwdFilterAlgo_t = miopenConvBwdWeightsAlgorithm_t;
107 
108 constexpr dnnConvolutionMode_t DNN_CROSS_CORRELATION = miopenConvolution;
109 constexpr dnnNanPropagation_t DNN_PROPAGATE_NAN = MIOPEN_PROPAGATE_NAN;
110 constexpr dnnMathType_t DNN_DEFAULT_MATH = 0;
111 constexpr dnnTensorFormat_t DNN_TENSOR_NCHW = miopenTensorNCHW;
112 constexpr dnnRNGType_t DNN_RNG_PSEUDO_XORWOW = MIOPEN_RNG_PSEUDO_XORWOW;
113 constexpr dnnLRNMode_t DNN_LRN_CROSS_CHANNEL = miopenLRNCrossChannel;
114 constexpr dnnMathType_t DNN_TENSOR_OP_MATH_ALLOW_CONVERSION =
115  -1; // not supported with ROCm
116 
118 // Functions for to/from MIOpen types conversion
120 
123 inline miopenConvFwdAlgorithm_t to_miopen(fwd_conv_alg a)
124 {
125  switch (a) {
127  return miopenConvolutionFwdAlgoImplicitGEMM;
128  case fwd_conv_alg::GEMM:
129  return miopenConvolutionFwdAlgoGEMM;
131  return miopenConvolutionFwdAlgoDirect;
132  case fwd_conv_alg::FFT:
133  return miopenConvolutionFwdAlgoFFT;
135  return miopenConvolutionFwdAlgoWinograd;
136  default:
137  LBANN_ERROR("Invalid forward convolution algorithm requested.");
138  }
139 }
140 
143 inline fwd_conv_alg from_miopen(miopenConvFwdAlgorithm_t a)
144 {
145  switch (a) {
146  case miopenConvolutionFwdAlgoGEMM:
147  return fwd_conv_alg::GEMM;
148  case miopenConvolutionFwdAlgoImplicitGEMM:
150  case miopenConvolutionFwdAlgoDirect:
151  return fwd_conv_alg::DIRECT;
152  case miopenConvolutionFwdAlgoFFT:
153  return fwd_conv_alg::FFT;
154  case miopenConvolutionFwdAlgoWinograd:
155  return fwd_conv_alg::WINOGRAD;
156  default:
157  std::ostringstream err;
158  err << "miopenConvFwdAlgorithm_t " << a << " is not supported by LBANN.";
159  LBANN_ERROR(err.str());
160  }
161 }
162 
165 inline miopenConvBwdDataAlgorithm_t to_miopen(bwd_data_conv_alg a)
166 {
167  switch (a) {
169  return miopenConvolutionBwdDataAlgoGEMM;
171  return miopenConvolutionBwdDataAlgoDirect;
173  return miopenConvolutionBwdDataAlgoFFT;
175  return miopenConvolutionBwdDataAlgoWinograd;
177  return miopenConvolutionBwdDataAlgoWinograd;
179  return miopenConvolutionBwdDataAlgoImplicitGEMM;
180  default:
181  LBANN_ERROR("Invalid backward convolution algorithm requested.");
182  }
183 }
184 
187 inline bwd_data_conv_alg from_miopen(miopenConvBwdDataAlgorithm_t a)
188 {
189  switch (a) {
190  case miopenConvolutionBwdDataAlgoGEMM:
192  case miopenConvolutionBwdDataAlgoDirect:
194  case miopenConvolutionBwdDataAlgoFFT:
195  return bwd_data_conv_alg::FFT;
196  case miopenConvolutionBwdDataAlgoWinograd:
198  case miopenConvolutionBwdDataAlgoImplicitGEMM:
200  default:
201  std::ostringstream err;
202  err << "miopenConvBwdDataAlgorithm_t " << a
203  << " is not supported by LBANN.";
204  LBANN_ERROR(err.str());
205  }
206 }
207 
211 inline miopenConvBwdWeightsAlgorithm_t to_miopen(bwd_filter_conv_alg a)
212 {
213  switch (a) {
215  return miopenConvolutionBwdWeightsAlgoGEMM;
217  return miopenConvolutionBwdWeightsAlgoDirect;
219  return miopenConvolutionBwdWeightsAlgoWinograd;
221  return miopenConvolutionBwdWeightsAlgoImplicitGEMM;
222  default:
223  LBANN_ERROR("Invalid backward convolution filter requested.");
224  }
225 }
226 
229 inline bwd_filter_conv_alg from_miopen(miopenConvBwdWeightsAlgorithm_t a)
230 {
231  switch (a) {
232  case miopenConvolutionBwdWeightsAlgoGEMM:
234  case miopenConvolutionBwdWeightsAlgoDirect:
236  case miopenConvolutionBwdWeightsAlgoWinograd:
238  case miopenConvolutionBwdWeightsAlgoImplicitGEMM:
240  default:
241  std::ostringstream err;
242  err << "miopenConvBwdWeightsAlgorithm_t " << a
243  << " is not supported by LBANN.";
244  LBANN_ERROR(err.str());
245  }
246 }
247 
249 inline miopenPoolingMode_t to_miopen(pooling_mode m)
250 {
251  const int rank = lbann::get_rank_in_world();
252  switch (m) {
253  case pooling_mode::MAX:
254  return miopenPoolingMax;
255 #ifdef LBANN_DETERMINISTIC
256  if (rank == 0) {
257  LBANN_WARNING("Deterministic max pooling mode not supported in MIOpen");
258  }
259  return miopenPoolingMax;
260 #else
261  return miopenPoolingMax;
262 #endif // LBANN_DETERMINISTIC
264  return miopenPoolingAverageInclusive;
266  return miopenPoolingAverage;
268  if (rank == 0) {
269  LBANN_WARNING("Deterministic max pooling mode not supported in MIOpen");
270  }
271  return miopenPoolingMax;
272  default:
273  LBANN_ERROR("Invalid pooling mode requested");
274  }
275 }
276 
277 inline pooling_mode from_miopen(miopenPoolingMode_t m)
278 {
279  switch (m) {
280  case miopenPoolingMax:
281  return pooling_mode::MAX;
282  case miopenPoolingAverageInclusive:
284  case miopenPoolingAverage:
286  default:
287  LBANN_ERROR("Invalid pooling mode requested.");
288  }
289 }
290 
292 inline miopenSoftmaxMode_t to_miopen(softmax_mode m)
293 {
294  switch (m) {
296  return MIOPEN_SOFTMAX_MODE_INSTANCE;
298  return MIOPEN_SOFTMAX_MODE_CHANNEL;
300  default:
301  LBANN_ERROR("Invalid softmax mode requested.");
302  }
303 }
304 
306 inline miopenSoftmaxAlgorithm_t to_miopen(softmax_alg alg)
307 {
308  switch (alg) {
309  case softmax_alg::FAST:
310  return MIOPEN_SOFTMAX_FAST;
312  return MIOPEN_SOFTMAX_ACCURATE;
313  case softmax_alg::LOG:
314  return MIOPEN_SOFTMAX_LOG;
315  default:
316  LBANN_ERROR("Invalid softmax algorithm requested.");
317  }
318 }
319 
320 } // namespace miopen
321 } // namespace lbann
322 
323 #endif // LBANN_HAS_MIOPEN
324 #endif // LBANN_UTILS_DNN_LIB_MIOPEN_HPP
Position-wise softmax.
#define LBANN_ERROR(...)
Definition: exception.hpp:37
Sample-wise softmax.
bwd_data_conv_alg
Which backward convolution algorithm to use.
Definition: dnn_enums.hpp:45
bwd_filter_conv_alg
Which backward convolution filter algorithm to use.
Definition: dnn_enums.hpp:57
int get_rank_in_world()
softmax_alg
Internal LBANN names for supported softmax algorithms.
Definition: dnn_enums.hpp:110
pooling_mode
Which pooling mode to use.
Definition: dnn_enums.hpp:78
fwd_conv_alg
Which forward convolution algorithm to use.
Definition: dnn_enums.hpp:32
#define LBANN_WARNING(...)
Definition: exception.hpp:53
softmax_mode
Which tensor dimensions to apply softmax over.
Definition: dnn_enums.hpp:87