LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
hadamard.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_HADAMARD_HPP_INCLUDED
28 #define LBANN_LAYER_HADAMARD_HPP_INCLUDED
29 
32 #include <vector>
33 
34 namespace lbann {
35 
37 template <typename TensorDataType,
39  El::Device Dev = El::Device::CPU>
40 class hadamard_layer : public data_type_layer<TensorDataType>
41 {
42 public:
43  hadamard_layer(lbann_comm* comm) : data_type_layer<TensorDataType>(comm)
44  {
45  this->m_expected_num_parent_layers = -1; // No limit on parents
46  }
47 
48  hadamard_layer* copy() const override { return new hadamard_layer(*this); }
49 
51 
53  template <typename ArchiveT>
54  void serialize(ArchiveT& ar);
55 
57 
58  std::string get_type() const override { return "Hadamard"; }
59  data_layout get_data_layout() const override { return T_layout; }
60  El::Device get_device_allocation() const override { return Dev; }
61  bool can_run_inplace() const override { return true; }
62  int get_backprop_requirements() const override
63  {
64  if (this->get_num_parents() > 1)
66  return ERROR_SIGNALS;
67  }
68 
69 protected:
71  void write_specific_proto(lbann_data::Layer& proto) const final;
72 
73  friend class cereal::access;
75 
76  void setup_pointers() override
77  {
79  if (this->get_num_parents() < 1) {
80  std::stringstream err;
81  err << get_type() << " layer \"" << this->get_name() << "\" "
82  << "has no parent layers";
83  LBANN_ERROR(err.str());
84  }
85  }
86 
87  void setup_dims() override
88  {
90  this->set_output_dims(this->get_input_dims());
91 
92  // Check that input dimensions match
93  const auto& output_dims = this->get_output_dims();
94  for (int i = 0; i < this->get_num_parents(); ++i) {
95  if (this->get_input_dims(i) != output_dims) {
96  const auto& parents = this->get_parent_layers();
97  std::stringstream err;
98  err << get_type() << " layer \"" << this->get_name() << "\" "
99  << "has input tensors with incompatible dimensions (";
100  for (int j = 0; j < this->get_num_parents(); ++j) {
101  const auto& dims = this->get_input_dims(j);
102  err << (j > 0 ? ", " : "") << "layer \"" << parents[j]->get_name()
103  << "\" outputs ";
104  for (size_t k = 0; k < dims.size(); ++k) {
105  err << (k > 0 ? " x " : "") << dims[k];
106  }
107  }
108  err << ")";
109  LBANN_ERROR(err.str());
110  }
111  }
112  }
113 
114  void fp_compute() override
115  {
116  auto& output = this->get_activations();
117  switch (this->get_num_parents()) {
118  case 0:
119  El::Fill(output, El::TypeTraits<TensorDataType>::One());
120  break;
121  case 1:
122  El::LockedView(output, this->get_prev_activations());
123  break;
124  default:
125  El::Hadamard(this->get_prev_activations(0),
126  this->get_prev_activations(1),
127  output);
128  for (int i = 2; i < this->get_num_parents(); ++i) {
129  El::Hadamard(this->get_prev_activations(i), output, output);
130  }
131  }
132  }
133 
134  void bp_compute() override
135  {
136  const int num_parents = this->get_num_parents();
137  const auto& gradient_wrt_output = this->get_prev_error_signals();
138  switch (num_parents) {
139  case 0:
140  break;
141  case 1:
142  El::LockedView(this->get_error_signals(), gradient_wrt_output);
143  break;
144  default:
145  for (int i = 0; i < num_parents; ++i) {
146  auto& gradient_wrt_input = this->get_error_signals(i);
147  El::Copy(gradient_wrt_output, gradient_wrt_input);
148  for (int j = 0; j < num_parents; ++j) {
149  if (i != j) {
150  El::Hadamard(this->get_prev_activations(j),
151  gradient_wrt_input,
152  gradient_wrt_input);
153  }
154  }
155  }
156  }
157  }
158 };
159 
160 #ifndef LBANN_HADAMARD_LAYER_INSTANTIATE
161 #define PROTO_DEVICE(T, Device) \
162  extern template class hadamard_layer<T, data_layout::DATA_PARALLEL, Device>; \
163  extern template class hadamard_layer<T, data_layout::MODEL_PARALLEL, Device>
164 
166 #undef PROTO_DEVICE
167 #endif // LBANN_HADAMARD_LAYER_INSTANTIATE
168 
169 } // namespace lbann
170 
171 #endif // LBANN_LAYER_HADAMARD_HPP_INCLUDED
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.
#define LBANN_ERROR(...)
Definition: exception.hpp:37
int get_num_parents() const noexcept
Get number of parent layers.
Definition: layer.hpp:574
std::vector< int > get_input_dims(size_t input_index=0) const
Get input tensor dimensions.
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: hadamard.hpp:62
std::string get_type() const override
Get the layer type&#39;s name.
Definition: hadamard.hpp:58
constexpr El::Device Device
void serialize(ArchiveT &ar)
OutputAbsDistMatrixType & get_prev_error_signals(int child_index=0)
InputAbsDistMatrixType & get_prev_activations(int parent_index=0)
friend class cereal::access
Definition: hadamard.hpp:73
void setup_pointers() override
Setup layer pointers. Called by the &#39;setup&#39; function. Pointers to parent/child layers are assumed to ...
Definition: hadamard.hpp:76
const OutputAbsDistMatrixType & get_activations(const Layer &child) const override
hadamard_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: hadamard.hpp:48
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
Definition: hadamard.hpp:134
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
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.
Definition: hadamard.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: hadamard.hpp:87
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: hadamard.hpp:59
std::vector< const Layer * > get_parent_layers() const
std::string get_name() const
Get the layer instance&#39;s name.
Definition: layer.hpp:332
void write_specific_proto(lbann_data::Layer &proto) const final
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
hadamard_layer(lbann_comm *comm)
Definition: hadamard.hpp:43
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: hadamard.hpp:61
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: hadamard.hpp:60
virtual void setup_pointers()
Setup layer pointers. Called by the &#39;setup&#39; function. Pointers to parent/child layers are assumed to ...
std::vector< int > get_output_dims(size_t output_index=0) const
Get output tensor dimensions.
int m_expected_num_parent_layers
Definition: layer.hpp:838
const InputAbsDistMatrixType & get_error_signals(const Layer &parent) const override
Entry-wise tensor product.
Definition: hadamard.hpp:40