LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
identity.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_IDENTITY_HPP_INCLUDED
28 #define LBANN_LAYERS_ACTIVATIONS_IDENTITY_HPP_INCLUDED
29 
31 #include "lbann/utils/distconv.hpp"
32 
33 namespace lbann {
34 
35 #ifdef LBANN_HAS_DISTCONV
36 template <typename TensorDataType, data_layout Layout, El::Device Device>
37 class identity_distconv_adapter
38  : public data_type_distconv_adapter<TensorDataType>
39 {
40 public:
41  using TensorDevType =
43  identity_distconv_adapter(Layer& layer)
44  : data_type_distconv_adapter<TensorDataType>(layer)
45  {}
46  virtual ~identity_distconv_adapter() = default;
47  void setup_distributions(tensor_overlap_constraints& constraints) override;
48  std::unique_ptr<TensorDevType> setup_activations_i(int index) const override;
49  std::unique_ptr<TensorDevType>
50  setup_error_signals_i(int index) const override;
51 };
52 #endif // LBANN_HAS_DISTCONV
53 
59 template <typename TensorDataType, data_layout Layout, El::Device Device>
60 class identity_layer : public data_type_layer<TensorDataType>
61 {
62 public:
63  identity_layer() : data_type_layer<TensorDataType>(nullptr) {}
64  identity_layer* copy() const override { return new identity_layer(*this); }
65  std::string get_type() const override { return "identity"; }
66  data_layout get_data_layout() const override { return Layout; }
67  El::Device get_device_allocation() const override { return Device; }
68  bool can_run_inplace() const override { return false; }
69  int get_backprop_requirements() const override { return ERROR_SIGNALS; }
70 
71 #ifdef LBANN_HAS_ONNX
72  std::string get_onnx_op_type() const override { return "Identity"; }
73 #endif // LBANN_HAS_ONNX
74 
76 
78  template <typename ArchiveT>
79  void serialize(ArchiveT& ar);
80 
82 
83 protected:
85  void write_specific_proto(lbann_data::Layer& proto) const final;
86 
87  void setup_dims() override
88  {
90  this->set_output_dims(this->get_input_dims());
91  }
92  void fp_setup_outputs() override
93  {
94 #ifdef LBANN_HAS_DISTCONV
95  // Copy activations when distconv is enabled
96 
97  if (this->distconv_enabled()) {
99 
100  return;
101  }
102 #endif // LBANN_HAS_DISTCONV
103  El::LockedView(this->get_activations(), this->get_prev_activations());
104  }
106  {
107 #ifdef LBANN_HAS_DISTCONV
108  // Copy gradients wrt inputs when distconv is enabled
109 
110  if (this->distconv_enabled()) {
112 
113  return;
114  }
115 #endif // LBANN_HAS_DISTCONV
116  El::LockedView(this->get_error_signals(), this->get_prev_error_signals());
117  }
118  void fp_compute() override {}
119  void bp_compute() override {}
120 #ifdef LBANN_HAS_DISTCONV
121 protected:
122  bool is_distconv_supported() const override
123  {
124  return Device == El::Device::GPU && Layout == data_layout::DATA_PARALLEL;
125  }
126  void setup_distconv_adapter() override
127  {
128  this->get_distconv_adapter_ptr() = std::make_unique<
129  identity_distconv_adapter<TensorDataType, Layout, Device>>(*this);
130  }
131 #endif // LBANN_HAS_DISTCONV
132 };
133 
134 #ifndef LBANN_IDENTITY_LAYER_INSTANTIATE
135 #define PROTO_DEVICE(T, Device) \
136  extern template class identity_layer<T, data_layout::DATA_PARALLEL, Device>; \
137  extern template class identity_layer<T, data_layout::MODEL_PARALLEL, Device>
138 
140 #undef PROTO_DEVICE
141 #endif // LBANN_IDENTITY_LAYER_INSTANTIATE
142 
143 } // namespace lbann
144 
145 #endif // LBANN_LAYERS_ACTIVATIONS_IDENTITY_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: identity.hpp:66
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.
std::string get_type() const override
Get the layer type&#39;s name.
Definition: identity.hpp:65
identity_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: identity.hpp:64
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: identity.hpp:69
Output the input tensor.
Definition: identity.hpp:60
void serialize(std::ostream &os, google::protobuf::Message const &msg)
Serialize the protobuf message to a stream.
constexpr El::Device Device
void fp_setup_outputs() override
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
Definition: identity.hpp:119
void bp_setup_gradient_wrt_inputs() override
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: identity.hpp:68
void bp_setup_gradient_wrt_inputs() override
Setup gradient w.r.t. input tensors. Called by the &#39;back_prop&#39; function. Each gradient w...
Definition: identity.hpp:105
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: identity.hpp:118
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: identity.hpp:67
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
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: identity.hpp:87
void fp_setup_outputs() override
Setup output tensors. Called by the &#39;forward_prop&#39; function. Each output tensor is resized to match t...
Definition: identity.hpp:92
dc::TensorDev< OutputTensorDataType > TensorDevType