Hzfengsy commented on code in PR #11589:
URL: https://github.com/apache/tvm/pull/11589#discussion_r893112264


##########
tests/python/unittest/test_tir_te_extern_primfunc.py:
##########
@@ -0,0 +1,257 @@
+# 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 sys
+import pytest
+import numpy as np
+
+import tvm
+import tvm.testing
+from tvm import tir, te, TVMError
+from tvm.script import tir as T
+from tvm.arith import _ffi_api as _ffi_arith_api
+from tvm.tir.schedule import _ffi_api as _ffi_schedule_api
+
+
+# TODO(csullivan): Additional tests cases needed:
+# - PrimFunc with 1 arg, inplace update
+# - PrimFunc with buffer that uses custom storage_scope
+
+
+@T.prim_func
+def func_1(A: T.Buffer[(16,), "float32"], C: T.Buffer[(1,), "float32"]):
+    for i in T.serial(
+        0,
+        16,
+    ):
+        with T.block():
+            B = T.alloc_buffer((1,), dtype="float32")
+            with T.block():
+                B[0] = A[i] * T.float32(2)
+            with T.block():
+                C[0] = C[0] + A[i] + B[0] + T.float32(1)
+                A[i] = B[0] + T.float32(1)
+
+
+def verify_func_1(module):
+    a_np = np.random.randint(low=-128, high=127, size=(16,)).astype(np.float32)
+    c_np = np.zeros((1,), dtype=np.float32)
+    a = tvm.nd.array(a_np, device=tvm.cpu(0))
+    c = tvm.nd.array(c_np, device=tvm.cpu(0))
+
+    module(a, c)
+    tvm.testing.assert_allclose(c_np + np.sum(3 * a_np + 1), c.numpy(), 
rtol=1e-4)
+    # also test in place update
+    tvm.testing.assert_allclose(a_np * 2 + 1, a.numpy(), rtol=1e-4)
+
+
+@T.prim_func
+def func_2(
+    C: T.Buffer[(1,), "float32"], A: T.Buffer[(16,), "float32"], D: 
T.Buffer[(2,), "float32"]
+):
+    for i in T.serial(
+        0,
+        16,
+    ):
+        with T.block():
+            B = T.alloc_buffer((1,), dtype="float32")
+            with T.block():
+                B[0] = A[i] * T.float32(2)
+            with T.block():
+                C[0] = C[0] + A[i] + B[0] + T.float32(1) + D[0]
+                A[i] = B[0] + T.float32(1) + D[1]
+
+
+def verify_func_2(module):
+    a_np = np.random.randint(low=-128, high=127, size=(16,)).astype(np.float32)
+    d_np = np.random.randint(low=-128, high=127, size=(2,)).astype(np.float32)
+    c_np = np.zeros((1,), dtype=np.float32)
+    a = tvm.nd.array(a_np, device=tvm.cpu(0))
+    d = tvm.nd.array(d_np, device=tvm.cpu(0))
+    c = tvm.nd.array(c_np, device=tvm.cpu(0))
+
+    module(c, a, d)
+    tvm.testing.assert_allclose(c_np + np.sum(3 * a_np + 1 + d_np[0]), 
c.numpy(), rtol=1e-4)
+    tvm.testing.assert_allclose(a_np * 2 + 1 + d_np[1], a.numpy(), rtol=1e-4)
+
+
+@T.prim_func
+def func_3(
+    C: T.Buffer[(1,), "float32"],
+    A: T.Buffer[(16,), "float32"],
+    D: T.Buffer[(2,), "float32"],
+    E: T.Buffer[(16,), "float32"],
+    F: T.Buffer[(16,), "float32"],
+):
+    for i in T.serial(
+        0,
+        16,
+    ):
+        with T.block():
+            B = T.alloc_buffer((1,), dtype="float32")
+            with T.block():
+                B[0] = A[i] * T.float32(2)
+            with T.block():
+                E[i] = A[i]
+                F[i] = E[i] + 1.0
+                C[0] = C[0] + A[i] + B[0] + T.float32(1) + D[0]
+                A[i] = B[0] + T.float32(1) + D[1]
+
+
+def verify_func_3(module):
+    a_np = np.random.randint(low=-128, high=127, size=(16,)).astype(np.float32)
+    d_np = np.random.randint(low=-128, high=127, size=(2,)).astype(np.float32)
+    c_np = np.zeros((1,), dtype=np.float32)
+    e_np = np.zeros((16,), dtype=np.float32)
+    f_np = np.zeros((16,), dtype=np.float32)
+    a = tvm.nd.array(a_np, device=tvm.cpu(0))
+    d = tvm.nd.array(d_np, device=tvm.cpu(0))
+    c = tvm.nd.array(c_np, device=tvm.cpu(0))
+    e = tvm.nd.array(e_np, device=tvm.cpu(0))
+    f = tvm.nd.array(f_np, device=tvm.cpu(0))
+
+    module(c, a, d, e, f)
+    tvm.testing.assert_allclose(c_np + np.sum(3 * a_np + 1 + d_np[0]), 
c.numpy(), rtol=1e-4)
+    tvm.testing.assert_allclose(a_np * 2 + 1 + d_np[1], a.numpy(), rtol=1e-4)
+    tvm.testing.assert_allclose(a_np, e.numpy(), rtol=1e-4)
+    tvm.testing.assert_allclose(a_np + 1, f.numpy(), rtol=1e-4)
+
+
+@T.prim_func
+def func_4(
+    C: T.Buffer[(1,), "float32"],
+    A: T.Buffer[(16,), "float32"],
+    F: T.Buffer[(16,), "float32"],
+    D: T.Buffer[(2,), "float32"],
+    E: T.Buffer[(16,), "float32"],
+):
+    for i in T.serial(
+        0,
+        16,
+    ):

Review Comment:
   ```suggestion
       for i in range(16):
   ```



##########
tests/python/unittest/test_tir_te_extern_primfunc.py:
##########
@@ -0,0 +1,257 @@
+# 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 sys
+import pytest
+import numpy as np
+
+import tvm
+import tvm.testing
+from tvm import tir, te, TVMError
+from tvm.script import tir as T
+from tvm.arith import _ffi_api as _ffi_arith_api
+from tvm.tir.schedule import _ffi_api as _ffi_schedule_api
+
+
+# TODO(csullivan): Additional tests cases needed:
+# - PrimFunc with 1 arg, inplace update
+# - PrimFunc with buffer that uses custom storage_scope
+
+
+@T.prim_func
+def func_1(A: T.Buffer[(16,), "float32"], C: T.Buffer[(1,), "float32"]):
+    for i in T.serial(
+        0,
+        16,
+    ):

Review Comment:
   ```suggestion
       for i in range(16):
   ```



##########
tests/python/unittest/test_tir_te_extern_primfunc.py:
##########
@@ -0,0 +1,257 @@
+# 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 sys
+import pytest
+import numpy as np
+
+import tvm
+import tvm.testing
+from tvm import tir, te, TVMError
+from tvm.script import tir as T
+from tvm.arith import _ffi_api as _ffi_arith_api
+from tvm.tir.schedule import _ffi_api as _ffi_schedule_api
+
+
+# TODO(csullivan): Additional tests cases needed:
+# - PrimFunc with 1 arg, inplace update
+# - PrimFunc with buffer that uses custom storage_scope
+
+
+@T.prim_func
+def func_1(A: T.Buffer[(16,), "float32"], C: T.Buffer[(1,), "float32"]):
+    for i in T.serial(
+        0,
+        16,
+    ):
+        with T.block():
+            B = T.alloc_buffer((1,), dtype="float32")
+            with T.block():
+                B[0] = A[i] * T.float32(2)
+            with T.block():
+                C[0] = C[0] + A[i] + B[0] + T.float32(1)
+                A[i] = B[0] + T.float32(1)
+
+
+def verify_func_1(module):
+    a_np = np.random.randint(low=-128, high=127, size=(16,)).astype(np.float32)
+    c_np = np.zeros((1,), dtype=np.float32)
+    a = tvm.nd.array(a_np, device=tvm.cpu(0))
+    c = tvm.nd.array(c_np, device=tvm.cpu(0))
+
+    module(a, c)
+    tvm.testing.assert_allclose(c_np + np.sum(3 * a_np + 1), c.numpy(), 
rtol=1e-4)
+    # also test in place update
+    tvm.testing.assert_allclose(a_np * 2 + 1, a.numpy(), rtol=1e-4)
+
+
+@T.prim_func
+def func_2(
+    C: T.Buffer[(1,), "float32"], A: T.Buffer[(16,), "float32"], D: 
T.Buffer[(2,), "float32"]
+):
+    for i in T.serial(
+        0,
+        16,
+    ):

Review Comment:
   ```suggestion
       for i in range(16):
   ```



##########
tests/python/unittest/test_tir_te_extern_primfunc.py:
##########
@@ -0,0 +1,257 @@
+# 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 sys
+import pytest
+import numpy as np
+
+import tvm
+import tvm.testing
+from tvm import tir, te, TVMError
+from tvm.script import tir as T
+from tvm.arith import _ffi_api as _ffi_arith_api
+from tvm.tir.schedule import _ffi_api as _ffi_schedule_api
+
+
+# TODO(csullivan): Additional tests cases needed:
+# - PrimFunc with 1 arg, inplace update
+# - PrimFunc with buffer that uses custom storage_scope
+
+
+@T.prim_func
+def func_1(A: T.Buffer[(16,), "float32"], C: T.Buffer[(1,), "float32"]):
+    for i in T.serial(
+        0,
+        16,
+    ):
+        with T.block():
+            B = T.alloc_buffer((1,), dtype="float32")
+            with T.block():
+                B[0] = A[i] * T.float32(2)
+            with T.block():
+                C[0] = C[0] + A[i] + B[0] + T.float32(1)
+                A[i] = B[0] + T.float32(1)
+
+
+def verify_func_1(module):
+    a_np = np.random.randint(low=-128, high=127, size=(16,)).astype(np.float32)
+    c_np = np.zeros((1,), dtype=np.float32)
+    a = tvm.nd.array(a_np, device=tvm.cpu(0))
+    c = tvm.nd.array(c_np, device=tvm.cpu(0))
+
+    module(a, c)
+    tvm.testing.assert_allclose(c_np + np.sum(3 * a_np + 1), c.numpy(), 
rtol=1e-4)
+    # also test in place update
+    tvm.testing.assert_allclose(a_np * 2 + 1, a.numpy(), rtol=1e-4)
+
+
+@T.prim_func
+def func_2(
+    C: T.Buffer[(1,), "float32"], A: T.Buffer[(16,), "float32"], D: 
T.Buffer[(2,), "float32"]
+):
+    for i in T.serial(
+        0,
+        16,
+    ):
+        with T.block():
+            B = T.alloc_buffer((1,), dtype="float32")
+            with T.block():
+                B[0] = A[i] * T.float32(2)
+            with T.block():
+                C[0] = C[0] + A[i] + B[0] + T.float32(1) + D[0]
+                A[i] = B[0] + T.float32(1) + D[1]
+
+
+def verify_func_2(module):
+    a_np = np.random.randint(low=-128, high=127, size=(16,)).astype(np.float32)
+    d_np = np.random.randint(low=-128, high=127, size=(2,)).astype(np.float32)
+    c_np = np.zeros((1,), dtype=np.float32)
+    a = tvm.nd.array(a_np, device=tvm.cpu(0))
+    d = tvm.nd.array(d_np, device=tvm.cpu(0))
+    c = tvm.nd.array(c_np, device=tvm.cpu(0))
+
+    module(c, a, d)
+    tvm.testing.assert_allclose(c_np + np.sum(3 * a_np + 1 + d_np[0]), 
c.numpy(), rtol=1e-4)
+    tvm.testing.assert_allclose(a_np * 2 + 1 + d_np[1], a.numpy(), rtol=1e-4)
+
+
+@T.prim_func
+def func_3(
+    C: T.Buffer[(1,), "float32"],
+    A: T.Buffer[(16,), "float32"],
+    D: T.Buffer[(2,), "float32"],
+    E: T.Buffer[(16,), "float32"],
+    F: T.Buffer[(16,), "float32"],
+):
+    for i in T.serial(
+        0,
+        16,
+    ):

Review Comment:
   ```suggestion
       for i in range(16):
   ```



-- 
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: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to