Skip to content

Conversation

@masahi
Copy link
Member

@masahi masahi commented Jul 13, 2023

Consider this subgraph:

lv_ = R.matmul(x2, y2)
lv = R.matmul(x, y)
lv1 = R.add(lv, bias)
lv2 = R.add(lv1, lv_)
out = R.add(lv2, residual)

This subgraph matches to two different residual patterns matmul -> add -> add starting from each matmul and matching two out of three adds. Currently we end up generating the following partition, since the second add matches to both residual patterns and the two groups are merged incorrectly after the second matching.

@R.function
def gv(x2_1: R.Tensor((32, 8), dtype="float16"), y2_1: R.Tensor((8, 8), dtype="float16"), x_1: R.Tensor((32, 8), dtype="float16"), y_1: R.Tensor((8, 8), dtype="float16"), bias_1: R.Tensor((8,), dtype="float16"), residual_1: R.Tensor((32, 8), dtype="float16")) -> R.Tensor((32, 8), dtype="float16"):
    R.func_attr({"Composite": "cutlass.matmul_bias_residual_add", "Primitive": 1})
    with R.dataflow():
        lv_: R.Tensor((32, 8), dtype="float16") = R.matmul(x2_1, y2_1, out_dtype="float16")
        lv: R.Tensor((32, 8), dtype="float16") = R.matmul(x_1, y_1, out_dtype="float16")
        lv1: R.Tensor((32, 8), dtype="float16") = R.add(lv, bias_1)
        lv2: R.Tensor((32, 8), dtype="float16") = R.add(lv1, lv_)
        gv_1: R.Tensor((32, 8), dtype="float16") = R.add(lv2, residual_1)
        R.output(gv_1)
    return gv_1

gv1: R.Tensor((32, 8), dtype="float16") = gv(x2, y2, x, y, bias, residual)

This bug has been fixed by checking, on successful pattern matching, whether there exists a matched expression that is part of a previously matched group.

@yelite @vinx13

@tvm-bot
Copy link
Collaborator

tvm-bot commented Jul 13, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

@masahi masahi force-pushed the pattern-partition-fix branch from 976b60d to d91819d Compare July 13, 2023 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants