LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
leaky_relu.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_ACTIVATIONS_LEAKY_RELU_HPP_INCLUDED
28 #define LBANN_LAYERS_ACTIVATIONS_LEAKY_RELU_HPP_INCLUDED
29 
31 #include "lbann/layers/layer.hpp"
33 #include "lbann/proto/layers.pb.h"
34 #include "lbann/utils/distconv.hpp"
35 
36 #ifdef LBANN_HAS_DISTCONV
37 #include "distconv/dnn_backend/leaky_relu.hpp"
38 #include "lbann/utils/distconv.hpp"
39 #endif
40 
41 namespace lbann {
42 
43 #ifdef LBANN_HAS_DISTCONV
44 namespace dc {
45 using Backend = ::distconv::BackendDNNLib;
46 using LeakyReLU = ::distconv::LeakyReLU<Backend>;
47 } // namespace dc
48 
49 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
50 class leaky_relu_distconv_adapter
51  : public data_type_distconv_adapter<TensorDataType>
52 {
53 public:
54  using TensorDevType =
56 
57  leaky_relu_distconv_adapter(Layer& layer)
58  : data_type_distconv_adapter<TensorDataType>(layer)
59  {}
60  virtual ~leaky_relu_distconv_adapter() = default;
61 
62  void setup_distributions(tensor_overlap_constraints& constraints) override;
63  void setup_layer(size_t workspace_capacity) override;
64 
65  std::unique_ptr<dc::LeakyReLU> m_leaky_relu;
66 };
67 #endif // LBANN_HAS_DISTCONV
68 
83 template <typename TensorDataType, data_layout Layout, El::Device Device>
84 class leaky_relu_layer : public data_type_layer<TensorDataType>
85 {
86 public:
87  leaky_relu_layer() : leaky_relu_layer(nullptr, El::To<TensorDataType>(0.01))
88  {}
90  TensorDataType negative_slope = El::To<TensorDataType>(0.01))
91  : data_type_layer<TensorDataType>(comm), m_negative_slope(negative_slope)
92  {}
93  leaky_relu_layer* copy() const override
94  {
95  return new leaky_relu_layer(*this);
96  }
97  std::string get_type() const override { return "leaky ReLU"; }
98  data_layout get_data_layout() const override { return Layout; }
99  El::Device get_device_allocation() const override { return Device; }
100 
101  description get_description() const override
102  {
104  desc.add("Negative slope", m_negative_slope);
105  return desc;
106  }
107 
109 
111  template <typename ArchiveT>
112  void serialize(ArchiveT& ar);
113 
115 
116 protected:
118  void write_specific_proto(lbann_data::Layer& proto) const final;
119 
120  void setup_dims() override
121  {
123  this->set_output_dims(this->get_input_dims());
124  }
125  void fp_compute() override;
126  void bp_compute() override;
127 
128  bool can_run_inplace() const override { return true; }
129  int get_backprop_requirements() const override
130  {
131  return ERROR_SIGNALS | ACTIVATIONS;
132  }
133 
134 private:
136  TensorDataType m_negative_slope;
137 
138 #ifdef LBANN_HAS_DISTCONV
139 protected:
140  bool is_distconv_supported() const override
141  {
142  return Device == El::Device::GPU && Layout == data_layout::DATA_PARALLEL;
143  }
144  void setup_distconv_adapter() override
145  {
146  this->get_distconv_adapter_ptr() = std::make_unique<
147  leaky_relu_distconv_adapter<TensorDataType, Layout, Device>>(*this);
148  }
149  leaky_relu_distconv_adapter<TensorDataType, Layout, Device>&
150  get_distconv_adapter() override;
151  const leaky_relu_distconv_adapter<TensorDataType, Layout, Device>&
152  get_distconv_adapter() const override;
153 #endif // LBANN_HAS_DISTCONV
154 };
155 
156 #ifndef LBANN_LEAKY_RELU_LAYER_INSTANTIATE
157 #define PROTO_DEVICE(T, Device) \
158  extern template class leaky_relu_layer<T, \
159  data_layout::DATA_PARALLEL, \
160  Device>; \
161  extern template class leaky_relu_layer<T, data_layout::MODEL_PARALLEL, Device>
162 
164 #undef PROTO_DEVICE
165 #endif // LBANN_LEAKY_RELU_LAYER_INSTANTIATE
166 
167 } // namespace lbann
168 
169 #endif // LBANN_LAYERS_ACTIVATIONS_LEAKY_RELU_HPP_INCLUDED
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: leaky_relu.hpp:98
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.
leaky_relu_layer(lbann_comm *comm, TensorDataType negative_slope=El::To< TensorDataType >(0.01))
Definition: leaky_relu.hpp:89
leaky_relu_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: leaky_relu.hpp:93
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: leaky_relu.hpp:99
std::string get_type() const override
Get the layer type&#39;s name.
Definition: leaky_relu.hpp:97
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
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: leaky_relu.hpp:120
description get_description() const override
Human-readable description.
Definition: leaky_relu.hpp:101
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: leaky_relu.hpp:128
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: leaky_relu.hpp:129
TensorDataType m_negative_slope
Definition: leaky_relu.hpp:136
dc::TensorDev< OutputTensorDataType > TensorDevType