function.h#
Helpers for constructing and expanding onnx_light::FunctionProto bodies,
including onnx_light::FunctionBodyHelper and
onnx_light::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
Alias that makes onnx-light headers compatible with code that references
ONNX_LIGHT_NAMESPACE(the macro used in the standard onnx package).Set to
ONNX_LIGHT_NAMESPACEso both names resolve to the same namespace.Symbol-visibility attribute for the public onnx-light C++ API.
Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when
lib_onnx_protouses hidden visibility by default.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_light(via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs fromonnx.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.
g – Graph 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 Types
-
using NodeList = utils::RepeatedProtoField<NodeProto>#
-
using OperatorSetList = utils::RepeatedProtoField<OperatorSetIdProto>#
Public Static Functions
-
static NodeList BuildNodes(const std::vector<NodeDef> &node_defs)#
Builds NodeProto entries from lightweight node definitions.
Example:
{{"Z"}, "Add", {"X", "Y"}}modelsZ = 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_defs – Node definitions describing outputs, op type, inputs, and attributes.
- Returns:
List of materialized nodes for a function body.
-
static void BuildNodes(NodeList &nodes, const std::vector<NodeDef> &node_defs)#
Appends NodeProto entries into a repeated proto field from lightweight node definitions.
- Parameters:
nodes – Repeated proto field receiving the appended nodes.
node_defs – Node definitions describing outputs, op type, inputs, and attributes.
-
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_defs – Node definitions to materialize and append.
-
static bool BuildFunctionProto(FunctionProto &functionProto, const OpSchema &schema, const std::vector<NodeDef> &node_defs, const OperatorSetList &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_defs – Node 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<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)#
Public Members
-
AttributeProto proto#
-
AttributeProtoWrapper() = default#
-
using NodeList = utils::RepeatedProtoField<NodeProto>#
-
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#
-
inline explicit FunctionBuilder(FunctionProto &funProto_)#
-
void FunctionExpandHelper(const NodeProto &node, const FunctionProto &func, GraphProto &g, const std::string &node_prefix = "")#