LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
l2_norm2.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_L2_NORM2_HPP_INCLUDED
28 #define LBANN_LAYERS_LOSS_L2_NORM2_HPP_INCLUDED
29 
32 #include "lbann/proto/layers.pb.h"
33 
34 namespace lbann {
35 
40 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
41 class l2_norm2_layer : public data_type_layer<TensorDataType>
42 {
43 public:
45 
48  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
49 
51 
52 public:
53  l2_norm2_layer(lbann_comm* comm) : data_type_layer<TensorDataType>(comm) {}
54 
56  : data_type_layer<TensorDataType>(other),
57  m_workspace(other.m_workspace ? other.m_workspace->Copy() : nullptr)
58  {}
60  {
62  m_workspace.reset(other.m_workspace ? other.m_workspace->Copy() : nullptr);
63  return *this;
64  }
65 
66  l2_norm2_layer* copy() const override { return new l2_norm2_layer(*this); }
67 
69 
71  template <typename ArchiveT>
72  void serialize(ArchiveT& ar);
73 
75 
76  std::string get_type() const override { return "L2 norm squared"; }
77  data_layout get_data_layout() const override { return T_layout; }
78  El::Device get_device_allocation() const override { return Dev; }
79  bool can_run_inplace() const override { return false; }
80  int get_backprop_requirements() const override
81  {
83  }
84 
85  void setup_dims() override
86  {
88  this->set_output_dims({1});
89  }
90 
91  void setup_data(size_t max_mini_batch_size) override
92  {
94 
95  // Initialize workspace
96  auto dist = this->get_prev_activations().DistData();
97  dist.colDist = El::STAR;
98  m_workspace.reset(AbsDistMatrixType::Instantiate(dist));
99 #ifdef HYDROGEN_HAVE_CUB
100  if (m_workspace->GetLocalDevice() == El::Device::GPU) {
101  m_workspace->Matrix().SetMemoryMode(1); // CUB memory pool
102  }
103 #endif // HYDROGEN_HAVE_CUB
104  }
105 
106  void fp_compute() override;
107 
108  void bp_compute() override;
109 
110 protected:
112  void write_specific_proto(lbann_data::Layer& proto) const final;
113 
114  friend class cereal::access;
116 
117 private:
119  void local_fp_compute();
121  void local_bp_compute();
122 
124  std::unique_ptr<AbsDistMatrixType> m_workspace;
125 };
126 
127 #ifndef LBANN_L2_NORM2_LAYER_INSTANTIATE
128 
129 #define PROTO_DEVICE(T, Device) \
130  extern template class l2_norm2_layer<T, data_layout::DATA_PARALLEL, Device>; \
131  extern template class l2_norm2_layer<T, data_layout::MODEL_PARALLEL, Device>
132 
134 #undef PROTO_DEVICE
135 
136 #endif // LBANN_L2_NORM2_LAYER_INSTANTIATE
137 
138 } // namespace lbann
139 
140 #endif // LBANN_LAYERS_LOSS_L2_NORM2_HPP_INCLUDED
l2_norm2_layer(lbann_comm *comm)
Definition: l2_norm2.hpp:53
virtual void setup_dims()
Setup tensor dimensions Called by the &#39;setup&#39; function. If there are any input tensors, the base method sets all uninitialized output tensor dimensions equal to the first input tensor dimensions.
std::string get_type() const override
Get the layer type&#39;s name.
Definition: l2_norm2.hpp:76
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
Definition: l2_norm2.hpp:78
std::unique_ptr< AbsDistMatrixType > m_workspace
Definition: l2_norm2.hpp:124
l2_norm2_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: l2_norm2.hpp:66
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.
constexpr El::Device Device
InputAbsDistMatrixType & get_prev_activations(int parent_index=0)
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
l2_norm2_layer(const l2_norm2_layer &other)
Definition: l2_norm2.hpp:55
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
El::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
Definition: l2_norm2.hpp:48
Square of L2 vector norm.
Definition: l2_norm2.hpp:41
void serialize(ArchiveT &ar)
void write_specific_proto(lbann_data::Layer &proto) const final
friend class cereal::access
Definition: l2_norm2.hpp:114
void setup_dims() override
Setup tensor dimensions Called by the &#39;setup&#39; function. If there are any input tensors, the base method sets all uninitialized output tensor dimensions equal to the first input tensor dimensions.
Definition: l2_norm2.hpp:85
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
l2_norm2_layer & operator=(const l2_norm2_layer &other)
Definition: l2_norm2.hpp:59
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: l2_norm2.hpp:79
void setup_data(size_t max_mini_batch_size) override
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...
Definition: l2_norm2.hpp:91
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: l2_norm2.hpp:80
data_type_layer & operator=(data_type_layer &&other)=default
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
Definition: l2_norm2.hpp:77