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


##########
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:
   To support automatic inference of index maps, I can think of one possible 
way. The original and replacement PrimFunc has layout string specified for each 
param buffer using function attributes. We can then infer the layout 
transformation on the buffers. 
   
   The contract would be:
   * name of the function attribute specifying layouts, say "layouts"
   * the length of `layouts` is same as number of params on PrimFunc, so 
layouts[i] corresponds to param[i] layout.
   * the notation used for layouts between original and replacement PrimFunc 
matches. So if `original_conv2d` uses "NCHW" notation it should match the 
`replacement_conv2d` notation, otherwise we throw an error.
   
   I was hesitant on introducing this contract but if there is consensus on it, 
I am happy to support it and make index_maps optional. Let me know in comments.
   
   ```python
   @T.prim_func
   def original_conv2d(arg0: T.Buffer((32, 3, 224, 224), "float32"), arg1: 
T.Buffer((5, 5, 3, 64), "float32"), output: T.Buffer((32, 64, 224, 224), 
"float32")):
       T.func_attr({"operator_name": "relax.conv2d", "layouts": ["NCHW", 
"HWIO"]})
       ...
   
   @T.prim_func
   def replacement_conv2d(arg0: T.Buffer((32, 224, 224, 3), "float32"), arg1: 
T.Buffer((64, 3, 5, 5), "float32"), output: T.Buffer((32, 224, 224, 64), 
"float32")):
       T.func_attr({"operator_name": "relax.conv2d", "layouts": ["NHWC", 
"OIHW"]})
       ...
   ```
   
   



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