LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
rowwise_weights_norms.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_MISC_ROWWISE_WEIGHTS_NORMS_HPP_INCLUDED
28 #define LBANN_LAYERS_MISC_ROWWISE_WEIGHTS_NORMS_HPP_INCLUDED
29 
32 #include "lbann/proto/layers.pb.h"
35 
36 namespace lbann {
37 
53 template <typename TensorDataType,
55  El::Device Device = El::Device::CPU>
56 class rowwise_weights_norms_layer : public data_type_layer<TensorDataType>
57 {
58 public:
61  default;
63  operator=(const rowwise_weights_norms_layer& other) = default;
64 
65  rowwise_weights_norms_layer* copy() const override;
66 
68 
70  template <typename ArchiveT>
71  void serialize(ArchiveT& ar);
72 
74 
75  std::string get_type() const override;
76  data_layout get_data_layout() const override;
77  El::Device get_device_allocation() const override;
78  bool can_run_inplace() const override { return false; }
79  int get_backprop_requirements() const override
80  {
81  return ERROR_SIGNALS | WEIGHTS;
82  }
83 
84 protected:
86  void write_specific_proto(lbann_data::Layer& proto) const final;
87 
88  void setup_dims() override;
89 
90  void fp_compute() override;
91  void bp_compute() override;
92 
93 private:
94  using LocalMat = El::Matrix<TensorDataType, Device>;
96 
97  static void row_sqsums(const LocalMat& mat, LocalMat& row_sqsums);
98  static void sqrt(LocalMat& mat);
99  static void divide(LocalMat& numer, const LocalMat& denom);
100  static void row_axpy(TensorDataType alpha,
101  const LocalMat& a_vec,
102  const LocalMat& x_mat,
103  TensorDataType beta,
104  LocalMat& y_mat);
105 };
106 
107 template <typename T, data_layout L, El::Device D>
109  lbann_data::Layer& proto) const
110 {
111  proto.set_datatype(proto::ProtoDataType<T>);
112  proto.mutable_rowwise_weights_norms();
113 }
114 
115 #ifndef LBANN_ROWWISE_WEIGHTS_NORMS_LAYER_INSTANTIATE
116 #define PROTO_DEVICE(T, Device) \
117  extern template class rowwise_weights_norms_layer< \
118  T, \
119  data_layout::DATA_PARALLEL, \
120  Device>; \
121  extern template class rowwise_weights_norms_layer< \
122  T, \
123  data_layout::MODEL_PARALLEL, \
124  Device>;
126 #undef PROTO_DEVICE
127 #endif // LBANN_ROWWISE_WEIGHTS_NORMS_LAYER_INSTANTIATE
128 
129 } // namespace lbann
130 
131 #endif // LBANN_LAYERS_MISC_ROWWISE_WEIGHTS_NORMS_HPP_INCLUDED
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
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.
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
std::string get_type() const override
Get the layer type&#39;s name.
static void sqrt(LocalMat &mat)
constexpr El::Device Device
rowwise_weights_norms_layer & operator=(const rowwise_weights_norms_layer &other)=default
static void row_sqsums(const LocalMat &mat, LocalMat &row_sqsums)
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
L2 norm of each row of a weights matrix.
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
static void row_axpy(TensorDataType alpha, const LocalMat &a_vec, const LocalMat &x_mat, TensorDataType beta, LocalMat &y_mat)
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
static void divide(LocalMat &numer, const LocalMat &denom)
El::Matrix< TensorDataType, Device > LocalMat
rowwise_weights_norms_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
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.
void write_specific_proto(lbann_data::Layer &proto) const final