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.
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 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"}}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:
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_defs – Node 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_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#
-
static std::vector<NodeProto> BuildNodes(const std::vector<NodeDef> &node_defs)#
-
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 = "")#