graph_builder.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_GRAPH_BUILDER_HPP
27 #define CASADI_GRAPH_BUILDER_HPP
28 
29 #include "function.hpp"
30 
31 namespace casadi {
32 
33  class GraphBuilderInternal;
34 
49  class CASADI_EXPORT GraphBuilder
50  : public SharedObject,
51  public SWIG_IF_ELSE(PrintableCommon, Printable<GraphBuilder>) {
52  public:
54  std::string type_name() const { return "GraphBuilder"; }
55 
58 
60  explicit GraphBuilder(const std::string& model_path, const Dict& opts = Dict());
61 
63  explicit GraphBuilder(const Function& f, const Dict& opts = Dict());
64 
65 #ifndef SWIG
67  GraphBuilder(const std::string& name, const std::vector<uint8_t>& model_data,
68  const std::string& format, const Dict& opts = Dict());
69 #endif // SWIG
70 
72  const std::string& name() const;
73 
75 
76  casadi_int n_in() const;
77  casadi_int n_out() const;
78  std::vector<std::string> name_in() const;
79  std::vector<std::string> name_out() const;
81  std::vector<casadi_int> dimension(const std::string& name) const;
83  std::string dtype(const std::string& name) const;
85  std::vector<std::string> dimension_param(const std::string& name) const;
87  std::vector<std::string> dynamic_params() const;
89 
91 
93  void bind_dim(const std::string& param, casadi_int value);
95  void bind_shape(const std::string& input_name, const std::vector<casadi_int>& shape);
97  void set(const std::string& input_name, const std::vector<double>& value);
99  void set(const std::string& input_name, double value);
101 
111  Function create(const std::string& name,
112  const std::vector<std::string>& name_in,
113  const std::vector<std::string>& name_out,
114  const Dict& opts = Dict()) const;
115 
122  Function create(const std::string& name, const Dict& opts = Dict()) const;
123 
127  Function create() const { return create(name() + "_graph"); }
128 
132  void export_onnx(const std::string& filename, const Dict& opts = Dict());
133 
134 #ifndef SWIG
137  GraphBuilderInternal* operator->();
138  const GraphBuilderInternal* operator->() const;
139  GraphBuilderInternal* get() const;
141 #endif // SWIG
142  };
143 
144 } // namespace casadi
145 
146 #endif // CASADI_GRAPH_BUILDER_HPP
Function object.
Definition: function.hpp:60
A mutable, format-neutral interface to a computational-graph model.
GraphBuilder()
Default constructor.
GraphBuilder(const Function &f, const Dict &opts=Dict())
Construct from a CasADi Function (export lifecycle)
std::string dtype(const std::string &name) const
Element type name of a tensor (input or output) by name (FLOAT, INT64, ...)
std::vector< casadi_int > dimension(const std::string &name) const
Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions.
void set(const std::string &input_name, double value)
Bake a scalar value into an input.
void bind_dim(const std::string &param, casadi_int value)
Bind a symbolic/dynamic dimension to a concrete size.
std::vector< std::string > dynamic_params() const
Names of the symbolic/dynamic dimensions in the model.
const std::string & name() const
Name of the model.
Function create(const std::string &name, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict()) const
Freeze into an evaluable Function.
casadi_int n_in() const
Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions.
std::vector< std::string > dimension_param(const std::string &name) const
Per-axis symbolic dimension name of a tensor by name ("" for static axes)
std::string type_name() const
Readable name of the class.
std::vector< std::string > name_in() const
Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions.
Function create() const
Freeze into an evaluable Function, default naming.
GraphBuilder(const std::string &model_path, const Dict &opts=Dict())
Construct from a model file (import lifecycle; format from the file suffix)
void set(const std::string &input_name, const std::vector< double > &value)
Bake a fixed value into an input; it is fed at create() and not exposed as a Function input.
void bind_shape(const std::string &input_name, const std::vector< casadi_int > &shape)
Pin the full shape of an input.
casadi_int n_out() const
Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions.
std::vector< std::string > name_out() const
Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions.
void export_onnx(const std::string &filename, const Dict &opts=Dict())
Export to an ONNX model file.
Function create(const std::string &name, const Dict &opts=Dict()) const
Freeze into an evaluable Function, exposing all model inputs and outputs.
GenericShared implements a reference counting framework similar for efficient and.
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.