Question regarding Relax pattern matcher

Hello, I was working on Relax IR module and trying to optimize certain patterns. I am going through relax pattern matcher examples and listed down below questions.

  1. pre and post ops information will be available in the callback function when using Relay’s pattern matcher. But I don’t see any such information in rewriter function definition for Relax pattern matcher. How to we retrieve pre and post ops when using relax pattern matcher? Pattern of Interest: op_x > nn.maxpool > op_y

  2. Can we identify Topi ops using relax pattern matcher?

cc: @masahi


What exactly are pre and post?

Suppose, if we are having example like below,

lv1 = R.call_tir(requantize, (lv0, .... )
lv2 = R.call_tir(max_pool2d, (lv1,), .... )
lv3 = R.call_tir(conv2d, (lv2, .... )


pre_op = requantize
op = nn.maxpool
post_op = conv2d

Hi @masahi, I will refine my question.

Can we do pattern-matching on Relax IR to capture an op with its predecessor and successor ops?

As described in the above comment, I want to patter-match maxpool op, along with its predecessor op(requantize) and successor op(conv2d). The predecessor and successor ops can change over the IR module.

I was facing difficulty in giving successive op information as wildcard() while defining the pattern. Is it even possible to acheive this?

I see, the pattern rewriters in and Relay and Relax are implemented very differently, and due to the visit order of Relax ExprMutator, we don’t have any information about a successor expr when visiting a target expr to match and rewrite.

Yes, you can match against call_tir and check the name of the function being called.

1 Like

Thanks for clarifying.