ONNX Function Decorator Guidelines¶
This guide summarizes the current behaviour and guardrails for the @onnx_function
decorator. It complements the plugin quickstart by detailing how function reuse,
namespacing, and testing conventions work in practice.
Goals and Defaults¶
@onnx_functionmarks a callable as an ONNX function boundary. Each invocation emits aFunctionProtoin the exported model.- By default, every call-site receives its own function instance; the node’s
op_typemirrors the callable name, and the domain uses the"custom"namespace. - Optional flags allow you to reuse function bodies (
unique=True) and control the domain prefix (namespace=...), without sacrificing readability in tools like Netron.
Flags¶
unique=Truede-duplicates call-sites that share the same capture signature.namespaceoverrides the domain prefix. When omitted, it defaults to"custom".type(preferred) orname(alias) overrides the human-readable base name/op_type used for the function. When omitted, the callable’s Python name is used.
Domain Naming Rules¶
- Non-unique functions use
{namespace}.{base}.{counter}. - Unique functions use
{namespace}.{base}.uniquefor the first instance and{namespace}.{base}.unique.{N}for additional variants when the capture differs. op_typeequals the sanitized base name (type/namewhen provided, otherwise the callable name) so node “types” stay human-friendly.
Examples (default namespace):
| Setting | First FunctionProto Domain | Second Variant |
|---|---|---|
| unique=False | custom.MyBlock.1 |
custom.MyBlock.2 |
| unique=True | custom.MyBlock.unique |
custom.MyBlock.unique.2 |
Custom namespace:
Produces domain="my.model.square.unique" for all reused call-sites.
Reuse Mechanics¶
- Function identity considers:
- Qualified target name.
- Input shapes/dtypes from the parent equation.
- Captured parameters. For classes, we fingerprint the module state via
jax.tree_util.tree_flatten. - When
unique=True, call-sites with identical captures share the sameFunctionProto; otherwise each cooperative invocation gets its own domain suffix.
Testing & Examples¶
- The regression suite lives at
tests/extra_tests/converter/test_onnx_function_unique.py. It covers duplicate reuse, distinct captures, and custom namespaces. - Example
onnx_functions_017demonstrates two call-sites sharing a unique function. - Regenerate fixtures after behaviour changes:
Best Practices¶
- Use
construct_and_call(...).with_requested_dtype(...).with_rng_seed(...)in metadata so tests can rebuild deterministic f32/f64 variants. - Keep
post_check_onnx_graphexpectations focused on meaningful structural checks. Function selectors accept either the exactdomain:nameor a domain prefix (e.g."custom.MyFn.1"matches"custom.MyFn.1:MyFn"). - When migrating existing decorators, ensure no conflicting namespace choices are applied to the same target; the decorator raises if mixed namespacing is detected.