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 Function Inliner

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


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

Overview

Concrete tool for inlining ONNX function call nodes by replacing them with the function body while handling name conflicts, attribute binding, and opset version compatibility, provided by the ONNX library.

Description

The inliner.cc file implements the function inlining transformation for ONNX models. Function inlining replaces nodes that represent calls to model-local functions or schema-defined functions with the actual body of those functions, expanding them directly into the caller graph.

The implementation is organized around several key internal classes. OpsetMap manages opset version compatibility checking between models and functions, determining whether a function can be inlined directly or requires version conversion. NameGenerator traverses graphs and functions to collect all existing names, then generates unique names to avoid collisions during inlining. InliningRenamer extends the visitor pattern to perform systematic renaming of all variables and node names in the inlined function body, using a two-level scheme: first a suffix specific to the call site (e.g., "__1", "__2") is appended, then an iterative uniqueness check ensures no collisions with pre-existing names.

ComputeInputs identifies all input variables used by a node, including implicit inputs referenced in subgraphs of control-flow operators. This is needed for version conversion, which wraps the function body in a temporary model.

The core InlinerImpl struct orchestrates the entire inlining process. For each node, it checks whether the node should be inlined (via GetCallee), retrieves the function definition from either the model-local function map or the schema registry, binds attributes from the call site, renames variables, optionally converts opset versions, and appends the function body nodes in place of the original call node. The process handles recursive inlining (functions calling other functions) and properly processes subgraphs in control-flow operators.

The public API provides three entry points: InlineLocalFunctions inlines all model-local functions with compatible opset versions; InlineSelectedLocalFunctions inlines only specified functions; and InlineSelectedFunctions supports both local and schema-defined functions. A Renamer class is also exposed for external use, wrapping the InliningRenamer with a clean public interface.

Usage

Use this module when you need to flatten ONNX models by replacing function calls with their implementations. This is essential for model optimization (enabling cross-function optimizations), deployment to runtimes that do not support custom functions, and model analysis tools that need a flat graph representation. Version conversion is automatically handled when function opset requirements differ from the model.

Code Reference

Source Location

Signature

// Public API functions
void InlineLocalFunctions(ModelProto& model, bool convert_version);

void InlineSelectedLocalFunctions(ModelProto& model, const FunctionIdSet& to_inline);

void InlineSelectedFunctions(ModelProto& model, const FunctionIdSet& to_inline);

void InlineSelectedFunctions(
    ModelProto& model,
    const FunctionIdSet& to_inline,
    const ISchemaRegistry* schema_registry);

// FunctionIdSet factory
std::unique_ptr<FunctionIdSet> FunctionIdSet::Create(
    FunctionIdVector&& function_ids,
    bool invert);

// Renamer class for external use
class Renamer {
public:
    Renamer(const std::string& prefix, const GraphProto& graph);
    Renamer(const std::string& prefix, const FunctionProto& function);
    ~Renamer();
    void BindName(const std::string& formal_name, const std::string& actual_name);
    void RenameNode(NodeProto& node);
    std::string BindToUniqueName(const std::string& original_name);
};

Import

#include "onnx/inliner/inliner.h"

I/O Contract

Inputs

Name Type Required Description
model ModelProto& Yes The ONNX model containing functions to inline; modified in-place
convert_version bool Yes (for InlineLocalFunctions) Whether to perform opset version conversion when function and model opsets differ
to_inline const FunctionIdSet& Yes (for selected inlining) Set of function identifiers (domain, name) to inline
schema_registry const ISchemaRegistry* No Schema registry for looking up schema-defined functions; defaults to OpSchemaRegistry::Instance()
function_ids FunctionIdVector&& Yes (for FunctionIdSet::Create) Vector of (domain, name) pairs identifying functions
invert bool Yes (for FunctionIdSet::Create) If true, the set contains all functions except those listed

Outputs

Name Type Description
(modified model) ModelProto& The model is modified in-place: call nodes are replaced with function body nodes, inlined functions are removed from the model's function list
function_id_set std::unique_ptr<FunctionIdSet> A function ID set for use with selective inlining APIs

Usage Examples

#include "onnx/inliner/inliner.h"

// Inline all model-local functions (with version conversion)
ModelProto model = LoadModel("model.onnx");
ONNX_NAMESPACE::inliner::InlineLocalFunctions(model, /*convert_version=*/true);

// Inline all model-local functions (without version conversion)
ONNX_NAMESPACE::inliner::InlineLocalFunctions(model, /*convert_version=*/false);

// Inline only specific functions by domain and name
auto to_inline = ONNX_NAMESPACE::inliner::FunctionIdSet::Create(
    {{"my.domain", "MyFunction"}, {"my.domain", "AnotherFunction"}},
    /*invert=*/false);
ONNX_NAMESPACE::inliner::InlineSelectedLocalFunctions(model, *to_inline);

// Inline selected functions including schema-defined functions
auto to_inline2 = ONNX_NAMESPACE::inliner::FunctionIdSet::Create(
    {{"", "Gelu"}},
    /*invert=*/false);
ONNX_NAMESPACE::inliner::InlineSelectedFunctions(model, *to_inline2);

// Inline all functions except specific ones (using invert=true)
auto keep_these = ONNX_NAMESPACE::inliner::FunctionIdSet::Create(
    {{"my.domain", "KeepThisFunction"}},
    /*invert=*/true);
ONNX_NAMESPACE::inliner::InlineSelectedLocalFunctions(model, *keep_these);

// Using the Renamer class directly
ONNX_NAMESPACE::inliner::Renamer renamer("suffix", model.graph());
renamer.BindName("old_name", "new_name");
NodeProto node;
renamer.RenameNode(node);

Related Pages

Page Connections

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