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.

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.

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