LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
save_model.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.
25 //
26 // save_model .hpp .cpp - Callbacks to save model, currently as protobuf
28 
29 #ifndef LBANN_CALLBACKS_CALLBACK_SAVE_MODEL_HPP_INCLUDED
30 #define LBANN_CALLBACKS_CALLBACK_SAVE_MODEL_HPP_INCLUDED
31 
32 #include <utility>
33 
35 #include "lbann/io/persist.hpp"
37 
38 #include <google/protobuf/message.h>
39 
40 // Forward-declare protobuf classes
41 namespace lbann_data {
42 class Model;
43 }
44 
45 namespace lbann {
46 namespace callback {
47 
51 class save_model : public callback_base
52 {
53 public:
59  save_model(std::string dir,
60  bool disable_save_after_training,
61  std::string extension = "prototext")
62  : callback_base(),
63  m_dir(std::move(dir)),
64  m_disable_save_after_training(disable_save_after_training),
65  m_extension(std::move(extension))
66  {}
67  save_model(const save_model&) = default;
68  save_model& operator=(const save_model&) = default;
69  save_model* copy() const override { return new save_model(*this); }
70  void on_train_end(model* m) override;
71  std::string name() const override { return "save model"; }
72  void set_target_dir(const std::string& dir) { m_dir = dir; }
73  const std::string& get_target_dir() { return m_dir; }
74 
75 protected:
76  friend class lbann::model;
77 
78  bool do_save_model(model* m);
79  bool do_save_model_weights(model* m);
80 
81 private:
83  void write_specific_proto(lbann_data::Callback& proto) const final;
84 
85  std::string m_dir; // directory to save file
88  std::string m_extension; // file extension
90 
91  void write_proto_binary(const lbann_data::Model& proto,
92  const std::string filename);
93  void write_proto_text(const lbann_data::Model& proto,
94  const std::string filename);
95 };
96 
97 inline std::string get_save_model_dirname(const std::string& trainer_name,
98  const std::string& model_name,
99  const std::string& dir)
100 {
101  return build_string(dir, '/', trainer_name, '/', model_name, '/');
102 }
103 
104 // Builder function
105 std::unique_ptr<callback_base>
106 build_save_model_callback_from_pbuf(const google::protobuf::Message&,
107  std::shared_ptr<lbann_summary> const&);
108 
109 } // namespace callback
110 } // namespace lbann
111 
112 #endif // LBANN_CALLBACKS_CALLBACK_SAVE_MODEL_HPP_INCLUDED
const std::string & get_target_dir()
Definition: save_model.hpp:73
std::string name() const override
Return this callback&#39;s name.
Definition: save_model.hpp:71
save_model(std::string dir, bool disable_save_after_training, std::string extension="prototext")
Definition: save_model.hpp:59
std::string build_string(Args &&... args)
Build a string from the arguments.
Definition: exception.hpp:157
void set_target_dir(const std::string &dir)
Definition: save_model.hpp:72
Base class for callbacks during training/testing.
Definition: callback.hpp:76
Abstract base class for neural network models.
Definition: model.hpp:83
std::string get_save_model_dirname(const std::string &trainer_name, const std::string &model_name, const std::string &dir)
Definition: save_model.hpp:97
save_model * copy() const override
Definition: save_model.hpp:69
bool m_disable_save_after_training
Disables the normal behavior of saving when training is complete.
Definition: save_model.hpp:87
std::unique_ptr< callback_base > build_save_model_callback_from_pbuf(const google::protobuf::Message &, std::shared_ptr< lbann_summary > const &)