foss-xtensa/nnlib-hifi4

Incorrect input checks for depthwise conv

Closed this issue · 1 comments

The input checks incorrectly fail when the input size is smaller than the kernel size because the padding is ignored while comparing. Upon inspection the code appears to do the padding independently from the conv operation, so there is no harm to allow inputs smaller than kernel size as long as the padding is sufficient.

The issue is at this line and one below https://github.com/foss-xtensa/nnlib-hifi4/blob/master/xa_nnlib/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_asym8xasym8.c#L660

I think it can be fixed with the below change

diff --git a/xa_nnlib/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_asym8xasym8.c b/xa_nnlib/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_asym8xasym8.c
index 23c95ca..1b82d1b 100644
--- a/xa_nnlib/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_asym8xasym8.c
+++ b/xa_nnlib/algo/kernels/cnn/hifi4/xa_nn_conv2d_depthwise_asym8xasym8.c
@@ -657,8 +657,8 @@ WORD32 xa_nn_conv2d_depthwise_asym8xasym8
     XA_NNLIB_ARG_CHK_COND((input_height <= 0 || input_width <= 0), -1);
     XA_NNLIB_ARG_CHK_COND((input_channels <= 0), -1);
     XA_NNLIB_ARG_CHK_COND((kernel_height <= 0 || kernel_width <= 0), -1);
-    XA_NNLIB_ARG_CHK_COND((kernel_height > input_height), -1);
-    XA_NNLIB_ARG_CHK_COND((kernel_width > input_width), -1);
+    XA_NNLIB_ARG_CHK_COND((kernel_height > (input_height + 2 * y_padding)), -1);
+    XA_NNLIB_ARG_CHK_COND((kernel_width > (input_width + 2 * x_padding)), -1);
     XA_NNLIB_ARG_CHK_COND((channels_multiplier <= 0), -1);
     XA_NNLIB_ARG_CHK_COND((y_stride <= 0 || x_stride <= 0), -1);
     XA_NNLIB_ARG_CHK_COND((y_padding < 0 || x_padding < 0), -1);

Let me know if you would like me to create a pull request.

Side note: It seems to affect several other operators as well.

@vishsangale ,
Thanks for the input and we are aware of it.

we are working on it and would be taking care of the condition check.
and we will update the PR on github in couple of weeks.