LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
operator.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_OPERATORS_OPERATOR_HPP_INCLUDED
28 #define LBANN_OPERATORS_OPERATOR_HPP_INCLUDED
29 
30 #include "lbann/base.hpp"
36 #include "lbann/utils/tensor.hpp"
37 #include "lbann/utils/typename.hpp"
38 
39 #include "lbann/proto/operators.pb.h"
40 
41 #include <h2/meta/Core.hpp>
42 #include <h2/meta/TypeList.hpp>
43 
44 #include <google/protobuf/message.h>
45 
46 #include <string>
47 #include <vector>
48 
49 namespace lbann {
50 
51 using supported_operator_data_type = h2::meta::TL<
52 #ifdef LBANN_HAS_GPU_FP16
53  fp16,
54 #endif
55 #ifdef LBANN_HAS_HALF
56  cpu_fp16,
57 #endif
58  float,
59  double,
60  El::Complex<float>,
61  El::Complex<double>>;
62 
84 template <typename InputT, typename OutputT, El::Device D>
85 class Operator : public AbstractCloneableBase<Operator<InputT, OutputT, D>>,
86  public Describable,
88 {
89 public:
97 
98 public:
99  static_assert(
100  h2::meta::tlist::MemberV<InputT, supported_operator_data_type>(),
101  "Must use a supported input type.");
102  static_assert(
103  h2::meta::tlist::MemberV<OutputT, supported_operator_data_type>(),
104  "Must use a supported output type.");
105 
107  Operator() = default;
109  virtual ~Operator() = default;
110 
115  virtual std::string get_type() const = 0;
116 
121  virtual int get_backprop_requirements() const
122  {
124  }
125 
127  Description get_description() const override;
128 
134  void write_proto(google::protobuf::Message& msg) const;
135 
137 
139  template <typename ArchiveT>
140  void serialize(ArchiveT& ar);
141 
143 
144 
150  virtual void
151  fp_compute(std::vector<ConstInputTensorType> const& inputs,
152  std::vector<OutputTensorType> const& outputs) const = 0;
153 
159  virtual void
160  bp_compute(std::vector<ConstInputTensorType> const& inputs,
161  std::vector<ConstOutputTensorType> const& gradient_wrt_outputs,
162  std::vector<InputTensorType> const& gradient_wrt_inputs) const;
164 
165 protected:
166  Operator(Operator&& other) noexcept = default;
167  Operator& operator=(Operator&& other) noexcept = default;
168  Operator(Operator const& other) = default;
169  Operator& operator=(Operator const& other) = default;
170 
171 private:
173  virtual void set_proto_params(lbann_data::Operator&) const = 0;
175  virtual void do_fill_description(Description&) const = 0;
176 };
177 
178 template <typename InputT, typename OutputT, El::Device D>
180  google::protobuf::Message& msg) const
181 {
182  lbann_data::Operator operator_msg;
183  operator_msg.set_input_datatype(proto::ProtoDataType<InputT>);
184  operator_msg.set_output_datatype(proto::ProtoDataType<OutputT>);
185  operator_msg.set_device_allocation(proto::ProtoDevice<D>);
186 
187  this->set_proto_params(operator_msg);
188 
189  msg.CopyFrom(operator_msg);
190 }
191 
192 template <typename InputT, typename OutputT, El::Device D>
194 {
195 
196  // Construct description object
197  Description desc(this->get_type());
198 
199  // DataType
200  desc.add("Input data type", TypeName<InputT>());
201  desc.add("Output data type", TypeName<OutputT>());
202 
203  this->do_fill_description(desc);
204 
205  return desc;
206 }
207 
208 template <typename InputT, typename OutputT, El::Device D>
210  std::vector<ConstInputTensorType> const&,
211  std::vector<ConstOutputTensorType> const&,
212  std::vector<InputTensorType> const&) const
213 {}
214 
215 template <typename InputT, typename OutputT, El::Device D>
216 template <typename ArchiveT>
218 {}
219 
220 } // namespace lbann
221 #endif // LBANN_OPERATORS_OPERATOR_HPP_INCLUDED
virtual void do_fill_description(Description &) const =0
Concrete operator description.
Inject polymorphic clone functions into hierarchies.
Definition: cloneable.hpp:94
virtual int get_backprop_requirements() const
Returns the necessary tensors for computing backpropagation for this operator.
Definition: operator.hpp:121
Represents a class that is describable in LBANN&#39;s protobuf specification.
virtual ~Operator()=default
Destructor.
Generates nicely formatted description messages.
Definition: description.hpp:49
virtual void fp_compute(std::vector< ConstInputTensorType > const &inputs, std::vector< OutputTensorType > const &outputs) const =0
Apply operator&#39;s forward operation.
virtual void bp_compute(std::vector< ConstInputTensorType > const &inputs, std::vector< ConstOutputTensorType > const &gradient_wrt_outputs, std::vector< InputTensorType > const &gradient_wrt_inputs) const
Compute operator&#39;s "backward" operation.
Definition: operator.hpp:209
Operator & operator=(Operator &&other) noexcept=default
Operator()=default
Constructor.
Description get_description() const override
Get the description of the operator.
Definition: operator.hpp:193
A class that can generate self-descriptions.
Definition: describable.hpp:37
void serialize(ArchiveT &ar)
Definition: operator.hpp:217
void add(std::string line)
void write_proto(google::protobuf::Message &msg) const
Write a protobuf description of the operator.
Definition: operator.hpp:179
virtual std::string get_type() const =0
Get the operator type&#39;s name.
h2::meta::TL< float, double, El::Complex< float >, El::Complex< double > > supported_operator_data_type
Definition: operator.hpp:61
virtual void set_proto_params(lbann_data::Operator &) const =0
Fill the concrete operator parameters.
Neural network tensor operation.
Definition: operator.hpp:85