gemini-code-assist[bot] commented on code in PR #18633:
URL: https://github.com/apache/tvm/pull/18633#discussion_r2659803819
##########
tests/python/relax/test_transform_to_mixed_precision.py:
##########
@@ -1064,5 +1064,56 @@ def tir_identity(
tvm.ir.assert_structural_equal(Expected, After)
+def test_dynamic_strided_slice():
+ @I.ir_module
+ class Input:
+ @R.function
+ def main(
+ x: R.Tensor((2, 3, 28, 28), "float32"),
+ w: R.Tensor((4, 3, 3, 3), "float32"),
+ begin: R.Tensor((4,), "int64"),
+ end: R.Tensor((4,), "int64"),
+ strides: R.Tensor((4,), "int64"),
+ ) -> R.Tensor(None, "float32", ndim=4):
+ with R.dataflow():
+ lv: R.Tensor((2, 4, 26, 26), "float32") = R.nn.conv2d(x, w,
out_dtype="float32")
+ gv = R.dynamic_strided_slice(lv, begin, end, strides)
+ R.output(gv)
+ return gv
+
+ @I.ir_module
+ class Expected:
+ @R.function
+ def main(
+ x: R.Tensor((2, 3, 28, 28), dtype="float32"),
+ w: R.Tensor((4, 3, 3, 3), dtype="float32"),
+ begin: R.Tensor((4,), dtype="int64"),
+ end: R.Tensor((4,), dtype="int64"),
+ strides: R.Tensor((4,), dtype="int64"),
+ ) -> R.Tensor(None, dtype="float32", ndim=4):
+ with R.dataflow():
+ lv: R.Tensor((2, 3, 28, 28), dtype="float16") = R.astype(x,
dtype="float16")
+ lv1: R.Tensor((4, 3, 3, 3), dtype="float16") = R.astype(w,
dtype="float16")
+ lv2: R.Tensor((2, 4, 26, 26), dtype="float32") = R.nn.conv2d(
+ lv,
+ lv1,
+ strides=[1, 1],
+ padding=[0, 0, 0, 0],
+ dilation=[1, 1],
+ groups=1,
+ data_layout="NCHW",
+ kernel_layout="OIHW",
+ out_layout="NCHW",
+ out_dtype="float32",
+ )
+ lv3: R.Tensor((2, 4, 26, 26), dtype="float16") = R.astype(lv2,
dtype="float16")
+ lv4: R.Tensor((2, 4, 26, 26), dtype="float32") = R.astype(lv3,
dtype="float32")
+ gv: R.Tensor(None, dtype="float32", ndim=4) =
R.dynamic_strided_slice(lv4, begin, end, strides)
Review Comment:

The `Expected` module contains a redundant sequence of casts. The `conv2d`
output `lv2` is `float32`. It is cast to `float16` (as `lv3`), then immediately
back to `float32` (as `lv4`), before being used in `dynamic_strided_slice`.
This is inefficient.
Since `dynamic_strided_slice` is a `kFollow` operator and its input from the
preceding `conv2d` is already `float32`, the intermediate casts are
unnecessary. The `dynamic_strided_slice` operator can directly use the `lv2`
tensor. The expected transformed code should be more optimal.
```suggestion
gv: R.Tensor(None, dtype="float32", ndim=4) =
R.dynamic_strided_slice(lv2, begin, end, strides)
```
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]