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