masahi commented on code in PR #14215:
URL: https://github.com/apache/tvm/pull/14215#discussion_r1133612004


##########
tests/python/relax/test_transform_alter_op_impl.py:
##########
@@ -0,0 +1,342 @@
+# 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.
+
+import pytest
+import tvm.testing
+
+from tvm import relax
+from tvm.script import tir as T, ir as I, relax as R
+
+kOperatorName = "operator_name"
+
+
+def _check(before, expected, operator_name, replacement_primfunc, 
layout_changes):
+    after = relax.transform.AlterOpImpl(
+        {operator_name: replacement_primfunc}, {operator_name: layout_changes}
+    )(before)
+    after = relax.transform.RemoveUnusedFunctions()(after)
+    tvm.ir.assert_structural_equal(after, expected)
+
+
+def test_single_output():
+    # fmt: off
+    @I.ir_module
+    class Before:
+        @T.prim_func
+        def add(arg0: T.Buffer((16,), "float32"), arg1: T.Buffer((16,), 
"float32"), output: T.Buffer((16,), "float32")):
+            T.func_attr({"operator_name": "relax.add"})
+            for ax0 in range(16):
+                with T.block("T_add"):
+                    v_ax0 = T.axis.spatial(16, ax0)
+                    T.reads(arg0[v_ax0], arg1[v_ax0])
+                    T.writes(output[v_ax0])
+                    output[v_ax0] = arg0[v_ax0] + arg1[v_ax0]
+
+        @R.function
+        def main(x: R.Tensor((16,), dtype="float32"), y: R.Tensor((16,), 
dtype="float32")) -> R.Tensor((16,), dtype="float32"):
+            with R.dataflow():
+                lv = R.call_tir(add, (x, y), out_sinfo=R.Tensor((16,), 
dtype="float32"))
+                gv: R.Tensor((16,), dtype="float32") = lv
+                R.output(gv)
+            return gv
+    @I.ir_module
+    class Expected:
+        @T.prim_func
+        def relax_add_replacement(arg0: T.Buffer((4, 4), "float32"), arg1: 
T.Buffer((4, 4), "float32"), output: T.Buffer((4, 4), "float32")):
+            T.func_attr({"operator_name": "relax.add"})
+            for ax0, ax1 in T.grid(4, 4):
+                with T.block("T_add"):
+                    v_ax0, v_ax1 = T.axis.remap("SS", [ax0, ax1])
+                    T.reads(arg0[v_ax0, v_ax1], arg1[v_ax0, v_ax1])
+                    T.writes(output[v_ax0, v_ax1])
+                    output[v_ax0, v_ax1] = arg0[v_ax0, v_ax1] + arg1[v_ax0, 
v_ax1]
+
+        @R.function
+        def main(x: R.Tensor((16,), dtype="float32"), y: R.Tensor((16,), 
dtype="float32")) -> R.Tensor((16,), dtype="float32"):
+            with R.dataflow():
+                lv: R.Tensor((4, 4), dtype="float32") = R.layout_transform(x, 
index_map=lambda i: (i // 4, i % 4), pad_value=None)
+                lv1: R.Tensor((4, 4), dtype="float32") = R.layout_transform(y, 
index_map=lambda i: (i // 4, i % 4), pad_value=None)
+                lv2 = R.call_tir(relax_add_replacement, (lv, lv1), 
out_sinfo=R.Tensor((4, 4), dtype="float32"))
+                lv_1: R.Tensor((16,), dtype="float32") = 
R.layout_transform(lv2, index_map=lambda axis0, axis1: (axis0 * 4 + axis1,), 
pad_value=None)
+                gv: R.Tensor((16,), dtype="float32") = lv_1
+                R.output(gv)
+            return gv
+
+    @T.prim_func
+    def add_2d(arg0: T.Buffer((4, 4), "float32"), arg1: T.Buffer((4, 4), 
"float32"), output: T.Buffer((4, 4), "float32")):
+        for ax0, ax1 in T.grid(4, 4):
+            with T.block("T_add"):
+                v_ax0, v_ax1 = T.axis.remap("SS", [ax0, ax1])
+                T.reads(arg0[v_ax0, v_ax1], arg1[v_ax0, v_ax1])
+                T.writes(output[v_ax0, v_ax1])
+                output[v_ax0, v_ax1] = arg0[v_ax0, v_ax1] + arg1[v_ax0, v_ax1]
+    # fmt: on
+    index_map = lambda i: (i // 4, i % 4)
+    _check(
+        Before,
+        Expected,
+        operator_name="relax.add",
+        replacement_primfunc=add_2d,
+        layout_changes=[index_map, index_map, index_map],

Review Comment:
   It would be great if we can automatically infer the index maps from the 
original and the replacement func.



-- 
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]

Reply via email to