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

A mutable, format-neutral interface to a computational-graph model. More...

#include <graph_builder.hpp>

Detailed Description

Two-stage workflow: explore and configure a model (introspection, dynamic-dimension binding) with GraphBuilder, then freeze it into an immutable, evaluable Function.

GraphBuilder b("model.onnx");
b.bind_dim("batch", 4);
Function f = b.create("net");
Date
2026

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

Definition at line 49 of file graph_builder.hpp.

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

Public Types

using internal_base_type = SharedObjectInternal
 
using base_type = SharedObject
 

Public Member Functions

std::string type_name () const
 Readable name of the class. More...
 
 GraphBuilder ()
 Default constructor. More...
 
 GraphBuilder (const std::string &model_path, const Dict &opts=Dict())
 Construct from a model file (import lifecycle; format from the file suffix) More...
 
 GraphBuilder (const Function &f, const Dict &opts=Dict())
 Construct from a CasADi Function (export lifecycle) More...
 
 GraphBuilder (const std::string &name, const std::vector< uint8_t > &model_data, const std::string &format, const Dict &opts=Dict())
 Construct from model data in memory. More...
 
const std::string & name () const
 Name of the model. More...
 
Function create (const std::string &name, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict()) const
 Freeze into an evaluable Function. More...
 
Function create (const std::string &name, const Dict &opts=Dict()) const
 Freeze into an evaluable Function, exposing all model inputs and outputs. More...
 
Function create () const
 Freeze into an evaluable Function, default naming. More...
 
void export_onnx (const std::string &filename, const Dict &opts=Dict())
 Export to an ONNX model file. More...
 
std::string class_name () const
 Get class name. More...
 
void disp (std::ostream &stream, bool more=false) const
 Print a description of the object. More...
 
std::string get_str (bool more=false) const
 Get string representation. More...
 
void print_ptr (std::ostream &stream=casadi::uout()) const
 
void own (SharedObjectInternal *node)
 
void assign (SharedObjectInternal *node)
 Assign the node to a node class pointer without reference counting. More...
 
casadi_int getCount () const
 Get the reference count. More...
 
void swap (GenericShared &other)
 Swap content with another instance. More...
 
std::string debug_repr () const
 
bool is_null () const
 Is a null pointer? More...
 
casadi_int __hash__ () const
 Returns a number that is unique for a given Node. More...
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 
Model introspection
casadi_int n_in () const
 Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions. More...
 
casadi_int n_out () const
 Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions. More...
 
std::vector< std::string > name_in () const
 Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions. More...
 
std::vector< std::string > name_out () const
 Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions. More...
 
std::vector< casadi_int > dimension (const std::string &name) const
 Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions. More...
 
std::string dtype (const std::string &name) const
 Element type name of a tensor (input or output) by name (FLOAT, INT64, ...) More...
 
std::vector< std::string > dimension_param (const std::string &name) const
 Per-axis symbolic dimension name of a tensor by name ("" for static axes) More...
 
std::vector< std::string > dynamic_params () const
 Names of the symbolic/dynamic dimensions in the model. More...
 
Configuration
void bind_dim (const std::string &param, casadi_int value)
 Bind a symbolic/dynamic dimension to a concrete size. More...
 
void bind_shape (const std::string &input_name, const std::vector< casadi_int > &shape)
 Pin the full shape of an input. More...
 
void set (const std::string &input_name, const std::vector< double > &value)
 Bake a fixed value into an input; it is fed at create() and not exposed as a Function input. More...
 
void set (const std::string &input_name, double value)
 Bake a scalar value into an input. More...
 
GraphBuilderInternaloperator-> ()
 
const GraphBuilderInternaloperator-> () const
 
GraphBuilderInternalget () const
 

Protected Member Functions

void count_up ()
 
void count_down ()
 

Member Typedef Documentation

◆ base_type

Definition at line 103 of file shared_object.hpp.

◆ internal_base_type

Definition at line 102 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ GraphBuilder() [1/4]

casadi::GraphBuilder::GraphBuilder ( )

Definition at line 62 of file graph_builder.cpp.

62  {
63  }

◆ GraphBuilder() [2/4]

casadi::GraphBuilder::GraphBuilder ( const std::string &  model_path,
const Dict opts = Dict() 
)
explicit

Definition at line 65 of file graph_builder.cpp.

65  {
66  std::ifstream file(model_path, std::ios::binary | std::ios::ate);
67  casadi_assert(file.is_open(), "Cannot open model file: " + model_path);
68  std::streamsize size = file.tellg();
69  file.seekg(0, std::ios::beg);
70  std::vector<uint8_t> data(static_cast<size_t>(size));
71  casadi_assert(file.read(reinterpret_cast<char*>(data.data()), size),
72  "Cannot read model file: " + model_path);
73  own(new GraphBuilderInternal(model_path, data, format_from_path(model_path), opts));
74  }
static std::string format_from_path(const std::string &path)

References casadi::format_from_path(), and casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

◆ GraphBuilder() [3/4]

casadi::GraphBuilder::GraphBuilder ( const Function f,
const Dict opts = Dict() 
)
explicit

Definition at line 76 of file graph_builder.cpp.

76  {
77  own(new GraphBuilderInternal(f.name(), f, opts));
78  }

References casadi::Function::name(), and casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

◆ GraphBuilder() [4/4]

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

Definition at line 80 of file graph_builder.cpp.

81  {
82  own(new GraphBuilderInternal(name, model_data, format, opts));
83  }
const std::string & name() const
Name of the model.

References name(), and casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

Member Function Documentation

◆ __hash__()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::__hash__
inherited

If the Object does not point to any node, "0" is returned.

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

Definition at line 123 of file generic_shared_impl.hpp.

148  {
149  return reinterpret_cast<casadi_int>(get());
150  }

◆ assign()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::assign ( Internal *  node)
inherited

improper use will cause memory leaks!

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

Definition at line 96 of file generic_shared_impl.hpp.

86  {
87  node = node_;
88  }

◆ bind_dim()

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

Definition at line 117 of file graph_builder.cpp.

117  {
118  (*this)->bind_dim(param, value);
119  }

◆ bind_shape()

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

Definition at line 120 of file graph_builder.cpp.

121  {
122  (*this)->bind_shape(input_name, shape);
123  }

◆ class_name()

std::string casadi::SharedObject::class_name ( ) const
inherited

◆ count_down()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_down
protectedinherited

Definition at line 133 of file generic_shared_impl.hpp.

46  {
47 #ifdef WITH_EXTRA_CHECKS
48  casadi_assert_dev(Function::call_depth_==0);
49 #endif // WITH_EXTRA_CHECKS
50  if (!node) return;
51 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
52  GenericWeakRef<Shared, Internal>* weak_ref =
53  node->weak_ref_.load(std::memory_order_acquire);
54 #else
55  GenericWeakRef<Shared, Internal>* weak_ref = node->weak_ref_;
56 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
57  if (weak_ref) {
58 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
59  // get_mutex() returns a shared_ptr copy, so the mutex outlives this lock
60  // even if delete node (below) destroys the WeakRefInternal holding it
61  auto mutex = weak_ref->get_mutex();
62  // Avoid triggering a delete while a weak_ref.shared_if_alive is being called
63  std::lock_guard<std::mutex> lock(*mutex);
64 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
65 
66  if (--static_cast<Internal*>(node)->count == 0) {
67  delete node;
68  node = nullptr;
69  }
70  } else {
71  if (--static_cast<Internal*>(node)->count == 0) {
72  delete node;
73  node = nullptr;
74  }
75  }
76  }

◆ count_up()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_up
protectedinherited

Definition at line 132 of file generic_shared_impl.hpp.

36  {
37 #ifdef WITH_EXTRA_CHECKS
38  casadi_assert_dev(Function::call_depth_==0);
39 #endif // WITH_EXTRA_CHECKS
40 
41  if (node) static_cast<Internal*>(node)->count++;
42 
43  }

◆ create() [1/3]

Function casadi::GraphBuilder::create ( ) const
inline

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

Definition at line 127 of file graph_builder.hpp.

127 { return create(name() + "_graph"); }
Function create() const
Freeze into an evaluable Function, default naming.

References create().

Referenced by create().

◆ create() [2/3]

Function casadi::GraphBuilder::create ( const std::string &  name,
const Dict opts = Dict() 
) const
Parameters
nameName assigned to the resulting Function
optsSee the full create() overload

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

Definition at line 137 of file graph_builder.cpp.

137  {
138  return (*this)->create_function(name, {}, {}, opts);
139  }

References name().

◆ create() [3/3]

Function casadi::GraphBuilder::create ( const std::string &  name,
const std::vector< std::string > &  name_in,
const std::vector< std::string > &  name_out,
const Dict opts = Dict() 
) const
Parameters
nameName assigned to the resulting Function
name_inNames of the inputs to expose (empty = all model inputs)
name_outNames of the outputs to expose (empty = all model outputs)
opts"symbolic" (bool, default false) and "backend" (numeric backend, default "ort"); any remaining options pass through to the backend.

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

Definition at line 131 of file graph_builder.cpp.

134  {
135  return (*this)->create_function(name, name_in, name_out, opts);
136  }
std::vector< std::string > name_in() const
Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions.
std::vector< std::string > name_out() const
Declared shape of a tensor (input or output) by name; -1 for dynamic dimensions.

References name(), name_in(), and name_out().

Referenced by casadi::DaeBuilderInternal::load_fmi_description().

◆ debug_repr()

std::string casadi::GenericShared< SharedObject , SharedObjectInternal >::debug_repr
inherited

Definition at line 112 of file generic_shared_impl.hpp.

91  {
92  if (node) {
93  return node->debug_repr(node);
94  } else {
95  return "NULL";
96  }
97  }
std::string debug_repr(const Internal *) const

◆ dimension()

std::vector< casadi_int > casadi::GraphBuilder::dimension ( const std::string &  name) const

Definition at line 104 of file graph_builder.cpp.

104  {
105  return (*this)->node(name).dimension;
106  }

References name().

◆ dimension_param()

std::vector< std::string > casadi::GraphBuilder::dimension_param ( const std::string &  name) const

Definition at line 110 of file graph_builder.cpp.

110  {
111  return (*this)->node(name).dim_params;
112  }

References name().

◆ disp()

void casadi::SharedObject::disp ( std::ostream &  stream,
bool  more = false 
) const
inherited

Definition at line 35 of file shared_object.cpp.

35  {
36  if (is_null()) {
37  stream << "NULL";
38  } else {
39  (*this)->disp(stream, more);
40  }
41  }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::is_null().

Referenced by casadi::Nlpsol::disp_more(), and casadi::CsparseInterface::nfact().

◆ dtype()

std::string casadi::GraphBuilder::dtype ( const std::string &  name) const

Definition at line 107 of file graph_builder.cpp.

107  {
108  return (*this)->node(name).dtype;
109  }

References name().

◆ dynamic_params()

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

Definition at line 113 of file graph_builder.cpp.

113  {
114  return (*this)->dynamic_params();
115  }

◆ export_onnx()

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

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

Definition at line 140 of file graph_builder.cpp.

140  {
141  (*this)->export_onnx(filename, opts);
142  }
std::string filename(const std::string &path)
Definition: ghc.cpp:55

References casadi::filename().

◆ get()

GraphBuilderInternal * casadi::GraphBuilder::get ( ) const

Access functions of the node

Definition at line 91 of file graph_builder.cpp.

91  {
92  return static_cast<GraphBuilderInternal*>(SharedObject::get());
93  }
SharedObjectInternal * get() const
Get a const pointer to the node.

References casadi::GenericShared< SharedObject, SharedObjectInternal >::get().

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

◆ get_str()

std::string casadi::SharedObject::get_str ( bool  more = false) const
inlineinherited

Definition at line 91 of file shared_object.hpp.

91  {
92  std::stringstream ss;
93  disp(ss, more);
94  return ss.str();
95  }
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.

◆ getCount()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::getCount
inherited

Definition at line 102 of file generic_shared_impl.hpp.

138  {
139  return (*this)->getCount();
140  }

◆ is_null()

Definition at line 116 of file generic_shared_impl.hpp.

120  {
121  return node==nullptr;
122  }

◆ n_in()

casadi_int casadi::GraphBuilder::n_in ( ) const

Definition at line 100 of file graph_builder.cpp.

100 { return (*this)->n_in(); }

◆ n_out()

casadi_int casadi::GraphBuilder::n_out ( ) const

Definition at line 101 of file graph_builder.cpp.

101 { return (*this)->n_out(); }

◆ name()

const std::string & casadi::GraphBuilder::name ( ) const

Definition at line 95 of file graph_builder.cpp.

95  {
96  static std::string null = "null";
97  return is_null() ? null : (*this)->name_;
98  }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::is_null().

Referenced by create(), dimension(), dimension_param(), dtype(), and GraphBuilder().

◆ name_in()

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

Definition at line 102 of file graph_builder.cpp.

102 { return (*this)->name_in(); }

Referenced by create().

◆ name_out()

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

Definition at line 103 of file graph_builder.cpp.

103 { return (*this)->name_out(); }

Referenced by create().

◆ operator->() [1/2]

GraphBuilderInternal * casadi::GraphBuilder::operator-> ( )

Access functions of the node

Definition at line 85 of file graph_builder.cpp.

85  {
86  return static_cast<GraphBuilderInternal*>(SharedObject::operator->());
87  }
SharedObjectInternal * operator->() const
Access a member function or object.

References casadi::GenericShared< SharedObject, SharedObjectInternal >::operator->().

◆ operator->() [2/2]

const GraphBuilderInternal * casadi::GraphBuilder::operator-> ( ) const

Access functions of the node

Definition at line 88 of file graph_builder.cpp.

88  {
89  return static_cast<const GraphBuilderInternal*>(SharedObject::operator->());
90  }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::operator->().

◆ own()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::own ( Internal *  node)
inherited

Assign the node to a node class pointer (or null)

Definition at line 89 of file generic_shared_impl.hpp.

◆ print_ptr()

void casadi::SharedObject::print_ptr ( std::ostream &  stream = casadi::uout()) const
inherited

Print the pointer to the internal class

Definition at line 43 of file shared_object.cpp.

43  {
44  stream << get();
45  }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::get().

◆ set() [1/2]

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

Definition at line 124 of file graph_builder.cpp.

124  {
125  (*this)->set_value(input_name, value);
126  }

◆ set() [2/2]

void casadi::GraphBuilder::set ( const std::string &  input_name,
double  value 
)

Definition at line 127 of file graph_builder.cpp.

127  {
128  (*this)->set_value(input_name, std::vector<double>(1, value));
129  }

◆ swap()

Definition at line 105 of file generic_shared_impl.hpp.

131  {
132  GenericShared<Shared, Internal> temp = *this;
133  *this = other;
134  other = temp;
135  }

◆ type_name()

std::string casadi::GraphBuilder::type_name ( ) const
inline

Definition at line 54 of file graph_builder.hpp.

54 { return "GraphBuilder"; }

◆ weak()

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

Definition at line 130 of file generic_shared_impl.hpp.

143  {
144  return (*this)->weak();
145  }

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