LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
channelwise_fully_connected.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_CHANNELWISE_FULLY_CONNECTED_HPP_INCLUDED
28 #define LBANN_LAYERS_LEARNING_CHANNELWISE_FULLY_CONNECTED_HPP_INCLUDED
29 
31 #ifdef LBANN_HAS_DISTCONV
34 #endif
35 namespace lbann {
36 
37 #ifdef LBANN_HAS_DISTCONV
38 namespace dc {
39 template <typename TensorDataType>
40 using ChannelwiseFullyConnected =
41  ::distconv::ChannelwiseFullyConnected<Backend, TensorDataType>;
42 } // namespace dc
43 
44 template <typename TensorDataType, data_layout Layout, El::Device Device>
45 class channelwise_fully_connected_distconv_adapter
46  : public data_type_distconv_adapter<TensorDataType>
47 {
48 
49 public:
50  using TensorDevType =
52 
53  channelwise_fully_connected_distconv_adapter(Layer& layer)
54  : data_type_distconv_adapter<TensorDataType>(layer)
55  {}
56  virtual ~channelwise_fully_connected_distconv_adapter() = default;
57 
58  void setup_fp_tensors() override;
59  void setup_bp_tensors() override;
60  void setup_distributions(tensor_overlap_constraints& constraints) override;
61  void setup_layer(size_t workspace_capacity) override;
62 
63  void fp_compute();
64  void bp_compute();
65 
66  dc::Shape get_activations_local_shape(int index = 0) const override;
67 
68  std::unique_ptr<dc::ChannelwiseFullyConnected<TensorDataType>>
69  m_linear_operator;
70  std::unique_ptr<TensorDevType> m_linear;
71  std::unique_ptr<TensorDevType> m_bias;
72  std::unique_ptr<TensorDevType> m_linearity_gradient;
73  std::unique_ptr<TensorDevType> m_bias_gradient;
74 }; // class definition channelwise_fully_connected_distconv_adapter
75 
76 #endif // LBANN_HAS_DISTCONV
77 
93 template <typename TensorDataType, data_layout Layout, El::Device Device>
94 class channelwise_fully_connected_layer : public data_type_layer<TensorDataType>
95 {
96 
97  static_assert(Layout == data_layout::DATA_PARALLEL,
98  "channelwise_fully_connected layer "
99  "only supports data parallel layout");
100 
101 public:
109  channelwise_fully_connected_layer(std::vector<size_t> output_channel_dims,
110  bool bias,
111  bool transpose);
112 
114  const channelwise_fully_connected_layer& other) = default;
116  operator=(const channelwise_fully_connected_layer& other) = default;
118 
119  channelwise_fully_connected_layer* copy() const override;
120  std::string get_type() const override;
121  data_layout get_data_layout() const override;
122  El::Device get_device_allocation() const override;
123  bool can_run_inplace() const override { return false; }
124  int get_backprop_requirements() const override
125  {
127  }
128 
129  description get_description() const override;
130 
132 
134  template <typename ArchiveT>
135  void serialize(ArchiveT& ar);
136 
138 
139 protected:
141  void write_specific_proto(lbann_data::Layer& proto) const final;
142 
143  friend class cereal::access;
145 
146  void setup_dims() override;
147  void setup_data(size_t max_mini_batch_size) override;
148 
149  void fp_compute() override;
150  void bp_compute() override;
151  std::vector<int> get_linearity_dims() const;
152  std::vector<int> get_bias_dims() const;
153 
154 #ifdef LBANN_HAS_DISTCONV
155  friend class channelwise_fully_connected_distconv_adapter<TensorDataType,
156  Layout,
157  Device>;
158 
159 protected:
160  void setup_distconv_adapter() override;
161  bool is_distconv_supported() const override;
162  channelwise_fully_connected_distconv_adapter<TensorDataType, Layout, Device>&
163  get_distconv_adapter() override;
164  const channelwise_fully_connected_distconv_adapter<TensorDataType,
165  Layout,
166  Device>&
167  get_distconv_adapter() const override;
168 #endif
169 
170 private:
175 };
176 
177 // Builder function
178 LBANN_DEFINE_LAYER_BUILDER(channelwise_fully_connected);
179 
180 // Explicit template instantiation
181 #ifndef LBANN_CHANNELWISE_FULLY_CONNECTED_LAYER_INSTANTIATE
182 #define PROTO_DEVICE(T, Device) \
183  extern template class channelwise_fully_connected_layer< \
184  T, \
185  data_layout::DATA_PARALLEL, \
186  Device>
188 #undef PROTO_DEVICE
189 #endif // LBANN_CHANNELWISE_FULLY_CONNECTED_LAYER_INSTANTIATE
190 
191 } // namespace lbann
192 
193 #endif // LBANN_LAYERS_LEARNING_CHANNELWISE_FULLY_CONNECTED_HPP_INCLUDED
std::map< El::Int, std::set< El::Int > > transpose(const std::set< El::Int > &nodes, const std::map< El::Int, std::set< El::Int >> &edges)
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
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
constexpr El::Device Device
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Apply affine transformation to tensor channels.
::distconv::tensor::Shape Shape
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
LBANN_DEFINE_LAYER_BUILDER(elu)
dc::TensorDev< OutputTensorDataType > TensorDevType