LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
export_onnx.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 // export_onnx .hpp .cpp - Exports trained model to onnx format
28 
29 #ifndef LBANN_CALLBACKS_EXPORT_ONNX_HPP_INCLUDED
30 #define LBANN_CALLBACKS_EXPORT_ONNX_HPP_INCLUDED
31 
33 #include <lbann/base.hpp>
34 
35 #include <onnx/onnx_pb.h>
36 
37 #include <google/protobuf/message.h>
38 
39 #include <iostream>
40 #include <memory>
41 #include <vector>
42 
43 namespace lbann {
44 namespace callback {
45 
49 class export_onnx : public callback_base
50 {
51 
52 public:
58  export_onnx(std::string output_filename, std::string debug_string_filename)
59  : callback_base(/*batch_interval=*/1),
60  m_output_filename{output_filename.size() ? std::move(output_filename)
61  : std::string("lbann.onnx")},
62  m_debug_string_filename{std::move(debug_string_filename)}
63  {}
64 
66  export_onnx* copy() const override { return new export_onnx(*this); }
67 
69  std::string name() const override { return "export_onnx"; }
70 
71  /* @brief gather graph/layer info */
72  void on_train_end(model* m) override;
73 
74 private:
76  void write_specific_proto(lbann_data::Callback& proto) const final;
77 
78  /* @brief name of output file. Default = lbann.onnx */
79  std::string m_output_filename;
80 
81  /* @brief option to print onnx debug file. Default = none */
83 
84 }; // class export_onnx
85 
86 std::unique_ptr<callback_base>
87 build_export_onnx_callback_from_pbuf(const google::protobuf::Message& proto_msg,
88  const std::shared_ptr<lbann_summary>&);
89 
90 } // namespace callback
91 } // namespace lbann
92 
93 #endif // LBANN_CALLBACKS_EXPORT_ONNX_HPP_INCLUDED
void write_specific_proto(lbann_data::Callback &proto) const final
std::unique_ptr< callback_base > build_export_onnx_callback_from_pbuf(const google::protobuf::Message &proto_msg, const std::shared_ptr< lbann_summary > &)
std::string name() const override
Return name of callback.
Definition: export_onnx.hpp:69
Base class for callbacks during training/testing.
Definition: callback.hpp:76
Abstract base class for neural network models.
Definition: model.hpp:83
void on_train_end(model *m) override
Called at the end of training.
export_onnx * copy() const override
Copy interface.
Definition: export_onnx.hpp:66
Callback to export a trained model to onnx format.
Definition: export_onnx.hpp:49
export_onnx(std::string output_filename, std::string debug_string_filename)
export_onnx Constructor.
Definition: export_onnx.hpp:58