LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
operator_layer.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 #ifndef LBANN_LAYERS_OPERATOR_LAYER_HPP_INCLUDED
27 #define LBANN_LAYERS_OPERATOR_LAYER_HPP_INCLUDED
28 
30 #include "lbann/layers/layer.hpp"
33 #include "lbann/utils/tensor.hpp"
34 
35 #include <algorithm>
36 #include <cereal/access.hpp>
37 #include <memory>
38 #include <vector>
39 
40 namespace lbann {
41 
46 template <typename InputT, typename OutputT, data_layout Layout, El::Device D>
47 class OperatorLayer final : public data_type_layer<InputT, OutputT>
48 {
51  using OperatorPtr = std::unique_ptr<OperatorType>;
52 
53  std::vector<OperatorPtr> m_ops;
54 
55 public:
57 
61  OperatorLayer(lbann_comm& comm, std::vector<OperatorPtr> operators);
62 
64  OperatorLayer(OperatorLayer const& other);
66  OperatorLayer& operator=(OperatorLayer const& other);
67 
69  OperatorLayer(OperatorLayer&& other) = default;
71  OperatorLayer& operator=(OperatorLayer&& other) = default;
72 
74  ~OperatorLayer() = default;
75 
77  OperatorLayer* copy() const final;
79 
80  std::string get_type() const final;
81  data_layout get_data_layout() const final;
82  El::Device get_device_allocation() const final;
83  bool can_run_inplace() const final;
84  int get_backprop_requirements() const final;
85 
86  void fp_compute() final;
87  void bp_compute() final;
88 
89  description get_description() const final;
90 
91  template <typename ArchiveT>
92  void serialize(ArchiveT&);
93 
94 protected:
96  void write_specific_proto(lbann_data::Layer& proto) const final;
97 
98 private:
99  friend cereal::access;
100  OperatorLayer();
101 
102  static std::vector<OperatorPtr>
103  clone_ops(std::vector<OperatorPtr> const& ops);
104 
105  static std::vector<size_t> fix_type(std::vector<int> const& in);
106 
107  std::vector<utils::ConstDistTensorView<InputT, D>> get_inputs() const;
108  std::vector<utils::DistTensorView<OutputT, D>> get_outputs();
109  std::vector<utils::ConstDistTensorView<OutputT, D>>
110  get_grad_wrt_outputs() const;
111  std::vector<utils::DistTensorView<InputT, D>> get_grad_wrt_inputs();
112 
113 }; // class OperatorLayer
114 
115 template <typename InputT,
116  typename OutputT,
117  data_layout Layout,
118  El ::Device Device>
120  lbann_data::Layer const&);
121 
122 } // namespace lbann
123 #endif // LBANN_LAYERS_OPERATOR_LAYER_HPP_INCLUDED
std::string get_type() const final
Get the layer type&#39;s name.
std::vector< utils::ConstDistTensorView< OutputT, D > > get_grad_wrt_outputs() const
std::vector< OperatorPtr > m_ops
static std::vector< size_t > fix_type(std::vector< int > const &in)
void write_specific_proto(lbann_data::Layer &proto) const final
El::Device get_device_allocation() const final
Get the device allocation for the data tensors. We assume that the decice allocation of the previous ...
OperatorLayer & operator=(OperatorLayer const &other)
Copy assignment.
description get_description() const final
Human-readable description.
Neural network tensor operation.
Definition: layer.hpp:285
Generates nicely formatted description messages.
Definition: description.hpp:49
constexpr El::Device Device
std::unique_ptr< Layer > build_operator_layer_from_pbuf(lbann_comm *, lbann_data::Layer const &)
~OperatorLayer()=default
Destructor.
std::vector< utils::ConstDistTensorView< InputT, D > > get_inputs() const
void bp_compute() final
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
static std::vector< OperatorPtr > clone_ops(std::vector< OperatorPtr > const &ops)
OperatorLayer * copy() const final
Polymorphic copy.
int get_backprop_requirements() const final
Returns the necessary tensors for computing backpropagation.
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
void fp_compute() final
Apply layer operation. Called by the &#39;forward_prop&#39; function. Given the input tensors, the output tensors are populated with computed values.
data_layout get_data_layout() const final
Get data layout of the data tensors. We assume that the data layouts of the previous activations...
Neural network tensor operation.
Definition: operator.hpp:85
std::vector< utils::DistTensorView< OutputT, D > > get_outputs()
bool can_run_inplace() const final
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
std::unique_ptr< OperatorType > OperatorPtr
Layer composed of one or more operator objects.
std::vector< utils::DistTensorView< InputT, D > > get_grad_wrt_inputs()