psrivas2 commented on code in PR #14394:
URL: https://github.com/apache/tvm/pull/14394#discussion_r1168909689
##########
src/relax/analysis/well_formed.cc:
##########
@@ -56,6 +56,14 @@
* * The op or args fields of Call nodes
* * Inside the fields of Tuple nodes
* 13. Expr always has checked_type_ (with the exception of Op).
+ * 14. DataflowBlocks may not contain If nodes.
+ * 15. DataflowBlocks may not contain calls to impure functions or operators
+ * (only checked if check_struct_info is true).
+ * 16. If a function is annotated as pure (kIsPure is true)
+ * and purity is not forced (kForcePure is true),
+ * the body may not contain any impure call
+ * (only checked if check_struct_info is true).
+ * 17. If a function's purity is forced, kForcePure cannot be true
Review Comment:
I don't understand `17`. If the function's purity is forced, wouldn't
kForcePure have to be set as `true`?
##########
src/relax/op/image/resize.cc:
##########
@@ -122,7 +122,8 @@ TVM_REGISTER_OP("relax.image.resize2d")
.add_argument("size", "Shape", "The output image shape.")
.set_attr<FInferStructInfo>("FInferStructInfo", InferStructInfoResize2D)
.set_attr<FRelaxInferLayout>("FRelaxInferLayout", InferLayoutResize2d)
- .set_attr<TMixedPrecisionPolicy>("TMixedPrecisionPolicy",
MixedPrecisionPolicyKind::kFollow);
+ .set_attr<TMixedPrecisionPolicy>("TMixedPrecisionPolicy",
MixedPrecisionPolicyKind::kFollow)
+ .set_attr<Bool>("FPurity", Bool(true));
Review Comment:
seems like all ops have to be marked as pure except `call_dps_packed` and
`call_packed`.
cc @junrushao if there is any implication of this for op_schema which is
WIP.
##########
src/relax/op/op.cc:
##########
@@ -292,7 +349,10 @@ RELAY_REGISTER_OP("relax.invoke_closure")
.set_num_inputs(2)
.add_argument("closure", "Expr", "The VMClosure.")
.add_argument("args", "Tuple", "The captured variables.")
- .set_attr<FInferStructInfo>("FInferStructInfo",
InferStructInfoInvokeClosure);
+ .set_attr<FInferStructInfo>("FInferStructInfo",
InferStructInfoInvokeClosure)
+ // TODO: This might be another case where we would want a macro or even
use an attr.
+ // It may depend on the particulars of the closure
+ .set_attr<Bool>("FPurity", Bool(false));
Review Comment:
this concern is resolved now with ForcePure and RemoveTrackingPurity, right?
##########
src/relax/op/op.cc:
##########
@@ -464,7 +554,9 @@
TVM_REGISTER_GLOBAL("relax.op.memory.kill_storage").set_body_typed(MakeMemKillSt
RELAY_REGISTER_OP("relax.memory.kill_tensor")
.set_num_inputs(1)
.add_argument("tensor", "Expr", "The tensor to be killed.")
- .set_attr<FInferStructInfo>("FInferStructInfo", ReturnVoidStructInfo);
+ .set_attr<FInferStructInfo>("FInferStructInfo", ReturnVoidStructInfo)
+ // memory deallocation also isn't considered a "visible effect" as far as
purity is concerned
Review Comment:
same as above
##########
tests/python/relax/test_analysis_contains_impure_call.py:
##########
@@ -0,0 +1,104 @@
+# 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 as rx
+from tvm.relax.analysis import contains_impure_call
+from tvm.script import relax as R
+
+
+def test_simple_pure_case():
+ @tvm.script.ir_module
+ class PureTest:
+ @R.function
+ def pure_func(x: R.Tensor((), "int32")) -> R.Tensor((), "int32"):
+ y = R.add(x, x)
+ z = R.multiply(x, y)
+ return R.add(z, R.const(1, "int32"))
+
+ assert not contains_impure_call(PureTest["pure_func"])
+
+
+def test_simple_impure_case():
+ @tvm.script.ir_module
+ class ImpureTest:
+ @R.function
+ def impure_func() -> R.Object:
+ R.func_attr({"IsPure": False})
+ y = R.print(format="I am a message")
+ return y
+
+ assert contains_impure_call(ImpureTest["impure_func"])
+
+
+def test_nested_function():
+ @tvm.script.ir_module
+ class NestedTest:
+ @R.function
+ def pure_with_impure_nested() -> R.Tensor((), "int32"):
+ # unused
+ @R.function
+ def impure_inner() -> R.Object:
+ R.func_attr({"IsPure": False})
+ y = R.print(format="Another, worse, message")
+ return y
+
+ x = R.const(0, dtype="int32")
+ return R.add(x, x)
+
+ assert not contains_impure_call(NestedTest["pure_with_impure_nested"])
+ assert contains_impure_call(
+ NestedTest["pure_with_impure_nested"].body.blocks[0].bindings[0].value
+ )
+
+
+def test_ignoring_recursive_call():
+ # Ignoring a recursive call. This can be useful if some transformation
+ # removes an impure operation and the compiler needs to check if the impure
+ # function has become pure
+ @tvm.script.ir_module
+ class RecursiveTest:
+ @R.function
+ def recursive_impure() -> R.Object:
+ R.func_attr({"IsPure": False})
+ x = R.const(1, "int32")
+ y = R.add(x, x)
+ z = R.print(x, y, format="{} {}")
+ w = RecursiveTest.recursive_impure()
+ return w
+
+ assert contains_impure_call(RecursiveTest["recursive_impure"])
+ # but if we remove the impure call...
+ body = RecursiveTest["recursive_impure"].body
+ own_name = body.blocks[0].bindings[-1].value.op
+ # skipping the call to print...
+ new_bindings = [
+ body.blocks[0].bindings[0],
+ body.blocks[0].bindings[1],
+ body.blocks[0].bindings[-1],
+ ]
+ new_body = rx.SeqExpr([rx.BindingBlock(new_bindings)], body.body)
Review Comment:
can we create the new function using TVMScript again? Not sure if using rx.*
API is making it easier to follow or saving space.
##########
src/relax/op/op.cc:
##########
@@ -450,7 +538,9 @@
TVM_REGISTER_GLOBAL("relax.op.memory.alloc_tensor").set_body_typed(MakeMemAllocT
RELAY_REGISTER_OP("relax.memory.kill_storage")
.set_num_inputs(1)
.add_argument("storage", "Expr", "The storage to be killed.")
- .set_attr<FInferStructInfo>("FInferStructInfo", ReturnVoidStructInfo);
+ .set_attr<FInferStructInfo>("FInferStructInfo", ReturnVoidStructInfo)
+ // deallocation also isn't considered a "visible effect" as far as purity
is concerned
+ .set_attr<Bool>("FPurity", Bool(false));
Review Comment:
the comment says that deallocation is not considered a visible effect, yet
it is marked as impure?
--
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]