# How to use dynamic shape in relax?

**URL:** <https://discuss.tvm.apache.org/t/how-to-use-dynamic-shape-in-relax/15369>\
**Category:** unity\
**Created:** [July 21, 2023, 2:35am UTC](https://discuss.tvm.apache.org/t/how-to-use-dynamic-shape-in-relax/15369 "2023-07-21T02:35:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![leiwen83](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/leiwen83/32/3991_2.png) [@leiwen83](https://discuss.tvm.apache.org/u/leiwen83)\
**Post date:** [July 21, 2023, 2:35am UTC](https://discuss.tvm.apache.org/t/how-to-use-dynamic-shape-in-relax/15369/1 "2023-07-21T02:35:04Z")

</div>

Hi,

I recently begin to take to look for new dynamic shape feature supported by relax. While checking testcases as tests/python/relax/test\_codegen\_cutlass.py, I don’t see any dynamic shape infer has been made as a show case, or am I missing?

So what I want to know is for one simple conv2d forward, how could I test its dynamic shape feature with relax and use backend like cutlass?

Thx

---

<div class="post-metadata">

**Author:** ![WashSwang](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/w/edb3f5/32.png) [@WashSwang](https://discuss.tvm.apache.org/u/WashSwang)\
**Post date:** [July 21, 2023, 2:59am UTC](https://discuss.tvm.apache.org/t/how-to-use-dynamic-shape-in-relax/15369/2 "2023-07-21T02:59:09Z")

</div>

I’m also new to relax. I think currently the best practice for dynamic shape in relax is MLC-LLM that uses it to handle the dynamic sequence length. As for your question, I find that there are some symbolic shapes like \_vars[“a”] in tests/python/relax/test\_codegen\_cutlass.py. Relax uses it to represent the dynamic axis in the shape.

---

<div class="post-metadata">

**Author:** ![leiwen83](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/leiwen83/32/3991_2.png) [@leiwen83](https://discuss.tvm.apache.org/u/leiwen83)\
**Post date:** [July 21, 2023, 6:23am UTC](https://discuss.tvm.apache.org/t/how-to-use-dynamic-shape-in-relax/15369/3 "2023-07-21T06:23:44Z")

</div>

MLC-LLM is too complex to use as a start point to investigate the dynamic shape feature…

I try to modify test case in test\_codegen\_cutlass.py as below, and enable dynamic shape as below.

```auto
@tvm.script.ir_module
class Conv2dBiasReLU:
    @R.function
    def main(
        data: R.Tensor((1, tvm.tir.expr.Var("a", "int64"), 400, 16), "float32"),
        weight: R.Tensor((32, 3, 3, 16), "float32"),
        bias: R.Tensor((1, 1, 1, 32), "float32"),
    ):
        with R.dataflow():
            conv1 = R.nn.relu(
                R.nn.conv2d(data, weight, padding=(1, 1), data_layout="NHWC", kernel_layout="OHWI")
                + bias,
            )
            R.output(conv1)

        return conv1

mod = Conv2dBiasReLU
mod = partition_for_cutlass(mod)
codegen_pass = relax.transform.RunCodegen({"cutlass": {"sm": 80, "find_first_valid": True}})
mod = codegen_pass(mod)

```

But error occurs:

```auto
    op_name, op_def, _ = self.conv2d_profiler.profile(
  File "/data/dev/tvm_dev/mlc-llm/3rdparty/tvm/python/tvm/contrib/cutlass/gen_conv2d.py", line 356, in profile
    op = self.select_op(
  File "/data/dev/tvm_dev/mlc-llm/3rdparty/tvm/python/tvm/contrib/cutlass/gen_conv2d.py", line 309, in select_op
    args = (
TypeError: %d format: a real number is required, not Var

```

So it seems to me that build stage I still cannot make the data shape as dynamic?

---

<div class="post-metadata">

**Author:** ![junrushao](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/junrushao/32/205_2.png) [@junrushao](https://discuss.tvm.apache.org/u/junrushao)\
**Post date:** [July 21, 2023, 6:35am UTC](https://discuss.tvm.apache.org/t/how-to-use-dynamic-shape-in-relax/15369/4 "2023-07-21T06:35:58Z")

</div>

You may learn dynamic shape support from my latest Unity branch (going to PR soon), which implements Llama from scratch in ~100 lines: [https://github.com/junrushao/tvm/blob/b89e975eb994800f052809f59cf0178814bd7700/main.py#L109-L213](https://github.com/junrushao/tvm/blob/b89e975eb994800f052809f59cf0178814bd7700/main.py#L109-L213)

---

<div class="post-metadata">

**Author:** ![anders](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/anders/32/6011_2.png) [@anders](https://discuss.tvm.apache.org/u/anders)\
**Post date:** [July 21, 2023, 6:54am UTC](https://discuss.tvm.apache.org/t/how-to-use-dynamic-shape-in-relax/15369/5 "2023-07-21T06:54:26Z")

</div>

there is an official dynamic-shape example with dynamic\_strided\_slice() op in: [tvm/tests/python/relax/test\_e2e\_op\_dynamic.py at unity · apache/tvm (github.com)](https://github.com/apache/tvm/blob/unity/tests/python/relax/test_e2e_op_dynamic.py)

---

<div class="post-metadata">

**Author:** ![WashSwang](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/w/edb3f5/32.png) [@WashSwang](https://discuss.tvm.apache.org/u/WashSwang)\
**Post date:** [July 21, 2023, 7:02am UTC](https://discuss.tvm.apache.org/t/how-to-use-dynamic-shape-in-relax/15369/6 "2023-07-21T07:02:24Z")

</div>

The dynamic shape for Conv2D in cutlass BYOC hasn’t been supported yet, by checking the commit history. The test also doesn’t cover dynamic shape for Conv2D.

But matmul is supported now:

> <https://github.com/apache/tvm/blob/7d98f601442c9dd1ad02355704a0ac3ce09d35db/tests/python/relax/test_codegen_cutlass.py#L307-L393>

---

<div class="post-metadata">

**Author:** ![leiwen83](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/leiwen83/32/3991_2.png) [@leiwen83](https://discuss.tvm.apache.org/u/leiwen83)\
**Post date:** [July 21, 2023, 8:01am UTC](https://discuss.tvm.apache.org/t/how-to-use-dynamic-shape-in-relax/15369/7 "2023-07-21T08:01:41Z")

</div>

I see. I checked the matmul test, and yes it support dynamic shape well.
