zxybazh commented on code in PR #14624:
URL: https://github.com/apache/tvm/pull/14624#discussion_r1185621213


##########
tests/python/relax/test_transform_few_shot_tuning.py:
##########
@@ -0,0 +1,436 @@
+# 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.
+# pylint: disable=invalid-name,,missing-function-docstring
+from typing import List, Tuple
+import numpy as np
+
+import tvm
+from tvm.script import tir as T
+from tvm.tir.tensor_intrin.cuda import *  # pylint: 
disable=wildcard-import,unused-wildcard-import
+from tvm.tir.tensor_intrin.x86 import *  # pylint: 
disable=wildcard-import,unused-wildcard-import
+from tvm.meta_schedule.testing.tune_utils import generate_input_data
+from tvm.meta_schedule.arg_info import ArgInfo
+from tvm.relax.transform import FewShotTuning
+import tvm.testing
+
+# pylint: disable=no-self-argument,missing-class-docstring,line-too-long
+# fmt: off
[email protected]_module
+class MatMul:
+    @T.prim_func
+    def matmul(
+        A: T.Buffer((32, 32), "float16"),
+        B: T.Buffer((32, 32), "float16"),
+        C: T.Buffer((32, 32), "float16"),
+    ):
+        T.func_attr({"global_symbol": "main", "tir.noalias": True})
+        # with T.block("root"):
+        for i, j, k in T.grid(32, 32, 32):
+            with T.block("C"):
+                v_i, v_j, v_k = T.axis.remap("SSR", [i, j, k])
+                T.reads(A[v_i, v_k], B[v_k, v_j])
+                T.writes(C[v_i, v_j])
+                with T.init():
+                    C[v_i, v_j] = T.float16(0)
+                C[v_i, v_j] = C[v_i, v_j] + A[v_i, v_k] * B[v_k, v_j]
+# fmt: on
+# pylint: enable=no-self-argument,missing-class-docstring,line-too-long
+
+
+def _target():
+    return tvm.target.Target("llvm -num-cores=4")
+    # for local testing only
+    # return tvm.target.Target("nvidia/geforce-rtx-3070")
+
+
+def _get_input_output_info(func: tvm.tir.PrimFunc):
+    args = ArgInfo.from_prim_func(func)
+    inputs = [generate_input_data(x.shape, x.dtype) for x in args[:-1]]
+    output_shape = args[-1].shape
+    output_dtype = args[-1].dtype
+    return inputs, output_shape, output_dtype
+
+
+def _expected_results(
+    mod: tvm.ir.IRModule, inputs: List[np.ndarray], output_shape: Tuple, 
output_dtype: str
+):
+    rt_mod = tvm.build(mod, target="llvm")
+    data = [
+        tvm.nd.array(x)
+        for x in [
+            *inputs,
+            np.zeros(output_shape, dtype=output_dtype),
+        ]
+    ]
+    rt_mod(*data)
+    return data[-1].numpy()
+
+
+def _actual_results(
+    actual: tvm.ir.IRModule, inputs: List[np.ndarray], output_shape: Tuple, 
output_dtype: str
+):
+    target = _target()
+    actual_rt_mod = tvm.build(actual, target=target)
+    actual_data = [
+        tvm.nd.array(x, device=tvm.cuda() if target.kind.name == "cuda" else 
tvm.cpu())
+        for x in [
+            *inputs,
+            np.zeros(output_shape, dtype=output_dtype),
+        ]
+    ]
+    actual_rt_mod(*actual_data)
+    return actual_data[-1].numpy()
+
+
+def _assert_allclose(mod: tvm.ir.IRModule, actual: tvm.ir.IRModule, func: 
tvm.tir.PrimFunc):
+    inputs, output_shape, output_dtype = _get_input_output_info(func)
+    expected_output = _expected_results(mod, inputs, output_shape, 
output_dtype)
+    actual_output = _actual_results(actual, inputs, output_shape, output_dtype)
+    tvm.testing.assert_allclose(expected_output, actual_output, rtol=1e-2, 
atol=1e-2)

Review Comment:
   Thanks Sung, the reason we were using 1e-2 is because the unit test was 
running on cuda, which has slight difference compare to on our baseline on 
llvm. Now that the default target is changed to llvm I'll change the threshold 
accordingly!



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