-
Notifications
You must be signed in to change notification settings - Fork 55
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
Variable cost to variant #399
Comments
It's not currently possible. We had two PRs with different ways of adding custom costs #355 #353 but punted on it, because we wanted a better idea first of what the space was of user needs here. If you could give a larger example how you would use this, that would be helpful in designing a solution around it. |
Sure, here is my use case: I want to optimize tensor multiplications according to the shape of the tensors. Assume you have 3 tensors |
I guess in this case I would need a dimension analysis, then set the cost of each operation using that analysis. Do you have an example with dimension analysis? |
Thanks!
Sort of: https://github.com/egraphs-good/egglog/blob/main/tests/matrix.egg |
Let's say I have the following datatype and rewrite rule:
Using the current egglog ways, how would I tell the extract command to return the
If I can't do this with the extract function, how can I achieve this goal with the current egglog commands? |
The only command that can be used to influence extraction at a per node basis is So am I understanding it correctly that you want to model the cost of a So I think with #355 your case could maybe be supported like this? (dataype MatrixOp
(Matrix String i64 i64) ; (<name> <nrows> <ncols>)
(MatMul MatrixOp MatrixOp)
)
(birewrite
(MatMul ?a (MatMul ?b ?c))
(MatMul (MatMul ?a ?b) ?c)
)
(function nrows (MatrixOp) i64)
(function ncols (MatrixOp) i64)
(rule ((= ?m (Matrix ?s ?r ?c)))
((set (nrows ?m) ?r)
(set (ncols ?m) ?c)))
(rule ((= ?m (MatMul ?a ?b))
(= ?r (nrows ?a))
(= ?m (ncols ?a) (nrows ?b))
(= ?c (ncols ?b)))
((set (nrows ?m) ?r)
(set (ncols ?m) ?c)
(cost (MathMul ?a ?b) (* (* r m) c)))) Does that seem right? |
Yes, this looks right. |
A way to do it with the current comands is maybe a conditional rewrite? so
Assuming
Then
|
Oh yeah good idea! I think you can do something like that with the (dataype MatrixOp
(Matrix String i64 i64) ; (<name> <nrows> <ncols>)
(MatMul MatrixOp MatrixOp)
)
(function nrows (MatrixOp) i64)
(function ncols (MatrixOp) i64)
(rule ((= ?m (Matrix ?s ?r ?c)))
((set (nrows ?m) ?r)
(set (ncols ?m) ?c)))
(rule ((= ?m (MatMul ?a ?b))
(= ?r (nrows ?a))
(= ?c (ncols ?b)))
((set (nrows ?m) ?r)
(set (ncols ?m) ?c)))
(rewrite
(MatMul ?x (MatMul ?y ?z))
(MatMul (MatMul ?x ?y) ?z)
:when (
(<
(+ (* (nrows ?x) (* (ncols ?x) (nrows ?y))) (* (ncols ?x) (* (nrows ?y) (ncols ?z))))
(+ (* (nrows ?y) (* (ncols ?y) (nrows ?z))) (* (ncols ?y) (* (nrows ?z) (ncols ?z))))
)
)
:subsume
)
(rewrite
(MatMul (MatMul ?x ?y) ?z)
(MatMul ?x (MatMul ?y ?z))
:when (
(<
(+ (* (nrows ?y) (* (ncols ?y) (nrows ?z))) (* (ncols ?y) (* (nrows ?z) (ncols ?z))))
(+ (* (nrows ?x) (* (ncols ?x) (nrows ?y))) (* (ncols ?x) (* (nrows ?y) (ncols ?z))))
)
)
:subsume
) |
Closing as duplicate of #294 |
How do we assign a variable cost to a variant? For example
The text was updated successfully, but these errors were encountered: