ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

Program Crashes depend on the size of the input and output tensors

Open jialiang19 opened this issue 9 years ago • 5 comments

Dear ARM supporting group:

I have stuck on a bug for two days now. Although I made a little progress, I am still bewildered by the bug. In the following code, I am trying to implement a convolution layer with the function clconvolutionlayer(). for the line

#define SIZE 120 

if I define SIZE as any integer smaller than 128, the program will work. The size here is the size of the input tensor and output tensor for the clconvolutionlayer() ( I have specify the strides and padding in such a way that the size of the input and the size of the output tensor would be the same).

However, if the SIZE is greater than 128, the program will only run if the SIZE is a power of two. For example, the program works when the SIZE is 256 or 512. However, if I make the input size as 217, the program will crash with following error message:

terminate called after throwing an instance of 'cl::Error'
  what():  clEnqueueNDRangeKernel
Aborted (core dumped)

with the help of the gdb backtrace, I have found out more detailed errors:

#0  __libc_do_syscall () at ../sysdeps/unix/sysv/linux/arm/libc-do-syscall.S:47
#1  0xb6b79648 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#2  0xb6b7a34a in __GI_abort () at abort.c:89
#3  0xb6cd9ad2 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/arm-linux-gnueabihf/libstdc++.so.6
#4  0xb6cd8330 in ?? () from /usr/lib/arm-linux-gnueabihf/libstdc++.so.6
#5  0xb6cd8386 in std::terminate() () from /usr/lib/arm-linux-gnueabihf/libstdc++.so.6
#6  0xb6cd85aa in __cxa_throw () from /usr/lib/arm-linux-gnueabihf/libstdc++.so.6
#7  0xb6f1fd06 in cl::detail::errHandler (err=-54, errStr=0xb6f9946c "clEnqueueNDRangeKernel") at include/CL/cl2.hpp:716
#8  0xb6f204ca in cl::CommandQueue::enqueueNDRangeKernel (this=0xb6fd5094 <arm_compute::CLScheduler::get()::scheduler+4>, kernel=..., offset=..., global=..., local=..., events=0x0, event=0x0)
    at include/CL/cl2.hpp:7835
#9  0xb6f20000 in arm_compute::enqueue (queue=..., kernel=..., window=..., lws_hint=...) at src/core/CL/ICLKernel.cpp:61
#10 0xb6f4d2e6 in arm_compute::CLIm2ColKernel::run_generic (this=0xbeffee40, window=..., queue=...) at src/core/CL/kernels/CLIm2ColKernel.cpp:173
#11 0xb6f4cfee in arm_compute::CLIm2ColKernel::run (this=0xbeffee40, window=..., queue=...) at src/core/CL/kernels/CLIm2ColKernel.cpp:131
#12 0xb6f6c9be in arm_compute::CLScheduler::enqueue (this=0xb6fd5090 <arm_compute::CLScheduler::get()::scheduler>, kernel=..., flush=true) at src/runtime/CL/CLScheduler.cpp:43
#13 0xb6f7997c in arm_compute::CLConvolutionLayer::run (this=0xbeffee3c) at src/runtime/CL/functions/CLConvolutionLayer.cpp:154
#14 0x0001aa1e in main ()


Specificly:

#7  0xb6f1fd06 in cl::detail::errHandler (err=-54, errStr=0xb6f9946c "clEnqueueNDRangeKernel") at include/CL/cl2.hpp:716

When I looked up the error -54 on OpenCL.headers, I found following:

#define CL_INVALID_WORK_GROUP_SIZE -54

Here is the hypothesis of the error:

Since I used

CLScheduler::get().default_init();

in my code, with the default initialization of CLScheduler, the WORK_GROUP_SIZE will be automatically assigned as the size of the output tensor of the convolutionlayer when the size of the output tensor is not a power of 2. (since when size is 256 or 512, the program works).

Do you have any idea?

the following is my code:

/*
  A simple test
*/  
#define ARM_COMPUTE_CL /* So that OpenCL exceptions get caught too */

#include "arm_compute/core/Types.h"
#include "arm_compute/runtime/CL/CLFunctions.h"
#include "arm_compute/runtime/CL/CLScheduler.h"
#include "jprog/example1/MNIST_READER.h" 
#include "arm_compute/core/CL/ICLTensor.h"
#include "arm_compute/runtime/CL/functions/CLSoftmaxLayer.h" 
#include "arm_compute/runtime/Tensor.h"
#include "arm_compute/runtime/NEON/functions/NESoftmaxLayer.h" 
#include "arm_compute/runtime/CL/CLTensor.h" 

#include <iostream> 
#include <fstream> 
#include <vector>

#define SIZE 120 

using namespace arm_compute;
using namespace std;

int main(int argc, const char **argv)
{

   /* set up the archetecture */
   CLImage    src, convw1, convb1, convr1;
   
   CLScheduler::get().default_init();
   constexpr  uint16_t SRC_X =  SIZE;
   constexpr  uint16_t SRC_Y =  SIZE;
   constexpr  uint16_t SRC_Z =  1;   


// Kernels:		
   //Pointer:----------------------------0-------1-----------
   constexpr  uint16_t CONVW_X[] =   {   3,      3};
   constexpr  uint16_t CONVW_Y[] =   {   3,      3};
   constexpr  uint16_t CONVW_IFM[] = {   1,      1};	
   constexpr  uint16_t CONVW_OFM[] = {   3,      4};
// Conv Output:
   constexpr  uint16_t CONVR_X[] =   {  SIZE,      3};
   constexpr  uint16_t CONVR_Y[] =   {  SIZE,      3};
   constexpr  uint16_t CONVR_Z[] =   {   3,      4};
// Stide Info:
   constexpr  uint16_t STRIDE_X[] =   {  1,      3};
   constexpr  uint16_t STRIDE_Y[] =   {  1,      3};
   constexpr  uint16_t PAD_X[]    =   {  1,      4};
   constexpr  uint16_t PAD_Y[]    =   {  1,      4}; 
   
   int pointer =0;

   /* 1st Convolution Layer */
   const TensorShape src_shape(SRC_X,SRC_Y,SRC_Z);
   const TensorShape convw1_shape(CONVW_X[pointer],CONVW_Y[pointer],SRC_Z,CONVW_OFM[pointer]);
   const TensorShape convb1_shape(CONVW_OFM[pointer]);
   const TensorShape convr1_shape(SRC_X,SRC_Y,CONVW_OFM[pointer]);

   PadStrideInfo ConvInfo1(STRIDE_X[pointer], STRIDE_Y[pointer], PAD_X[pointer], PAD_Y[pointer]);

   src.allocator()->init(TensorInfo(src_shape, Format::F32));
   convw1.allocator()->init(TensorInfo(convw1_shape, Format::F32));
   convb1.allocator()->init(TensorInfo(convb1_shape, Format::F32));
   convr1.allocator()->init(TensorInfo(convr1_shape, Format::F32));


   CLConvolutionLayer convlayer;
   convlayer.configure(&src, &convw1, &convb1, &convr1, ConvInfo1);

   src.allocator()->allocate();
   convw1.allocator()->allocate();
   convb1.allocator()->allocate();
   convr1.allocator()->allocate();


   ////////////////////fill the input cltensor ///////////////

   src.map(); 
   convw1.map();
   convb1.map(); 

   Window input_src_window;
   input_src_window.use_tensor_dimensions(src.info());

   Window input_convw1_window;
   input_convw1_window.use_tensor_dimensions(convw1.info());

   Window input_convb1_window;
   input_convb1_window.use_tensor_dimensions(convb1.info());

   Iterator input_src(&src, input_src_window);
   Iterator input_convw1(&convw1, input_convw1_window);
   Iterator input_convb1(&convb1, input_convb1_window);

   int counter = 0;

   execute_window_loop(input_src_window, [&](const Coordinates & id)
   		{
   		//std::cout << "Setting item [" << id.x() << "," << id.y() << "," << id.z() << "]\n";
   		*reinterpret_cast<float *>(input_src.ptr()) = counter%255;
   		counter++;
   		},
   		input_src);


   ///////////////////fill the weights and convb1 ///////////////////////////

   constexpr  uint16_t weights[]   = {  /* first kernal */ 
   	0,   1,   0,
   	0,   1,   0, 
   	0,   1,   0, 
   	/* second kernal */ 
   	1,   1,   1, 
   	0,   0,   0, 
   	0,   0,   0, 
   	/* third kernal*/ 
   	1,   0,   0, 
   	0,   1,   0, 
   	0,   0,   1    };

   counter = 0;
   execute_window_loop(input_convw1_window, [&](const Coordinates & id)
   		{
   		//std::cout << "Setting item [" << id.x() << "," << id.y() << "," << id.z() << "]\n";
   		*reinterpret_cast<float *>(input_convw1.ptr()) = weights[counter];
   		counter++; 	
   		},
   		input_convw1);

   constexpr  uint16_t bias_array[]   = { 0.05, 0.05, 0.05 }; 
   counter = 0;
   execute_window_loop(input_convb1_window, [&](const Coordinates & id)
   		{
   		//std::cout << "Setting item [" << id.x() << "," << id.y() << "," << id.z() << "]\n";
   		*reinterpret_cast<float *>(input_convb1.ptr()) = bias_array[counter];
   		counter++;	
   		},
   		input_convb1);


   CLScheduler::get().sync();

   src.unmap();
   convw1.unmap();
   convb1.unmap();

   convlayer.run();
   CLScheduler::get().sync();


   // just printout output layer.  
   auto *result_data = new float[CONVR_X[pointer] * CONVR_Y[pointer] * CONVR_Z[pointer]];

   convr1.map();
   Window output_window;
   output_window.use_tensor_dimensions(convr1.info(),Window::DimY); 
   Iterator output_it(&convr1, output_window);
   execute_window_loop(output_window, [&](const Coordinates & id)
   		{
   		// Copy one whole row:
   		memcpy(result_data + id.z() * CONVR_Y[pointer] * CONVR_X[pointer] + id.y() * CONVR_X[pointer], output_it.ptr(), CONVR_X[pointer] * sizeof(float));
   		},
   		output_it);
   CLScheduler::get().sync();

   convr1.unmap();

   //copy to cpu mem done, and display
   std::cout << "========result_data of Convolution: =======" << "\n";
   for(unsigned int b = 0; b < CONVR_Z[pointer]; b++)
   {
   	std::cout << "Batch " << b << "\n";
   	for(unsigned int h = 0; h < CONVR_Y[pointer]; h++)
   	{
   		for(unsigned int w = 0; w < CONVR_X[pointer]; w++)
   		{
   			std::cout << result_data[b * CONVR_Y[pointer] * CONVR_X[pointer] + h * CONVR_X[pointer] + w] << ",\t";
   		}
   		std::cout << "\n";
   	}
   	std::cout << "\n";
   }
   std::cout << "\n";

} 


jialiang19 avatar Jun 19 '17 17:06 jialiang19

Dear ARM supporting group:

I have been trying to solve this problem by digging deeper into code. Since the error message is CL_INVALID_WORK_GROUP_SIZE, I open the source code of src/core/CL/ICLKernel.cpp,and try to print out the value of global_work_size and local_work_size.

arm_computing_issue_141

After a little analysis, it is clear that on the very last line, the global_work_size on dimension one 757 is not divisible by 8, the local_work_size on dimension one. With OpenCL version below 2.0, it is required that global_work_size has to be divisible by local_work_size in every dimension. Thus the error CL_INVALID_WORK_GROUP_SIZE is thrown.

After staring at the source code of source code of src/core/CL/ICLKernel.cpp, I found that the problem is caused by the following code, screenshot from 2017-06-21 16-45-40

where ''' lws ''' is local_work_size, and it is defined as (8, 8) in a function in an another file called it.

After commenting this code out,

 if((lws_hint[0] <= gws[0]) && (lws_hint[1] <= gws[1]) && (lws_hint[2] <= gws[2]))
    {
        lws = lws_hint;
    }

my program starts to work, but my local_work_size would always be set to NULL, which means that the local_work_size will always be determined by the OpenCL implementations (without optimisation?).

I temporarily get around the problem, but I believe, there is a potential bug in the source code that determines the global_work_size and local_work_size. The potential bug is causing that global_work_size is not divisible by local_work_size.

I believe it may worth the effort to re-exam src/core/CL/ICLKernel.cpp

Thanks.

jialiang19 avatar Jun 21 '17 16:06 jialiang19

Hello @jialiang19,

This is rather strange. We use cl_arm_non_uniform_work_group_size when compiling the kernels thus the lws is allowed not to be an integer factor of the gws. Are you trying to run this on an ARM GPU? If yes, what is your driver version?

GeorgeARM avatar Jun 21 '17 16:06 GeorgeARM

I am using ARM MALI T628 GPU, and the driver I am using is samsung-chromebook2/malit62xr4p002rel0linux1fbdevtar.gz GNU/Linux armhf fbdev. Apparently, the machine I am using that is too old to update to new drivers. Thanks.

jialiang19 avatar Jun 22 '17 12:06 jialiang19

@jialiang, You appear to be using the 4.0 version. I use the 6.0 version of OpenCL on the same platform (the last one ARM made available) but I am not sure it supports this extension either.

psyhtest avatar Jun 22 '17 15:06 psyhtest

I have updated my GPU driver with a new Mali GPU. Now the issue is fixed by the update! The previous GPU drive does not recognize the flag cl_arm_non_uniform_work_group_size. Thanks for supporting!

jialiang19 avatar Jun 29 '17 16:06 jialiang19