LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
base_convolution.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_LAYERS_LEARNING_BASE_CONVOLUTION_HPP_INCLUDED
28 #define LBANN_LAYERS_LEARNING_BASE_CONVOLUTION_HPP_INCLUDED
29 
31 #include "lbann/layers/layer.hpp"
32 #ifdef LBANN_HAS_DNN_LIB
35 #endif // LBANN_HAS_DNN_LIB
36 #include "lbann/utils/memory.hpp"
37 
38 #include <vector>
39 
40 #ifdef LBANN_HAS_DISTCONV
41 #include "distconv/dnn_backend/convolution.hpp"
42 #include "lbann/utils/distconv.hpp"
43 #endif
44 
45 namespace lbann {
46 
47 #ifdef LBANN_HAS_DISTCONV
48 namespace dc {
49 using Backend = ::distconv::BackendDNNLib;
50 template <typename TensorDataType>
51 using Convolution = ::distconv::Convolution<Backend, TensorDataType>;
52 } // namespace dc
53 
54 template <typename TensorDataType, El::Device Device>
55 class base_convolution_adapter
56  : public data_type_distconv_adapter<TensorDataType>
57 {
58 public:
59  using TensorDevType =
61 
62  base_convolution_adapter(Layer& layer)
63  : data_type_distconv_adapter<TensorDataType>(layer)
64  {}
65  virtual ~base_convolution_adapter() = default;
66 
67  void setup_fp_tensors() override;
68  void setup_bp_tensors() override;
69  void setup_layer(size_t workspace_capacity) override;
70  std::unique_ptr<TensorDevType>
71  setup_error_signals_i(int index) const override;
72 
73  void fp_compute_convolution();
74  void fp_apply_bias();
75 
76  void bp_compute_convolution_data();
77  void bp_compute_convolution_filter();
78 
79  std::unique_ptr<dc::Convolution<TensorDataType>> m_conv;
80  std::unique_ptr<TensorDevType> m_kernel;
81  std::unique_ptr<TensorDevType> m_bias;
82  std::unique_ptr<TensorDevType> m_kernel_gradient;
83  std::unique_ptr<TensorDevType> m_bias_gradient;
84 
85  std::string m_fwd_algo;
86  std::string m_bwd_data_algo;
87  std::string m_bwd_filter_algo;
88 };
89 #endif // LBANN_HAS_DISTCONV
90 
93 template <typename TensorDataType, El::Device Device>
94 class base_convolution_layer : public data_type_layer<TensorDataType>
95 {
96 public:
98 
102 
105 
106  template <El::Device D>
107  using DMatDT = El::Matrix<TensorDataType, D>;
108 
109 #ifdef LBANN_HAS_DNN_LIB
110  using ScalingType = dnn_lib::ScalingParamType<TensorDataType>;
111 #else
112  using ScalingType = TensorDataType;
113 #endif // LBANN_HAS_DNN_LIB
114 
116 
118  const std::vector<int>& get_conv_dims() const { return m_conv_dims; }
119  const std::vector<int>& get_pads() const { return m_pads; }
120  const std::vector<int>& get_strides() const { return m_strides; }
121  const std::vector<int>& get_dilations() const { return m_dilations; }
122 
123 protected:
128  std::vector<int> m_conv_dims;
130  std::vector<int> m_pads;
132  std::vector<int> m_strides;
134  std::vector<int> m_dilations;
140  int m_groups;
141 
146 
147 #ifdef LBANN_HAS_DNN_LIB
148 
152  dnn_lib::dnnMathType_t m_convolution_math_type =
153  dnn_lib::get_default_convolution_math_type();
155  dnn_lib::FilterDescriptor m_kernel_dnn_desc;
157  dnn_lib::ConvolutionDescriptor m_convolution_dnn_desc;
159  dnn_lib::TensorDescriptor m_bias_dnn_desc;
162  m_tensors_dnn_desc;
164  std::unordered_map<int, fwd_conv_alg> m_fwd_dnn_algos;
166  std::unordered_map<int, bwd_data_conv_alg> m_bwd_data_dnn_algos;
168  std::unordered_map<int, bwd_filter_conv_alg> m_bwd_filter_dnn_algos;
169 
170 #endif // LBANN_HAS_DNN_LIB
171 
172 public:
174  base_convolution_layer(int num_data_dims,
175  int output_channels,
176  std::vector<int> conv_dims,
177  std::vector<int> pads,
178  std::vector<int> strides,
179  std::vector<int> dilations,
180  int groups,
181  bool has_bias);
182 
184 
185  base_convolution_layer& operator=(const base_convolution_layer& other);
186 
188 
189 #ifdef LBANN_HAS_DNN_LIB
190  void set_dnn_math_mode(dnn_lib::dnnMathType_t math_type) noexcept;
191 #endif // LBANN_HAS_DNN_LIB
192 
193  description get_description() const override;
194  void setup_dims() override;
195 
199  void setup_data(size_t max_mini_batch_size) override;
200 
202  void setup_gpu() override;
203 
205 
207  template <typename ArchiveT>
208  void serialize(ArchiveT& ar);
209 
211 
212 protected:
214  virtual std::vector<int> get_kernel_dims() const = 0;
215 
217  void apply_convolution_dnn(bool during_forward_prop);
218 
220  void apply_transposed_convolution_dnn(bool during_forward_prop);
221 
222  void apply_bias_dnn();
223  void compute_gradients_dnn(bool using_transposed_convolution);
224 
226  void apply_convolution_im2col(bool during_forward_prop);
227 
229  void apply_transposed_convolution_im2col(bool during_forward_prop);
230 
231  void apply_bias_cpu();
232 
233  void compute_gradients_im2col(bool using_transposed_convolution);
234 
235 private:
236 #ifdef LBANN_HAS_DNN_LIB
237 
240  get_forward_algo_dnn(const int local_mini_batch_size,
241  const dnn_lib::TensorDescriptor& input_desc,
242  const TensorDataType* input,
243  const dnn_lib::FilterDescriptor& kernel_desc,
244  const TensorDataType* kernel,
245  const dnn_lib::ConvolutionDescriptor& conv_desc,
246  const dnn_lib::TensorDescriptor& output_desc,
247  TensorDataType* output,
248  size_t ws_size,
249  TensorDataType* ws);
250 
252  bwd_data_conv_alg get_backward_data_algo_dnn(
253  const int local_mini_batch_size,
254  const dnn_lib::FilterDescriptor& kernel_desc,
255  const TensorDataType* kernel,
256  const dnn_lib::TensorDescriptor& prev_error_signal_desc,
257  const TensorDataType* prev_error_signal,
258  const dnn_lib::ConvolutionDescriptor& conv_desc,
259  const dnn_lib::TensorDescriptor& error_signal_desc,
260  TensorDataType* error_signal,
261  size_t ws_size,
262  TensorDataType* ws);
263 
268  bwd_filter_conv_alg get_backward_filter_algo_dnn(
269  const int local_mini_batch_size,
270  const dnn_lib::TensorDescriptor& input_desc,
271  const TensorDataType* input,
272  const dnn_lib::TensorDescriptor& prev_error_signal_desc,
273  const TensorDataType* prev_error_signal,
274  const dnn_lib::ConvolutionDescriptor& conv_desc,
275  const dnn_lib::FilterDescriptor& kernel_gradient_desc,
276  size_t ws_size,
277  TensorDataType* ws);
278 #endif // LBANN_HAS_DNN_LIB
279 
280 #ifdef LBANN_HAS_DISTCONV
281  friend class base_convolution_adapter<TensorDataType, Device>;
282 
283 protected:
284  using BaseConvAdapterType = base_convolution_adapter<TensorDataType, Device>;
285  void setup_distconv_adapter() override;
286  BaseConvAdapterType& get_distconv_adapter() override;
287  const BaseConvAdapterType& get_distconv_adapter() const override;
288 #endif // LBANN_HAS_DISTCONV
289 };
290 
291 } // namespace lbann
292 #endif // LBANN_LAYERS_LEARNING_BASE_CONVOLUTION_HPP_INCLUDED
const std::vector< int > & get_pads() const
std::vector< int > m_conv_dims
Spatial dimensions for convolution kernel.
void serialize(std::ostream &os, google::protobuf::Message const &msg)
Serialize the protobuf message to a stream.
Generates nicely formatted description messages.
Definition: description.hpp:49
constexpr El::Device Device
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
const std::vector< int > & get_conv_dims() const
Get convolutional layer parameters.
const std::vector< int > & get_dilations() const
fwd_conv_alg
Which forward convolution algorithm to use.
Definition: dnn_enums.hpp:32
El::Matrix< TensorDataType, D > DMatDT
Computation kernels for convolution and deconvolution layers.
const std::vector< int > & get_strides() const
dc::TensorDev< OutputTensorDataType > TensorDevType