ort_interface.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_ORT_INTERFACE_HPP
27 #define CASADI_ORT_INTERFACE_HPP
28 
29 #include "casadi/core/onnx_function_impl.hpp"
30 #include <casadi/interfaces/ort/casadi_onnx_ort_export.h>
31 
32 #include <onnxruntime_c_api.h>
33 #include "ort_runtime.h"
34 
38 namespace casadi {
39 
46  struct OnnxRuntimeMemory : public OnnxMemory {
47  casadi_onnxruntime_data d{};
48  };
49 
61  class OnnxRuntimeInterface : public OnnxFunction {
62  public:
63  OnnxRuntimeInterface(const std::string& name,
64  const GraphBuilderInternal* gb,
65  const std::vector<std::string>& inputs,
66  const std::vector<std::string>& outputs);
67 
73  ~OnnxRuntimeInterface() override;
74 
76  static OnnxFunction* creator(const std::string& name,
77  const GraphBuilderInternal* gb,
78  const std::vector<std::string>& inputs,
79  const std::vector<std::string>& outputs,
80  const Dict& opts) {
81  return new OnnxRuntimeInterface(name, gb, inputs, outputs);
82  }
83 
84  const char* plugin_name() const override { return "ort"; }
85  std::string class_name() const override { return "OnnxRuntimeInterface"; }
86 
88 
89  static const Options options_;
90  const Options& get_options() const override { return options_; }
92 
94  void init(const Dict& opts) override;
95 
97  void* alloc_mem() const override { return new OnnxRuntimeMemory(); }
98 
100  int init_mem(void* mem) const override;
101 
103  void free_mem(void* mem) const override;
104 
106  int eval(const double** arg, double** res,
107  casadi_int* iw, double* w, void* mem) const override;
108 
110  bool has_codegen() const override { return true; }
111 
113  void codegen_declarations(CodeGenerator& g) const override;
114 
116  void codegen_body(CodeGenerator& g) const override;
117 
119  void serialize_body(SerializingStream &s) const override;
120 
122  static ProtoFunction* deserialize(DeserializingStream& s) {
123  return new OnnxRuntimeInterface(s);
124  }
125 
127  static const std::string meta_doc;
128 
129  private:
131  explicit OnnxRuntimeInterface(DeserializingStream& s);
132 
134  void ort_check(OrtStatus* status, const std::string& what) const;
135 
137  void build_prob();
138 
139  const OrtApi* ort_api_;
140  std::string provider_;
141 
142  // Backing storage for prob_ (kept alive for the runtime's lifetime); inputs cover all_in_
143  std::vector<const char*> in_names_c_, out_names_c_;
144  std::vector<casadi_int> in_elem_, out_elem_, in_ndim_, out_ndim_;
145  std::vector<casadi_int> in_dims_, out_dims_, in_numel_, out_numel_;
146  casadi_onnxruntime_prob prob_;
147  };
148 
149 } // namespace casadi
151 
152 #endif // CASADI_ORT_INTERFACE_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.