LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
layers/learning/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_CONVOLUTION_HPP_INCLUDED
28 #define LBANN_LAYERS_LEARNING_CONVOLUTION_HPP_INCLUDED
29 
31 #include "lbann/utils/distconv.hpp"
33 
34 namespace lbann {
35 
36 #ifdef LBANN_HAS_DISTCONV
37 template <typename TensorDataType, data_layout Layout, El::Device Device>
38 class convolution_distconv_adapter
39  : public base_convolution_adapter<TensorDataType, Device>
40 {
41 public:
42  using TensorDevType =
43  typename base_convolution_adapter<TensorDataType, Device>::TensorDevType;
44 
45  convolution_distconv_adapter(Layer& layer)
46  : base_convolution_adapter<TensorDataType, Device>(layer)
47  {}
48  virtual ~convolution_distconv_adapter() = default;
49 
50  void setup_distributions(tensor_overlap_constraints& constraints) override;
51  void setup_layer(size_t workspace_capacity) override;
52  dc::Shape get_activations_local_shape(int index = 0) const override;
53 };
54 #endif // LBANN_HAS_DISTCONV
55 
67 template <typename TensorDataType,
69  El::Device Device = El::Device::CPU>
70 class convolution_layer : public base_convolution_layer<TensorDataType, Device>
71 {
72 
73  static_assert(Layout == data_layout::DATA_PARALLEL,
74  "convolution layer only supports DATA_PARALLEL");
75 
76 public:
77  convolution_layer(int num_data_dims,
78  int num_output_channels,
79  int conv_dim,
80  int pad,
81  int stride,
82  int dilation,
83  int groups,
84  bool has_bias = true);
85 
86  convolution_layer(int num_data_dims,
87  int num_output_channels,
88  std::vector<int> conv_dims,
89  std::vector<int> pads,
90  std::vector<int> strides,
91  std::vector<int> dilations,
92  int groups,
93  bool has_bias = true);
94 
95  convolution_layer* copy() const override
96  {
97  return new convolution_layer(*this);
98  }
99 
100  std::string get_type() const override { return "convolution"; }
101 
102  data_layout get_data_layout() const override { return Layout; }
103 
104  El::Device get_device_allocation() const override { return Device; }
105 
106  bool can_run_inplace() const override { return false; }
107 
108  int get_backprop_requirements() const override
109  {
111  }
112 
113 #ifdef LBANN_HAS_ONNX
114  std::string get_onnx_op_type() const override { return "Conv"; }
115  void fill_onnx_node(onnx::GraphProto& graph) const override;
116 #endif // LBANN_HAS_ONNX
117 
119 
121  template <typename ArchiveT>
122  void serialize(ArchiveT& ar);
123 
125 
126 protected:
128  void write_specific_proto(lbann_data::Layer& proto) const final;
129 
130  friend class cereal::access;
132 
133  void setup_dims() override;
134  std::vector<int> get_kernel_dims() const override;
135  void fp_compute() override;
136  void bp_compute() override;
137 
138 #ifdef LBANN_HAS_DISTCONV
139  friend class convolution_distconv_adapter<TensorDataType, Layout, Device>;
140 
141 protected:
142  void setup_distconv_adapter() override;
143  bool is_distconv_supported() const override;
144 #endif // LBANN_HAS_DISTCONV
145 };
146 
147 // Builder function
148 LBANN_DEFINE_LAYER_BUILDER(convolution);
149 
150 #ifndef LBANN_CONVOLUTION_LAYER_INSTANTIATE
151 
152 #define PROTO_DEVICE(T, Device) \
153  extern template class convolution_layer<T, \
154  data_layout::DATA_PARALLEL, \
155  Device>;
156 
158 #undef PROTO_DEVICE
159 
160 #endif // LBANN_CONVOLUTION_LAYER_INSTANTIATE
161 
162 } // namespace lbann
163 
164 #endif // LBANN_LAYERS_LEARNING_CONVOLUTION_HPP_INCLUDED
std::basic_string< T > pad(const std::basic_string< T > &s, typename std::basic_string< T >::size_type n, T c)
Definition: file_utils.hpp:93
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
convolution_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
void serialize(std::ostream &os, google::protobuf::Message const &msg)
Serialize the protobuf message to a stream.
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
constexpr El::Device Device
::distconv::tensor::Shape Shape
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
std::string get_type() const override
Get the layer type&#39;s name.
LBANN_DEFINE_LAYER_BUILDER(elu)
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
Computation kernels for convolution and deconvolution layers.