Skip to content

[IR] Improve pass infra #2120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 26, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions onnxscript/ir/passes/_pass_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,31 @@

Class attributes:
in_place: Whether the pass modifies the model in place.
destructive: Whether the pass will destroy the input model when ``in_place=False``.
"""

in_place: bool = True
destructive: bool = False

def __call__(self, model: ir.Model) -> PassResult:
return self.call(model)
# Check preconditions
try:
self.requires(model)
except PreconditionError:
raise
except Exception as e:
raise PreconditionError("Pre-condition failed") from e

Check warning on line 86 in onnxscript/ir/passes/_pass_infra.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L83-L86

Added lines #L83 - L86 were not covered by tests

result = self.call(model)

# Check postconditions
try:
self.ensures(model)
except PostconditionError:
raise
except Exception as e:
raise PostconditionError("Post-condition failed") from e

Check warning on line 96 in onnxscript/ir/passes/_pass_infra.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L93-L96

Added lines #L93 - L96 were not covered by tests
return result

@abc.abstractmethod
def call(self, model: ir.Model) -> PassResult:
Expand All @@ -97,26 +116,23 @@
del model # Unused


class PassManager:
class PassManager(PassBase):
"""Pass manager for the IR.

The PassManager is a callable that runs a sequence of passes on a model.
The PassManager is a Pass that runs a sequence of passes on a model.

Attributes:
passes: The passes to run.
check_invariants: Whether to check invariants before and after each pass.
steps: The number of times to run the passes.
"""

def __init__(
self,
passes: Sequence[PassBase],
check_invariants: bool = False,
steps: int = 1,
):
# TODO(justinchuby): Implement constraints
self.passes = list(passes)
self.check_invariants = check_invariants
self.steps = steps

def __call__(self, model: ir.Model) -> PassResult:
Expand All @@ -137,17 +153,10 @@
modified = False
for i, pass_ in enumerate(self.passes):
logger.debug("Running the %s-th pass '%s', (step %s)", i, pass_, step)

# 1. Check preconditions
if self.check_invariants:
try:
pass_.requires(model)
except Exception as e:
raise PreconditionError(f"Pre-condition failed for {pass_}") from e

# 2. Run the pass
try:
pass_result = pass_(model)
except (PreconditionError, PostconditionError):
raise

Check warning on line 159 in onnxscript/ir/passes/_pass_infra.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L158-L159

Added lines #L158 - L159 were not covered by tests
except Exception as e:
prev_pass_names = [str(p) for p in self.passes[:i]]
raise PassError(
Expand All @@ -163,10 +172,4 @@
model = pass_result.model
modified = modified or pass_result.modified

# 3. Check postconditions
if self.check_invariants:
try:
pass_.ensures(model)
except Exception as e:
raise PostconditionError(f"Post-condition failed for {pass_}") from e
return PassResult(model, modified)
Loading