List of all members | Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes
casadi::GraphModelInternal Class Referenceabstract

Base interface for format-specific graph-model backends. More...

#include <graph_model_internal.hpp>

Detailed Description

Subclasses (e.g. Onnx) own the heavy dependency and implement metadata fill, symbolic import and symbolic export. Registered as plugins by format name.

Date
2026

Extra doc: https://github.com/casadi/casadi/wiki/L_2ji

Definition at line 47 of file graph_model_internal.hpp.

Inheritance diagram for casadi::GraphModelInternal:
Inheritance graph
[legend]
Collaboration diagram for casadi::GraphModelInternal:
Collaboration graph
[legend]

Classes

struct  Exposed
 No statically-exposed plugin functions. More...
 

Public Types

typedef GraphModelInternal *(* Creator) (const std::vector< uint8_t > &model_data)
 Plugin creator function type. More...
 
using weak_ref_type = WeakRefInternal
 
typedef int(* RegFcn) (Plugin *plugin)
 

Public Member Functions

 GraphModelInternal (const std::vector< uint8_t > &model_data)
 
 ~GraphModelInternal () override
 
std::string class_name () const override
 Readable name of the internal class. More...
 
void disp (std::ostream &stream, bool more) const override
 Print a description of the object. More...
 
void construct (const Dict &opts)
 Prepare the backend for use. More...
 
virtual void init (const Dict &opts)
 Initialize. More...
 
virtual void fill_metadata (GraphBuilderInternal &gb) const =0
 Populate a builder's Node metadata from the parsed model. More...
 
virtual Function import_symbolic (const GraphBuilderInternal &gb, const std::string &name)=0
 Rebuild the graph as a CasADi Function (symbolic import; mutates the backend's engine) More...
 
virtual std::vector< uint8_t > export_symbolic (const Function &f, const Dict &opts)=0
 Serialize a CasADi Function as model bytes (symbolic export; mutates the backend's engine) More...
 
const std::vector< uint8_t > & model_data () const
 Raw model bytes. More...
 
const char * plugin_name () const override=0
 Query plugin name. More...
 
casadi_int getCount () const
 Get the reference count. More...
 
std::string debug_repr (const SharedObjectInternal *) const
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 
virtual void deps_version_check (const std::string &stage) const
 
void serialize_type (SerializingStream &s) const
 Serialize type information. More...
 

Static Public Member Functions

static std::string shortname ()
 Short name. More...
 
static bool has_plugin (const std::string &pname, bool verbose=false)
 Check if a plugin is available or can be loaded. More...
 
static const Optionsplugin_options (const std::string &pname)
 Get the plugin options. More...
 
static Deserialize plugin_deserialize (const std::string &pname)
 Get the plugin deserialize_map. More...
 
static Plugin pluginFromRegFcn (RegFcn regfcn)
 Instantiate a Plugin struct from a factory function. More...
 
static Plugin load_plugin (const std::string &pname, bool register_plugin=true, bool needs_lock=true)
 Load a plugin dynamically. More...
 
static handle_t load_library (const std::string &libname, std::string &resultpath, bool global)
 Load a library dynamically. More...
 
static void registerPlugin (const Plugin &plugin, bool needs_lock=true)
 Register an integrator in the factory. More...
 
static void registerPlugin (RegFcn regfcn, bool needs_lock=true)
 Register an integrator in the factory. More...
 
static Plugin & getPlugin (const std::string &pname)
 Load and get the creator function. More...
 
static GraphModelInternalinstantiate (const std::string &fname, const std::string &pname, Problem problem)
 
static ProtoFunctiondeserialize (DeserializingStream &s)
 Deserialize with type disambiguation. More...
 

Static Public Attributes

static std::map< std::string, Plugin > solvers_
 Collection of available format plugins. More...
 
static const std::string infix_ = "graphmodel"
 Infix used to form plugin registration symbols (casadi_register_graphmodel_<name>) More...
 

Protected Member Functions

void initSingleton ()
 
void destroySingleton ()
 
shared_from_this ()
 Get a shared object from the current internal object. More...
 
const B shared_from_this () const
 Get a shared object from the current internal object. More...
 

Protected Attributes

std::vector< uint8_t > model_data_
 Raw model bytes (empty when constructed for export only) More...
 
bool verbose_
 Verbose – for debugging. More...
 
static const Options options_
 Options. More...
 
virtual const Optionsget_options () const
 Options. More...
 

Member Typedef Documentation

◆ Creator

typedef GraphModelInternal*(* casadi::GraphModelInternal::Creator) (const std::vector< uint8_t > &model_data)

Definition at line 58 of file graph_model_internal.hpp.

◆ RegFcn

typedef int(* casadi::PluginInterface< GraphModelInternal >::RegFcn) (Plugin *plugin)
inherited

Definition at line 73 of file plugin_interface.hpp.

◆ weak_ref_type

Definition at line 152 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ GraphModelInternal()

casadi::GraphModelInternal::GraphModelInternal ( const std::vector< uint8_t > &  model_data)
explicit

Definition at line 83 of file graph_model.cpp.

84  : model_data_(model_data), verbose_(false) {
85  }
const std::vector< uint8_t > & model_data() const
Raw model bytes.
bool verbose_
Verbose – for debugging.
std::vector< uint8_t > model_data_
Raw model bytes (empty when constructed for export only)

◆ ~GraphModelInternal()

casadi::GraphModelInternal::~GraphModelInternal ( )
override

Definition at line 87 of file graph_model.cpp.

87  {
88  }

Member Function Documentation

◆ class_name()

std::string casadi::GraphModelInternal::class_name ( ) const
inlineoverridevirtual

Implements casadi::SharedObjectInternal.

Reimplemented in casadi::Onnx.

Definition at line 54 of file graph_model_internal.hpp.

54 { return "GraphModelInternal"; }

◆ construct()

void casadi::GraphModelInternal::construct ( const Dict opts)

Definition at line 96 of file graph_model.cpp.

96  {
97  for (auto&& op : opts) {
98  if (op.first == "verbose") verbose_ = op.second;
99  }
100  init(opts);
101  }
virtual void init(const Dict &opts)
Initialize.

References init(), and verbose_.

◆ debug_repr()

std::string casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::debug_repr ( const Internal *  i) const
inherited

Definition at line 62 of file generic_shared_internal.hpp.

175  {
176  // Note: i != this because of something something multiple inheritance
177  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
178  }
std::string str(const T &v)
String representation, any type.

◆ deps_version_check()

virtual void casadi::PluginInterface< GraphModelInternal >::deps_version_check ( const std::string &  stage) const
inlinevirtualinherited

Definition at line 112 of file plugin_interface.hpp.

112 {}

◆ deserialize()

static ProtoFunction* casadi::PluginInterface< GraphModelInternal >::deserialize ( DeserializingStream s)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_rr

Definition at line 124 of file plugin_interface.hpp.

124  {
125  std::string class_name, plugin_name;
126  s.unpack("PluginInterface::plugin_name", plugin_name);
128  return deserialize(s);
129  }
static Deserialize plugin_deserialize(const std::string &pname)
Get the plugin deserialize_map.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
virtual const char * plugin_name() const=0
ProtoFunction *(* Deserialize)(DeserializingStream &)

◆ destroySingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::destroySingleton ( )
inlineprotectedinherited

Called in the destructor of singletons

Definition at line 77 of file generic_shared_internal.hpp.

77  {
78  static_cast<Internal*>(this)->count--;
79  }

◆ disp()

void casadi::GraphModelInternal::disp ( std::ostream &  stream,
bool  more 
) const
overridevirtual

Implements casadi::SharedObjectInternal.

Definition at line 106 of file graph_model.cpp.

106  {
107  stream << "GraphModel(" << plugin_name() << ")";
108  }
const char * plugin_name() const override=0
Query plugin name.

References plugin_name().

◆ export_symbolic()

virtual std::vector<uint8_t> casadi::GraphModelInternal::export_symbolic ( const Function f,
const Dict opts 
)
pure virtual

Implemented in casadi::Onnx.

◆ fill_metadata()

virtual void casadi::GraphModelInternal::fill_metadata ( GraphBuilderInternal gb) const
pure virtual

Implemented in casadi::Onnx.

◆ get_options()

virtual const Options& casadi::GraphModelInternal::get_options ( ) const
inlinevirtual

Extra doc: https://github.com/casadi/casadi/wiki/L_2jj

Reimplemented in casadi::Onnx.

Definition at line 68 of file graph_model_internal.hpp.

68 { return options_; }
static const Options options_
Options.

◆ getCount()

Definition at line 60 of file generic_shared_internal.hpp.

205  {
206  return static_cast<const Internal*>(this)->count;
207  }

◆ getPlugin()

PluginInterface< GraphModelInternal >::Plugin & casadi::PluginInterface< GraphModelInternal >::getPlugin ( const std::string &  pname)
staticinherited

Definition at line 102 of file plugin_interface.hpp.

295  {
296 
297 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
298  std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
299 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
300 
301  // Check if the solver has been loaded
302  auto it=Derived::solvers_.find(pname);
303 
304  // Load the solver if needed
305  if (it==Derived::solvers_.end()) {
306  load_plugin(pname, true, false);
307  it=Derived::solvers_.find(pname);
308  }
309  casadi_assert_dev(it!=Derived::solvers_.end());
310  return it->second;
311  }
static Plugin load_plugin(const std::string &pname, bool register_plugin=true, bool needs_lock=true)
Load a plugin dynamically.

◆ has_plugin()

bool casadi::PluginInterface< GraphModelInternal >::has_plugin ( const std::string &  pname,
bool  verbose = false 
)
staticinherited

Definition at line 76 of file plugin_interface.hpp.

134  {
135 
136 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
137  std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
138 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
139 
140  // Quick return if available
141  if (Derived::solvers_.find(pname) != Derived::solvers_.end()) {
142  return true;
143  }
144 
145  // Try loading the plugin
146  try {
147  (void)load_plugin(pname, false, false);
148  return true;
149  } catch (CasadiException& ex) {
150  if (verbose) {
151  casadi_warning(ex.what());
152  }
153  return false;
154  }
155  }
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros

◆ import_symbolic()

virtual Function casadi::GraphModelInternal::import_symbolic ( const GraphBuilderInternal gb,
const std::string &  name 
)
pure virtual

Implemented in casadi::Onnx.

Referenced by casadi::GraphModel::import_symbolic().

◆ init()

void casadi::GraphModelInternal::init ( const Dict opts)
virtual

Extra doc: https://github.com/casadi/casadi/wiki/L_2jk

Reimplemented in casadi::Onnx.

Definition at line 103 of file graph_model.cpp.

103  {
104  }

Referenced by construct(), and casadi::Onnx::init().

◆ initSingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::initSingleton ( )
inlineprotectedinherited

Called in the constructor of singletons to avoid that the counter reaches zero

Definition at line 71 of file generic_shared_internal.hpp.

71  {
72  casadi_assert_dev(static_cast<Internal*>(this)->count==0);
73  static_cast<Internal*>(this)->count++;
74  }

◆ instantiate()

GraphModelInternal * casadi::PluginInterface< GraphModelInternal >::instantiate ( const std::string &  fname,
const std::string &  pname,
Problem  problem 
)
staticinherited

Definition at line 106 of file plugin_interface.hpp.

317  {
318 
319  // Assert the plugin exists (needed for adaptors)
320  if (!has_plugin(pname, true)) {
321  casadi_error("Plugin '" + pname + "' is not found.");
322  }
323  return getPlugin(pname).creator(fname, problem);
324  }
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.

◆ load_library()

handle_t casadi::PluginInterface< GraphModelInternal >::load_library ( const std::string &  libname,
std::string &  resultpath,
bool  global 
)
staticinherited

Definition at line 92 of file plugin_interface.hpp.

187  {
188 
189 #ifndef WITH_DL
190  casadi_error("WITH_DL option needed for dynamic loading");
191 #else // WITH_DL
192 
193  // Get the name of the shared library
194  std::string lib = std::string(CasadiMeta::shared_library_prefix()) + libname +
195  CasadiMeta::shared_library_suffix();
196 
197  // Build up search paths;
198  std::vector<std::string> search_paths = get_search_paths();
199  return open_shared_library(lib, search_paths, resultpath,
200  "PluginInterface::load_plugin", global);
201 
202 #endif // WITH_DL
203  }
std::vector< std::string > get_search_paths()
Definition: casadi_os.cpp:79

◆ load_plugin()

PluginInterface< GraphModelInternal >::Plugin casadi::PluginInterface< GraphModelInternal >::load_plugin ( const std::string &  pname,
bool  register_plugin = true,
bool  needs_lock = true 
)
staticinherited

Definition at line 88 of file plugin_interface.hpp.

208  {
209 
210 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
211  casadi::conditional_lock_guard<std::mutex> lock(Derived::mutex_solvers_, needs_lock);
212 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
213 
214  // Issue warning and quick return if already loaded
215  if (Derived::solvers_.find(pname) != Derived::solvers_.end()) {
216  casadi_warning("PluginInterface: Solver " + pname + " is already in use. Ignored.");
217  return Plugin();
218  }
219 
220  // Logger singletons are lazily instantiated on first uout()/uerr() calls
221  // This instantation may lead to a data race with potential instatiations in plugin
222  // To be safe, trigger instantatin before any plugin loading
223  uout();
224  uerr();
225 
226 #ifndef WITH_DL
227  casadi_error("WITH_DL option needed for dynamic loading");
228 #else // WITH_DL
229  // Retrieve the registration function
230  RegFcn reg;
231 
232  // Load the dll
233  std::string regName = "casadi_register_" + Derived::infix_ + "_" + pname;
234 
235  std::string searchpath;
236  handle_t handle = load_library("casadi_" + Derived::infix_ + "_" + pname, searchpath,
237  false);
238 
239 #ifdef _WIN32
240 
241 #if __GNUC__
242 #pragma GCC diagnostic push
243 #pragma GCC diagnostic ignored "-Wcast-function-type"
244 #endif
245  reg = reinterpret_cast<RegFcn>(GetProcAddress(handle, TEXT(regName.c_str())));
246 #if __GNUC__
247 #pragma GCC diagnostic pop
248 #endif
249 
250 #else // _WIN32
251  // Reset error
252  dlerror();
253 
254  // Load creator
255  reg = reinterpret_cast<RegFcn>(dlsym(handle, regName.c_str()));
256 #endif // _WIN32
257  casadi_assert(reg!=nullptr,
258  "PluginInterface::load_plugin: no \"" + regName + "\" found in " + searchpath + ".");
259 
260  // Create a temporary struct
261  Plugin plugin = pluginFromRegFcn(reg);
262  // Register the plugin
263  if (register_plugin) {
264  registerPlugin(plugin, false);
265  }
266 
267  return plugin;
268 
269 #endif // WITH_DL
270  }
static handle_t load_library(const std::string &libname, std::string &resultpath, bool global)
Load a library dynamically.
static Plugin pluginFromRegFcn(RegFcn regfcn)
Instantiate a Plugin struct from a factory function.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
std::ostream & uerr()
void * handle_t
Definition: casadi_os.hpp:109
std::ostream & uout()

◆ model_data()

const std::vector<uint8_t>& casadi::GraphModelInternal::model_data ( ) const
inline

Definition at line 90 of file graph_model_internal.hpp.

90 { return model_data_; }

Referenced by casadi::Onnx::Onnx().

◆ plugin_deserialize()

Deserialize casadi::PluginInterface< GraphModelInternal >::plugin_deserialize ( const std::string &  pname)
staticinherited

Definition at line 82 of file plugin_interface.hpp.

165  {
166  Deserialize m = getPlugin(pname).deserialize;
167  casadi_assert(m, "Plugin \"" + pname + "\" does not support deserialize");
168  return m;
169  }

◆ plugin_name()

const char* casadi::GraphModelInternal::plugin_name ( ) const
overridepure virtual

Implements casadi::PluginInterface< GraphModelInternal >.

Implemented in casadi::Onnx.

Referenced by disp().

◆ plugin_options()

const Options & casadi::PluginInterface< GraphModelInternal >::plugin_options ( const std::string &  pname)
staticinherited

Definition at line 79 of file plugin_interface.hpp.

158  {
159  const Options *op = getPlugin(pname).options;
160  casadi_assert(op!=nullptr, "Plugin \"" + pname + "\" does not support options");
161  return *op;
162  }

◆ pluginFromRegFcn()

PluginInterface< GraphModelInternal >::Plugin casadi::PluginInterface< GraphModelInternal >::pluginFromRegFcn ( RegFcn  regfcn)
staticinherited

Definition at line 85 of file plugin_interface.hpp.

173  {
174  // Create a temporary struct
175  Plugin plugin;
176 
177  // Set the fields
178  int flag = regfcn(&plugin);
179  casadi_assert(flag==0, "Registration of plugin failed.");
180 
181  return plugin;
182  }

◆ registerPlugin() [1/2]

void casadi::PluginInterface< GraphModelInternal >::registerPlugin ( const Plugin &  plugin,
bool  needs_lock = true 
)
staticinherited

Definition at line 96 of file plugin_interface.hpp.

278  {
279 
280 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
281  casadi::conditional_lock_guard<std::mutex> lock(Derived::mutex_solvers_, needs_lock);
282 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
283 
284  // Check if the solver name is in use
285  typename std::map<std::string, Plugin>::iterator it=Derived::solvers_.find(plugin.name);
286  casadi_assert(it==Derived::solvers_.end(),
287  "Solver " + str(plugin.name) + " is already in use");
288 
289  // Add to list of solvers
290  Derived::solvers_[plugin.name] = plugin;
291  }

◆ registerPlugin() [2/2]

void casadi::PluginInterface< GraphModelInternal >::registerPlugin ( RegFcn  regfcn,
bool  needs_lock = true 
)
staticinherited

Definition at line 99 of file plugin_interface.hpp.

273  {
274  registerPlugin(pluginFromRegFcn(regfcn), needs_lock);
275  }

◆ serialize_type()

void casadi::PluginInterface< GraphModelInternal >::serialize_type ( SerializingStream s) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_rq

Definition at line 117 of file plugin_interface.hpp.

117  {
118  s.pack("PluginInterface::plugin_name", std::string(plugin_name()));
119  }

◆ shared_from_this() [1/2]

B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( )
inlineprotectedinherited

Definition at line 83 of file generic_shared_internal.hpp.

83  {
84  casadi_assert_dev(B::test_cast(static_cast<Internal*>(this)));
85  B ret;
86  ret.own(static_cast<Internal*>(this));
87  return ret;
88  }

◆ shared_from_this() [2/2]

const B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( ) const
inlineprotectedinherited

Definition at line 92 of file generic_shared_internal.hpp.

92  {
93  casadi_assert_dev(B::test_cast(static_cast<const Internal*>(this)));
94  B ret;
95  ret.own(const_cast<Internal*>(static_cast<const Internal*>(this)));
96  return ret;
97  }

◆ shortname()

static std::string casadi::GraphModelInternal::shortname ( )
inlinestatic

Definition at line 106 of file graph_model_internal.hpp.

106 { return "graphmodel"; }

◆ weak()

Extra doc: https://github.com/casadi/casadi/wiki/L_1ai

Definition at line 67 of file generic_shared_internal.hpp.

210  {
211 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
212  auto* w = weak_ref_.load(std::memory_order_acquire);
213  if (!w) {
214  auto* nw = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
215  GenericWeakRef<Shared, Internal>* expected = nullptr;
216  if (weak_ref_.compare_exchange_strong(
217  expected, nw, std::memory_order_release, std::memory_order_acquire)) {
218  w = nw;
219  } else {
220  delete nw; // lost the race; another thread published first
221  w = expected;
222  }
223  }
224  return w;
225 #else
226  if (weak_ref_==nullptr) {
227  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
228  }
229  return weak_ref_;
230 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
231  }

Member Data Documentation

◆ infix_

const std::string casadi::GraphModelInternal::infix_ = "graphmodel"
static

Definition at line 103 of file graph_model_internal.hpp.

◆ model_data_

std::vector<uint8_t> casadi::GraphModelInternal::model_data_
protected

Definition at line 110 of file graph_model_internal.hpp.

◆ options_

const Options casadi::GraphModelInternal::options_
static
Initial value:
= {{},
{{"verbose",
{OT_BOOL, "Verbose evaluation -- for debugging"}}}
}

Extra doc: https://github.com/casadi/casadi/wiki/L_2jj

Definition at line 67 of file graph_model_internal.hpp.

◆ solvers_

std::map< std::string, GraphModelInternal::Plugin > casadi::GraphModelInternal::solvers_
static

Definition at line 96 of file graph_model_internal.hpp.

◆ verbose_

bool casadi::GraphModelInternal::verbose_
protected

The documentation for this class was generated from the following files: