List of all members | Public Types | Public Member Functions | Public Attributes | Protected Member Functions
casadi::GraphBuilderInternal Class Reference

Internal class for GraphBuilder. More...

#include <graph_builder_internal.hpp>

Detailed Description

Single mutable holder of graph metadata + configuration; dependency-free. Metadata is populated either from a parsed model (via a GraphModel backend) or from a source Function.

Date
2026

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

Definition at line 65 of file graph_builder_internal.hpp.

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

Public Types

using weak_ref_type = WeakRefInternal
 

Public Member Functions

 GraphBuilderInternal (const std::string &name, const std::vector< uint8_t > &model_data, const std::string &format, const Dict &opts)
 Construct from parsed model bytes of a given format. More...
 
 GraphBuilderInternal (const std::string &name, const Function &f, const Dict &opts)
 Construct from a source Function (export lifecycle) More...
 
 ~GraphBuilderInternal () 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...
 
casadi_int n_in () const
 
casadi_int n_out () const
 
std::vector< std::string > name_in () const
 
std::vector< std::string > name_out () const
 
std::vector< casadi_int > input_shape (const std::string &name) const
 
std::vector< casadi_int > output_shape (const std::string &name) const
 
std::vector< std::string > dynamic_params () const
 
Node node (const std::string &name) const
 
std::vector< Nodenodes () const
 
void bind_dim (const std::string &param, casadi_int value)
 
void bind_shape (const std::string &input_name, const std::vector< casadi_int > &shape)
 
void set_value (const std::string &input_name, const std::vector< double > &value)
 
Function create_function (const std::string &name, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts) const
 
void export_onnx (const std::string &filename, const Dict &opts)
 
void add_node (const Node &n)
 Append a tensor descriptor (called by a backend during fill_metadata) More...
 
void clear_nodes ()
 Drop all tensor descriptors (called by a backend before re-filling) More...
 
std::vector< casadi_int > resolved_shape (const Node &n) const
 
const Nodefind (const std::string &name, const std::string &io) const
 Locate a node by name in a given I/O role (throws if absent) 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...
 
const std::vector< Node > & node_list () const
 
const std::map< std::string, casadi_int > & dim_bindings () const
 

Public Attributes

std::string name_
 
std::string format_
 
std::vector< uint8_t > model_data_
 
Function fun_
 Source Function (export lifecycle); null when built from a model. More...
 
GraphModel model_
 Parsed model backend (import lifecycle); null when built from a Function. More...
 
std::vector< Nodenodes_
 Tensor metadata (inputs followed by outputs) More...
 
std::map< std::string, casadi_int > dim_bindings_
 Pending configuration carried into create() More...
 
std::map< std::string, std::vector< casadi_int > > input_shapes_
 
std::map< std::string, std::vector< double > > input_values_
 

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...
 

Member Typedef Documentation

◆ weak_ref_type

Definition at line 152 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ GraphBuilderInternal() [1/2]

casadi::GraphBuilderInternal::GraphBuilderInternal ( const std::string &  name,
const std::vector< uint8_t > &  model_data,
const std::string &  format,
const Dict opts 
)

Definition at line 146 of file graph_builder.cpp.

149  : name_(name), format_(format), model_data_(model_data) {
150  model_ = GraphModel(format, model_data, opts);
151  model_.fill_metadata(*this);
152  }
GraphModel model_
Parsed model backend (import lifecycle); null when built from a Function.
void fill_metadata(GraphBuilderInternal &gb) const
Populate a builder's Node metadata from the parsed model.
Definition: graph_model.cpp:53

References casadi::GraphModel::fill_metadata(), and model_.

◆ GraphBuilderInternal() [2/2]

casadi::GraphBuilderInternal::GraphBuilderInternal ( const std::string &  name,
const Function f,
const Dict opts 
)

Definition at line 154 of file graph_builder.cpp.

156  : name_(name), format_("onnx"), fun_(f) {
157  populate_from_function();
158  }
Function fun_
Source Function (export lifecycle); null when built from a model.

◆ ~GraphBuilderInternal()

casadi::GraphBuilderInternal::~GraphBuilderInternal ( )
override

Definition at line 160 of file graph_builder.cpp.

160  {
161  }

Member Function Documentation

◆ add_node()

void casadi::GraphBuilderInternal::add_node ( const Node n)
inline

Definition at line 98 of file graph_builder_internal.hpp.

98 { nodes_.push_back(n); }
std::vector< Node > nodes_
Tensor metadata (inputs followed by outputs)

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

◆ bind_dim()

void casadi::GraphBuilderInternal::bind_dim ( const std::string &  param,
casadi_int  value 
)
inline

Definition at line 87 of file graph_builder_internal.hpp.

87 { dim_bindings_[param] = value; }
std::map< std::string, casadi_int > dim_bindings_
Pending configuration carried into create()

◆ bind_shape()

void casadi::GraphBuilderInternal::bind_shape ( const std::string &  input_name,
const std::vector< casadi_int > &  shape 
)

Definition at line 260 of file graph_builder.cpp.

261  {
262  const Node& t = find(input_name, "input");
263  casadi_assert(shape.size() == t.dimension.size(),
264  "bind_shape: rank mismatch for '" + input_name + "'");
265  input_shapes_[input_name] = shape;
266  // Pinning a named dynamic axis also binds that dim everywhere it appears (e.g. outputs)
267  for (size_t k = 0; k < t.dimension.size(); ++k) {
268  if (t.dimension[k] < 0 && !t.dim_params[k].empty()) dim_bindings_[t.dim_params[k]] = shape[k];
269  }
270  }
const Node & find(const std::string &name, const std::string &io) const
Locate a node by name in a given I/O role (throws if absent)
std::map< std::string, std::vector< casadi_int > > input_shapes_

References dim_bindings_, casadi::Node::dim_params, casadi::Node::dimension, find(), and input_shapes_.

◆ class_name()

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

Implements casadi::SharedObjectInternal.

Definition at line 74 of file graph_builder_internal.hpp.

74 { return "GraphBuilderInternal"; }

◆ clear_nodes()

void casadi::GraphBuilderInternal::clear_nodes ( )
inline

Definition at line 100 of file graph_builder_internal.hpp.

100 { nodes_.clear(); }

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

◆ create_function()

Function casadi::GraphBuilderInternal::create_function ( const std::string &  name,
const std::vector< std::string > &  name_in,
const std::vector< std::string > &  name_out,
const Dict opts 
) const

Definition at line 272 of file graph_builder.cpp.

275  {
276  bool symbolic = false;
277  std::string backend = "ort";
278  Dict o;
279  for (auto&& op : opts) {
280  if (op.first == "symbolic") symbolic = op.second;
281  else if (op.first == "backend") backend = op.second.to_string();
282  else
283  o[op.first] = op.second;
284  }
285 
286  if (symbolic) {
287  casadi_assert(!model_.is_null(),
288  "GraphBuilder: symbolic create requires a parsed model (build from a file)");
289  return model_.import_symbolic(*this, name);
290  }
291 
292  // Numeric path: OnnxFunction freezes a snapshot directly from this builder's config
293  casadi_assert(!model_data_.empty(), "GraphBuilder: numeric create requires model bytes");
294  return OnnxFunction::create(backend, name, this, inputs, outputs, o);
295  }
bool is_null() const
Is a null pointer?
Function import_symbolic(const GraphBuilderInternal &gb, const std::string &name) const
Symbolic import: rebuild the graph as a CasADi Function.
Definition: graph_model.cpp:56
static Function create(const std::string &solver, const std::string &name, const GraphBuilderInternal *gb, const std::vector< std::string > &inputs, const std::vector< std::string > &outputs, const Dict &opts)
Plugin factory.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.

References casadi::OnnxFunction::create(), casadi::GraphModel::import_symbolic(), casadi::GenericShared< Shared, Internal >::is_null(), model_, and model_data_.

◆ 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.

◆ 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  }

◆ dim_bindings()

const std::map<std::string, casadi_int>& casadi::GraphBuilderInternal::dim_bindings ( ) const
inline

Read-only views for backends

Definition at line 105 of file graph_builder_internal.hpp.

105 { return dim_bindings_; }

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

◆ disp()

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

Implements casadi::SharedObjectInternal.

Definition at line 311 of file graph_builder.cpp.

311  {
312  stream << "GraphBuilder '" << name_ << "': " << n_in() << " input(s), "
313  << n_out() << " output(s)";
314  if (!more) return;
315  stream << "\nInputs:";
316  for (const Node& n : nodes_) if (n.io == "input") stream << "\n " << n.get_str();
317  stream << "\nOutputs:";
318  for (const Node& n : nodes_) if (n.io == "output") stream << "\n " << n.get_str();
319  std::vector<std::string> dp = dynamic_params();
320  if (!dp.empty()) {
321  stream << "\nDynamic dimensions:";
322  for (const std::string& p : dp) stream << " " << p;
323  }
324  }
std::vector< std::string > dynamic_params() const

References dynamic_params(), n_in(), n_out(), name_, and nodes_.

◆ dynamic_params()

std::vector< std::string > casadi::GraphBuilderInternal::dynamic_params ( ) const

Definition at line 232 of file graph_builder.cpp.

232  {
233  std::vector<std::string> r;
234  for (const Node& n : nodes_) {
235  for (size_t k = 0; k < n.dimension.size(); ++k) {
236  if (n.dimension[k] < 0 && !n.dim_params[k].empty() &&
237  std::find(r.begin(), r.end(), n.dim_params[k]) == r.end()) {
238  r.push_back(n.dim_params[k]);
239  }
240  }
241  }
242  return r;
243  }

References nodes_.

Referenced by disp().

◆ export_onnx()

void casadi::GraphBuilderInternal::export_onnx ( const std::string &  filename,
const Dict opts 
)

Definition at line 297 of file graph_builder.cpp.

297  {
298  std::vector<uint8_t> bytes;
299  if (!fun_.is_null()) {
300  GraphModel gm(format_);
301  bytes = gm.export_symbolic(fun_, opts);
302  } else {
303  casadi_assert(!model_data_.empty(), "GraphBuilder: nothing to export");
304  bytes = model_data_;
305  }
306  std::ofstream out(filename, std::ios::binary);
307  casadi_assert(out.good(), "Cannot open output file: " + filename);
308  out.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
309  }
std::string filename(const std::string &path)
Definition: ghc.cpp:55

References casadi::GraphModel::export_symbolic(), casadi::filename(), format_, fun_, casadi::GenericShared< Shared, Internal >::is_null(), and model_data_.

◆ find()

const Node & casadi::GraphBuilderInternal::find ( const std::string &  name,
const std::string &  io 
) const

Definition at line 200 of file graph_builder.cpp.

200  {
201  for (const Node& n : nodes_) if (n.io == io && n.name == name) return n;
202  casadi_error("Graph tensor '" + name + "' (" + io + ") not found in model '" + name_ + "'");
203  }

References name_, and nodes_.

Referenced by bind_shape(), input_shape(), output_shape(), and set_value().

◆ getCount()

Definition at line 60 of file generic_shared_internal.hpp.

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

◆ 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  }

◆ input_shape()

std::vector< casadi_int > casadi::GraphBuilderInternal::input_shape ( const std::string &  name) const

Definition at line 225 of file graph_builder.cpp.

225  {
226  return find(name, "input").dimension;
227  }
std::vector< casadi_int > dimension
Declared shape, -1 for dynamic dimensions.

References casadi::Node::dimension, and find().

◆ n_in()

casadi_int casadi::GraphBuilderInternal::n_in ( ) const

Definition at line 205 of file graph_builder.cpp.

205  {
206  casadi_int c = 0;
207  for (const Node& n : nodes_) if (n.io == "input") ++c;
208  return c;
209  }

References nodes_.

Referenced by disp().

◆ n_out()

casadi_int casadi::GraphBuilderInternal::n_out ( ) const

Definition at line 210 of file graph_builder.cpp.

210  {
211  casadi_int c = 0;
212  for (const Node& n : nodes_) if (n.io == "output") ++c;
213  return c;
214  }

References nodes_.

Referenced by disp().

◆ name_in()

std::vector< std::string > casadi::GraphBuilderInternal::name_in ( ) const

Definition at line 215 of file graph_builder.cpp.

215  {
216  std::vector<std::string> r;
217  for (const Node& n : nodes_) if (n.io == "input") r.push_back(n.name);
218  return r;
219  }

References nodes_.

◆ name_out()

std::vector< std::string > casadi::GraphBuilderInternal::name_out ( ) const

Definition at line 220 of file graph_builder.cpp.

220  {
221  std::vector<std::string> r;
222  for (const Node& n : nodes_) if (n.io == "output") r.push_back(n.name);
223  return r;
224  }

References nodes_.

◆ node()

Node casadi::GraphBuilderInternal::node ( const std::string &  name) const

Definition at line 245 of file graph_builder.cpp.

245  {
246  for (const Node& n : nodes_) if (n.name == name) return n;
247  casadi_error("Graph tensor '" + name + "' not found in model '" + name_ + "'");
248  }

References name_, and nodes_.

◆ node_list()

const std::vector<Node>& casadi::GraphBuilderInternal::node_list ( ) const
inline

Read-only views for backends

Definition at line 104 of file graph_builder_internal.hpp.

104 { return nodes_; }

Referenced by casadi::OnnxFunction::OnnxFunction().

◆ nodes()

std::vector<Node> casadi::GraphBuilderInternal::nodes ( ) const
inline

Definition at line 85 of file graph_builder_internal.hpp.

85 { return nodes_; }

◆ output_shape()

std::vector< casadi_int > casadi::GraphBuilderInternal::output_shape ( const std::string &  name) const

Definition at line 228 of file graph_builder.cpp.

228  {
229  return find(name, "output").dimension;
230  }

References casadi::Node::dimension, and find().

◆ resolved_shape()

std::vector< casadi_int > casadi::GraphBuilderInternal::resolved_shape ( const Node n) const

Resolve a node's declared shape to concrete sizes: input_shapes_ override (inputs), else dynamic axes bound via dim_bindings_ (named) or defaulted to 1

Definition at line 183 of file graph_builder.cpp.

183  {
184  if (n.io == "input") {
185  auto ov = input_shapes_.find(n.name);
186  if (ov != input_shapes_.end()) return ov->second; // explicit override
187  }
188  std::vector<casadi_int> shape;
189  for (size_t k = 0; k < n.dimension.size(); ++k) {
190  casadi_int d = n.dimension[k];
191  if (d < 0) { // dynamic: bound name else default 1
192  auto it = dim_bindings_.find(n.dim_params[k]);
193  d = (it != dim_bindings_.end()) ? it->second : 1;
194  }
195  shape.push_back(d);
196  }
197  return shape;
198  }

References dim_bindings_, casadi::Node::dim_params, casadi::Node::dimension, input_shapes_, casadi::Node::io, and casadi::Node::name.

Referenced by casadi::OnnxFunction::OnnxFunction().

◆ set_value()

void casadi::GraphBuilderInternal::set_value ( const std::string &  input_name,
const std::vector< double > &  value 
)

Definition at line 250 of file graph_builder.cpp.

251  {
252  find(input_name, "input"); // validate the name
253  input_values_[input_name] = value;
254  for (Node& n : nodes_) if (n.io == "input" && n.name == input_name) {
255  n.value = value;
256  n.baked = true;
257  }
258  }
std::map< std::string, std::vector< double > > input_values_

References find(), input_values_, and nodes_.

◆ 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  }

◆ 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

◆ dim_bindings_

std::map<std::string, casadi_int> casadi::GraphBuilderInternal::dim_bindings_

◆ format_

std::string casadi::GraphBuilderInternal::format_

Definition at line 116 of file graph_builder_internal.hpp.

Referenced by export_onnx().

◆ fun_

Function casadi::GraphBuilderInternal::fun_

Definition at line 120 of file graph_builder_internal.hpp.

Referenced by export_onnx().

◆ input_shapes_

std::map<std::string, std::vector<casadi_int> > casadi::GraphBuilderInternal::input_shapes_

◆ input_values_

std::map<std::string, std::vector<double> > casadi::GraphBuilderInternal::input_values_

◆ model_

GraphModel casadi::GraphBuilderInternal::model_

Definition at line 122 of file graph_builder_internal.hpp.

Referenced by create_function(), and GraphBuilderInternal().

◆ model_data_

std::vector<uint8_t> casadi::GraphBuilderInternal::model_data_

◆ name_

std::string casadi::GraphBuilderInternal::name_

Definition at line 115 of file graph_builder_internal.hpp.

Referenced by disp(), find(), and node().

◆ nodes_

std::vector<Node> casadi::GraphBuilderInternal::nodes_

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