LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
constant.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_CONSTANT_HPP_INCLUDED
28 #define LBANN_LAYER_CONSTANT_HPP_INCLUDED
29 
31 
32 namespace lbann {
33 
35 template <typename TensorDataType,
37  El::Device Dev = El::Device::CPU>
38 class constant_layer : public data_type_layer<TensorDataType>
39 {
40 public:
41  constant_layer(lbann_comm* comm, TensorDataType value, std::vector<int> dims)
42  : data_type_layer<TensorDataType>(comm), m_value(value)
43  {
44  this->set_output_dims(dims);
46  }
47 
48  constant_layer* copy() const override { return new constant_layer(*this); }
49 
51 
53  template <typename ArchiveT>
54  void serialize(ArchiveT& ar);
55 
57 
58  std::string get_type() const override { return "constant"; }
59  data_layout get_data_layout() const override { return T_layout; }
60  El::Device get_device_allocation() const override { return Dev; }
61  bool can_run_inplace() const override { return false; }
62  int get_backprop_requirements() const override { return PROPAGATE_NOTHING; }
63 
64  description get_description() const override
65  {
67  desc.add("Value", m_value);
68  return desc;
69  }
70 
71 protected:
73  void write_specific_proto(lbann_data::Layer& proto) const final;
74 
75  friend class cereal::access;
76  constant_layer() : constant_layer(nullptr, El::To<TensorDataType>(0), {1}) {}
77 
78  void fp_compute() override
79  {
80  if (m_value == EvalType(0)) {
81  El::Zero(this->get_activations());
82  }
83  else {
84  El::Fill(this->get_activations(), m_value);
85  }
86  }
87 
88 private:
90  TensorDataType m_value;
91 };
92 
93 #ifndef LBANN_CONSTANT_LAYER_INSTANTIATE
94 #define PROTO_DEVICE(T, Device) \
95  extern template class constant_layer<T, data_layout::DATA_PARALLEL, Device>; \
96  extern template class constant_layer<T, data_layout::MODEL_PARALLEL, Device>
97 
99 #undef PROTO_DEVICE
100 #endif // LBANN_CONSTANT_LAYER_INSTANTIATE
101 
102 } // namespace lbann
103 
104 #endif // LBANN_LAYER_CONSTANT_HPP_INCLUDED
void serialize(ArchiveT &ar)
constant_layer(lbann_comm *comm, TensorDataType value, std::vector< int > dims)
Definition: constant.hpp:41
constant_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: constant.hpp:48
Generates nicely formatted description messages.
Definition: description.hpp:49
virtual description get_description() const
Human-readable description.
void write_specific_proto(lbann_data::Layer &proto) const final
constexpr El::Device Device
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: constant.hpp:78
description get_description() const override
Human-readable description.
Definition: constant.hpp:64
const OutputAbsDistMatrixType & get_activations(const Layer &child) const override
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: constant.hpp:59
void set_output_dims(std::vector< int > dims, size_t output_index=0)
Set output tensor dimensions.
friend class cereal::access
Definition: constant.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: constant.hpp:60
Output tensor filled with a single value.
Definition: constant.hpp:38
TensorDataType m_value
Definition: constant.hpp:90
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: constant.hpp:61
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
std::string get_type() const override
Get the layer type&#39;s name.
Definition: constant.hpp:58
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: constant.hpp:62
int m_expected_num_parent_layers
Definition: layer.hpp:838
double EvalType
Definition: base.hpp:189