LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
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_LAYER_ACTIVATION_RELU_HPP_INCLUDED
28 #define LBANN_LAYER_ACTIVATION_RELU_HPP_INCLUDED
29 
32 #include "lbann/proto/layers.pb.h"
33 #include "lbann/utils/distconv.hpp"
34 
35 #ifdef LBANN_HAS_DISTCONV
36 #include "distconv/dnn_backend/relu.hpp"
37 #include "lbann/utils/distconv.hpp"
38 #endif
39 
40 namespace lbann {
41 
42 #ifdef LBANN_HAS_DISTCONV
43 namespace dc {
44 using Backend = ::distconv::BackendDNNLib;
45 using ReLU = ::distconv::ReLU<Backend>;
46 } // namespace dc
47 
48 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
49 class relu_distconv_adapter : public data_type_distconv_adapter<TensorDataType>
50 {
51 public:
52  using TensorDevType =
54  relu_distconv_adapter(Layer& layer)
55  : data_type_distconv_adapter<TensorDataType>(layer)
56  {}
57  virtual ~relu_distconv_adapter() = default;
58  void setup_distributions(tensor_overlap_constraints& constraints) override;
59  void setup_layer(size_t workspace_capacity) override;
60  std::unique_ptr<dc::ReLU> m_relu;
61 };
62 #endif // LBANN_HAS_DISTCONV
63 
68 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
69 class relu_layer : public data_type_layer<TensorDataType>
70 {
71 public:
72  relu_layer() : data_type_layer<TensorDataType>(nullptr) {}
73  relu_layer(lbann_comm* comm) : data_type_layer<TensorDataType>(comm) {}
74  relu_layer* copy() const override { return new relu_layer(*this); }
75  std::string get_type() const override { return "ReLU"; }
76  data_layout get_data_layout() const override { return T_layout; }
77  El::Device get_device_allocation() const override { return Dev; }
78  bool can_run_inplace() const override { return true; }
79  int get_backprop_requirements() const override
80  {
81  return ERROR_SIGNALS | ACTIVATIONS;
82  }
83 
84 #ifdef LBANN_HAS_ONNX
85  std::string get_onnx_op_type() const override { return "Relu"; }
86 #endif // LBANN_HAS_ONNX
87 
89 
91  template <typename ArchiveT>
92  void serialize(ArchiveT& ar);
93 
95 
96 protected:
98  void write_specific_proto(lbann_data::Layer& proto) const final;
99 
100  void fp_compute() override;
101  void bp_compute() override;
102 #ifdef LBANN_HAS_DISTCONV
103  bool is_distconv_supported() const override
104  {
105  return Dev == El::Device::GPU && T_layout == data_layout::DATA_PARALLEL;
106  }
107  void setup_distconv_adapter() override
108  {
109  this->get_distconv_adapter_ptr() =
110  std::make_unique<relu_distconv_adapter<TensorDataType, T_layout, Dev>>(
111  *this);
112  }
113  relu_distconv_adapter<TensorDataType, T_layout, Dev>&
114  get_distconv_adapter() override;
115  const relu_distconv_adapter<TensorDataType, T_layout, Dev>&
116  get_distconv_adapter() const override;
117 #endif // LBANN_HAS_DISTCONV
118 };
119 
120 #ifndef LBANN_RELU_LAYER_INSTANTIATE
121 #define PROTO_DEVICE(T, Device) \
122  extern template class relu_layer<T, data_layout::DATA_PARALLEL, Device>; \
123  extern template class relu_layer<T, data_layout::MODEL_PARALLEL, Device>
124 
126 #undef PROTO_DEVICE
127 #endif // LBANN_RELU_LAYER_INSTANTIATE
128 
129 } // namespace lbann
130 
131 #endif // LBANN_LAYER_ACTIVATION_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: relu.hpp:76
std::string get_type() const override
Get the layer type&#39;s name.
Definition: relu.hpp:75
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: relu.hpp:77
void serialize(std::ostream &os, google::protobuf::Message const &msg)
Serialize the protobuf message to a stream.
constexpr El::Device Device
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: relu.hpp:79
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: relu.hpp:78
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
relu_layer(lbann_comm *comm)
Definition: relu.hpp:73
Rectified linear unit.
Definition: relu.hpp:69
relu_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: relu.hpp:74
dc::TensorDev< OutputTensorDataType > TensorDevType