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 
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 
123  return new OnnxRuntimeInterface(s);
124  }
125 
127  static const std::string meta_doc;
128 
129  private:
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
Helper class for C code generation.
Helper class for Serialization.
Internal class for GraphBuilder.
Black-box ONNX function base; backends (e.g. onnxruntime) derive from this.
'ort' plugin for Onnx
static const std::string meta_doc
Documentation.
int init_mem(void *mem) const override
Initialize memory block: create the ONNX Runtime session.
static OnnxFunction * creator(const std::string &name, const GraphBuilderInternal *gb, const std::vector< std::string > &inputs, const std::vector< std::string > &outputs, const Dict &opts)
Plugin factory.
void free_mem(void *mem) const override
Free memory block: tear down the session and reusable scaffolding.
int eval(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Evaluate numerically.
const char * plugin_name() const override
std::string class_name() const override
Readable name of the internal class.
void init(const Dict &opts) override
Initialize.
void codegen_declarations(CodeGenerator &g) const override
Generate code for the declarations.
static const Options options_
Options.
const Options & get_options() const override
Options.
void codegen_body(CodeGenerator &g) const override
Generate code for the body.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize into an OnnxRuntimeInterface.
OnnxRuntimeInterface(const std::string &name, const GraphBuilderInternal *gb, const std::vector< std::string > &inputs, const std::vector< std::string > &outputs)
void * alloc_mem() const override
Create memory block.
bool has_codegen() const override
Is codegen supported?
Base class for FunctionInternal and LinsolInternal.
Helper class for Serialization.
The casadi namespace.
Definition: archiver.cpp:28
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Per-checkout ONNX Runtime state (session + reusable eval scaffolding)
casadi_onnxruntime_data d
Options metadata for a class.
Definition: options.hpp:40