LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
mean_squared_error.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_MEAN_SQUARED_ERROR_HPP_INCLUDED
28 #define LBANN_LAYERS_LOSS_MEAN_SQUARED_ERROR_HPP_INCLUDED
29 
32 #include "lbann/proto/layers.pb.h"
33 
34 #ifdef LBANN_HAS_DISTCONV
35 #include "distconv/dnn_backend/mean_squared_error.hpp"
36 #include "lbann/utils/distconv.hpp"
37 #endif
38 
39 namespace lbann {
40 
41 #ifdef LBANN_HAS_DISTCONV
42 namespace dc {
43 using Backend = ::distconv::BackendDNNLib;
44 using MeanSquaredError = ::distconv::MeanSquaredError<Backend>;
45 } // namespace dc
46 
47 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
48 class mean_squared_error_distconv_adapter
49  : public data_type_distconv_adapter<TensorDataType>
50 {
51 public:
52  using TensorDevType =
54  mean_squared_error_distconv_adapter(Layer& layer)
55  : data_type_distconv_adapter<TensorDataType>(layer)
56  {}
57  virtual ~mean_squared_error_distconv_adapter() = default;
58  void setup_distributions(tensor_overlap_constraints& constraints) override;
59  dc::Shape get_prev_activations_shape(int index) const override;
60  dc::Shape get_activations_shape(int index) const override;
61  dc::Shape get_activations_local_shape(int index) const override;
62  void setup_layer(size_t workspace_capacity) override;
63  std::unique_ptr<dc::MeanSquaredError> m_mean_squared_error;
64 };
65 #endif // LBANN_HAS_DISTCONV
66 
75 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
76 class mean_squared_error_layer : public data_type_layer<TensorDataType>
77 {
78 public:
80 
83  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
84 
86 
87 public:
89  : data_type_layer<TensorDataType>(comm)
90  {
91  this->m_expected_num_parent_layers = 2;
92  }
93 
95  : data_type_layer<TensorDataType>(other)
96  {
97  m_workspace.reset(other.m_workspace ? other.m_workspace->Copy() : nullptr);
98  }
99 
101  {
103  m_workspace.reset(other.m_workspace ? other.m_workspace->Copy() : nullptr);
104  return *this;
105  }
106 
107  mean_squared_error_layer* copy() const override
108  {
109  return new mean_squared_error_layer(*this);
110  }
111 
113 
115  template <typename ArchiveT>
116  void serialize(ArchiveT& ar);
117 
119 
120  std::string get_type() const override { return "mean squared error"; }
121  data_layout get_data_layout() const override { return T_layout; }
122  El::Device get_device_allocation() const override { return Dev; }
123  bool can_run_inplace() const override { return false; }
124  int get_backprop_requirements() const override
125  {
127  }
128 
129 #ifdef LBANN_HAS_ONNX
130  void fill_onnx_node(onnx::GraphProto& graph) const override;
131 #endif // LBANN_HAS_ONNX
132 
133  void setup_dims() override;
134 
135  void setup_data(size_t max_mini_batch_size) override;
136 
137  void fp_compute() override;
138 
139  void bp_compute() override;
140 
141 protected:
143  void write_specific_proto(lbann_data::Layer& proto) const final;
144 
145  friend class cereal::access;
147 
148 private:
150  void local_fp_compute();
152  void local_bp_compute();
153 
155  std::unique_ptr<AbsDistMatrixType> m_workspace;
156 
157 #ifdef LBANN_HAS_DISTCONV
158  friend class mean_squared_error_distconv_adapter<TensorDataType,
159  T_layout,
160  Dev>;
161 
162 protected:
163  bool is_distconv_supported() const override
164  {
165  return Dev == El::Device::GPU && T_layout == data_layout::DATA_PARALLEL;
166  }
167 
168  void setup_distconv_adapter() override
169  {
170  this->get_distconv_adapter_ptr() = std::make_unique<
171  mean_squared_error_distconv_adapter<TensorDataType, T_layout, Dev>>(
172  *this);
173  }
174 
175  mean_squared_error_distconv_adapter<TensorDataType, T_layout, Dev>&
176  get_distconv_adapter() override;
177  const mean_squared_error_distconv_adapter<TensorDataType, T_layout, Dev>&
178  get_distconv_adapter() const override;
179 
180  void fp_compute_distconv()
181  {
182  assert_always(this->distconv_enabled());
183  get_distconv_adapter().m_mean_squared_error->forward(
184  this->get_distconv_adapter().get_prev_activations(0),
185  this->get_distconv_adapter().get_prev_activations(1),
186  this->get_distconv_adapter().get_activations());
187  }
188 
189  void bp_compute_distconv()
190  {
191  assert_always(this->distconv_enabled());
192  get_distconv_adapter().m_mean_squared_error->backward(
193  this->get_distconv_adapter().get_prev_activations(0),
194  this->get_distconv_adapter().get_prev_activations(1),
195  this->get_distconv_adapter().get_prev_error_signals(0),
196  this->get_distconv_adapter().get_error_signals(0),
197  this->get_distconv_adapter().get_error_signals(1));
198  }
199 #endif // LBANN_HAS_DISTCONV
200 };
201 
202 #ifndef LBANN_MEAN_SQUARED_ERROR_LAYER_INSTANTIATE
203 
204 #define PROTO_DEVICE(T, Device) \
205  extern template class mean_squared_error_layer<T, \
206  data_layout::DATA_PARALLEL, \
207  Device>; \
208  extern template class mean_squared_error_layer<T, \
209  data_layout::MODEL_PARALLEL, \
210  Device>
211 
213 #undef PROTO_DEVICE
214 
215 #endif // LBANN_MEAN_SQUARED_ERROR_LAYER_INSTANTIATE
216 
217 } // namespace lbann
218 
219 #endif // LBANN_LAYERS_LOSS_MEAN_SQUARED_ERROR_HPP_INCLUDED
mean_squared_error_layer(const mean_squared_error_layer &other)
mean_squared_error_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
std::unique_ptr< AbsDistMatrixType > m_workspace
mean_squared_error_layer & operator=(const mean_squared_error_layer &other)
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.
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
std::string get_type() const override
Get the layer type&#39;s name.
constexpr El::Device Device
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...
::distconv::tensor::Shape Shape
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.
data_type_layer & operator=(data_type_layer &&other)=default
dc::TensorDev< OutputTensorDataType > TensorDevType