Lunderberg commented on code in PR #16204:
URL: https://github.com/apache/tvm/pull/16204#discussion_r1416625283


##########
tests/python/relax/test_transform_convert_dataflow.py:
##########
@@ -0,0 +1,493 @@
+# 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 tvm
+import tvm.testing
+from tvm import relax
+from tvm.script import ir as I
+from tvm.script import relax as R
+
+
+class ExtractCompare(tvm.testing.CompareBeforeAfter):
+    transform = relax.transform.ConvertToDataflow()
+
+
+# functions that will not change
+class TestTrivial(ExtractCompare):
+    @I.ir_module
+    class Before:
+        # already a DF block
+        @R.function
+        def main(A: R.Tensor, B: R.Tensor):
+            with R.dataflow():
+                x = R.add(A, B)
+                y = R.multiply(x, A)
+                z = R.add(x, y)
+                q = R.multiply(y, z)
+                p = R.add(z, q)
+                R.output(p)
+            return p
+
+        # too small
+        @R.function
+        def func(A: R.Tensor, B: R.Tensor) -> R.Tensor:
+            x = R.add(A, B)
+            return x
+
+        # too few pure ops between non-dataflow ops
+        @R.function(pure=False)
+        def func2(A: R.Tensor, B: R.Tensor) -> R.Tensor:
+            _ = R.print(format="Hi there!")
+            y = R.add(A, B)
+            _ = R.print(y, format="Sum: {}")
+            x = R.multiply(y, y)
+            if R.const(False):
+                _ = R.print(format="True branch")
+                q = R.add(x, y)
+                _ = R.print(q, format="Value of q: {}")
+                w = q
+            else:
+                _ = R.print(format="False branch")
+                q = R.subtract(x, y)
+                _ = R.print(q, format="Value of q: {}")
+                w = q
+            p = R.multiply(w, w)
+            return p
+
+    Expected = Before
+
+
+class TestBasic(ExtractCompare):
+    @I.ir_module
+    class Before:
+        @R.function
+        def main(x: R.Tensor, y: R.Tensor) -> R.Tensor:
+            z = R.add(x, y)
+            w = R.multiply(z, y)
+            v = R.add(w, x)
+            return v
+
+    @I.ir_module
+    class Expected:
+        @R.function
+        def main(x: R.Tensor, y: R.Tensor) -> R.Tensor:
+            with R.dataflow():
+                z = R.add(x, y)
+                w = R.multiply(z, y)
+                v = R.add(w, x)
+                R.output(v)
+            return v
+
+
+class TestMultipleBlocks(ExtractCompare):
+    @I.ir_module
+    class Before:
+        @R.function(pure=False)
+        def main(x: R.Tensor, y: R.Tensor) -> R.Tensor:
+            z = R.add(x, y)
+            w = R.multiply(z, y)
+            v = R.add(w, x)
+            _ = R.print(format="Hi mom!")
+            a = R.multiply(v, v)
+            b = R.add(a, a)
+            c = R.subtract(b, a)
+            d = R.add(c, c)
+            return d
+
+    @I.ir_module
+    class Expected:
+        @R.function(pure=False)
+        def main(x: R.Tensor, y: R.Tensor) -> R.Tensor:
+            with R.dataflow():
+                z = R.add(x, y)
+                w = R.multiply(z, y)
+                v = R.add(w, x)
+                R.output(v)
+            _ = R.print(format="Hi mom!")
+            with R.dataflow():
+                a = R.multiply(v, v)
+                b = R.add(a, a)
+                c = R.subtract(b, a)
+                d = R.add(c, c)
+                R.output(d)
+            return d
+
+
+class TestExtractInsideBranches(ExtractCompare):
+    @I.ir_module
+    class Before:
+        @R.function(pure=False)
+        def main(x: R.Tensor, y: R.Tensor) -> R.Tensor:
+            z = R.add(x, y)
+            w = R.multiply(z, y)
+            v = R.add(w, x)
+            if R.const(True):
+                q = R.multiply(v, v)
+                a = R.add(q, q)
+                b = R.multiply(a, a)
+            else:
+                q = R.add(v, v)
+                a = R.multiply(q, q)
+                b = R.add(a, a)
+            c = R.multiply(b, b)
+            d = R.add(c, c)
+            e = R.multiply(d, d)
+            return e
+
+    @I.ir_module
+    class Expected:
+        @R.function(pure=False)
+        def main(x: R.Tensor, y: R.Tensor) -> R.Tensor:
+            with R.dataflow():
+                z = R.add(x, y)
+                w = R.multiply(z, y)
+                v = R.add(w, x)
+                R.output(v)
+
+            if R.const(True):
+                with R.dataflow():
+                    q = R.multiply(v, v)
+                    a = R.add(q, q)
+                    b = R.multiply(a, a)
+                    R.output(b)
+                # weird but the parser requires this construct
+                c = b
+            else:
+                with R.dataflow():
+                    q = R.add(v, v)
+                    a = R.multiply(q, q)
+                    b = R.add(a, a)
+                    R.output(b)
+                c = b
+            with R.dataflow():
+                d = R.multiply(c, c)
+                e = R.add(d, d)
+                f = R.multiply(e, e)
+                R.output(f)
+            return f
+
+
+class TestTreatNonCallAsPure(ExtractCompare):
+    @I.ir_module
+    class Before:
+        @R.function
+        def tuples_and_const(x: R.Tensor, y: R.Tensor) -> R.Tensor:
+            t1 = (x, y, x)
+            t2 = (y, y, x)
+            c = R.const([1, 2, 3], dtype="int32")
+            return c
+
+        @R.function
+        def shapes() -> R.Shape:
+            s1 = R.shape((1, 2, 3))
+            s2 = R.shape((4, 5, 6))
+            s3 = R.shape((7, 8, 9))
+            return s3
+
+        @R.function
+        def prim_values():

Review Comment:
   I think a more stable way to have the pretty-printing from TVMScript would 
be to remove all of the try/catch that only exist to re-throw as 
DiagnosticError, and instead allow the exception to bubble up to the entry 
point.  Catching the exception at the entry point, the `exc.__traceback__` can 
then be inspected.  The TVMScript location of the error is then the lowest 
stack frame that contains a variable of type `doc.AST`, and that object can be 
provided to `Parser.report_error` as the location of the error in the TVMScript.
   
   This would avoid requiring every node to have its own try/catch/re-throw, 
and avoid the segfault that we ran into here.
   
   @junrushao Since you've spent more time on the TVMScript parser than I have, 
what are your thoughts on this idea?



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