You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/python-integration.md
+21-38Lines changed: 21 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -638,47 +638,30 @@ egraph.saturate(r)
638
638
639
639
## Custom Cost Models
640
640
641
-
Custom cost models are also supported by subclassing `CostModel[T]` or `DefaultCostModel` and passing in an instance as the `cost_model` kwargs to `EGraph.extract`. The `CostModel` is paramterized by a cost type `T`, which in the `DefaultCostModel` is `int`. Any cost must be able to be compared to choose the lowest cost.
641
+
By default, when extracting from the e-graph, we use a simple cost model, that looks at the costs assigned to each
642
+
function and any custom costs set with `set_cost`, and finds the lowest cost expression looking at the total tree size.
642
643
643
-
The `Expr`s passed to your cost model represent partial program trees. Any builtin values (containers or single primitives) will be fully evaluated, but any values that return user defined classes will be last as opaque "values",
644
-
representing an e-class in the e-graph. The only thing you can do with values is to compare them to each other or
645
-
use them in `EGraph.lookup_function_value` to lookup the resulting value of a call with values in it.
644
+
Custom cost models are also supported, which can be passed into `extract` as the `cost_model` keyword argument. They
645
+
are defined as functions followed the `CostModel` protocol, that take in an e-graph, an expression, and the costs of the children, and return the total cost of that expression. Costs don't have to be integers, they can be any type that supports comparison.
646
646
647
-
For example, here is a cost model that uses boolean values to determine if a model is extractable or not:
647
+
There are a few builtin cost models:
648
+
649
+
-`default_cost_model`: The default cost model, which uses integer costs and sums them up.
650
+
-`greedy_dag_cost_model(inner_cost_model=default_cost_model)`: A cost model which uses a greedy DAG algorithm to find the lowest cost expression, allowing for shared sub-expressions. It takes in another cost model to use for the base costs of each expression.
651
+
652
+
Note that when passed into your cost model, the expression won't be a full tree. Instead, only the top level call be present, and all of it's arguments will be opaque "value" expressions, representing e-classes in the e-graph. You can't do much with them except use them to construct other expression to pass into `egraph.lookup_function_value` to get the resulting value of a call with those arguments. The only exception is all builtin types, like ints, vecs, strings, etc. will be fully evaluated recursively, so they can be matched against.
653
+
654
+
For example, here is a cost model that has a boolean cost if the value is even or not:
0 commit comments