[RFC][Relay] Program Matching for Relay Pt 1: A pattern language

I really like the regex style matching syntax! I think it’d be helpful to have an example showing how a matched subgraph could be used. For example, after matching a diamond in graph, how we would annotate that subgraph to run on a different device than the rest of the graph? Is it worth considering the post-match API in this RFC or would that be part of future work?

Sounds great! When you say “graph rewrites”, are you proposing a interface to define a “graph grammer” ?

I was think similar to to regex rewriting, e.g.

"def\(\s*\"(?P<fn_name>[a-z0-9|_]+)" -> "<fn_name>xyz"

Will allow us to represent the rewrite that extracts the function name after def and append xyz to it. We can brainstorm possible rewrites that we might do for graphs(the concrete DAG->DAG pattern is fine and we know how to do that, just wonder if there is a need for wildcard like patterns like the above)

I can see this being a very useful feature, it looks great! One question I had was will we also be able to specify certain properties for an op to match against? E.g. for a convolution op: data layout = NHWC, groups = 1

I guess there is a need for optional ops – as @mbaret pointed out (that could be handled by “?” pattern, I guess ) and a list of ops like [abc] in regex. Not sure about a full wildcard though.

In the multi output scenario, we need to make sure that any output is not an ancestor to any input of the same pattern through connections that are outside the pattern. Right? If the goal is targeting offloading to backends OR at least provide an interface to specify to avoid such matches ? (as discussed in [Relay] Improved graph partitioning algorithm)

What you are describing belongs to A3 – the algorithm e.g. graph partition need to ensure that the there is no overlapping patterns in the partitioned graph.

Of course, it would be interesting to ask whether the above line itself is a pattern(for a bigger graph) which could be overkill for now(then it comes back to A0)

Torch also has a mechanism for subgraph matching and rewrite. In their case, the pattern and replacement are both textual representation of their IR.

For example, their pass for automatically transforming fp32 graphs to quantized ones are based on pat matching. Here is an example of a pattern and its replacement.

It is indeed interesting to see if we can have a textual repr for pattern that is related to IR. I wonder though how can we go beyond the concrete DAG->DAG pattern to wildcard related, as discussed in the current proposal

1 Like

+1, wildcards and generalizations are really important in making this a flexible system.

I wonder how practical can the infrastructure be shared with low-level tir?

1 Like

Great discussions so far, it would be great if we can comment on API choices. So far I see the need to have ? pattern.

@jroesch can you incorporate the feedback and give a couple of API design choices, then we can try to conclude and move to implementation(by sending a formal RFC to github)

1 Like

Seconding @yzhliu’s question on sharing infrastructure and code with the low level TIR, especially w.r.t. python bindings. For the Hexagon backend, we want to do more work in the TIR but a lack of good pattern matching utilities in python is hindering our efforts there. A unified pattern matcher for relay and TIR would be a big +1 for us.

To followup discussions on TIR pattern matching. it would be great to get a sense of potential use-cases

1 Like

@jroesch, is there any update on this feature? I’m really excited to try it out :slight_smile:

Sorry for the lack of updates been super oversubscribed lately (in the process of finishing my thesis) plus lots of stuff at OctoML. @mbrookhart is a hero and has a production quality version he has been working on in C++. I think he is getting really close to shipping a first version of the matcher, and is working on a rewriter based on this. I will talk with him at (virtual)work tomorrow and try and craft an update.

3 Likes

Hi @jonso,

You can take a look at the matcher here: https://github.com/apache/incubator-tvm/pull/5231

Need to add a little more documentation, and I’m working on a second stage pattern-based rewriter now.

1 Like

Awesome! Thanks so much for the update. @jroesch sorry to bug you while you’re working on your thesis, I’ll bug @mbrookhart if I have any questions :slight_smile:

I think the PR is ready for review as V1 of the pattern language, it contains some documentation, testing, and the language itself, the matcher, and pattern-based expression rewriting.

V2 will include graph partitioning and verification to better support bring your own code gen, starting in on those passes now.

Any and all comments are appreciated!

I’m very interested in this. Using composite, I have to generate many slightly different patterns (with_bias, with_pad etc), leading to combinatorial explosion. Optional matching can clean this up a lot.

Maybe this has been discussed before, but if we integrate full blown pattern matching into BYOC, what is the future of composite? Do we still have a use case for it?