ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

Inconsistent output between ACL's NEConvolutionLayer and Caffe's ConvolutionLayer.

Open yangyanzhan opened this issue 9 years ago • 11 comments

Hi, everyone. Recently I'm trying to replace Caffe's ConvolutionLayer with ACL's NEConvolutionLayer. I've half done with it and the result is inspiring. However, although most of the time NEConvolutionLayer has the same result as Caffe's, there're still some bad cases. Could anyone please help me out?

I've selected one test case that could reproduce the inconsistency in the forward process of a single layer. For simplicity, I've dumped the input data, weights and biases of the layer into text files. These files are generated by dumping the mutable_cpu_data of the corresponding blobs for the input data, weights and biases. I've written two handler source files to load these data for both Caffe and ACL. One of them is "caffe_main.cpp" which could be compiled and run directly. Another is "acl_snippet.cpp" which needs a little more refactoring to work properly.

And I've run them on my Android phone (Xiaomi MI 5). The result is as follows (output.txt for ACL; output_caffe.txt for Caffe):

2017-04-18 4 05 02

You can get all the data and source code in my file attachments. And the two output files are also included in it:

demo.zip

yangyanzhan avatar Apr 18 '17 13:04 yangyanzhan

Hello @yangyanzhan ,

I didn't run your code but I have two suggestions for you to try when looking your acl_snippet First, perform the allocation after the configure stage of the kernel like this:

    NEConvolutionLayer layer;
    layer.configure(&iTensor, &wTensor, &bTensor, &oTensor, info);

    iTensor.allocator()->allocate();
    wTensor.allocator()->allocate();
    bTensor.allocator()->allocate();
    oTensor.allocator()->allocate();

This is required as the configure stage might add padding to your tensors. If you allocate first and then configure the execution window of the kernel might be reduced something that is destructive for Neural Networks. @AnthonyARM provides a good explanation here #43

Second you don't specify the DimensionRounding in the PadStrideInfo of the convolution layer. We default to FLOOR in our library, but I think caffe uses CEIL when calculating the output tensor dimensions. Thus, change the PadStrideInfo to:

PadStrideInfo info(stride, stride, pad, pad, DimensionRoundingType::CEIL);

Moreover use this function scaled_dimensions which you can see here This will help you calculate the width and height of the output tensor of the convolution. So after these changes check that the shapes of the output tensor match between caffe and ACL.

Hope this helps.

GeorgeARM avatar Apr 18 '17 13:04 GeorgeARM

Thank you very much, George. I'll change my code and give it a try.

yangyanzhan avatar Apr 18 '17 14:04 yangyanzhan

Hi, I've changed my code and have checked that output tensor's shapes match between caffe and ACL. However it still yields a different result.

yangyanzhan avatar Apr 19 '17 06:04 yangyanzhan

Could you please provide your updated code as well? I will then take a closer look.

mpflanzer avatar Apr 19 '17 08:04 mpflanzer

Hi, it looks like this:


template <int channels, int height, int width>
int offset(int n, int c, int h, int w) {
    return ((n * channels + c) * height + h) * width + w;
}

int main() {
    constexpr int width = 7, height = 7, stride = 1, pad = 0, kernel_size = 1, channel_out = 256, channel_in = 832;
    
    vector<float> iData(width * height * channel_in);
    vector<float> wData(kernel_size * kernel_size * channel_in * channel_out);
    vector<float> bData(channel_out);
    vector<float> oData(width * height * channel_out);
    
    ifstream file;
    file.open("input.txt");
    for (int i = 0; i < width * height * channel_in; i++) {
        file >> iData[i];
    }
    file.close();
    file.open("weight.txt");
    for (int i = 0; i < kernel_size * kernel_size * channel_in * channel_out; i++) {
        file >> wData[i];
    }
    file.close();
    file.open("bias.txt");
    for (int i = 0; i < channel_out; i++) {
        file >> bData[i];
    }
    file.close();
    
    PadStrideInfo info(stride, stride, pad, pad, DimensionRoundingType::CEIL);
    
    TensorShape iShape(width, height, channel_in);
    TensorShape wShape(kernel_size, kernel_size, channel_in, channel_out);
    TensorShape bShape(channel_out);
    TensorShape oShape(width, height, channel_out);
    
    Tensor iTensor, wTensor, bTensor, oTensor;
    
    iTensor.allocator()->init(TensorInfo(iShape, Format::F32));
    wTensor.allocator()->init(TensorInfo(wShape, Format::F32));
    bTensor.allocator()->init(TensorInfo(bShape, Format::F32));
    oTensor.allocator()->init(TensorInfo(oShape, Format::F32));
    
    NEConvolutionLayer layer;
    layer.configure(&iTensor, &wTensor, &bTensor, &oTensor, info);

    iTensor.allocator()->allocate();
    wTensor.allocator()->allocate();
    bTensor.allocator()->allocate();
    oTensor.allocator()->allocate();
    
    Window iWindow;
    iWindow.use_tensor_dimensions(iTensor.info());
    Window wWindow;
    wWindow.use_tensor_dimensions(wTensor.info());
    Window bWindow;
    bWindow.use_tensor_dimensions(bTensor.info());
    Window oWindow;
    oWindow.use_tensor_dimensions(oTensor.info());
    Iterator iIt(&iTensor, iWindow);
    Iterator wIt(&wTensor, wWindow);
    Iterator bIt(&bTensor, bWindow);
    Iterator oIt(&oTensor, oWindow);
    
    execute_window_loop(iWindow, [&] (const Coordinates &id) {
        int x = id[0], y = id[1], d = id[2];
        *reinterpret_cast<float *>(iIt.ptr()) = iData[offset<channel_in, height, width>(0, d, y, x)];
    }, iIt);
    execute_window_loop(wWindow, [&](const Coordinates &id){
        int x = id[0], y = id[1], iD = id[2], oD = id[3];
        *reinterpret_cast<float *>(wIt.ptr()) = wData[offset<channel_in, kernel_size, kernel_size>(oD, iD, y, x)];
    }, wIt);
    execute_window_loop(bWindow, [&](const Coordinates &id){
        int d = id[0];
        *reinterpret_cast<float *>(bIt.ptr()) = bData[offset<0, 0, 0>(d, 0, 0, 0)];
    }, bIt);
    
    layer.run();
    
    execute_window_loop(oWindow, [&] (const Coordinates &id) {
        int x = id[0], y = id[1], d = id[2];
        oData[offset<channel_out, height, width>(0, d, y, x)] = *reinterpret_cast<float *>(oIt.ptr());
    }, oIt);
    
}

yangyanzhan avatar Apr 20 '17 11:04 yangyanzhan

Hi @yangyanzhan

and many thanks for providing your code. We have been able to reproduce the problem and it is caused by an error in the NEIm2ColKernel kernel (https://github.com/ARM-software/ComputeLibrary/blob/master/src/core/NEON/kernels/NEIm2ColKernel.cpp#L175).

The issue will be fixed in the next release.

Thanks, Gian Marco

gmiodice avatar Apr 21 '17 11:04 gmiodice

@gmiodice I think computing the output shape of convolution in Caffe shoud use DimensionRoundingType::FLOOR instead of DimensionRoundingType::CEIL. Could you please double check?

please check this small test case:

int main() { unsigned int in_h= 6, in_w = 4, stride = 2, kernel_size = 3;

unsigned int out_w, out_h;

/* correct output dim:
    Height: (in_h-kernel_size)/stride +1  = 2
    Width:  (in_w -kernel_size)/stride +1 = 1
*/

std::tie(out_w,out_h)=scaled_dimensions(in_w,in_h,kernel_size,stride,stride,0,0,
         DimensionRoundingType::CEIL);

cout<<"out weight "<<out_w<<endl;
cout<<"out_height "<<out_h<<endl;

return 0;

}

Also, I read the source code of scaled_dimensions() and get confused why the code converts the arguments to float and then back to int. As integer division is always FLOOR behavior, is this complicated conversion to add CEIL behavior?

Thanks,

Haitao

cyberfire avatar Apr 26 '17 02:04 cyberfire

Hi @cyberfire, many thanks for posting the test and for checking.

You are right about Caffe. In Caffe we always floor the dimensions of the tensors so should be DimensionRoundingType::FLOOR.

Regarding the consideration "the code converts the arguments to float and then back to int", basically we wrote the code in that manner just for symmetry between the floor and ceil path.

I think also @mpflanzer clarified this aspect in #66

Once again thanks. Gian Marco

gmiodice avatar Apr 26 '17 10:04 gmiodice

@AnthonyARM an error in the NEIm2ColKernel kernel (https://github.com/ARM-software/ComputeLibrary/blob/master/src/core/NEON/kernels/NEIm2ColKernel.cpp#L175). If this problem is not solved, the NEconvolutionLayer cannot be used?

jackdonghai avatar Apr 29 '17 07:04 jackdonghai

Hi @jackdonghai

The error at https://github.com/ARM-software/ComputeLibrary/blob/master/src/core/NEON/kernels/NEIm2ColKernel.cpp#L175 will be fixed with the next release. This should fix the issue #50 and the issues with odd sizes of the input tensor for the ConvolutionLayer function.

Thanks.

gmiodice avatar Apr 29 '17 08:04 gmiodice

Closing this as the issue is fixed in v17.05. Please reopen if persists.

GeorgeARM avatar May 04 '17 11:05 GeorgeARM