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< Node > nodes () 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 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) 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

Dict opts_
 Original constructor options, retained for lazy derivative model loading. More...
 
std::string name_
 
std::string format_
 
std::string model_path_
 Absolute source filename, empty for in-memory models. More...
 
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< Node > nodes_
 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 ()
 
B 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 148 of file graph_builder.cpp.

151  : opts_(opts), name_(name), format_(format), model_data_(model_data) {
152  model_ = GraphModel(format, model_data, opts);
153  model_.fill_metadata(*this);
154  }
Dict opts_
Original constructor options, retained for lazy derivative model loading.
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 156 of file graph_builder.cpp.

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

◆ ~GraphBuilderInternal()

casadi::GraphBuilderInternal::~GraphBuilderInternal ( )
override

Definition at line 162 of file graph_builder.cpp.

162  {
163  }

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 262 of file graph_builder.cpp.

263  {
264  const Node& t = find(input_name, "input");
265  casadi_assert(shape.size() == t.dimension.size(),
266  "bind_shape: rank mismatch for '" + input_name + "'");
267  input_shapes_[input_name] = shape;
268  // Pinning a named dynamic axis also binds that dim everywhere it appears (e.g. outputs)
269  for (size_t k = 0; k < t.dimension.size(); ++k) {
270  if (t.dimension[k] < 0 && !t.dim_params[k].empty()) dim_bindings_[t.dim_params[k]] = shape[k];
271  }
272  }
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 274 of file graph_builder.cpp.

277  {
278  bool symbolic = false;
279  std::string backend = "ort";
280  Dict o;
281  for (auto&& op : opts) {
282  if (op.first == "symbolic") symbolic = op.second;
283  else if (op.first == "backend") backend = op.second.to_string();
284  else
285  o[op.first] = op.second;
286  }
287 
288  if (symbolic) {
289  casadi_assert(!model_.is_null(),
290  "GraphBuilder: symbolic create requires a parsed model (build from a file)");
291  Function f = model_.import_symbolic(*this, name);
292  if (o.empty()) return f;
293  std::vector<MX> args = f.mx_in(), res;
294  f.call(args, res, true);
295  return Function(name, args, res, f.name_in(), f.name_out(), o);
296  }
297 
298  // Numeric path: OnnxFunction freezes a snapshot directly from this builder's config
299  casadi_assert(!model_data_.empty(), "GraphBuilder: numeric create requires model bytes");
300  return OnnxFunction::create(backend, name, this, inputs, outputs, o);
301  }
const MX mx_in(casadi_int ind) const
Get symbolic primitives equivalent to the input expressions.
Definition: function.cpp:1781
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::Function::call(), casadi::OnnxFunction::create(), casadi::GraphModel::import_symbolic(), casadi::GenericShared< Shared, Internal >::is_null(), model_, model_data_, casadi::Function::mx_in(), casadi::Function::name_in(), and casadi::Function::name_out().

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

180  {
181  // Note: i != this because of something something multiple inheritance
182  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
183  }
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 317 of file graph_builder.cpp.

317  {
318  stream << "GraphBuilder '" << name_ << "': " << n_in() << " input(s), "
319  << n_out() << " output(s)";
320  if (!more) return;
321  stream << "\nInputs:";
322  for (const Node& n : nodes_) if (n.io == "input") stream << "\n " << n.get_str();
323  stream << "\nOutputs:";
324  for (const Node& n : nodes_) if (n.io == "output") stream << "\n " << n.get_str();
325  std::vector<std::string> dp = dynamic_params();
326  if (!dp.empty()) {
327  stream << "\nDynamic dimensions:";
328  for (const std::string& p : dp) stream << " " << p;
329  }
330  }
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 234 of file graph_builder.cpp.

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

References nodes_.

Referenced by disp().

◆ export_onnx()

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

Definition at line 303 of file graph_builder.cpp.

303  {
304  std::vector<uint8_t> bytes;
305  if (!fun_.is_null()) {
306  GraphModel gm(format_);
307  bytes = gm.export_symbolic(fun_, opts);
308  } else {
309  casadi_assert(!model_data_.empty(), "GraphBuilder: nothing to export");
310  bytes = model_data_;
311  }
312  std::ofstream out(filename, std::ios::binary);
313  casadi_assert(out.good(), "Cannot open output file: " + filename);
314  out.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
315  }
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 202 of file graph_builder.cpp.

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

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.

210  {
211  return static_cast<const Internal*>(this)->count;
212  }

◆ 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 227 of file graph_builder.cpp.

227  {
228  return find(name, "input").dimension;
229  }
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 207 of file graph_builder.cpp.

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

References nodes_.

Referenced by disp().

◆ n_out()

casadi_int casadi::GraphBuilderInternal::n_out ( ) const

Definition at line 212 of file graph_builder.cpp.

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

References nodes_.

Referenced by disp().

◆ name_in()

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

Definition at line 217 of file graph_builder.cpp.

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

References nodes_.

◆ name_out()

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

Definition at line 222 of file graph_builder.cpp.

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

References nodes_.

◆ node()

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

Definition at line 247 of file graph_builder.cpp.

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

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::init(), casadi::OnnxFunction::OnnxFunction(), and casadi::OnnxFunction::wrap_derivative().

◆ 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 230 of file graph_builder.cpp.

230  {
231  return find(name, "output").dimension;
232  }

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 185 of file graph_builder.cpp.

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

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 252 of file graph_builder.cpp.

253  {
254  find(input_name, "input"); // validate the name
255  input_values_[input_name] = value;
256  for (Node& n : nodes_) if (n.io == "input" && n.name == input_name) {
257  n.value = value;
258  n.baked = true;
259  }
260  }
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.

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

Member Data Documentation

◆ dim_bindings_

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

◆ format_

std::string casadi::GraphBuilderInternal::format_

Definition at line 119 of file graph_builder_internal.hpp.

Referenced by export_onnx().

◆ fun_

Function casadi::GraphBuilderInternal::fun_

Definition at line 124 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 126 of file graph_builder_internal.hpp.

Referenced by create_function(), and GraphBuilderInternal().

◆ model_data_

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

◆ model_path_

std::string casadi::GraphBuilderInternal::model_path_

◆ name_

std::string casadi::GraphBuilderInternal::name_

◆ nodes_

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

◆ opts_

Dict casadi::GraphBuilderInternal::opts_

Definition at line 116 of file graph_builder_internal.hpp.

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


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