Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Onnx Onnx FunctionExpandHelper

From Leeroopedia
Revision as of 16:12, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Onnx_Onnx_FunctionExpandHelper.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Model Optimization, Function Expansion
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for expanding ONNX function nodes into their constituent graph operations, provided by the ONNX library.

Description

The FunctionExpandHelper function is the core implementation for expanding function-based operator nodes into their underlying graph primitives. When an ONNX model contains a node that references a function definition (FunctionProto), this utility inlines the function body into a target graph, performing all necessary name mapping, attribute resolution, and tensor name generation.

The expansion process involves several steps. First, it builds a mapping from the function's formal input and output names to the actual input and output names provided by the calling node. It then resolves attributes by copying those provided on the node call and filling in default values from the operator schema for any attributes not explicitly specified. Finally, it iterates through each node in the function body, copying them into the target graph while remapping all input names, output names, and attribute references according to the established mappings. Internal tensor names that do not correspond to function inputs or outputs are given unique names using the InternalTensorNameGenerator helper, which prefixes them with "Func_" and the node name to avoid collisions.

This file also implements FunctionBodyHelper::BuildNodes for converting high-level NodeDef structures into NodeProto objects, FunctionBodyHelper::BuildFunctionProto for constructing complete FunctionProto objects from node definitions and opset dependencies, and FunctionBuilder::AddInlinedCall for inlining an entire graph into a function being built.

Usage

Use FunctionExpandHelper when you need to expand a function-based operator node into its primitive operations within a graph. This is typically needed during graph optimization passes, when preparing a model for execution on runtimes that do not support custom functions, or when performing analysis that requires visibility into function internals. Use FunctionBodyHelper::BuildNodes and BuildFunctionProto when programmatically defining operator function bodies as part of schema registration.

Code Reference

Source Location

Signature

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

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

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

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

FunctionBuilder& FunctionBuilder::AddInlinedCall(
    std::initializer_list<std::string_view> outputs,
    const GraphProto& graph,
    std::initializer_list<std::string_view> inputs,
    std::string_view prefix);

Import

#include "onnx/defs/function.h"

I/O Contract

Inputs

Name Type Required Description
node const NodeProto& Yes The function call node to be expanded
func const FunctionProto& Yes The function definition containing the body to expand
g GraphProto& Yes The target graph where expanded nodes will be added
node_prefix const std::string& No Optional prefix for generating unique internal tensor names (defaults to empty string, in which case the memory address of the node is used)

Outputs

Name Type Description
(mutated graph) GraphProto& The target graph g is modified in-place with the expanded function nodes appended

Usage Examples

// Expand a function node into a graph
NodeProto function_call_node;
FunctionProto func_definition;
GraphProto target_graph;

// Populate function_call_node and func_definition...

FunctionExpandHelper(function_call_node, func_definition, target_graph);

// Build nodes from high-level definitions
std::vector<FunctionBodyHelper::NodeDef> node_defs = {
    {{"Z"}, "Add", {"X", "Y"}},
    {{"W"}, "Relu", {"Z"}},
};
std::vector<NodeProto> nodes = FunctionBodyHelper::BuildNodes(node_defs);

// Build a complete function proto
FunctionProto func_proto;
OpSchema schema;
std::vector<OperatorSetIdProto> opsets;
FunctionBodyHelper::BuildFunctionProto(func_proto, schema, node_defs, opsets);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment