You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/10/07 12:35:30 UTC

[GitHub] [tvm] guberti opened a new issue, #13009: [Bug] "SAME" padding function in topi.nn.utils ignores stride

guberti opened a new issue, #13009:
URL: https://github.com/apache/tvm/issues/13009

   # Issue explanation
   Currently, some of the functions in `tvm.topi.nn` support having their `padding` passed in as the strings `"SAME"` or `"VALID"` (examples: [1](https://github.com/apache/tvm/blob/main/python/tvm/topi/nn/conv3d.py), [2](https://github.com/apache/tvm/blob/main/python/tvm/topi/nn/conv1d.py), [3](https://github.com/apache/tvm/blob/main/python/tvm/topi/nn/conv2d_transpose.py), [4](https://github.com/apache/tvm/blob/main/python/tvm/topi/nn/conv3d_transpose.py), [5](https://github.com/apache/tvm/blob/main/python/tvm/topi/nn/depthwise_conv2d.py)). Underneath, these operators use the function `tvm.topi.nn.utils.get_pad_tuple` (or a 1d/3d variant of it) to compute their paddings. When `"SAME"` is passed, `get_pad_tuple` [works like this](https://github.com/apache/tvm/blob/main/python/tvm/topi/nn/utils.py#L118):
   ```python
   def get_pad_tuple(padding, kernel):
       if padding == "SAME":
           pad_h = kernel[0] - 1
           pad_w = kernel[1] - 1
       else:
           pass # Other cases have been omitted for brevity
       pad_top = (pad_h + 1) // 2
       pad_left = (pad_w + 1) // 2
       return pad_top, pad_left, pad_h - pad_top, pad_w - pad_left
   ```
   
   However, when we import models from formats that use the "SAME" keyword (e.g. TFLite), this logic is not used. Instead, conversion is done inside the appropriate frontend (e.g. [this one for TFLite](https://github.com/apache/tvm/blob/main/python/tvm/relay/frontend/tflite.py)):
   ```python
   def get_pad_value(data, kernel, stride):
       """Get the pad tuple of value for SAME padding"""
       out = int(math.ceil(float(data) / float(stride)))
       pad = max(0, (out - 1) * stride + kernel - data)
       pad_before = pad // 2
       pad_after = pad - pad_before
       return pad_before, pad_after
   ```
   
   These functions behave differently, as the `frontend` functions take stride into account, while the `topi.nn` ones do not. Taking stride into account is the behavior we want, as it can reduce the amount of padding that is done.
   
   For example, consider a one-channel depthwise convolution on a `48x48` tensor with a `3x3` kernel, stride of `(2, 2)`, and `"SAME"` padding. The `topi.nn` function would add one row of padding on all sides, resulting in a `50x50` padded tensor. However, the `frontend` functions would see that only half that amount of padding was needed, and would pad only the ends of the tensor - resulting in a `49x49` padded tensor instead.
   
   ## Solution 1
   
   The easiest solution here is to modify [python/tvm/topi/nn/utils.py](https://github.com/apache/tvm/blob/main/python/tvm/topi/nn/utils.py) to take stride into account. 
   
   However, I don't think this is a good solution, as it leaves ambiguous the question of whether uneven padding should go before or after the tensor. Different formats have different conventions for this - TFLite and Tensorflow always prefer it after, while Onnx makes you specify `SAME_UPPER` or `SAME_LOWER`.
   
   ## Solution 2
   
   A better solution IMO is to just _remove_ the ability to support `SAME` and `VALID` padding inside TOPI. This functionality is already rarely used - our frontends already convert `SAME` and `VALID` to explicit padding tuples. This will also make it easier to implement fancier padding strategies at the Relay level.


-- 
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.apache.org

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


Re: [I] [Bug] "SAME" padding function in topi.nn.utils ignores stride [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen closed issue #13009: [Bug] "SAME" padding function in topi.nn.utils ignores stride
URL: https://github.com/apache/tvm/issues/13009


-- 
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