LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
metric.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_METRIC_HPP_INCLUDED
28 #define LBANN_METRIC_HPP_INCLUDED
29 
30 #include "lbann/base.hpp"
31 
32 #include <map>
33 #include <typeindex>
34 #include <vector>
35 
36 namespace lbann {
37 
38 // Forward declarations
39 class model;
40 class Layer;
41 class lbann_comm;
42 using ViewingLayerPtr = std::weak_ptr<Layer>;
43 class persist;
44 
47 {
55  metric_statistics(metric_statistics& other) = default;
57  metric_statistics(const metric_statistics& other) = default;
61  metric_statistics& operator=(const metric_statistics& other) = default;
63  ~metric_statistics() = default;
64 
66  template <class Archive>
67  void serialize(Archive& ar);
68 
70  void add_value(EvalType value, int num_samples = 1);
75  EvalType get_mean() const;
77  int get_num_samples() const { return m_num_samples; }
79  void reset();
80 };
81 
86 class metric
87 {
88 
89 public:
91  metric(lbann_comm* comm);
92 
94  metric(const metric& other) = default;
96  metric& operator=(const metric& other) = default;
98  virtual ~metric() = default;
100  virtual metric* copy() const = 0;
101 
103  template <class Archive>
104  void serialize(Archive& ar);
105 
107  virtual std::string name() const = 0;
112  virtual std::string get_unit() const { return ""; }
113 
115  virtual void setup(model& m) {}
116 
123  virtual EvalType evaluate(execution_mode mode, int mini_batch_size) = 0;
124 
127  {
128  for (auto& stats : m_statistics) {
129  stats.second.reset();
130  }
131  }
133  void reset_statistics(execution_mode mode) { m_statistics[mode].reset(); }
134 
139  EvalType get_mean_value(execution_mode mode) const;
141  int get_statistics_num_samples(execution_mode mode) const;
142 
144  virtual std::vector<ViewingLayerPtr> get_layer_pointers() const;
146  virtual void set_layer_pointers(std::vector<ViewingLayerPtr> layers);
147 
149  EvalType get_evaluate_time() const { return m_evaluate_time; }
151  EvalType& get_evaluate_time() { return m_evaluate_time; }
152 
154  void reset_counters() { m_evaluate_time = 0.0; }
155 
157  virtual bool save_to_checkpoint_shared(persist& p) = 0;
159  virtual bool load_from_checkpoint_shared(persist& p) = 0;
160 
161  virtual bool save_to_checkpoint_distributed(persist& p) = 0;
162  virtual bool load_from_checkpoint_distributed(persist& p) = 0;
163 
164 protected:
169  virtual EvalType evaluate_compute(const AbsDistMat& prediction,
170  const AbsDistMat& ground_truth) = 0;
171 
173  lbann_comm& get_comm() { return *m_comm; }
174 
176  std::map<execution_mode, metric_statistics>& get_statistics()
177  {
178  return m_statistics;
179  }
180 
181 private:
184 
186  std::map<execution_mode, metric_statistics> m_statistics;
187 
189  EvalType m_evaluate_time = 0.0;
190 };
191 
192 } // namespace lbann
193 
194 #endif // LBANN_METRIC_HPP_INCLUDED
virtual void setup(model &m)
Definition: metric.hpp:115
El::AbstractDistMatrix< DataType > AbsDistMat
Definition: base.hpp:120
std::weak_ptr< Layer > ViewingLayerPtr
Smart pointer to reference a layer object.
Definition: layer.hpp:133
EvalType get_mean() const
EvalType & get_evaluate_time()
Definition: metric.hpp:151
void reset_statistics()
Definition: metric.hpp:126
EvalType get_evaluate_time() const
Definition: metric.hpp:149
Abstract base class for neural network models.
Definition: model.hpp:83
std::map< execution_mode, metric_statistics > & get_statistics()
Definition: metric.hpp:176
execution_mode
Neural network execution mode.
Definition: base.hpp:229
int get_num_samples() const
Definition: metric.hpp:77
lbann_comm * m_comm
Definition: metric.hpp:183
void add_value(EvalType value, int num_samples=1)
lbann_comm & get_comm()
Definition: metric.hpp:173
std::map< execution_mode, metric_statistics > m_statistics
Definition: metric.hpp:186
void reset_counters()
Definition: metric.hpp:154
metric_statistics & operator=(metric_statistics &other)=default
void serialize(Archive &ar)
void reset_statistics(execution_mode mode)
Definition: metric.hpp:133
virtual std::string get_unit() const
Definition: metric.hpp:112
double EvalType
Definition: base.hpp:189