LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
evaluation.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_EVALUATION_HPP_INCLUDED
28 #define LBANN_LAYER_EVALUATION_HPP_INCLUDED
29 
31 
32 namespace lbann {
33 
35 template <typename TensorDataType>
36 class abstract_evaluation_layer : public data_type_layer<TensorDataType>
37 {
38 public:
39 #ifdef LBANN_DETERMINISTIC
40  using EvalDataType = EvalType;
41 #else
42  using EvalDataType = TensorDataType;
43 #endif
44  using CPUMatType = El::Matrix<EvalDataType, El::Device::CPU>;
45 
46 public:
48  EvalType get_scale() const { return m_scale; }
50  void set_scale(EvalType scale) { m_scale = scale; }
52  EvalType get_value(bool scaled = true);
53 
58  construct(lbann_comm* comm, data_layout layout, El::Device device);
59 
60 protected:
63 
65  template <typename ArchiveT>
66  void serialize(ArchiveT& ar);
67 
69 
70  friend class cereal::access;
72 
73  void setup_dims() override;
74  void setup_data(size_t max_mini_batch_size) override;
75  void fp_compute() override;
76  void bp_compute() override;
77 
78 private:
87 #ifdef LBANN_HAS_GPU
88 
89  gpu_lib::event_wrapper m_copy_event;
90 #endif // LBANN_HAS_GPU
91 };
92 
97 template <typename TensorDataType,
99  El::Device Dev = El::Device::CPU>
100 class evaluation_layer : public abstract_evaluation_layer<TensorDataType>
101 {
102 public:
104  : abstract_evaluation_layer<TensorDataType>(comm)
105  {}
106  evaluation_layer* copy() const override
107  {
108  return new evaluation_layer(*this);
109  }
110 
112 
114  template <typename ArchiveT>
115  void serialize(ArchiveT& ar);
116 
118 
119  std::string get_type() const override { return "evaluation"; }
120  data_layout get_data_layout() const override { return T_layout; }
121  El::Device get_device_allocation() const override { return Dev; }
122  bool can_run_inplace() const override { return false; }
123  int get_backprop_requirements() const override { return ERROR_SIGNALS; }
124 
125 #ifdef LBANN_HAS_ONNX
126  void fill_onnx_node(onnx::GraphProto& graph) const override;
127 #endif // LBANN_HAS_ONNX
128 
129 protected:
131  void write_specific_proto(lbann_data::Layer& proto) const final;
132 
133  friend class cereal::access;
135 };
136 
137 #ifdef LBANN_HAS_ONNX
138 template <typename T, data_layout L, El::Device D>
139 void evaluation_layer<T, L, D>::fill_onnx_node(onnx::GraphProto& graph) const
140 {
141  auto* eval = graph.add_node();
142  for (auto const* parent : this->get_parent_layers()) {
143  size_t idx = parent->find_child_layer_index(*this);
144  eval->add_input(parent->get_name() + "_" + std::to_string(idx));
145  }
146  eval->add_output(this->get_name());
147  eval->set_name(this->get_name());
148  eval->set_op_type("Identity");
149  eval->set_domain("");
150  eval->set_doc_string(this->get_type());
151 
152  // Add graph output
153  auto graph_output = graph.add_output();
154  graph_output->set_name(eval->output(0));
155  auto* graph_output_type = graph_output->mutable_type();
156  graph_output_type->mutable_tensor_type()->set_elem_type(
157  onnx::AttributeProto::FLOAT);
158 
159  auto* dims =
160  graph_output_type->mutable_tensor_type()->mutable_shape()->add_dim();
161  dims->set_dim_param("batch");
162  dims = graph_output_type->mutable_tensor_type()->mutable_shape()->add_dim();
163  dims->set_dim_value(1);
164 }
165 #endif // LBANN_HAS_ONNX
166 
167 #ifndef LBANN_EVALUATION_LAYER_INSTANTIATE
168 #define PROTO(T) extern template class abstract_evaluation_layer<T>
169 
170 #define LBANN_INSTANTIATE_CPU_HALF
171 #define LBANN_INSTANTIATE_GPU_HALF
173 #undef PROTO
174 #undef LBANN_INSTANTIATE_CPU_HALF
175 #undef LBANN_INSTANTIATE_GPU_HALF
176 
177 #define PROTO_DEVICE(T, Device) \
178  extern template class evaluation_layer<T, \
179  data_layout::DATA_PARALLEL, \
180  Device>; \
181  extern template class evaluation_layer<T, data_layout::MODEL_PARALLEL, Device>
182 
184 #undef PROTO_DEVICE
185 #endif // LBANN_EVALUATION_LAYER_INSTANTIATE
186 
187 } // namespace lbann
188 
189 #endif // LBANN_LAYER_EVALUATION_HPP_INCLUDED
void set_scale(EvalType scale)
Definition: evaluation.hpp:50
static abstract_evaluation_layer * construct(lbann_comm *comm, data_layout layout, El::Device device)
void setup_data(size_t max_mini_batch_size) override
Setup layer data. Called by the &#39;setup&#39; function. Memory is allocated for distributed matrices...
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: evaluation.hpp:120
EvalType get_value(bool scaled=true)
Interface with objective function and metrics.
Definition: evaluation.hpp:36
int get_backprop_requirements() const override
Returns the necessary tensors for computing backpropagation.
Definition: evaluation.hpp:123
virtual void write_specific_proto(lbann_data::Layer &proto) const =0
Add layer specific data to prototext.
virtual std::string get_type() const =0
Get the layer type&#39;s name.
constexpr El::Device Device
std::string to_string(El::Device const &d)
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: evaluation.hpp:121
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< const Layer * > get_parent_layers() const
std::string get_name() const
Get the layer instance&#39;s name.
Definition: layer.hpp:332
friend class cereal::access
Definition: evaluation.hpp:70
bool can_run_inplace() const override
If True, the computation can run in-place (feeding each input activations tensor as the corresponding...
Definition: evaluation.hpp:122
data_layout
Data layout that is optimized for different modes of parallelism.
Definition: base.hpp:218
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.
evaluation_layer * copy() const override
Copy function. This function dynamically allocates memory for a layer instance and instantiates a cop...
Definition: evaluation.hpp:106
size_t find_child_layer_index(const Layer &l) const
void bp_compute() override
Compute objective funciton gradients. Called by the &#39;back_prop&#39; function. Given the input...
evaluation_layer(lbann_comm *comm)
Definition: evaluation.hpp:103
double EvalType
Definition: base.hpp:189
El::Matrix< EvalDataType, El::Device::CPU > CPUMatType
Definition: evaluation.hpp:44
std::string get_type() const override
Get the layer type&#39;s name.
Definition: evaluation.hpp:119