LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
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_FULLY_CONNECTED_HPP_INCLUDED
28 #define LBANN_LAYERS_LEARNING_FULLY_CONNECTED_HPP_INCLUDED
29 
31 #include "lbann/models/model.hpp"
32 
33 #include <string>
34 
35 namespace lbann {
36 
55 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
56 class fully_connected_layer : public data_type_layer<TensorDataType>
57 {
58 public:
60 
63  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
64 
67 
70 
72 
73 public:
75  fully_connected_layer(int output_size,
76  bool transpose = false,
77  WeightsType* weight = nullptr,
78  bool has_bias = true);
79 
81 
83 
84  ~fully_connected_layer() override;
85 
86  fully_connected_layer* copy() const override
87  {
88  return new fully_connected_layer(*this);
89  }
90 
91  std::string get_type() const override { return "fully connected"; }
92  data_layout get_data_layout() const override { return T_layout; }
93  El::Device get_device_allocation() const override { return Dev; }
94  bool can_run_inplace() const override { return false; }
95  int get_backprop_requirements() const override
96  {
98  }
99 
100 #ifdef LBANN_HAS_ONNX
101  void fill_onnx_node(onnx::GraphProto& graph) const override;
102 #endif // LBANN_HAS_ONNX
103 
104  description get_description() const override;
105 
107 
109  template <typename ArchiveT>
110  void serialize(ArchiveT& ar);
111 
113 
114 protected:
116  void write_specific_proto(lbann_data::Layer& proto) const final;
117 
118  friend class cereal::access;
120 
121  void setup_data(size_t max_mini_batch_size) override;
122 
123  void fp_compute() override;
124  void bp_compute() override;
125 
126 private:
130  TensorDataType m_bias_scaling_factor;
131 
137 
140 
143  {
144  if (m_bias_gradient != nullptr)
145  delete m_bias_gradient;
146  }
147 
148  template <typename U>
150  template <typename U>
152 };
153 
154 // Builder function
155 LBANN_DEFINE_LAYER_BUILDER(fully_connected);
156 
157 #ifndef LBANN_FULLY_CONNECTED_LAYER_INSTANTIATE
158 
159 #define PROTO_DEVICE(T, Device) \
160  extern template class fully_connected_layer<T, \
161  data_layout::DATA_PARALLEL, \
162  Device>; \
163  extern template class fully_connected_layer<T, \
164  data_layout::MODEL_PARALLEL, \
165  Device>
166 
168 #undef PROTO_DEVICE
169 
170 #endif // LBANN_FULLY_CONNECTED_LAYER_INSTANTIATE
171 
172 } // namespace lbann
173 
174 #endif // LBANN_LAYERS_LEARNING_FULLY_CONNECTED_HPP_INCLUDED
friend void fp_compute_impl(fully_connected_layer< U, T_layout, Dev > &l)
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)
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
friend void bp_compute_impl(fully_connected_layer< U, T_layout, Dev > &l)
description get_description() const override
Human-readable description.
fully_connected_layer & operator=(const fully_connected_layer &other)
Generates nicely formatted description messages.
Definition: description.hpp:49
std::string get_type() const override
Get the layer type&#39;s name.
AbsDistMatrixType * m_bias_gradient
constexpr El::Device Device
void setup_data(size_t max_mini_batch_size) override
Setup layer data. Called by the &#39;setup&#39; function. Memory is allocated for distributed matrices...
void write_specific_proto(lbann_data::Layer &proto) const final
void serialize(ArchiveT &ar)
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
void fp_compute() override
Apply layer operation. Called by the &#39;forward_prop&#39; function. Given the input tensors, the output tensors are populated with computed values.
fully_connected_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
LBANN_DEFINE_LAYER_BUILDER(elu)
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Affine transformation.
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.