Skip to main content

ClikaRT::io::OnnxModel

class

Header: ClikaRT/io/onnx_model.h

An ONNX model, opened from disk or built from scratch. Move-only.

Static member functions​

open()​

static OnnxModel open(std::string_view path, OnnxModelOptions options = {})

Open a .onnx file. The graph structure loads into memory (memory-mapped parse); initializer payloads referenced as external data stay on disk beside the model until something consumes them. No compute graph is built and nothing is validated beyond the wire format; compile is where operator support is decided.

Declared in ClikaRT/io/onnx_model.h, line 103

create()​

static OnnxModel create(
    std::string_view graph_name,
    std::int64_t opset_version = 0,
    OnnxModelOptions options = {}
)

Create an empty model: a named graph at the given ai.onnx opset version (and OnnxModelOptions::ir_version). A version <= 0 means "solve it": both unset → the library defaults; only the opset given → the minimum IR that supports it; only the IR given → the highest opset it supports. A fully-specified pair is validated against the official ONNX version table; an unsupported combination is an error, so a saved model is version-valid by construction. Build the graph up with add_input / add_initializer / add_node / add_output.

Declared in ClikaRT/io/onnx_model.h, line 117

from_graph()​

static OnnxModel from_graph(const graph::ModelGraph& graph, const graph::OnnxExportOptions& options = {})

Build the ONNX model of graph in memory: the model graph.export_onnx(path, options) writes, before it is saved. Inspect, edit, optimize, save or compile it like any other model. The model reads the graph's weights only when it is saved or compiled, so it holds no copy of them, and it keeps them alive for as long as it lives. Refused as ModelGraph::export_onnx is refused.

Declared in ClikaRT/io/onnx_model.h, line 131

Member functions​

OnnxModel(OnnxModel)​

OnnxModel(OnnxModel&&) noexcept

Move-only: transfers the model.

Declared in ClikaRT/io/onnx_model.h, line 137

operator=(OnnxModel)​

OnnxModel& operator=(OnnxModel&&) noexcept

Move-assign: transfers the model.

Declared in ClikaRT/io/onnx_model.h, line 139

OnnxModel(OnnxModel)​

OnnxModel(const OnnxModel&) =delete

Declared in ClikaRT/io/onnx_model.h, line 140

operator=(OnnxModel)​

OnnxModel& operator=(const OnnxModel&) =delete

Declared in ClikaRT/io/onnx_model.h, line 141

~OnnxModel()​

~OnnxModel()

Releases the parsed model.

Declared in ClikaRT/io/onnx_model.h, line 143

num_nodes()​

std::size_t num_nodes() const noexcept

graph node count

Declared in ClikaRT/io/onnx_model.h, line 146

num_initializers()​

std::size_t num_initializers() const noexcept

initializer (weight) count

Declared in ClikaRT/io/onnx_model.h, line 147

opset()​

std::int64_t opset() const noexcept

The default (ai.onnx) opset version / the model IR version.

Declared in ClikaRT/io/onnx_model.h, line 149

ir_version()​

std::int64_t ir_version() const noexcept

the file's ONNX IR version

Declared in ClikaRT/io/onnx_model.h, line 150

inputs()​

std::vector<spec::TensorSpec> inputs() const

Graph inputs / outputs as (name, dtype, dims) specs, declaration order. A dynamic dimension reads as spec::TensorSpec::kDynamicDim.

Declared in ClikaRT/io/onnx_model.h, line 155

outputs()​

std::vector<spec::TensorSpec> outputs() const

Graph outputs: the same (name, dtype, dims) spec shape as inputs(), declaration order.

Throws

  • ClikaRT::Error: when the specs cannot be read.

Declared in ClikaRT/io/onnx_model.h, line 159

nodes()​

std::vector<OnnxNodeInfo> nodes() const

Every node of the graph in graph order (a valid topological order): name, op type, domain, and the tensor names on its input and output slots. The names are what add_node returned and what remove_node takes, so a node added earlier can be found and removed by name.

Declared in ClikaRT/io/onnx_model.h, line 166

add_opset_domain()​

void add_opset_domain(std::string_view domain, std::int64_t version)

Declare an additional opset import (e.g. ("com.microsoft", 1)) for nodes added under a non-standard domain. A model imports a domain once: declaring it again keeps the higher of the two versions, as merge does.

Declared in ClikaRT/io/onnx_model.h, line 172

add_input(string_view, DataType, vector<spec::DimOrInt>)​

std::string add_input(
    std::string_view name,
    DataType dtype,
    const std::vector<spec::DimOrInt>& shape
)

Register a graph input / output; returns the tensor name. The shape mixes all three dim spellings (see spec::DimOrInt): a concrete extent, an anonymous dynamic dim (any value <= 0, each occurrence its own), and a spec::Dim object, named or identity-tied, so the SAME spec::Dim passed to several declarations ties those dims (an anonymous spec::Dim gets a systematic model-assigned name: "dim_0", "dim_1", …). The spec::TensorSpec overload takes back exactly what inputs() / outputs() (here and on graph::ModelGraph) hand out: one IO-slot vocabulary across the library.

Declared in ClikaRT/io/onnx_model.h, line 185

add_input(spec::TensorSpec)​

std::string add_input(const spec::TensorSpec& spec)

Register a graph input / output; returns the tensor name. The shape mixes all three dim spellings (see spec::DimOrInt): a concrete extent, an anonymous dynamic dim (any value <= 0, each occurrence its own), and a spec::Dim object, named or identity-tied, so the SAME spec::Dim passed to several declarations ties those dims (an anonymous spec::Dim gets a systematic model-assigned name: "dim_0", "dim_1", …). The spec::TensorSpec overload takes back exactly what inputs() / outputs() (here and on graph::ModelGraph) hand out: one IO-slot vocabulary across the library.

Declared in ClikaRT/io/onnx_model.h, line 189

add_output(string_view, DataType, vector<spec::DimOrInt>)​

std::string add_output(
    std::string_view name,
    DataType dtype,
    const std::vector<spec::DimOrInt>& shape
)

Declared in ClikaRT/io/onnx_model.h, line 194

add_output(spec::TensorSpec)​

std::string add_output(const spec::TensorSpec& spec)

Declared in ClikaRT/io/onnx_model.h, line 198

add_initializer()​

std::string add_initializer(const Tensor& value)

Create an initializer (a graph constant) from a materialized tensor's bytes. Returns the generated initializer name.

Declared in ClikaRT/io/onnx_model.h, line 205

add_node()​

std::vector<std::string> add_node(
    std::string_view op_type,
    const std::vector<std::string>& inputs,
    int num_outputs,
    const std::vector<OnnxAttr>& attrs = {},
    std::string_view domain = {},
    const std::vector<std::string>& output_names = {}
)

Append a node of any op type. inputs are tensor names in the op's positional input slots (an empty string is an omitted interior optional); each of the num_outputs outputs gets a generated name, returned in node output order, or, with a non-empty output_names (one per output), the caller's exact names (an in-use name is an error). domain empty = the standard ai.onnx domain. Any other domain must be declared first with add_opset_domain; a node under a domain the model does not import is refused with InvalidArgument, and the model is left as it was.

Declared in ClikaRT/io/onnx_model.h, line 224

remove_node()​

void remove_node(std::string_view name)

Remove the node named name (an error if absent).

Declared in ClikaRT/io/onnx_model.h, line 233

optimize()​

int optimize()

Run the ONNX graph-rewrite pipeline to a fixed point: collapse double casts, drop redundant quantize/dequantize pairs, fuse shape movement, fold constants, eliminate dead nodes. Returns the number of pipeline passes executed.

Declared in ClikaRT/io/onnx_model.h, line 240

merge()​

void merge(OnnxModel second, const std::vector<std::pair<std::string, std::string>>& io_map)

Combine second into this model (consuming it): each io_map entry (this_output, second_input) connects one of THIS model's outputs to one of second's inputs; the connected outputs stop being graph outputs and the connected inputs stop being graph inputs; everything unconnected remains, so the combined model's inputs are this model's plus second's unconnected ones, and its outputs are second's plus this model's unconnected ones. Name collisions between the two graphs are resolved automatically (the second model's colliding names are renamed); opset imports merge to the max version per domain.

Declared in ClikaRT/io/onnx_model.h, line 253

save()​

void save(std::string_view path) const

Assemble the model and write it to path. Initializer payloads ride inline or in a <stem>_data sidecar per OnnxModelOptions::save_as_external_data. A model opened with external-data references saves those references verbatim; keep its sidecar beside the saved file.

Declared in ClikaRT/io/onnx_model.h, line 265

compile()​

graph::ModelGraph compile(const graph::CompileOptions& options = {}) const

Build the executable form: parse every node into its runtime operator (an unsupported op fails here, naming it), bind the weights, resolve shapes per options (see graph::CompileOptions), and return the graph::ModelGraph as built: optimize() it when wanted, then finalize() it to run. The OnnxModel is unchanged and reusable.

Declared in ClikaRT/io/onnx_model.h, line 274