LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
cross_entropy.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_LOSS_CROSS_ENTROPY_HPP_INCLUDED
28 #define LBANN_LAYERS_LOSS_CROSS_ENTROPY_HPP_INCLUDED
29 
31 #include "lbann/layers/layer.hpp"
33 #include "lbann/proto/layers.pb.h"
34 
35 #ifdef LBANN_HAS_DISTCONV
36 #include "distconv/dnn_backend/cross_entropy.hpp"
37 #include "lbann/utils/distconv.hpp"
38 #endif
39 
40 namespace lbann {
41 
42 #ifdef LBANN_HAS_DISTCONV
43 namespace dc {
44 using Backend = ::distconv::BackendDNNLib;
45 using CrossEntropy = ::distconv::CrossEntropy<Backend>;
46 } // namespace dc
47 
48 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
49 class cross_entropy_distconv_adapter
50  : public data_type_distconv_adapter<TensorDataType>
51 {
52 public:
53  using TensorDevType =
55  cross_entropy_distconv_adapter(Layer& layer, bool use_labels)
56  : data_type_distconv_adapter<TensorDataType>(layer),
57  m_use_labels(use_labels)
58  {}
59  virtual ~cross_entropy_distconv_adapter() = default;
60  void setup_distributions(tensor_overlap_constraints& constraints) override;
61  dc::Shape get_prev_activations_shape(int index) const override;
62  dc::Shape get_activations_shape(int index) const override;
63  dc::Shape get_activations_local_shape(int index) const override;
64  void setup_layer(size_t workspace_capacity) override;
65  std::unique_ptr<dc::CrossEntropy> m_cross_entropy;
66  bool m_use_labels;
67 };
68 #endif // LBANN_HAS_DISTCONV
69 
76 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
77 class cross_entropy_layer : public data_type_layer<TensorDataType>
78 {
79 public:
81 
84  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
85 
87 
88 public:
89  cross_entropy_layer(lbann_comm* comm, bool use_labels)
90  : data_type_layer<TensorDataType>(comm), m_use_labels(use_labels)
91  {
92  this->m_expected_num_parent_layers = 2;
93  }
94 
96  : data_type_layer<TensorDataType>(other), m_use_labels(other.m_use_labels)
97  {
98  m_workspace.reset(other.m_workspace ? other.m_workspace->Copy() : nullptr);
99  }
100 
102  {
104  m_use_labels = other.m_use_labels;
105  m_workspace.reset(other.m_workspace ? other.m_workspace->Copy() : nullptr);
106  return *this;
107  }
108 
109  cross_entropy_layer* copy() const override
110  {
111  return new cross_entropy_layer(*this);
112  }
113 
115 
117  template <typename ArchiveT>
118  void serialize(ArchiveT& ar);
119 
121 
122  std::string get_type() const override { return "cross entropy"; }
123  data_layout get_data_layout() const override { return T_layout; }
124  El::Device get_device_allocation() const override { return Dev; }
125  bool can_run_inplace() const override { return false; }
126  int get_backprop_requirements() const override
127  {
129  }
130 
131 #ifdef LBANN_HAS_ONNX
132  void fill_onnx_node(onnx::GraphProto& graph) const override;
133 #endif // LBANN_HAS_ONNX
134 
135  void setup_dims() override;
136 
137  void setup_data(size_t max_mini_batch_size) override;
138 
139  void fp_compute() override;
140 
141  void bp_compute() override;
142 
143 protected:
145  void write_specific_proto(lbann_data::Layer& proto) const final;
146 
147  friend class cereal::access;
149 
150 private:
152  void local_fp_compute();
154  void local_bp_compute();
155 
158 
160  std::unique_ptr<AbsDistMatrixType> m_workspace;
161 
162 #ifdef LBANN_HAS_DISTCONV
163  friend class cross_entropy_distconv_adapter<TensorDataType, T_layout, Dev>;
164 
165 protected:
166  bool is_distconv_supported() const override
167  {
168  return Dev == El::Device::GPU && T_layout == data_layout::DATA_PARALLEL;
169  }
170 
171  void setup_distconv_adapter() override
172  {
173  this->get_distconv_adapter_ptr() = std::make_unique<
174  cross_entropy_distconv_adapter<TensorDataType, T_layout, Dev>>(
175  *this,
176  m_use_labels);
177  }
178 
179  cross_entropy_distconv_adapter<TensorDataType, T_layout, Dev>&
180  get_distconv_adapter() override;
181  const cross_entropy_distconv_adapter<TensorDataType, T_layout, Dev>&
182  get_distconv_adapter() const override;
183 
184  void fp_compute_distconv()
185  {
186  assert_always(this->distconv_enabled());
187  get_distconv_adapter().m_cross_entropy->forward(
188  this->get_distconv_adapter().get_prev_activations(0),
189  this->get_distconv_adapter().get_prev_activations(1),
190  this->get_distconv_adapter().get_activations());
191  }
192 
193  void bp_compute_distconv()
194  {
195  assert_always(this->distconv_enabled());
196  get_distconv_adapter().m_cross_entropy->backward(
197  this->get_distconv_adapter().get_prev_activations(0),
198  this->get_distconv_adapter().get_prev_activations(1),
199  this->get_distconv_adapter().get_prev_error_signals(0),
200  this->get_distconv_adapter().get_error_signals(0),
201  this->get_distconv_adapter().get_error_signals(1));
202  }
203 #endif // LBANN_HAS_DISTCONV
204 };
205 
206 #ifndef LBANN_CROSS_ENTROPY_LAYER_INSTANTIATE
207 
208 #define PROTO_DEVICE(T, Device) \
209  extern template class cross_entropy_layer<T, \
210  data_layout::DATA_PARALLEL, \
211  Device>; \
212  extern template class cross_entropy_layer<T, \
213  data_layout::MODEL_PARALLEL, \
214  Device>
215 
217 #undef PROTO_DEVICE
218 
219 #endif // LBANN_CROSS_ENTROPY_LAYER_INSTANTIATE
220 
221 } // namespace lbann
222 
223 #endif // LBANN_LAYERS_LOSS_CROSS_ENTROPY_HPP_INCLUDED
cross_entropy_layer(lbann_comm *comm, bool use_labels)
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
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 get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
void serialize(std::ostream &os, google::protobuf::Message const &msg)
Serialize the protobuf message to a stream.
std::unique_ptr< AbsDistMatrixType > m_workspace
Cross entropy between probability vectors.
constexpr El::Device Device
cross_entropy_layer & operator=(const cross_entropy_layer &other)
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
::distconv::tensor::Shape Shape
cross_entropy_layer(const cross_entropy_layer &other)
cross_entropy_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
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.
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
data_type_layer & operator=(data_type_layer &&other)=default
dc::TensorDev< OutputTensorDataType > TensorDevType