LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
entrywise_scale_bias.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_LAYER_LEARNING_ENTRYWISE_SCALE_BIAS_HPP_INCLUDED
28 #define LBANN_LAYER_LEARNING_ENTRYWISE_SCALE_BIAS_HPP_INCLUDED
29 
31 #include "lbann/models/model.hpp"
33 #include "lbann/proto/layers.pb.h"
35 
36 namespace lbann {
37 
54 template <typename TensorDataType,
56  El::Device Device = El::Device::CPU>
57 class entrywise_scale_bias_layer : public data_type_layer<TensorDataType>
58 {
59 public:
61 
64  using AbsDistMatrixType = El::AbstractDistMatrix<TensorDataType>;
65 
68 
71 
73 
74 public:
75  entrywise_scale_bias_layer(lbann_comm* comm = nullptr);
79 
80  entrywise_scale_bias_layer* copy() const override
81  {
82  return new entrywise_scale_bias_layer(*this);
83  }
84 
85  std::string get_type() const override { return "entry-wise scale/bias"; }
86  data_layout get_data_layout() const override { return Layout; }
87  El::Device get_device_allocation() const override { return Device; }
88  bool can_run_inplace() const override { return true; }
89  int get_backprop_requirements() const override
90  {
92  }
93 
95 
97  template <typename ArchiveT>
98  void serialize(ArchiveT& ar);
99 
101 
102  void setup_data(size_t max_mini_batch_size) override;
103 
104 protected:
106  void write_specific_proto(lbann_data::Layer& proto) const final;
107 
108  void fp_compute() override;
109  void bp_compute() override;
110 
111 private:
113  std::unique_ptr<AbsDistMatrixType> m_weights_gradient;
114 };
115 
116 // Template implementation
117 
118 template <typename T, data_layout L, El::Device D>
120  lbann_data::Layer& proto) const
121 {
122  proto.set_datatype(proto::ProtoDataType<T>);
123  proto.mutable_entrywise_scale_bias();
124 }
125 
126 template <typename TensorDataType, data_layout Layout, El::Device Dev>
129  : data_type_layer<TensorDataType>(comm)
130 {}
131 
132 template <typename TensorDataType, data_layout Layout, El::Device Dev>
135  : data_type_layer<TensorDataType>(other),
137  other.m_weights_gradient ? other.m_weights_gradient->Copy() : nullptr)
138 {}
139 
140 template <typename TensorDataType, data_layout Layout, El::Device Dev>
143 {
145  m_weights_gradient.reset(
146  other.m_weights_gradient ? other.m_weights_gradient->Copy() : nullptr);
147  return *this;
148 }
149 
150 template <typename TensorDataType, data_layout Layout, El::Device Dev>
152  size_t max_mini_batch_size)
153 {
154  data_type_layer<TensorDataType>::setup_data(max_mini_batch_size);
155 
156  // Initialize output dimensions
157  this->set_output_dims(this->get_input_dims());
158  const auto& output_dims_ = this->get_output_dims();
159  std::vector<size_t> output_dims(output_dims_.begin(), output_dims_.end());
160  const auto output_size = this->get_output_size();
161 
162  // Construct default weights if needed
163  // Note: Scale is initialized to 1 and bias to 0
164  if (!this->has_weights()) {
165  auto w = std::make_shared<WeightsType>(*this->get_comm());
166  std::vector<TensorDataType> vals(2 * output_size,
167  El::TypeTraits<TensorDataType>::Zero());
168  std::fill(vals.begin(),
169  vals.begin() + output_size,
170  El::TypeTraits<TensorDataType>::One());
171  auto init = std::make_unique<value_initializer<TensorDataType>>(vals);
172  auto opt = this->m_model->template create_optimizer<TensorDataType>();
173  w->set_name(this->get_name() + "_weights");
174  w->set_initializer(std::move(init));
175  w->set_optimizer(std::move(opt));
176  this->add_weights(w);
177  this->m_model->add_weights(std::move(w));
178  }
179  if (this->num_weights() != 1) {
180  LBANN_ERROR("attempted to setup ",
181  this->get_type(),
182  " layer \"",
183  this->get_name(),
184  "\" ",
185  "with an invalid number of weights ",
186  "(expected 1, found ",
187  this->num_weights(),
188  ")");
189  }
190 
191  // Setup weights
192  auto dist = this->get_prev_activations().DistData();
193  dist.rowDist = El::STAR;
194  this->get_weights(0).set_dims(output_dims, {static_cast<int>(2)});
195  this->get_weights(0).set_matrix_distribution(dist);
196 
197  // Setup gradient w.r.t. weights
198  m_weights_gradient.reset(AbsDistMatrixType::Instantiate(dist));
199  m_weights_gradient->AlignWith(dist);
200  m_weights_gradient->Resize(output_size, 2);
201 }
202 
203 LBANN_DEFINE_LAYER_BUILDER(entrywise_scale_bias);
204 
205 #ifndef LBANN_ENTRYWISE_SCALE_BIAS_LAYER_INSTANTIATE
206 
207 #define PROTO_DEVICE(T, Device) \
208  extern template class entrywise_scale_bias_layer<T, \
209  data_layout::DATA_PARALLEL, \
210  Device>; \
211  extern template class entrywise_scale_bias_layer< \
212  T, \
213  data_layout::MODEL_PARALLEL, \
214  Device>
215 
217 #undef PROTO_DEVICE
218 
219 #endif // LBANN_ENTRYWISE_SCALE_BIAS_LAYER_INSTANTIATE
220 
221 } // namespace lbann
222 
223 #endif // LBANN_LAYER_LEARNING_ENTRYWISE_SCALE_BIAS_HPP_INCLUDED
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::AbstractDistMatrix< TensorDataType > AbsDistMatrixType
The tensor type expected in this object.
void fill(std::istream &is, google::protobuf::Message &msg)
Fill the protobuf message from a binary stream.
std::string get_type() const override
Get the layer type&#39;s name.
lbann_comm * get_comm() const
entrywise_scale_bias_layer & operator=(const entrywise_scale_bias_layer &other)
#define LBANN_ERROR(...)
Definition: exception.hpp:37
std::unique_ptr< AbsDistMatrixType > m_weights_gradient
Objective function gradient w.r.t. weights.
int get_output_size(size_t output_index=0) const
Get output tensor size.
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
entrywise_scale_bias_layer(lbann_comm *comm=nullptr)
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::vector< int > get_input_dims(size_t input_index=0) const
Get input tensor dimensions.
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
entrywise_scale_bias_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
void add_weights(OwningWeightsPtr &&w)
Add weights to model.
constexpr El::Device Device
InputAbsDistMatrixType & get_prev_activations(int parent_index=0)
weights const & get_weights(size_t idx) const
void set_name(std::string name)
Metadata Accessors.
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
size_t num_weights() const noexcept
Definition: layer.hpp:727
bool has_weights() const noexcept
Definition: layer.hpp:728
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 set_matrix_distribution(El::DistData dist)
std::string get_name() const
Get the layer instance&#39;s name.
Definition: layer.hpp:332
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
void set_dims(std::vector< size_t > matrix_height_dims, std::vector< size_t > matrix_width_dims={})
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...
LBANN_DEFINE_LAYER_BUILDER(elu)
Apply entry-wise scale and bias.
std::vector< int > get_output_dims(size_t output_index=0) const
Get output tensor dimensions.
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
void add_weights(ViewingWeightsPtr w)
Definition: layer.hpp:723
data_type_layer & operator=(data_type_layer &&other)=default
void write_specific_proto(lbann_data::Layer &proto) const final
model * m_model
Reference to model managing this layer.
Definition: layer.hpp:845