onnx_helper.h#

namespace ONNX_LIGHT_NAMESPACE

Functions

offset_t PopulateExternalData(ModelProto &model, size_t threshold, const std::string &external_data_location, bool use_external_data_location = true, int64_t max_external_file_size = 0, int64_t alignment = 0)#

The function populates external data for every tensor. The function does not remove anything from the model.

Parameters:
  • model – Model to update.

  • threshold – Minimum raw_data size (in bytes) to switch to external storage.

  • external_data_location – Relative or absolute path to the external weights file.

  • use_external_data_location – If true, tensors already marked as EXTERNAL keep their current external_data.location instead of being reassigned.

  • max_external_file_size – Maximum size in bytes for one external weights file. If > 0, tensors are split across multiple files by appending .1, .2

  • alignment – If > 0, each tensor’s offset within its weights file is rounded up to the nearest multiple of alignment bytes. Use 4096 for mmap-friendly page alignment.

Returns:

The total number of bytes in the external weights file(s), including any padding.

void ClearExternalData(ModelProto &model)#

Clears the external data from the model.

Parameters:

model – Model to update.

std::shared_ptr<uint8_t[]> ConsolidateTensorsToBuffer(ModelProto &model, const TensorBufferOptions &opts = TensorBufferOptions{})#

Transfers all tensor raw_data whose size is >= opts.raw_data_threshold into a single contiguous buffer owned via a shared_ptr, updating each qualifying tensor’s raw_data to borrow from that buffer. The buffer is kept alive by the shared_ptr stored inside each tensor’s ByteSpan; the caller does not need to retain the returned shared_ptr for the tensors to remain valid.

Mirrors the no-copy external-data loading scenario: each tensor borrows a slice of a single shared buffer, avoiding per-tensor allocations.

Parameters:
  • model – Model whose tensors will be consolidated in-place.

  • opts – Options controlling the size threshold and byte alignment.

    • raw_data_threshold: only tensors with raw_data.size() >= this value are moved.

    • alignment: if > 0, each tensor’s offset is padded to a multiple of this value.

Returns:

Shared ownership handle for the consolidated buffer, or nullptr if no tensors qualified. The buffer lifetime is also managed by the individual tensors.

template<typename T>
inline void SerializeProtoToStream(T&, utils::BinaryWriteStream&, SerializeOptions&, bool clear_external_data = true)#

The function saves the ONNX model to a binary stream. When external weights are written, temporary external_data metadata is removed by default (clear_external_data=true), so two-file serialization leaves ModelProto unchanged after the call.

Template Parameters:

T – ONNX proto type to serialize.

Parameters:
  • stream – Output stream.

  • options – Serialization options.

  • clear_external_data – If true, removes temporary external_data metadata after serialization.

void SerializeModelProtoToStream(ModelProto &model, utils::BinaryWriteStream &stream, SerializeOptions &options, bool clear_external_data = true)#

The function saves the ONNX model to a binary stream. When external weights are written, temporary external_data metadata is removed by default (clear_external_data=true), so two-file serialization leaves ModelProto unchanged after the call.

Parameters:
  • model – Model to serialize.

  • stream – Output stream.

  • options – Serialization options.

  • clear_external_data – If true, removes temporary external_data metadata after serialization.

template<>
inline void SerializeProtoToStream(ModelProto &model, utils::BinaryWriteStream &stream, SerializeOptions &options, bool clear_external_data)#

Specializes SerializeProtoToStream for ModelProto.

bool ReadIntegerValues(const TensorProto &tensor_proto, std::vector<int64_t> &out)#

Extracts integer payload values from a :cpp:class:TensorProto.

The function first reads from the type-specific repeated fields (int64_data, int32_data, uint64_data) when present, and otherwise falls back to decoding raw_data in little-endian order, as required by ONNX.

Supported element types are INT8/16/32/64 and UINT8/16/32/64.

Parameters:
  • tensor_protoTensor to read integer payload values from.

  • out – Output vector receiving extracted values in storage order. Cleared before being filled.

Returns:

true on successful extraction, false when tensor data is absent or the tensor type/encoding is not supported.

template<typename T>
inline void ParseProtoFromStream(T&, utils::BinaryStream&, ParseOptions&, bool clear_external_data = true)#

The function reads the ONNX model from a binary stream. If external weights is triggered, the model is modified to add external data.

Template Parameters:

T – ONNX proto type to parse.

Parameters:
  • stream – Input stream.

  • options – Parsing options.

  • clear_external_data – If true, removes temporary external_data metadata after parsing.

void ParseModelProtoFromStream(ModelProto &model, utils::BinaryStream &stream, ParseOptions &options, bool clear_external_data = true)#

The function reads the ONNX model from a binary stream. If external weights is triggered, the model is modified to add external data.

Parameters:
  • model – Model to parse.

  • stream – Input stream.

  • options – Parsing options.

  • clear_external_data – If true, removes temporary external_data metadata after parsing.

template<>
inline void ParseProtoFromStream(ModelProto &model, utils::BinaryStream &stream, ParseOptions &options, bool clear_external_data)#

Specializes ParseProtoFromStream for ModelProto.

template<typename ProtoT, typename Range>
inline void AddInputs(ProtoT &proto, const Range &names)#

Appends a batch of input names to proto in a single call.

Works with any ONNX proto exposing an add_input member that accepts the elements of names (typically NodeProto, FunctionProto and any other proto with a FIELD_REPEATED_STR(input, ...) field). Allows passing an std::initializer_list<const char *>, std::vector<std::string> or any other range whose elements are accepted by add_input.

Template Parameters:
  • ProtoT – ONNX proto type with an add_input member function.

  • Range – Range whose elements are accepted by ProtoT::add_input.

Parameters:
  • proto – Proto to append names to.

  • names – Range of input names to append, in order.

template<typename ProtoT, typename T>
inline void AddInputs(ProtoT &proto, std::initializer_list<T> names)#

initializer_list overload of :ref:AddInputs so call sites can pass a brace-enclosed list of names directly (e.g. AddInputs(node, {"a", "b"})) without specifying the template arguments explicitly.

template<typename ProtoT, typename Range>
inline void AddOutputs(ProtoT &proto, const Range &names)#

Appends a batch of output names to proto in a single call. See :ref:AddInputs for the requirements on ProtoT and Range.

Template Parameters:
  • ProtoT – ONNX proto type with an add_output member function.

  • Range – Range whose elements are accepted by ProtoT::add_output.

Parameters:
  • proto – Proto to append names to.

  • names – Range of output names to append, in order.

template<typename ProtoT, typename T>
inline void AddOutputs(ProtoT &proto, std::initializer_list<T> names)#

initializer_list overload of :ref:AddOutputs.

template<typename ProtoT>
inline void AddFloatAttribute(ProtoT &proto, const char *name, float value)#

Appends a single FLOAT attribute (name -> value) to proto.

Works with any ONNX proto exposing an add_attribute member that returns an AttributeProto * (typically NodeProto).

Template Parameters:

ProtoT – ONNX proto type with an add_attribute member function.

Parameters:
  • proto – Proto to append the attribute to.

  • name – Attribute name.

  • value – Attribute float value.

template<typename T>
AttributeProto *AddAttribute(NodeProto &node, const char *name, const T &value)#

Appends an AttributeProto carrying value named name to node and returns a pointer to the newly added attribute. The proto field used and the recorded AttributeProto::AttributeType are inferred from T. Specializations are provided for the most common attribute payloads (int64_t, float, strings, and homogeneous vectors thereof).

Template Parameters:

T – Attribute value type.

Parameters:
  • node – Target node.

  • name – Attribute name.

  • value – Attribute value.

Returns:

Pointer to the newly added attribute.

template<>
inline AttributeProto *AddAttribute(NodeProto &node, const char *name, const int64_t &value)#
template<>
inline AttributeProto *AddAttribute(NodeProto &node, const char *name, const float &value)#
template<>
inline AttributeProto *AddAttribute(NodeProto &node, const char *name, const std::string &value)#
template<>
inline AttributeProto *AddAttribute(NodeProto &node, const char *name, const std::vector<int64_t> &values)#
template<>
inline AttributeProto *AddAttribute(NodeProto &node, const char *name, const std::vector<float> &values)#
template<>
inline AttributeProto *AddAttribute(NodeProto &node, const char *name, const std::vector<std::string> &values)#
inline const AttributeProto *FindAttribute(const NodeProto &node, const char *name)#

Returns a pointer to the first attribute of node whose name equals name, or nullptr when no such attribute exists. The returned pointer is non-owning and remains valid for the lifetime of node.

Parameters:
  • nodeNode to scan.

  • name – Attribute name to look up (null-terminated C string).

Returns:

Pointer to the matching attribute, or nullptr if absent.

template<typename T>
T GetAttributeOr(const NodeProto &node, const char *name, const T &default_value)#

Returns the value of the scalar attribute name of node, or default_value when the attribute is absent. The proto accessor used to read the value is inferred from T. Specializations are provided for int64_t, float, and std::string.

Template Parameters:

T – Attribute scalar type.

Parameters:
  • nodeNode to scan.

  • name – Attribute name.

  • default_valueValue returned when the attribute is missing.

template<>
inline int64_t GetAttributeOr(const NodeProto &node, const char *name, const int64_t &default_value)#
template<>
inline float GetAttributeOr(const NodeProto &node, const char *name, const float &default_value)#
template<>
inline std::string GetAttributeOr(const NodeProto &node, const char *name, const std::string &default_value)#
inline bool GetAttributeInts(const NodeProto &node, const char *name, std::vector<int64_t> &out)#

Reads the repeated INTS attribute name of node. When present its values are appended to out in order and the function returns true; otherwise out is left unchanged and the function returns false.

Parameters:
  • nodeNode to scan.

  • name – Attribute name.

  • out – Destination vector. Values are appended (existing content is preserved).

Returns:

true when the attribute was found, false otherwise.

const GraphProto &FindGraphAttribute(const NodeProto &node, const char *attr_name, const char *context = nullptr)#

Returns a reference to the GraphProto carried by the attribute named attr_name on node. Throws std::invalid_argument when the attribute is missing or does not hold a GraphProto. The optional context string is prefixed to the thrown error message (e.g. the name of the caller) for diagnostic purposes.

Parameters:
  • nodeNode to inspect.

  • attr_name – Name of the attribute to look up.

  • context – Optional caller context used as a prefix in error messages.

Returns:

Const reference to the GraphProto attribute.

class IteratorTensorProto#
#include <onnx_helper.h>

IteratorTensorProto is an iterator that traverses all TensorProto objects.

Public Functions

inline explicit IteratorTensorProto(GraphProto *graph)#

Initializes the iterator from a graph root.

Parameters:

graph – Root graph to traverse.

inline TensorProto &operator*()#

Returns the current tensor reference.

inline TensorProto *operator->()#

Returns the current tensor pointer.

bool next()#

Advances to the next tensor. Returns true when one is found.

Private Members

TensorProto *tp_#

Stores the current tensor found by the traversal.

std::vector<Position> positions_#

Stores the DFS traversal stack.

struct Position#
#include <onnx_helper.h>

Tracks traversal indices for one graph level in the DFS stack.

Public Members

GraphProto *graph#

Points to the graph traversed at this stack level.

int node_index = 0#

Stores the current node index in graph->ref_node().

int attr_index = 0#

Stores the current attribute index in node->ref_attribute().

int node_initializer_index = 0#

Stores the current initializer index in graph->ref_initializer().