-
Notifications
You must be signed in to change notification settings - Fork 15
feat(py): ComposablePass protocol and ComposedPass for hugr-py
#2636
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
Changes from 35 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
eeb4c50
Agustin's suggested Protocol class
CalMacCQ c5cf1d7
minor fixes
CalMacCQ 8a3d6ba
more methods
CalMacCQ 7b58c05
import Self from typing_extensions
CalMacCQ 33e325b
Merge branch 'main' into cm/composable_pass
CalMacCQ 5e31a3d
fix import
CalMacCQ c52de85
remove to_dict and from_dict methods as well as supported Ops
CalMacCQ 6d7afc2
remove unused import
CalMacCQ 89fabf0
node -> entrypoint
CalMacCQ 91879cd
Merge branch 'main' into cm/composable_pass
CalMacCQ 4878d5f
rename and update __init__.py
CalMacCQ 7045270
Merge branch 'main' into cm/composable_pass
CalMacCQ d20aaaa
Merge branch 'main' into cm/composable_pass
CalMacCQ 954730f
make a passes module
CalMacCQ 448b5cf
move passes into hugr root
CalMacCQ 17d1dc8
remove from build
CalMacCQ b6fa9a5
add __init__.py for passes
CalMacCQ cc9c151
undo init changes
CalMacCQ 2d10e87
update __all__
CalMacCQ 45bd86a
fix __all__ again
CalMacCQ 6ca653e
minimal methods for ComposablePass
CalMacCQ 9171dba
add ComposedPass dataclass
CalMacCQ c6fbfc0
fix to comp_pass
CalMacCQ fcf00c0
docstrings
CalMacCQ 3ae525c
formatting stuff
CalMacCQ 78e7522
Merge branch 'main' into cm/composable_pass
CalMacCQ af225a3
more ruff check
CalMacCQ 92964a8
provide a default implementation of then() method
CalMacCQ f139d7c
fix default impl
CalMacCQ df0ab15
fixup then impl
CalMacCQ ac63dd5
fixup mypy and ruff
CalMacCQ 9a157b8
yet another fix
CalMacCQ 3db5bdd
more ruff nonsense
CalMacCQ 67d7d38
rm self
CalMacCQ 975ddbb
fixes
CalMacCQ 0165ae7
Update hugr-py/src/hugr/passes/composable_pass.py
CalMacCQ 2def5fc
fmt
CalMacCQ 9673dc2
make name a property
CalMacCQ 01b7126
add a dummy test
CalMacCQ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """A hugr-py passes module for hugr transformations.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """A Protocol for a composable pass.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING, Protocol | ||
|
|
||
| if TYPE_CHECKING: | ||
| from hugr.hugr.base import Hugr | ||
|
|
||
|
|
||
| class ComposablePass(Protocol): | ||
| """A Protocol which represents a composable Hugr transformation.""" | ||
|
|
||
| def __call__(self, hugr: Hugr) -> None: | ||
| """Call the pass to transform a HUGR.""" | ||
| ... | ||
|
|
||
| def then(self, other: ComposablePass) -> ComposablePass: | ||
| """Perform another composable pass after this pass.""" | ||
| # Provide a default implementation for composing passes. | ||
| pass_list = [] | ||
| if isinstance(self, ComposedPass): | ||
| pass_list.extend(self.passes) | ||
| else: | ||
| pass_list.append(self) | ||
|
|
||
| if isinstance(other, ComposedPass): | ||
| pass_list.extend(other.passes) | ||
| else: | ||
| pass_list.append(other) | ||
|
|
||
| return ComposedPass(pass_list) | ||
|
|
||
CalMacCQ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @dataclass | ||
| class ComposedPass(ComposablePass): | ||
| """A sequence of composable passes.""" | ||
|
|
||
| passes: list[ComposablePass] | ||
|
|
||
| def __call__(self, hugr: Hugr): | ||
| """Call all of the passes in sequence.""" | ||
| for comp_pass in self.passes: | ||
| comp_pass(hugr) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's complaining that there are no tests here.
You can see the codecov report linked from the message.
We could add a test with a dummy no-op pass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 01b7126
Some maybe the
isinstancechecks are not needed. I also madeComposablePassruntime checkable.