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 5 commits
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
74 changes: 47 additions & 27 deletions onnxscript/ir/passes/_pass_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,41 @@


class PassBase(abc.ABC):
"""Base class for all passes.
"""Base class for all passes."""

Class attributes:
in_place: Whether the pass modifies the model in place.
"""
@property
def in_place(self) -> bool:
"""Whether the pass modifies the model in place."""
return True

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

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L74

Added line #L74 was not covered by tests

in_place: bool = True
@property
def destructive(self) -> bool:
"""Whether the pass will destroy the input model when ``in_place=False``."""
return False

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

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L79

Added line #L79 was not covered by tests

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(

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

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L85-L88

Added lines #L85 - L88 were not covered by tests
f"Pre-condition for pass '{self.__class__.__name__}' failed"
) from e

result = self.call(model)

# Check postconditions
try:
self.ensures(model)
except PostconditionError:
raise
except Exception as e:
raise PostconditionError(

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

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L97-L100

Added lines #L97 - L100 were not covered by tests
f"Post-condition for pass '{self.__class__.__name__}' failed"
) from e
return result

@abc.abstractmethod
def call(self, model: ir.Model) -> PassResult:
Expand All @@ -97,29 +122,37 @@
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:
@property
def in_place(self) -> bool:
"""Whether the pass modifies the model in place."""
return all(pass_.in_place for pass_ in self.passes)

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

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L147

Added line #L147 was not covered by tests

@property
def destructive(self) -> bool:
"""Whether the pass will destroy the input model when ``in_place=False``."""
# This logic is a little conservative, but it is ok for now
return any(pass_.destructive for pass_ in self.passes)

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

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L153

Added line #L153 was not covered by tests

def call(self, model: ir.Model) -> PassResult:
"""Run the set of passes `steps` number of times or until the graph stops changing."""
overall_modified = False
for step in range(self.steps):
Expand All @@ -137,17 +170,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 176 in onnxscript/ir/passes/_pass_infra.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/passes/_pass_infra.py#L175-L176

Added lines #L175 - L176 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 +189,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