Ceil_mode in avg_pool2d seems to output wrong result

It seems that torch.nn.functional.avg_pool2d() doesn’t take ceil_mode=True into account when “doing math”.

import torch
t = torch.ones(1, 1, 4, 4)
out = torch.nn.functional.avg_pool2d(input=t, kernel_size=3, stride=2, padding=1, ceil_mode=True)
print(out)

#out:
#tensor([[[[0.4444, 0.6667, 0.3333],
#          [0.6667, 1.0000, 0.5000],
#          [0.3333, 0.5000, 0.2500]]]])

The whole input, when padded with padding=1 AND ceil_mode=True should look like this:

0 0 0 0 0 0 0
0 1 1 1 1 0 0
0 1 1 1 1 0 0
0 1 1 1 1 0 0
0 1 1 1 1 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

in order to get the proper output dim.

Isolating the top-right corner of the input that the kernel sees for the top-right output value:

0 0 0
1 0 0
1 0 0

The output pixel here should be 2 / 9 = 0.2222, but instead we get 2 / 6 = 0.3333, as if the extra padding from ceil_mode=True is not accounted for when “doing math”.

Similarly, all the other values along the right and bottom padded areas have, to me, unexpected values.

Is this a bug, or is it by design?