LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
stop_gradient.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 STOP_GRADIENT_HPP_INCLUDED
28 #define STOP_GRADIENT_HPP_INCLUDED
29 
31 
32 namespace lbann {
33 
42 template <typename TensorDataType, data_layout T_layout, El::Device Dev>
43 class stop_gradient_layer : public data_type_layer<TensorDataType>
44 {
45 public:
46  stop_gradient_layer(lbann_comm* comm) : data_type_layer<TensorDataType>(comm)
47  {}
48  stop_gradient_layer* copy() const override
49  {
50  return new stop_gradient_layer(*this);
51  }
52 
54 
56  template <typename ArchiveT>
57  void serialize(ArchiveT& ar);
58 
60 
61  std::string get_type() const override { return "stop_gradient"; }
62  data_layout get_data_layout() const override { return T_layout; }
63  El::Device get_device_allocation() const override { return Dev; }
64  bool can_run_inplace() const override { return false; }
65  int get_backprop_requirements() const override { return PROPAGATE_NOTHING; }
66 
67 protected:
69  void write_specific_proto(lbann_data::Layer& proto) const final;
70 
71  friend class cereal::access;
73 
74  void setup_dims() override
75  {
77  this->set_output_dims(this->get_input_dims());
78  }
79  void fp_setup_outputs() override
80  {
81  El::View(this->get_activations(), this->get_prev_activations());
82  }
83  void fp_compute() override {}
84 };
85 
86 #ifndef LBANN_STOP_GRADIENT_LAYER_INSTANTIATE
87 #define PROTO_DEVICE(T, Device) \
88  extern template class stop_gradient_layer<T, \
89  data_layout::DATA_PARALLEL, \
90  Device>; \
91  extern template class stop_gradient_layer<T, \
92  data_layout::MODEL_PARALLEL, \
93  Device>
94 
96 #undef PROTO_DEVICE
97 #endif // LBANN_STOP_GRADIENT_LAYER_INSTANTIATE
98 
99 } // namespace lbann
100 
101 #endif // STOP_GRADIENT_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.
data_layout get_data_layout() const override
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
stop_gradient_layer(lbann_comm *comm)
El::Device get_device_allocation() const override
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
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.
void fp_setup_outputs() override
Setup output tensors. Called by the &#39;forward_prop&#39; function. Each output tensor is resized to match t...
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.
std::vector< int > get_input_dims(size_t input_index=0) const
Get input tensor dimensions.
void serialize(ArchiveT &ar)
constexpr El::Device Device
void write_specific_proto(lbann_data::Layer &proto) const final
InputAbsDistMatrixType & get_prev_activations(int parent_index=0)
const OutputAbsDistMatrixType & get_activations(const Layer &child) const override
stop_gradient_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
Block error signals during back propagation.
friend class cereal::access
std::string get_type() const override
Get the layer type&#39;s name.
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.
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...