I have a piece of code where average_pool operations were calling 
tvm.relay.avg_pool2D functions with ceil_mode = False. If ceil_mode is True, 
the parameter is converted to False. To make it equal as if it was ceil_mode = 
True, there are calculations to get exact shape and add after padding, and then 
call it with ceil_mode = False

This was working fine until changes came up in this PR
https://github.com/apache/tvm/pull/9835/commits/846afdbf8320b79cd2af191c203355ac1f6df6ff#diff-4214ff85b5a2d6fa0a47bd0732fa314316eb579245c81f326f4034793e5956f7R542.
 With offset calculation inside if (ceil_mode) that offset will always be 0 for 
me. I suspect that is what it's making my code fail with stride == 2, but I am 
not certain. 

I have tried a lot of things, changing the calculation, hardcoding +1 and - 1 
to get the perfect padding. Nothing helped me. Here is the calculation, done to 
get the after pad, it is based on math found in pytorch documentation.

![scr2|499x500](upload://66OaI7pxEuzRQJ4KMwO6KaLt8wK.png) 

    def _calculate_dilated_1d_pooling_window_size(pool_size: int, dilation: 
int) -> int:
    return (pool_size - 1) * dilation + 1

    def _calculate_ceil_output_shape_along_one_dimension(shape: int,
                                                         pool_size: int,
                                                         dilation: int,
                                                         padding_before: int,
                                                         padding_after: int,
                                                         stride: int) -> int:
        """
        When ceil_mode=True, we use the ceil when computing the output shape. 
Another way of looking at it is
        if we determine the pooling window does not stride neatly within the 
bounds of the input, we add extra padding
        to make it in bounds effectively rounding up the shape of the output.
        """
        dilated_pooling_window_size = 
_calculate_dilated_1d_pooling_window_size(pool_size, dilation)
        pooling_regions_minus_one = shape + padding_before + padding_after - 
dilated_pooling_window_size
        pooling_regions_minus_one_given_stride = pooling_regions_minus_one / 
stride
        pooling_regions_given_stride = pooling_regions_minus_one_given_stride + 
1
        pooling_regions_given_stride_rounded_up = 
int(math.ceil(pooling_regions_given_stride))
        return pooling_regions_given_stride_rounded_up

    def _calculate_extra_ceil_mode_padding_along_one_dimension(shape: int,
                                                               pool_size: int,
                                                               dilation: int,
                                                               padding_before: 
int,
                                                               padding_after: 
int,
                                                               stride: int) -> 
int:
        """
        This function calculates the extra padding applied by ceil_mode == True 
during runtime along a single dimension.
        When ceil_mode=True, we use the ceil when computing the output shape. 
We then add extra padding at the end of the
        dimension to accommodate the output shape.
        """
        output_shape = _calculate_ceil_output_shape_along_one_dimension(shape, 
pool_size, dilation,
                                                                        
padding_before, padding_after, stride)
        dilated_pooling_window_size = 
_calculate_dilated_1d_pooling_window_size(pool_size, dilation)
        ceil_mode_padded_input_shape = dilated_pooling_window_size + 
(output_shape - 1) * stride
        existing_input_shape = shape + padding_before + padding_after
        return ceil_mode_padded_input_shape - existing_input_shape





---
[Visit 
Topic](https://discuss.tvm.apache.org/t/relay-op-nn-pooling-and-ceil-mode/12247/1)
 to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click 
here](https://discuss.tvm.apache.org/email/unsubscribe/255b6a5953c515599e3e0fe251dee9afc47b39155ac4aef3619ccd418fc808aa).

Reply via email to