Implementation:Onnx Onnx Inliner Cpp API
| Knowledge Sources | |
|---|---|
| Domains | Model Optimization, Function Inlining |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for inlining ONNX function calls at the C++ level, providing the core inlining API with selective function targeting and variable renaming, provided by the ONNX library.
Description
This header defines the public C++ API for ONNX function inlining. It provides interfaces for replacing function call nodes in a model with the actual function implementations, effectively flattening the computation graph hierarchy.
FunctionId is defined as a pair of strings (domain, function name) used to identify functions. FunctionIdVector is a vector of these identifiers.
FunctionIdSet is an abstract interface for representing sets of function identifiers. It provides a Contains method for membership testing and a static factory method Create that constructs a set from a FunctionIdVector, with an optional invert flag to represent the complement (all functions except those specified).
The header provides several inlining functions:
InlineSelectedFunctions (three-parameter overload) inlines schema-defined functions that are in the provided set. It accepts a schema registry for function lookup and only processes call-sites in the main graph.
InlineSelectedLocalFunctions inlines model-local functions that are in the provided set. This is the recommended function for inlining model-local functions, as it avoids confusion with the schema-defined function variant.
InlineSelectedFunctions (two-parameter overload, ⚠️ DEPRECATED) is equivalent to InlineSelectedLocalFunctions and is retained for backward compatibility. Use InlineSelectedLocalFunctions instead.
InlineLocalFunctions inlines all model-local functions in the model, with an optional convert_version parameter for automatic opset version conversion.
Renamer is a utility class that manages variable renaming during graph inlining operations. It uses the Pimpl (pointer to implementation) pattern to hide internal details. The Renamer can be constructed with a prefix and either a GraphProto or FunctionProto context. It provides BindName for mapping formal parameter names to actual names, BindToUniqueName for generating unique names with automatic prefix application, and RenameNode for updating all variable references in a node according to current bindings.
Usage
Use InlineLocalFunctions when you want to inline all model-local functions in a model. Use InlineSelectedLocalFunctions with a FunctionIdSet when you need selective inlining of specific model-local functions. Use the three-parameter InlineSelectedFunctions when you need to inline schema-defined functions as well. Use the Renamer class when implementing custom inlining logic that requires variable name management, such as when building functions with FunctionBuilder::AddInlinedCall.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/inliner/inliner.h
Signature
using FunctionId = std::pair<std::string, std::string>;
using FunctionIdVector = std::vector<FunctionId>;
class FunctionIdSet {
public:
virtual bool Contains(const std::string& function_domain,
const std::string& function_name) const = 0;
virtual ~FunctionIdSet() = default;
static std::unique_ptr<FunctionIdSet> Create(
FunctionIdVector&& function_ids, bool invert = false);
};
void InlineSelectedFunctions(ModelProto& model, const FunctionIdSet& to_inline,
const ISchemaRegistry* schema_registry);
void InlineSelectedLocalFunctions(ModelProto& model, const FunctionIdSet& to_inline);
void InlineSelectedFunctions(ModelProto& model, const FunctionIdSet& to_inline); // deprecated
void InlineLocalFunctions(ModelProto& model, bool convert_version = false);
class Renamer {
public:
explicit Renamer(const std::string& prefix, const GraphProto& graph);
explicit Renamer(const std::string& prefix, const FunctionProto& function);
~Renamer();
void BindName(const std::string& formal_name, const std::string& actual_name);
std::string BindToUniqueName(const std::string& original_name);
void RenameNode(NodeProto& node);
};
Import
#include "onnx/inliner/inliner.h"
I/O Contract
Inputs (InlineLocalFunctions)
| Name | Type | Required | Description |
|---|---|---|---|
| model | ModelProto& | Yes | The ONNX model to process; modified in-place |
| convert_version | bool | No | Enable automatic opset version conversion for inlined functions (default: false) |
Inputs (InlineSelectedLocalFunctions)
| Name | Type | Required | Description |
|---|---|---|---|
| model | ModelProto& | Yes | The ONNX model to process; modified in-place |
| to_inline | const FunctionIdSet& | Yes | The set of functions to inline, created via FunctionIdSet::Create |
Inputs (InlineSelectedFunctions with schema registry)
| Name | Type | Required | Description |
|---|---|---|---|
| model | ModelProto& | Yes | The ONNX model to process; modified in-place |
| to_inline | const FunctionIdSet& | Yes | The set of functions to inline |
| schema_registry | const ISchemaRegistry* | Yes | Schema registry for function lookup; use nullptr for the default registry |
Outputs
| Name | Type | Description |
|---|---|---|
| (mutated model) | ModelProto& | The model is modified in-place with function calls replaced by their implementations |
Usage Examples
#include "onnx/inliner/inliner.h"
using namespace ONNX_NAMESPACE::inliner;
ModelProto model;
// ... load model ...
// Inline all model-local functions
InlineLocalFunctions(model);
// Inline all model-local functions with version conversion
InlineLocalFunctions(model, /*convert_version=*/true);
// Inline only specific functions
FunctionIdVector ids = {{"my.domain", "MyOp"}, {"my.domain", "AnotherOp"}};
auto to_inline = FunctionIdSet::Create(std::move(ids));
InlineSelectedLocalFunctions(model, *to_inline);
// Inline all functions except specific ones
FunctionIdVector exclude_ids = {{"my.domain", "KeepThisOp"}};
auto to_inline_inverted = FunctionIdSet::Create(std::move(exclude_ids), /*invert=*/true);
InlineSelectedLocalFunctions(model, *to_inline_inverted);
// Inline schema-defined functions using the default registry
auto schema_set = FunctionIdSet::Create(FunctionIdVector{{"", "Gelu"}});
InlineSelectedFunctions(model, *schema_set, nullptr);
// Use Renamer for custom inlining logic
Renamer renamer("prefix_", graph);
renamer.BindName("formal_input", "actual_input");
std::string unique = renamer.BindToUniqueName("intermediate");
renamer.RenameNode(node);