foss-xtensa/nnlib-hifi4

Unexpected convolution output with 3x3 image and valid padding

Closed this issue · 2 comments

I am trying to call xa_nn_conv2d_std_f32 with a simple 3x3 kernel whose values are all 1s on an input which is also 3x3 and whose values are all 1s with VALID padding and no bias. I would expect that the output is 1x1 with a value of 9. However, I am seeing that the output is 5.

Here is my code:

#define N_INPUTS (9)
const float weights[N_INPUTS] = {1.0f};
const float bias[1] = {0.0f};

const float inputs[N_INPUTS] = {1.0f};
float outputs[1];
float scratch[99];

int err = xa_nn_conv2d_std_f32(
    outputs,
    inputs,
    weights,
    bias,
    3,  // input_height
    3,  // input_width
    1,  // input_channels
    3,  // kernel_height
    3,  // kernel_width
    1,  // out_channels
    1,  // x_stride
    1,  // y_stride
    0,  // x_padding
    0,  // y_padding
    1,  // out_height
    1,  // out_width
    0,  // out_data_format
    scratch);

PRINTF("Error code: %d\n", err);
PRINTF("Output: %f\n", outputs[0]);

The output of which is:

Error code: 0
Output: 5.000000

Am I calling this function incorrectly?

Hi @joe-antognini,
For the floating-point variant, the kernel is expected to be padded in the depth/channels dimension,
if the number of input_channels is not a multiple of 2.

Please refer to the ProgrammersGuide.

More details about the implementation: (3x3 input and 3x3 kernel) (P=0 and G=0/garbage)
Padding to input will be taken care by cadence NN library, so after padding input data looks like below
inputs => 1 P 1 P 1 P 1 P 1 P 1 P 1 P 1 P 1 P
weights without padding looks like below
weights=> 1 1 1 1 1 1 1 1 1 G G G G G G G G G

So your output is 5, which is not expected.

To get the expected output, which is 9.0,
Could you please pass the padded weights data to xa_nn_conv2d_std_f32 function.

float weights_pad[N_INPUTS * 2];
for(int i=0; i<N_INPUTS; i++)
{
weights_pad[(2 * i) + 0] = weights[i];
weights_pad[(2 * i) + 1] = 0;
}

Thanks

Thank you for the explanation and quick reply! I misunderstood how that worked.