function.h#

Helpers for constructing and expanding onnx::FunctionProto bodies, including onnx::FunctionBodyHelper and onnx::FunctionBuilder.

Declares helpers for constructing and expanding ONNX FunctionProto bodies.

This header provides utility types and helper APIs to build function-body nodes in C++, attach attributes and opsets, and inline graph fragments into function definitions used by operator schemas.

namespace ONNX_LIGHT_NAMESPACE

Functions

void FunctionExpandHelper(const NodeProto &node, const FunctionProto &func, GraphProto &g, const std::string &node_prefix = "")#

Expands a function-call node into graph nodes according to a function proto.

Parameters:
  • node – Function-call node to expand.

  • func – Function definition used for expansion.

  • gGraph receiving the expanded nodes.

  • node_prefix – Prefix prepended to generated value names.

class FunctionBodyHelper#
#include <function.h>

Provides utility types and helpers to build function-body nodes.

Public Static Functions

static std::vector<NodeProto> BuildNodes(const std::vector<NodeDef> &node_defs)#

Builds NodeProto entries from lightweight node definitions.

Example:

  • {{"Z"}, "Add", {"X", "Y"}} models Z = Add(X, Y).

  • {{"Y"}, "Concat", {"X1", "X2", "X3"}, {{"axis", 1}}} adds attributes (axis=1).

  • {{"Z"}, "Foo", {"X", "Y"}, {}, "customdomain"} targets a custom-domain operator.

Parameters:

node_defsNode definitions describing outputs, op type, inputs, and attributes.

Returns:

Materialized nodes for a function body.

static void BuildNodes(FunctionProto &functionProto, const std::vector<NodeDef> &node_defs)#

Appends nodes built from node_defs into functionProto.

Parameters:
  • functionProto – Function proto receiving appended nodes.

  • node_defsNode definitions to materialize and append.

static bool BuildFunctionProto(FunctionProto &functionProto, const OpSchema &schema, const std::vector<NodeDef> &node_defs, const std::vector<OperatorSetIdProto> &relied_opsets)#

Builds a complete FunctionProto body and relied-opset list.

Parameters:
  • functionProto – Function proto to populate.

  • schema – Schema describing inputs, outputs, and attributes.

  • node_defsNode definitions used to create the function body.

  • relied_opsets – Opsets that the generated function body relies on.

Returns:

True if the function body is built successfully.

template<typename T>
static inline NodeDef Const(const std::string &name, const T &value)#

Creates a scalar Constant node from a single value.

Template Parameters:

TValue type accepted by ToTensor.

Parameters:
  • name – Output value name for the Constant node.

  • value – Scalar value stored in the Constant tensor.

Returns:

Node definition for a Constant node.

template<typename T>
static inline NodeDef Const(const std::string &name, const std::vector<T> &values)#

Creates a Constant node from a 1D vector literal.

Template Parameters:

T – Element type accepted by ToTensor.

Parameters:
  • name – Output value name for the Constant node.

  • values – Literal values stored as a 1D Constant tensor.

Returns:

Node definition for a Constant node.

struct AttributeProtoWrapper#
#include <function.h>

Wraps an attribute proto or typed attribute value.

Public Functions

AttributeProtoWrapper() = default#
inline AttributeProtoWrapper(AttributeProto attr_prot)#
template<typename T>
inline AttributeProtoWrapper(const std::string &attr_name, const T &value)#

Public Members

AttributeProto proto#
struct NodeDef#
#include <function.h>

Describes one node definition used by BuildNodes.

Public Functions

inline NodeDef(std::vector<std::string> outputs, std::string op_type, std::vector<std::string> inputs, std::vector<AttributeProtoWrapper> attributes = {}, std::string domain = "")#

Public Members

std::vector<std::string> outputs#
std::string op_type#
std::vector<std::string> inputs#
std::vector<AttributeProtoWrapper> attributes#
std::string domain#
class FunctionBuilder#
#include <function.h>

Fluent builder for FunctionProto definitions used by operator schemas.

Public Functions

inline explicit FunctionBuilder(FunctionProto &funProto_)#
inline FunctionBuilder &Add(const char *nodes_txt)#
inline FunctionBuilder &Add(const char *node_txt, const AttributeProto &attr)#
template<typename T>
inline FunctionBuilder &Add(const char *node_txt, const std::string &attr_name, const T &attr_value)#
template<typename T>
inline FunctionBuilder &AddAttributeToNode(const std::string &attr_name, const T &attr_value)#
template<typename T, typename ...Args>
inline FunctionBuilder &AddAttributes(const std::string &attr_name, const T &attr_value, Args... args)#
template<typename ...Args>
inline FunctionBuilder &Add(const char *node_txt, const Args&... args)#
inline FunctionBuilder &Const(const std::string &name, const TensorProto &tensor)#
template<typename T>
inline FunctionBuilder &Const(const std::string &name, T const_value)#
template<typename T>
inline FunctionBuilder &Const1D(const std::string &name, T const_value)#
template<typename T>
inline FunctionBuilder &Const(const std::string &name, const std::vector<T> &values)#
inline FunctionBuilder &AddOpset(const char *domain, int version)#
FunctionBuilder &AddInlinedCall(std::initializer_list<std::string_view> outputs, const GraphProto &graph, std::initializer_list<std::string_view> inputs, std::string_view prefix)#

Adds an inlined call to a graph as a sequence of nodes in the function.

This method effectively inlines the logic from the given graph into the function being constructed. It:

  • Adds a Constant node for every initializer in the graph

  • Adds a copy of every node in the graph

  • Renames formal input parameters to match actual inputs

  • Renames formal output parameters to match actual outputs

  • Renames all other intermediate values with a unique prefix

  • Leaves references to undefined names (outer scope variables) unchanged

Parameters:
  • outputs – List of output variable names for the inlined call

  • graph – The graph to inline

  • inputs – List of input variable names for the inlined call

  • prefix – Prefix to add to intermediate variable names for uniqueness

Returns:

Reference to this FunctionBuilder for method chaining

Private Members

FunctionProto &funProto#