slyubomirsky commented on code in PR #14361: URL: https://github.com/apache/tvm/pull/14361#discussion_r1147092182
########## tests/python/relax/test_transform_cse.py: ########## @@ -0,0 +1,186 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Test eliminate common subexpr pass""" +import tvm +import tvm.testing +from tvm.relax.transform import EliminateCommonSubexpr +from tvm.script.parser import ir as I, relax as R, tir as T + +import numpy as np + + +def verify(input, expected): + tvm.ir.assert_structural_equal(EliminateCommonSubexpr()(input), expected) + + +def test_simple(): + @I.ir_module + class Before: + @R.function + def foo(x: R.Tensor((2, 3), dtype="float32"), y: R.Tensor((2, 3), dtype="float32")): + with R.dataflow(): + lv0 = R.add(x, y) + lv1 = R.add(x, y) + gv = R.multiply(lv0, lv1) + R.output(gv) + return gv + + @I.ir_module + class Expected: + @R.function + def foo(x: R.Tensor((2, 3), dtype="float32"), y: R.Tensor((2, 3), dtype="float32")): + with R.dataflow(): + lv0 = R.add(x, y) + # can combine with canonicalizing bindings + # and getting rid of unused bindings to eliminate this line too + lv1 = lv0 + gv = R.multiply(lv0, lv1) + R.output(gv) + return gv + + verify(Before, Expected) + + +def test_constants(): + @I.ir_module + class Before: + @R.function + def foo() -> R.Tuple(R.Tensor((), dtype="int32"), R.Tensor((2, 2), dtype="int32")): + with R.dataflow(): + # we are not going to bind the constant 1 to a var + lv0 = R.add(R.const(1, dtype="int32"), R.const(1, dtype="int32")) + # we expect to bind the repeated large constants + lv1 = R.add( + R.const(tvm.nd.array(np.zeros((2, 2), dtype="int32"))), + R.const(tvm.nd.array(np.zeros((2, 2), dtype="int32"))), + ) + gv = (lv0, lv1) + R.output(gv) + return gv + + @I.ir_module + class Expected: + @R.function + def foo() -> R.Tuple(R.Tensor((), dtype="int32"), R.Tensor((2, 2), dtype="int32")): + with R.dataflow(): + lv0 = R.add(R.const(1, dtype="int32"), R.const(1, dtype="int32")) + lv1 = R.const(tvm.nd.array(np.zeros((2, 2), dtype="int32"))) + lv2 = R.add(lv1, lv1) + gv = (lv0, lv2) + R.output(gv) + return gv + + verify(Before, Expected) + + +def test_repeated_inner_tuples(): + @I.ir_module + class Before: + @R.function + def foo(x: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"): + with R.dataflow(): + # repeated units: (x, x), (x, (x, x)), ((x, x), (x, (x, x))) + tup = (((x, x), (x, (x, x))), ((x, x), (x, (x, x))), (x, (x, x))) + gv = tup[0][0][1] + R.output(gv) + return gv + + @I.ir_module + class Expected: + @R.function + def foo(x: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"): + with R.dataflow(): + t1 = (x, x) + t2 = (x, t1) + t3 = (t1, t2) + t4 = (t3, t3, t2) + gv = t4[0][0][1] + R.output(gv) + return gv + + verify(Before, Expected) + + +def test_inner_function(): + @I.ir_module + class Before: + @R.function + def foo(x: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"): + with R.dataflow(): + # we are going to do CSE inside the local function + @R.function + def bar(y: R.Tensor((), dtype="int32")) -> R.Tensor((), dtype="int32"): Review Comment: To be clear, it would only handle it if the bindings happen inside a `DataflowBlock` :zany_face: We might want to consider expanding this pass to handle non-dataflow sections as well. Purity tracking would help with that -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
