onnx_function_impl.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_ONNX_FUNCTION_IMPL_HPP
27 #define CASADI_ONNX_FUNCTION_IMPL_HPP
28 
29 #include "onnx_function.hpp"
30 #include "function_internal.hpp"
31 #include "plugin_interface.hpp"
32 #include <set>
33 
35 
36 namespace casadi {
37 
38  class GraphBuilderInternal;
39 
41  struct OnnxTensorInfo {
42  std::string name;
43  std::vector<casadi_int> shape;
44  casadi_int elem_type;
45  casadi_int numel;
46  };
47 
48  struct CASADI_EXPORT OnnxMemory : public FunctionMemory {
49  };
50 
52  CASADI_EXPORT std::string onnx_dtype_name(casadi_int elem_type);
53 
55  CASADI_EXPORT casadi_int onnx_dtype_enum(const std::string& name);
56 
62  class CASADI_EXPORT OnnxFunction : public FunctionInternal, public PluginInterface<OnnxFunction> {
63  public:
65  OnnxFunction(const std::string& name,
66  const GraphBuilderInternal* gb,
67  const std::vector<std::string>& inputs,
68  const std::vector<std::string>& outputs);
69  ~OnnxFunction() override;
70 
72  typedef OnnxFunction* (*Creator)(const std::string& name,
73  const GraphBuilderInternal* gb,
74  const std::vector<std::string>& inputs,
75  const std::vector<std::string>& outputs,
76  const Dict& opts);
77 
79  struct Exposed { };
80 
82 
85  static const Options options_;
86  const Options& get_options() const override { return options_;}
88 
89  // The plugin fills tensor metadata before init(); the base exposes I/O from it
90  size_t get_n_in() override { return in_.size(); }
91  size_t get_n_out() override { return out_.size(); }
92  std::string get_name_in(casadi_int i) override { return in_.at(i).name; }
93  std::string get_name_out(casadi_int i) override { return out_.at(i).name; }
94  Sparsity get_sparsity_in(casadi_int i) override { return tensor_sparsity(in_.at(i).shape); }
95  Sparsity get_sparsity_out(casadi_int i) override { return tensor_sparsity(out_.at(i).shape); }
96 
100  void init(const Dict& opts) override;
101 
102  void* alloc_mem() const override { return new OnnxMemory(); }
103  void free_mem(void* mem) const override { delete static_cast<OnnxMemory*>(mem); }
104 
105  int eval(const double** arg, double** res,
106  casadi_int* iw, double* w, void* mem) const override = 0;
107 
108  bool uses_output() const override { return false; }
109 
110  // A model authored with CasADi's derivative naming can serve its own forward sensitivities:
111  // get_forward re-create()s from the same model selecting the fwd_* tensors. Otherwise CasADi
112  // falls back to finite differences.
113  bool has_forward(casadi_int nfwd) const override;
114  Function get_forward(casadi_int nfwd, const std::string& name,
115  const std::vector<std::string>& inames,
116  const std::vector<std::string>& onames,
117  const Dict& opts) const override;
118  bool has_reverse(casadi_int nadj) const override;
119  Function get_reverse(casadi_int nadj, const std::string& name,
120  const std::vector<std::string>& inames,
121  const std::vector<std::string>& onames,
122  const Dict& opts) const override;
123  bool has_jacobian() const override;
124  Function get_jacobian(const std::string& name,
125  const std::vector<std::string>& inames,
126  const std::vector<std::string>& onames,
127  const Dict& opts) const override;
128 
132  void serialize_body(SerializingStream &s) const override;
136  void serialize_type(SerializingStream &s) const override;
140  std::string serialize_base_function() const override { return "Onnx"; }
144  static ProtoFunction* deserialize(DeserializingStream& s);
145 
147  static std::string meta_doc;
148 
150  static Function create(const std::string& solver,
151  const std::string& name,
152  const GraphBuilderInternal* gb,
153  const std::vector<std::string>& inputs,
154  const std::vector<std::string>& outputs,
155  const Dict& opts);
156 
157  // Infix used to form plugin registration symbols (casadi_register_onnx_<name>)
158  static const std::string infix_;
159 
161  static std::map<std::string, Plugin> solvers_;
162 
164 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
165  static std::mutex mutex_solvers_;
166 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
167 
168  protected:
170  explicit OnnxFunction(DeserializingStream& s);
171 
173  static Sparsity tensor_sparsity(const std::vector<casadi_int>& shape);
174 
176  void build_io_map();
177 
179  bool diff_in(casadi_int i) const { return is_diff_in_.empty() || is_diff_in_.at(i); }
180  bool diff_out(casadi_int i) const { return is_diff_out_.empty() || is_diff_out_.at(i); }
181 
183  static Function from_model_data(const std::string& solver, const std::string& name,
184  const std::vector<uint8_t>& model_data,
185  const std::vector<std::string>& inputs,
186  const std::vector<std::string>& outputs,
187  const Dict& opts);
188 
191  Function wrap_derivative(const std::string& name,
192  const std::vector<std::string>& inames,
193  const std::vector<std::string>& onames,
194  const std::vector<Sparsity>& in_sp,
195  const std::vector<Sparsity>& out_sp,
196  const Dict& dim_bind, const Dict& opts) const;
197 
199  std::vector<uint8_t> model_data_;
200 
202  std::vector<OnnxTensorInfo> in_, out_;
203 
205  std::vector<OnnxTensorInfo> all_in_;
206 
208  std::vector<casadi_int> in_src_;
209 
211  std::vector<double> in_val_;
212 
214  std::set<std::string> model_inputs_, model_outputs_;
215 
217  std::string fwd_dim_ = "nfwd";
218  std::string adj_dim_ = "nadj";
219 
221  std::map<std::string, std::vector<double>> input_values_;
222  };
223 
224 } // namespace casadi
225 
227 
228 #endif // CASADI_ONNX_FUNCTION_IMPL_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.