onnx_ir.passes¶
Use built-in passes¶
Common, reusable passes are implemented in . You can use onnx_ir.passes.Sequential to chain passes or use onnx_ir.passes.PassManager which supports early stopping if no changes are made.
Example¶
import onnx_ir as ir
import onnx_ir.passes.common as common_passes
model = ir.load("model.onnx")
# You can chain passes with ir.passes.Sequential
passes = ir.passes.Sequential(
common_passes.DeduplicateHashedInitializersPass(size_limit=1024 * 1024),
common_passes.CommonSubexpressionEliminationPass(),
)
result = passes(model)
# Or you can run passes individually. Passing in result or result.model has the same effect
result = common_passes.ClearMetadataAndDocStringPass()(result)
print("The model was modified:", result.modified)
ir.save(result.model, "model.onnx")
For more advanced use cases, you can use onnx_ir.passes.PassManager to orchestrate passes with automatic iteration until convergence:
model = ir.load("model.onnx")
passes = ir.passes.PassManager(
[
# Pass managers can be nested
ir.passes.PassManager(
[
common_passes.DeduplicateHashedInitializersPass(size_limit=1024 * 1024),
common_passes.CommonSubexpressionEliminationPass(),
],
steps=2,
early_stop=True,
),
common_passes.ClearMetadataAndDocStringPass(),
],
steps=2,
early_stop=False,
)
result = passes(model)
Pass infrastructure¶
Inherit from onnx_ir.passes.InPlacePass or onnx_ir.passes.FunctionalPass to define a pass. You will need to implement the call method which returns a onnx_ir.passes.PassResult.
Alternatively, inherit from the base class onnx_ir.passes.PassBase and override the two properties changes_input and in_place to set properties of the pass.
Base class for all passes. |
|
A pass that modifies the input model in place and returns it. |
|
A pass that returns a new model but does not modify the input model. |
|
Run a sequence of passes in order. |
|
Result of a pass. |
|
Pass manager for the IR. |
Errors¶
- exception onnx_ir.passes.InvariantError¶
Raised when an invariant is violated.
- exception onnx_ir.passes.PreconditionError¶
Raised when a precondition is violated.
- exception onnx_ir.passes.PostconditionError¶
Raised when a postcondition is violated.
- exception onnx_ir.passes.PassError¶
Raised when an error occurs during a pass.
Utilities¶
- onnx_ir.passes.functionalize(pass_instance)¶
Produce a functional pass from a given pass.
A new functional pass is created that clones the input model before running the pass.
Added in version 0.1.14.
- Parameters:
pass_instance (PassBase) – The pass to convert.
- Returns:
A functional pass.
- Return type: