You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by "Lunderberg (via GitHub)" <gi...@apache.org> on 2023/08/10 19:54:08 UTC

[GitHub] [tvm] Lunderberg commented on a diff in pull request #15509: [Unity] Implement relax.Function.bind_symbolic_vars

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


##########
tests/python/relax/test_bind_symbolic_vars.py:
##########
@@ -0,0 +1,205 @@
+# 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 pytest
+
+import tvm
+import tvm.testing
+from tvm.script import relax as R, tir as T
+
+replace_by_tir_var = tvm.testing.parameter(
+    by_dict={"replace-by-string": False, "replace-by-tir-var": True}
+)
+
+
+def test_bind_static_value(replace_by_tir_var):
+    """Symbolic vars may be replaced
+
+    The replaced variables may be given either as strings, or as TIR variables
+    """
+
+    @R.function(private=True)
+    def before(A: R.Tensor(("M", "K")), B: R.Tensor(("K", "N"))) -> R.Tensor(("M", "N")):
+        return R.matmul(A, B)
+
+    @R.function(private=True)
+    def expected(A: R.Tensor((128, 64)), B: R.Tensor((64, 32))) -> R.Tensor((128, 32)):
+        return R.matmul(A, B)
+
+    if replace_by_tir_var:
+        M, K = before.params[0].struct_info.shape
+        _, N = before.params[1].struct_info.shape
+        symbolic_var_map = {M: 128, K: 64, N: 32}
+    else:
+        symbolic_var_map = {"M": 128, "K": 64, "N": 32}
+
+    after = before.bind_symbolic_vars(symbolic_var_map)
+    tvm.ir.assert_structural_equal(expected, after)
+
+
+def test_error_with_duplicate_var_names():
+    """Duplicate variable names may not be replaced by string

Review Comment:
   On the C++ side, both TIR variables and Relax variables have reference identity, and duplicate names can exist within the same function.  (Technically, the variables only have a `name_hint`, not a name, which I believe is to avoid the assumption of unique names.  (Though, double parenthetically, some of the python APIs expose `name_hint` as `name`.))  TVMScript has a tighter restriction, where variables tracked by the parser may not repeat within the same scope.  In this test, I define the variables with the same TIR variable name outside of TVMScript, to produce the ambiguous test case anyways.
   
   As a bit of fun, the function can be inspected, and even though TVMScript modifies the variable names to avoid printing duplicate variables, the underlying objects still have duplicate names.
   
   ```python
   M = tir.Var("M", "int64")
   N1 = tir.Var("N", "int64")
   N2 = tir.Var("N", "int64")
   
   dtype = "float32"
   
   
   @R.function
   def func(
       A: R.Tensor([M, N2], dtype), B: R.Tensor([N1, N2], dtype)
   ) -> R.Tensor([M, N1], dtype):
       A: R.Tensor([M, N2], dtype) = A
       B: R.Tensor([N1, N2], dtype) = B
       out = R.linear(A, B)
       return out
   
   
   # Prints  B: R.Tensor(("N", "N_1"), dtype="float32")
   func.show()
   # But the true name can still be extracted
   assert func.params[1].struct_info.shape[0].name == "N"
   assert func.params[1].struct_info.shape[1].name == "N"
   # And the function is well-formed, despite the duplicate variable
   # names.
   assert tvm.relax.analysis.well_formed(tvm.IRModule.from_expr(func))
   ```



-- 
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-unsubscribe@tvm.apache.org

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