Negative values in padding_r for the shapes_resnet_50_v1_5 2D convolutions
Good morning.
When benchmarking ResNet50 v1.5 shapes with
./tests/benchdnn/benchdnn --conv --mode=p --dir=FWD_I \
--perf-template=%desc%,%-time%,%0time%,%+time% \
--batch=./tests/benchdnn/inputs/conv/shapes_resnet_50_v1_5
some of the convolutions (e.g., ic256ih56oc512oh28kh1sh2ph0n"resnet_50_v1_5:res3a_branch1", ic512ih28oc1024oh14kh1sh2ph0n"resnet_50_v1_5:res4a_branch1") have {-1,-1} in cd.padding[1] inside conv_desc_init(), which sets the configuration as padding_{left,top,right,bottom} = {0, 0, -1, -1}. May you please comment, is it an unintended skip of some check before the conv_desc_init() call, or this is meant to be in such a way, and negative padding setup has some physical meaning? From ResNet50 NN architecture it seems, that the padding should be symmetric and equal to 0 or 1 on most of the layers, except the very first one. There are also cases with padding_l = {1, 1} && padding_r = {0, 0}, for example, ic128ih56oc128oh28kh3sh2ph1n"resnet_50_v1_5:res3a_branch2b".
Best wishes, Aleksandr Nikolaev
Hi Aleksandr Nikolaev,
For the shape ic256ih56oc512oh28kh1sh2ph0n if we just focus on height dimension then we have
ih = 56, oh = 28, kh = 1, sh = 2, ph (top padding) = 0
Now we have the following formula relating input and output heights AND the relation that can be used to calculate bottom padding
oh = (ih - kh + top_padding + bottom_padding) / sh + 1
(ih - kh + top_padding + bottom_padding) % sh == 0
So from above it seems that bottom padding indeed must equal -1. (Same argument goes for the width dimension)
As for reality check we can look at the "appropriately scaled" version of the above problem where ih = 4 and oh = 2 (while rest of the parameters are same). One can manually check that for this artificial problem last input value is not touched by the convolution. Say we have Input: i_1 i_2 i_3 i_4 and kernel: k_1 with stride = 2 and left padding = 0
Then convolution will roughly operate as follow:
Begin First step of convolution: multiply i_1 and k_1 and feed that to output o_1 Second step of convolution: multiply i_3 and k_1 and feed that to output o_2 Stop
... and so here too bottom padding must be equal to -1.
@alokbakshi I think these formulas should work with bottom_padding == 0 too if we use the floor function as in documentation:

See formulas here: https://oneapi-src.github.io/oneDNN/dev_guide_convolution.html
To me, negative padding looks confusing (what is the meaning of that?). Most likely we could do something like max(0, ...) in those places where now we get negative values. But need to check if it breaks something else.
The potential meaning of the negative padding could be processing the shrunk input image (without a need to play with logical dimensions and strides while keeping the original input intact).
However, oneDNN doesn't support such behavior, except for the cases when this negative padding doesn't play a role, like in the case above. This is basically what @echeresh mentioned.
Note, that we have a funny check pad_r + stride > 0 instead of pad_r >= 0.
From the implementation perspective it is often convenient to have a "normalized" (in some sense) right padding. For instance, the smallest possible padding that satisfies the shape conditions. Example:
- iw = 3, kw = 2, stride = 2, pad_l = 0, pad_r = 1
- iw = 3, kw = 2, stride = 2, pad_l = 0, pad_r = 2
For both 1st and 2nd cases the output shape is the same, and it might be desired for the kernel to always work with pad_r
equals 1. Our jitted implementations sometimes do that by recalculating the "effective" right padding.
A lot of words, sorry. What I am trying to say is that the suggestion to replace the negative padding in the case above in the code with 0 should probably be done on the implementation side. At least we definitely shouldn't change the value that user gave us. There might be a question if we want to disallow passing us the negative padding (in which case we would just need to fix benchdnn calculation formula for right padding), but I don't think we want to become stricter without a good justification, especially given that there is no much problem to dial with that on the implementation side.
@alokbakshi , the formulae expressing output sizes dependence on the input sizes, strides, dilations and padding involve int division, thus for some configurations there may exist more than one padding setup, which satisfies the sizes relations. Or do you mean, that in this particular case the very last input value are not touched anyway due to dh = 2, thus both pad_r = -1 and 0 will have the same effect on the result?
@emfomenk , yes, I have noticed this check, many thanks for explaining. My main question was, that you do not intentionally crop an image with the negative padding, because in ML image preprocessing usually happens before a convolution call, at least such was my experience.
Thanks everybody for the comments, I now understand, that in the examples above pad_r simply has no effect due to the specific setup of (ih,iw), pad_l and (dh,dw). I will add the relevant checks in the Compute Library pull request code (#820), because ACL treats padding as unsigned int.
Or do you mean, that in this particular case the very last input value are not touched anyway due to dh = 2, thus both pad_r = -1 and 0 will have the same effect on the result?
Yeah, that's exactly it. From oneDNN perspective there is no difference between passing pad_r = 0 or pad_r = -1 for this particular case. Same happens for the 1st and 2nd bullet in my previous comment: https://github.com/oneapi-src/oneDNN/issues/824#issuecomment-680249559. oneDNN behavior / computations will be absolutely the same.
My main question was, that you do not intentionally crop an image with the negative padding, because in ML image preprocessing usually happens before a convolution call, at least such was my experience.
You are right, we don't do the image cropping. Probably, later we could support cropping by using negative padding, but this not in the plan for the foreseeable future.