graph_builder_internal.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_INTERNAL_HPP
27 #define CASADI_GRAPH_BUILDER_INTERNAL_HPP
28 
29 #include "graph_builder.hpp"
30 #include "graph_model_impl.hpp"
31 #include "shared_object.hpp"
32 
34 
35 namespace casadi {
36 
42  struct CASADI_EXPORT Node
43  : public SWIG_IF_ELSE(PrintableCommon, Printable<Node>) {
44  std::string name;
45  std::string io;
46  std::string dtype;
47  std::vector<casadi_int> dimension;
48  std::vector<std::string> dim_params;
49  std::vector<double> value;
50  bool baked = false;
51 
52  std::string type_name() const { return "Node"; }
53  void disp(std::ostream& stream, bool more=false) const;
54  std::string get_str(bool more=false) const;
55  };
56 
65  class CASADI_EXPORT GraphBuilderInternal : public SharedObjectInternal {
66  public:
68  GraphBuilderInternal(const std::string& name, const std::vector<uint8_t>& model_data,
69  const std::string& format, const Dict& opts);
71  GraphBuilderInternal(const std::string& name, const Function& f, const Dict& opts);
72  ~GraphBuilderInternal() override;
73 
74  std::string class_name() const override { return "GraphBuilderInternal"; }
75  void disp(std::ostream& stream, bool more) const override;
76 
77  casadi_int n_in() const;
78  casadi_int n_out() const;
79  std::vector<std::string> name_in() const;
80  std::vector<std::string> name_out() const;
81  std::vector<casadi_int> input_shape(const std::string& name) const;
82  std::vector<casadi_int> output_shape(const std::string& name) const;
83  std::vector<std::string> dynamic_params() const;
84  Node node(const std::string& name) const;
85  std::vector<Node> nodes() const { return nodes_; }
86 
87  void bind_dim(const std::string& param, casadi_int value) { dim_bindings_[param] = value; }
88  void bind_shape(const std::string& input_name, const std::vector<casadi_int>& shape);
89  void set_value(const std::string& input_name, const std::vector<double>& value);
90 
91  Function create_function(const std::string& name,
92  const std::vector<std::string>& name_in,
93  const std::vector<std::string>& name_out,
94  const Dict& opts) const;
95  void export_onnx(const std::string& filename, const Dict& opts);
96 
98  void add_node(const Node& n) { nodes_.push_back(n); }
100  void clear_nodes() { nodes_.clear(); }
101 
104  const std::vector<Node>& node_list() const { return nodes_; }
105  const std::map<std::string, casadi_int>& dim_bindings() const { return dim_bindings_; }
107 
110  std::vector<casadi_int> resolved_shape(const Node& n) const;
111 
113  const Node& find(const std::string& name, const std::string& io) const;
114 
115  std::string name_;
116  std::string format_;
117  std::vector<uint8_t> model_data_;
118 
120  Function fun_;
122  GraphModel model_;
123 
125  std::vector<Node> nodes_;
126 
128  std::map<std::string, casadi_int> dim_bindings_;
129  std::map<std::string, std::vector<casadi_int>> input_shapes_;
130  std::map<std::string, std::vector<double>> input_values_;
131 
132  private:
134  void populate_from_function();
135  };
136 
137 } // namespace casadi
138 
140 
141 #endif // CASADI_GRAPH_BUILDER_INTERNAL_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.