LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
reduction.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_REDUCTION_HPP_INCLUDED
28 #define LBANN_LAYER_REDUCTION_HPP_INCLUDED
29 
31 
32 namespace lbann {
33 
34 enum class reduction_mode
35 {
36  INVALID,
37  SUM,
38  AVERAGE
39 };
40 
45 template <typename TensorDataType,
47  El::Device Device = El::Device::CPU>
48 class reduction_layer : public data_type_layer<TensorDataType>
49 {
50 private:
53 
54 public:
56 
57  reduction_layer* copy() const override { return new reduction_layer(*this); }
58 
60 
62  template <typename ArchiveT>
63  void serialize(ArchiveT& ar);
64 
66 
67  std::string get_type() const override { return "reduction"; }
68  data_layout get_data_layout() const override { return Layout; }
69  El::Device get_device_allocation() const override { return Device; }
70  bool can_run_inplace() const override { return false; }
71  int get_backprop_requirements() const override { return ERROR_SIGNALS; }
72 
73  description get_description() const override
74  {
76  std::string mode_str;
77  switch (m_mode) {
79  mode_str = "sum";
80  break;
82  mode_str = "average";
83  break;
85  default:
86  mode_str = "invalid";
87  }
88  desc.add("Mode", mode_str);
89  return desc;
90  }
91 
92 protected:
94  void write_specific_proto(lbann_data::Layer& proto) const final;
95 
96  void setup_dims() override;
97 
98  void fp_compute() override;
99 
100  void bp_compute() override;
101 };
102 
103 #ifndef LBANN_REDUCTION_LAYER_INSTANTIATE
104 #define PROTO_DEVICE(T, Device) \
105  extern template class reduction_layer<T, \
106  data_layout::DATA_PARALLEL, \
107  Device>; \
108  extern template class reduction_layer<T, data_layout::MODEL_PARALLEL, Device>
110 #undef PROTO_DEVICE
111 #endif // LBANN_REDUCTION_LAYER_INSTANTIATE
112 
113 } // namespace lbann
114 
115 #endif // LBANN_LAYER_REDUCTION_HPP_INCLUDED
reduction_mode m_mode
Definition: reduction.hpp:52
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: reduction.hpp:70
std::string get_type() const override
Get the layer type&#39;s name.
Definition: reduction.hpp:67
void serialize(std::ostream &os, google::protobuf::Message const &msg)
Serialize the protobuf message to a stream.
Generates nicely formatted description messages.
Definition: description.hpp:49
virtual description get_description() const
Human-readable description.
constexpr El::Device Device
reduction_mode
Definition: reduction.hpp:34
Reduce tensor to scalar.
Definition: reduction.hpp:48
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: reduction.hpp:69
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: reduction.hpp:68
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
reduction_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: reduction.hpp:57
description get_description() const override
Human-readable description.
Definition: reduction.hpp:73
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: reduction.hpp:71