-
Notifications
You must be signed in to change notification settings - Fork 16
Fix DAG extract bug #388
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
Open
saulshanabrook
wants to merge
2
commits into
main
Choose a base branch
from
fix-bug-extract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix DAG extract bug #388
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,131 @@ | ||
| """ | ||
| Tests extraction with a DAG-based cost model. | ||
| from https://github.com/egraphs-good/egglog-python/issues/387#issuecomment-3628927075 | ||
| """ | ||
|
|
||
| from dataclasses import dataclass, field | ||
|
|
||
| from egglog import * | ||
| from egglog import bindings | ||
|
|
||
| # A cost model, approximately equivalent to, greedy_dag_cost_model, | ||
saulshanabrook marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # which operates purely on the `bindings` level, for the sake of | ||
| # minimization. | ||
|
|
||
| ENode = tuple[str, tuple[bindings.Value, ...]] | ||
|
|
||
|
|
||
| @dataclass | ||
| class DAGCostValue: | ||
| """Cost value for DAG-based extraction.""" | ||
|
|
||
| cost: int | ||
| _values: dict[ENode, int] | ||
|
|
||
| def __eq__(self, rhs: object) -> bool: | ||
| if not isinstance(rhs, DAGCostValue): | ||
| return False | ||
| return self.cost == rhs.cost | ||
|
|
||
| def __lt__(self, other: "DAGCostValue") -> bool: | ||
| return self.cost < other.cost | ||
|
|
||
| def __le__(self, other: "DAGCostValue") -> bool: | ||
| return self.cost <= other.cost | ||
|
|
||
| def __gt__(self, other: "DAGCostValue") -> bool: | ||
| return self.cost > other.cost | ||
|
|
||
| def __ge__(self, other: "DAGCostValue") -> bool: | ||
| return self.cost >= other.cost | ||
|
|
||
| def __hash__(self) -> int: | ||
| return hash(self.cost) | ||
|
|
||
| def __str__(self) -> str: | ||
| return f"DAGCostValue(cost={self.cost})" | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"DAGCostValue(cost={self.cost}, nchildren={len(self._values)})" | ||
|
|
||
|
|
||
| @dataclass | ||
| class DAGCost: | ||
| """ | ||
| DAG-based cost model for e-graph extraction. | ||
|
|
||
| This cost model counts each unique e-node once, implementing | ||
| a greedy DAG extraction strategy. | ||
| """ | ||
|
|
||
| graph: bindings.EGraph | ||
| cache: dict[ENode, DAGCostValue] = field(default_factory=dict) | ||
|
|
||
| def merge_costs(self, costs: list[DAGCostValue], node: ENode, self_cost: int = 0) -> DAGCostValue: | ||
| # if node in self.cache: | ||
| # return self.cache[node] | ||
| values: dict[ENode, int] = {} | ||
| for child in costs: | ||
| values.update(child._values) | ||
| cost = DAGCostValue(cost=sum(values.values(), start=self_cost), _values=values) | ||
| cost._values[node] = self_cost | ||
| # self.cache[node] = cost | ||
| # print(f"merge {costs=} out={cost}") | ||
| return cost | ||
|
|
||
| def cost_fold(self, fn: str, enode: ENode, children_costs: list[DAGCostValue]) -> DAGCostValue: | ||
| return self.merge_costs(children_costs, enode, 1) | ||
| # print(f"fold {fn=} {out=}") | ||
saulshanabrook marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def enode_cost(self, name: str, args: list[bindings.Value]) -> ENode: | ||
| return (name, tuple(args)) | ||
|
|
||
| def container_cost(self, tp: str, value: bindings.Value, element_costs: list[DAGCostValue]) -> DAGCostValue: | ||
| return self.merge_costs(element_costs, (tp, (value,)), 1) | ||
|
|
||
| def base_value_cost(self, tp: str, value: bindings.Value) -> DAGCostValue: | ||
| return self.merge_costs([], (tp, (value,)), 1) | ||
|
|
||
| @property | ||
| def egg_cost_model(self) -> bindings.CostModel: | ||
| return bindings.CostModel( | ||
| fold=self.cost_fold, | ||
| enode_cost=self.enode_cost, | ||
| container_cost=self.container_cost, | ||
| base_value_cost=self.base_value_cost, | ||
| ) | ||
|
|
||
|
|
||
| def test_dag_cost_model(): | ||
| graph = EGraph() | ||
|
|
||
| commands = graph._egraph.parse_program(""" | ||
| (sort S) | ||
|
|
||
| (constructor Si (i64) S) | ||
| (constructor Swide (S S S S S S S S) S ) | ||
| (constructor Ssa (S) S) | ||
| (constructor Ssb (S) S) | ||
| (constructor Ssc (S) S) | ||
| (constructor Sp (S S) S) | ||
|
|
||
|
|
||
| (let w | ||
| (Swide (Si 0) (Si 1) (Si 2) (Si 3) (Si 4) (Si 5) (Si 6) (Si 7))) | ||
|
|
||
| (let l (Ssa (Ssb (Ssc (Si 0))))) | ||
| (let x (Ssa w)) | ||
| (let v (Sp w x)) | ||
|
|
||
| (union x l) | ||
| """) | ||
| graph._egraph.run_program(*commands) | ||
|
|
||
| cost_model = DAGCost(graph._egraph) | ||
| extractor = bindings.Extractor(["S"], graph._egraph, cost_model.egg_cost_model) | ||
| termdag = bindings.TermDag() | ||
| value = graph._egraph.lookup_function("v", []) | ||
| assert value is not None | ||
| cost, _term = extractor.extract_best(graph._egraph, termdag, value, "S") | ||
|
|
||
| assert cost.cost in {19, 21} | ||
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.
Uh oh!
There was an error while loading. Please reload this page.