Decide on Linalg morphism behaviour
From our previous discussions in the forum, and as exposed here, we may want a generic-to-named morphism to stop at category if there are no named ops. This is not the current behaviour, but it is somewhat expected to happen ("best effort"), so I think there's merit to this discussion.
I imagine the following behaviour:
generic-to-category: always stop at category, even if there isn't one and there are named ops. E.g.linalg.pack.generic-to-named: stop at category, if there is one and no named ops available. E.g.linalg.elementwise <add>.named-to-generic: always outputlinalg.generic.named-to-category: always stop at category. If no category available, leave as named.category-to-named: only convert if there exist an op, otherwise, leave category.category-to-generic: always outputlinalg.generic.
Basically:
- Generalization is always possible and should be done fully.
- Categorization is sometimes possible, but should not change if not.
- Specialization is sometimes possible, and should stop at category if no named exists.
@llvm/issue-subscribers-mlir-linalg
Author: Renato Golin (rengolin)
I imagine the following behaviour:
generic-to-category: always stop at category, even if there isn't one and there are named ops. E.g.linalg.pack.generic-to-named: stop at category, if there is one and no named ops available. E.g.linalg.elementwise <add>.named-to-generic: always outputlinalg.generic.named-to-category: always stop at category. If no category available, leave as named.category-to-named: only convert if there exist an op, otherwise, leave category.category-to-generic: always outputlinalg.generic.
Basically:
- Generalization is always possible and should be done fully.
- Categorization is sometimes possible, but should not change if not.
- Specialization is sometimes possible, and should stop at category if no named exists.
w.r.t "generic-to-named: stop at category," -- shouldn't the option be deleted instead of it doing something other than what it seems to imply.
In the current model, we need a way to say "stop at category" or "try to go further". There are tests that ask for specialization but stay in generic (even though there are category ones).
Today, for the two models I'm considering below, users then have to ask twice:
- Form choice:
named-to-category && generic-to-category - Best-effort:
generic-to-named || generic-to-category
For "Best effort" you can add a flag to say "stop at category" on generic to named conversions, but there's no way to have a single transform for the "Form choice".
Current State
There are two ways to looking at the forms:
Separate co-domains:
Intersecting co-domains:
While the form domains have been designed as intersecting, the morphism transforms currently treat them as separate:
- There are no intermediary stops, implicit or explicit.
- To convert all operations to the same form you always need two morphisms:
A -> B + C -> B. - The function is what the user requests / control, not the form.
Alternatives
My proposal is to stop looking at the co-domains as three separate sets and treat them as intersecting. The rationale is simply a transform-friendly morphism.
Form choice
What: User select a form, anything in other forms are converted (generalized / specialized) to it.
Rationale: For fine grained transforms, the rewrites match against a particular form, so moving an operation to a particular form is very helpful, regardless of what its original form was. In this case, selecting a form and converging to it would be the simplest.
Implementation: This can use the interfaces to match named and generic alike, and just replace with the destination form chosen.
Best effort
What: Specialization and Generalization try to go as far as possible, but stop in the middle if they can't reach the end.
Rationale: For whole-graph greedy passes, putting all the ops in their most specialized / generalized forms helps create a state in which multiple passes can run without having to convert forms back and forth. Here, canonicalize + specializing / generalizing all ops, would keep the graph as close as possible to the expectation of the follow-up passes.
Implementation: This is only relevant to 2-step conversions (generic<->named), so it's basically a second try on category if the conversion fails. Should reuse the same code as the form choice above.
To be clear, this is not a request to change the code, but the user-facing behaviour, which can be achieved with union/intersection of existing rewrites.
w.r.t "generic-to-named: stop at category," -- shouldn't the option be deleted instead of it doing something other than what it seems to imply.
For morphism, I agree, so I didn't change that in the PR. generic-to-named now work with matmuls, so I also didn't remove. #221191
The only thing I changed was to allow specialization to generate category operations if there are no named ones available. This is important for elementwise (that have no named forms anymore) and the various odd shapes of matmul that still work as contractions but we don't want to add all variations as named forms. Still is "specialization" so I think that should apply.
This will help us get rid of a lot of the less used named forms, like we did with the transpose variants last year.