Implementation:Onnx Onnx Inliner Python API
| Knowledge Sources | |
|---|---|
| Domains | Model Optimization, Function Inlining |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for inlining function calls in ONNX models from Python, provided by the ONNX library.
Description
This module provides the Python API for ONNX function inlining. It wraps the high-performance C++ inlining implementation (exposed via onnx.onnx_cpp2py_export.inliner) with a Pythonic interface. Function inlining replaces function call nodes in an ONNX model with the actual implementation of those functions, effectively flattening the computation graph.
inline_local_functions recursively inlines all model-local function definitions. It accepts an optional convert_version parameter that, when enabled, applies automatic version conversion to functions requiring a different ONNX opset version from the model. The function serializes the model to bytes, invokes the C++ inlining engine, and deserializes the result.
inline_selected_functions provides fine-grained control over which functions are inlined. It accepts a list of function identifiers as (domain, name) tuples and an exclude flag that inverts the selection (inline everything except the specified functions). The inline_schema_functions parameter controls whether schema-defined functions (not just model-local functions) are also eligible for inlining. Internally, this routes to either C.inline_selected_functions or C.inline_selected_functions2 depending on whether schema function inlining is requested.
Usage
Use inline_local_functions when you want to flatten all model-local functions in a model, for example when preparing a model for deployment on a runtime that does not support custom functions. Use inline_selected_functions when you need selective inlining, such as inlining only specific functions while keeping others as function calls for modularity. Enable convert_version when inlining functions that may use different opset versions than the model.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/inliner.py
Signature
def inline_local_functions(
model: onnx.ModelProto, convert_version: bool = False
) -> onnx.ModelProto
def inline_selected_functions(
model: onnx.ModelProto,
function_ids: list[tuple[str, str]],
exclude: bool = False,
inline_schema_functions: bool = False,
) -> onnx.ModelProto
Import
from onnx.inliner import inline_local_functions, inline_selected_functions
I/O Contract
Inputs (inline_local_functions)
| Name | Type | Required | Description |
|---|---|---|---|
| model | onnx.ModelProto | Yes | The ONNX model containing function definitions and function call nodes |
| convert_version | bool | No | If True, applies automatic version conversion for functions using different opset versions (default: False) |
Inputs (inline_selected_functions)
| Name | Type | Required | Description |
|---|---|---|---|
| model | onnx.ModelProto | Yes | The ONNX model containing function definitions and function call nodes |
| function_ids | list[tuple[str, str]] | Yes | List of (domain, function_name) tuples identifying functions to include or exclude |
| exclude | bool | No | If True, inlines all functions except those in function_ids; if False, inlines only those in function_ids (default: False) |
| inline_schema_functions | bool | No | If True, also inlines schema-defined functions in addition to model-local functions (default: False) |
Outputs
| Name | Type | Description |
|---|---|---|
| inlined_model | onnx.ModelProto | A new ModelProto with the specified functions inlined recursively |
Usage Examples
import onnx
from onnx.inliner import inline_local_functions, inline_selected_functions
# Load a model with function definitions
model = onnx.load("model_with_functions.onnx")
# Inline all model-local functions
inlined = inline_local_functions(model)
onnx.save(inlined, "model_inlined.onnx")
# Inline with version conversion enabled
inlined_vc = inline_local_functions(model, convert_version=True)
# Inline only specific functions by domain and name
inlined_selected = inline_selected_functions(
model,
function_ids=[("my.domain", "MyCustomOp"), ("my.domain", "AnotherOp")],
)
# Inline everything except specific functions
inlined_except = inline_selected_functions(
model,
function_ids=[("my.domain", "KeepThisFunction")],
exclude=True,
)
# Include schema-defined functions in inlining
inlined_schema = inline_selected_functions(
model,
function_ids=[("", "Gelu")],
inline_schema_functions=True,
)