Lunderberg commented on code in PR #16204: URL: https://github.com/apache/tvm/pull/16204#discussion_r1416620121
########## 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: Whoa, that's an impressive segfault. It looks like the sequence of events is as follows: 1. We hit [this TypeError](https://github.com/apache/tvm/blob/unity/python/tvm/script/parser/relax/entry.py#L452), which gives a diagnostic that the `R.Prim` needs to have one of the two argument types. 2. The TypeError is caught [here](https://github.com/apache/tvm/blob/unity/python/tvm/script/parser/relax/parser.py#L102), where it is passed to the diagnostic context to pretty-print and re-throw. 3. After showing the error message, [this conditional](https://github.com/apache/tvm/blob/unity/src/ir/diagnostic.cc#L129) resets the diagnostic renderer and raises an exception. 4. The DiagnosticError is caught [here](https://github.com/apache/tvm/blob/unity/python/tvm/script/parser/relax/parser.py#L111), where it is passed to the diagnostic context to pretty-print and re-throw. 5. When reaching [here](https://github.com/apache/tvm/blob/unity/src/ir/diagnostic.cc#L118), `(*this)->renderer` has been replaced with a default constructed instance. The default-constructed instance from step (3) has a nullptr for the renderer, which causes the segfault. It looks like there's a lot of `except DiagnosticError: raise` scattered throughout the parser to prevent this from occurring (by preventing step (4) from occurring), but these `eval_struct_info` and `eval_struct_info_proxy` don't have it. That seems like a rather fragile way to avoid some pretty opaque errors, so I'm wondering if we want to change that in the long term. -- 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]
