# \[Relay\] clip operator with dynamic min/max fails

**URL:** <https://discuss.tvm.apache.org/t/relay-clip-operator-with-dynamic-min-max-fails/9466>\
**Category:** Questions\
**Created:** [March 19, 2021, 8:34pm UTC](https://discuss.tvm.apache.org/t/relay-clip-operator-with-dynamic-min-max-fails/9466 "2021-03-19T20:34:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![apivovarov](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/apivovarov/32/206_2.png) [@apivovarov](https://discuss.tvm.apache.org/u/apivovarov)\
**Post date:** [March 19, 2021, 8:34pm UTC](https://discuss.tvm.apache.org/t/relay-clip-operator-with-dynamic-min-max-fails/9466/1 "2021-03-19T20:34:25Z")

</div>

I tried to compile Pytorch model which uses `clamp(min, max)` operator (`Relay.clip`) with `max` parameter equal to input image width. `relay.frontend.from_pytorch` fails to convert such model.

To reproduce the issue:

```auto
import torch
import tvm
from tvm import relay

class Net(torch.nn.Module):
    def __init__ (self):
        super(Net, self). __init__ ()
    def forward(self, a):
        amax = a.shape[1] / 10.0
        b = a.clamp(min=0, max=amax)
        return b

a = torch.tensor([[9,8,7,1],[7,2,5,4]]) / 10.0
net = Net()
net(a)

traced_net = torch.jit.trace(net, (a))

shape_list = [("input0", [2,4])]
mod, params = relay.frontend.from_pytorch(traced_net, shape_list)

```

Error:

```auto
>>> mod, params = relay.frontend.from_pytorch(traced_net, shape_list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/pivovaa/workspace/tvm/python/tvm/relay/frontend/pytorch.py", line 3253, in from_pytorch
    ret = converter.convert_operators(_get_operator_nodes(graph.nodes()), outputs, ret_name)[0]
  File "/Users/pivovaa/workspace/tvm/python/tvm/relay/frontend/pytorch.py", line 2674, in convert_operators
    relay_out = relay_op(
  File "/Users/pivovaa/workspace/tvm/python/tvm/relay/frontend/pytorch.py", line 1693, in clamp
    return _op.clip(data, amin, amax)
  File "/Users/pivovaa/workspace/tvm/python/tvm/relay/op/tensor.py", line 1059, in clip
    return _make.clip(a, a_min, a_max)
  File "/Users/pivovaa/workspace/tvm/python/tvm/_ffi/_ctypes/packed_func.py", line 237, in __call__
    raise get_last_ffi_error()
tvm._ffi.base.TVMError: Traceback (most recent call last):
  File "/Users/pivovaa/workspace/tvm/include/tvm/runtime/packed_func.h", line 721
TVMError: In function relay.op._make.clip: error while converting argument 2: [13:26:04] /Users/pivovaa/workspace/tvm/include/tvm/runtime/packed_func.h:501: ---------------------------------------------------------------
An internal invariant was violated during the execution of TVM.
Please read TVM's error reporting guidelines.
More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.
---------------------------------------------------------------

  Check failed: type_code_ == kDLFloat: expected float but got Object

```

What should be done to support dynamic `max` parameter in `Relay.clip` operator?

Current [clamp()](https://github.com/apache/tvm/blob/main/python/tvm/relay/frontend/pytorch.py#L1689) impl in `relay/frontend/pytorch.py`

---

<div class="post-metadata">

**Author:** ![apivovarov](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/apivovarov/32/206_2.png) [@apivovarov](https://discuss.tvm.apache.org/u/apivovarov)\
**Post date:** [March 20, 2021, 12:13am UTC](https://discuss.tvm.apache.org/t/relay-clip-operator-with-dynamic-min-max-fails/9466/2 "2021-03-20T00:13:58Z")

</div>

The fix for the issue [[torch] Use try\_infer\_value for clamp min/max by apivovarov · Pull Request #7712 · apache/tvm · GitHub](https://github.com/apache/tvm/pull/7712)
