jinhongyii commented on code in PR #16049:
URL: https://github.com/apache/tvm/pull/16049#discussion_r1385544554
##########
tests/python/relax/test_transform_lift_transform_params.py:
##########
@@ -642,5 +642,95 @@ def slice(
tvm.ir.assert_structural_equal(Expected, after)
+def test_symbolic_var_in_param_shape():
+ @tvm.script.ir_module
+ class Before:
+ @R.function
+ def main(
+ x: R.Tensor((1, 16, 224, "n"), "float32"),
+ w1: R.Tensor((16, "m", 3, 3), "float32"),
+ w2: R.Tensor((16, "m", 3, 3), "float32"),
+ ) -> R.Tensor((1, 16, 224, 224), "float32"):
+ m = T.int64()
+ n = T.int64()
+ R.func_attr({"num_input": 1})
+ with R.dataflow():
+ zeros = R.zeros((n, n), "float32")
+ w1 = R.add(w1, R.const(1, "float32"))
+ conv1 = R.nn.conv2d(x, w1, padding=(1, 1), data_layout="NCHW",
kernel_layout="OIHW")
+ conv2 = R.nn.conv2d(
+ conv1, w2, padding=(1, 1), data_layout="NCHW",
kernel_layout="OIHW"
+ )
+ R.output(conv2)
+ return conv2
+
+ @I.ir_module
+ class Expected:
+ @R.function
+ def main_transform_params(
+ params: R.Tuple(
+ R.Tensor((16, "m", 3, 3), dtype="float32"),
+ R.Tensor((16, "m", 3, 3), dtype="float32"),
+ )
+ ) -> R.Tuple(
+ R.Tensor((16, "m", 3, 3), dtype="float32"), R.Tensor((16, "m", 3,
3), dtype="float32")
+ ):
+ m = T.int64()
+ with R.dataflow():
+ lv: R.Tensor((16, m, 3, 3), dtype="float32") = params[1]
+ lv1: R.Tensor((16, m, 3, 3), dtype="float32") = params[0]
+ lv2: R.Tensor((16, m, 3, 3), dtype="float32") = R.add(lv1,
R.const(1, "float32"))
+ gv: R.Tuple(
+ R.Tensor((16, m, 3, 3), dtype="float32"),
+ R.Tensor((16, m, 3, 3), dtype="float32"),
+ ) = (lv, lv2)
+ R.output(gv)
+ return gv
+
+ @R.function
+ def main(
+ x: R.Tensor((1, 16, 224, "n"), dtype="float32"),
+ transformed_param_0: R.Tensor((16, "m", 3, 3), dtype="float32"),
+ transformed_param_1: R.Tensor((16, "m", 3, 3), dtype="float32"),
+ ) -> R.Tensor((1, 16, 224, 224), dtype="float32"):
+ n = T.int64()
+ m = T.int64()
+ R.func_attr({"num_input": 1})
+ with R.dataflow():
+ zeros: R.Tensor((n, n), dtype="float32") = R.zeros(R.shape([n,
n]), dtype="float32")
+ lv: R.Tensor((16, m, 3, 3), dtype="float32") =
transformed_param_1
+ conv1: R.Tensor((1, 16, 224, n), dtype="float32") =
R.nn.conv2d(
+ x,
+ lv,
+ strides=[1, 1],
+ padding=[1, 1, 1, 1],
+ dilation=[1, 1],
+ groups=1,
+ data_layout="NCHW",
+ kernel_layout="OIHW",
+ out_layout="NCHW",
+ out_dtype="void",
+ )
+ lv1: R.Tensor((16, m, 3, 3), dtype="float32") =
transformed_param_0
+ conv2: R.Tensor((1, 16, 224, n), dtype="float32") =
R.nn.conv2d(
+ conv1,
+ lv1,
+ strides=[1, 1],
+ padding=[1, 1, 1, 1],
+ dilation=[1, 1],
+ groups=1,
+ data_layout="NCHW",
+ kernel_layout="OIHW",
+ out_layout="NCHW",
+ out_dtype="void",
+ )
+ R.output(conv2)
+ return conv2
+
+ mod = Before
+ after = relax.transform.LiftTransformParams()(mod)
+ tvm.ir.assert_structural_equal(after, Expected)
+
Review Comment:
The unittest should be introduced in another PR because it involves another
problem. Below is the program after applying the pass and you can see that
main_transform_params is not well defined because m and n are not defined.
```@I.ir_module
class Module:
@R.function
def main_transform_params(params: R.Tuple(R.Tensor(("m * n",),
dtype="float32"))) -> R.Tuple(R.Tensor(dtype="float32", ndim=1)):
m = T.int64()
n = T.int64()
with R.dataflow():
lv: R.Tensor((m * n,), dtype="float32") = params[0]
gv: R.Tuple(R.Tensor((m * n,), dtype="float32")) = (lv,)
R.output(gv)
return gv
@R.function
def main(x: R.Tensor(("m", "n"), dtype="float32"), transformed_param_0:
R.Tensor(dtype="float32", ndim=1)) -> R.Tensor(("m", "n"), dtype="float32"):
m = T.int64()
n = T.int64()
R.func_attr({"num_input": 1})
with R.dataflow():
lv: R.Tensor(dtype="float32", ndim=1) = transformed_param_0
weight: R.Tensor(dtype="float32", ndim=1) = R.add(lv, R.const(1,
"float32"))
weight_1: R.Tensor((m, n), dtype="float32") = R.reshape(weight,
R.shape([m, n]))
output: R.Tensor((m, n), dtype="float32") = R.multiply(x,
weight_1)
R.output(output)
return output
```
An ideal output should be
```@I.ir_module
class Module:
@R.function
def main_transform_params(params: R.Tuple(R.Tensor(("k",),
dtype="float32"))) -> R.Tuple(R.Tensor(dtype="float32", ndim=1)):
k = T.int64()
with R.dataflow():
lv: R.Tensor((k,), dtype="float32") = params[0]
gv: R.Tuple(R.Tensor((k,), dtype="float32")) = (lv,)
R.output(gv)
return gv
@R.function
def main(x: R.Tensor(("m", "n"), dtype="float32"), transformed_param_0:
R.Tensor(dtype="float32", ndim=1)) -> R.Tensor(("m", "n"), dtype="float32"):
m = T.int64()
n = T.int64()
R.func_attr({"num_input": 1})
with R.dataflow():
lv: R.Tensor(dtype="float32", ndim=1) = transformed_param_0
weight: R.Tensor(dtype="float32", ndim=1) = R.add(lv, R.const(1,
"float32"))
weight_1: R.Tensor((m, n), dtype="float32") = R.reshape(weight,
R.shape([m, n]))
output: R.Tensor((m, n), dtype="float32") = R.multiply(x,
weight_1)
R.output(output)
return output
```
--
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]