How to match sequence of ops or an op inside a sequence

Hi I’m facing an issue, that might be a bug in the pattern matching mechanism and a second look on the issue will be very helpful and appreciated.

I want to match and rewrite conv->bias_add->relu->maxpool, or maxpool alone, in the same callback function. In my case I use this pattern:

self.inp = wildcard()
self.weight = is_constant()
self.conv2d = is_op("nn.conv2d")(self.inp, self.weight)
self.bias = is_constant()
self.bias_add = is_op("nn.bias_add")(self.conv2d, self.bias)
self.relu = is_op("nn.relu")(self.bias_add)
self.pattern = is_op("nn.max_pool2d")(self.relu) | is_op("nn.max_pool2d")(wildcard())

My graph has the following sequence:

Untitled

I’d expect the maxpool after the branching will match, but it isn’t. I do notice that when printing debug logs, I get that the conv-bias-relu-maxpool is rejected because of the branching I guess, but I don’t understand why the standalone maxpool is not matched.

Can anyone shed light on the matter?

This is the log I get (in short):

[13:34:01] /src/relay/ir/dataflow_matcher.cc:715: PatternRewriter: matched index 12 [13:34:01] /src/relay/ir/dataflow_matcher.cc:715: PatternRewriter: matched index 14 … PatternRewriter: Rejecting group since would create a cycle with output 113 for root 17 in graph:

P.S. my usecase is that I want to match the sequence or the standalone op, but not both. And if I run two separate rewrites, one that matches the sequence and the other that matches the standalone op, they will overlap. But I want to exclude the standalone if it has already been taken care of by the sequence rewrite.

Thanks!