ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

ACL on NEON is taking much more time than NCNN

Open krgor opened this issue 6 years ago • 3 comments

I am using ACL 20.02 on Android 8.1.

I have ported UltraFace detection on it and run() takes 278-280 ms for NEON. I am using below options for graph example:

LD_LIBRARY_PATH=/data/libs ./ultra_test --target=NEON --layout=NHWC --type=F32 --threads=6

Here is a network details.

It is running on top of NCNN and it is compiled with -DANDROID_ARM_NEON=ON option.

NCNN takes only 40-42ms.

This is a bit of concern to me as I was expecting ACL to be the best in terms of performance.

Here is our graph layers. We have used our custom input/output layers, which just feeds/reads the data to/from tensors. Please let me know if I am missing something or you need more details.

> #include "acl_ultra.h"
> 
> void ultra_output1(ITensor &tensor, void *obj)
> {
> 	ultra_graph_data_t *pGraphData = (ultra_graph_data_t *) obj;
> 
> 	std::cout << "ultra_output1 entry" << std::endl;
>     std::cout << "Shape " << tensor.info()->tensor_shape() << std::endl;
> 	struct timespec start, stop;
> 	clock_gettime(CLOCK_MONOTONIC, &start);
> #if DEBUG
>     static int cnt = 0;
>     if (cnt == 0) {
>         Window window;
>         window.use_tensor_dimensions(tensor.info()->tensor_shape());
> 
>         execute_window_loop(window, [&](const Coordinates & id)
>         {
>             if ((id.x() == 1 && id.y() == 0 && id.z() == 0) ||
>                 (id.x() == 0 && id.y() == 1 && id.z() == 0) ||
>                 (id.x() == 0 && id.y() == 0 && id.z() == 1) )
>                std::cout   << "Tensor Output [ " << id.x() << " " << id.y() << " " << id.z() << " ] = " 
>                                 << *reinterpret_cast<float *>(tensor.ptr_to_element(id)) << std::endl;
>         });
>         cnt++;
>     }
> #endif
> 
>     Window window;
>     window.use_tensor_dimensions(tensor.info()->tensor_shape());
> 
>     float *dest = pGraphData->outData1;
>     execute_window_loop(window, [&](const Coordinates & id)
>     {
> 		*dest = *reinterpret_cast<float *>(tensor.ptr_to_element(id));
>         dest++;
>     });
> 
> 	clock_gettime(CLOCK_MONOTONIC, &stop);
> 	std::cout << "out_callback time: " << (stop.tv_sec - start.tv_sec) * 1e3 + (stop.tv_nsec - start.tv_nsec) / 1e6 << " ms\n" << std::endl;
> }
> 
> callback_function ultraOut1Func = &ultra_output1;
> 
> void ultra_output2(ITensor &tensor, void *obj)
> {
> 	ultra_graph_data_t *pGraphData = (ultra_graph_data_t *) obj;
> 
> 	std::cout << "ultra_output2 entry" << std::endl;
>     std::cout << "Shape " << tensor.info()->tensor_shape() << std::endl;
> 	struct timespec start, stop;
> 	clock_gettime(CLOCK_MONOTONIC, &start);
> 
> #if DEBUG
>     std::cout << "Shape " << tensor.info()->tensor_shape() << std::endl;
>     std::cout << "\t\t C H W N" << std::endl;
> 
>     static int cnt = 0;
>     int count = 0;
>     if (cnt == 0) {
>         Window window;
>         window.use_tensor_dimensions(tensor.info()->tensor_shape());
> 
>         execute_window_loop(window, [&](const Coordinates & id)
>         {
>             if (((id.x() == 1 && id.y() == 0 && id.z() == 0) ||
>                 (id.x() == 0 && id.y() == 1 && id.z() == 0) ||
>                 (id.x() == 0 && id.y() == 0 && id.z() == 1) ) && count < 3)
>                {
>                     count++;
>                std::cout   << "Tensor Output [ " << id.x() << " " << id.y() << " " << id.z() << " ] = " 
>                                 << *reinterpret_cast<float *>(tensor.ptr_to_element(id)) << std::endl;
>                 }
>         });
>         cnt++;
>     }
> #endif
> 
>     Window window;
>     window.use_tensor_dimensions(tensor.info()->tensor_shape());
> 
>     float *dest = pGraphData->outData2;
>     execute_window_loop(window, [&](const Coordinates & id)
>     {
> 		*dest = *reinterpret_cast<float *>(tensor.ptr_to_element(id));
>         dest++;
>     });
> 
> 	clock_gettime(CLOCK_MONOTONIC, &stop);
> 	std::cout << "out_callback time: " << (stop.tv_sec - start.tv_sec) * 1e3 + (stop.tv_nsec - start.tv_nsec) / 1e6 << " ms\n" << std::endl;
> }
> 
> callback_function ultraOut2Func = &ultra_output2;
> 
> void ultra_output3(ITensor &tensor, void *obj)
> {
> 	ultra_graph_data_t *pGraphData = (ultra_graph_data_t *) obj;
> 
> 	std::cout << "ultra_output3 entry" << std::endl;
>     std::cout << "Shape " << tensor.info()->tensor_shape() << std::endl;
> 	struct timespec start, stop;
> 	clock_gettime(CLOCK_MONOTONIC, &start);
> 
> #if DEBUG
>     std::cout << "Shape " << tensor.info()->tensor_shape() << std::endl;
>     std::cout << "\t\t C H W N" << std::endl;
> 
>     static int cnt = 0;
>     int count = 0;
>     if (cnt == 0) {
>         Window window;
>         window.use_tensor_dimensions(tensor.info()->tensor_shape());
> 
>         execute_window_loop(window, [&](const Coordinates & id)
>         {
>             if (((id.x() == 1 && id.y() == 0 && id.z() == 0) ||
>                 (id.x() == 0 && id.y() == 1 && id.z() == 0) ||
>                 (id.x() == 0 && id.y() == 0 && id.z() == 1) ) && count < 3)
>                {
>                     count++;
>                std::cout   << "Tensor Output [ " << id.x() << " " << id.y() << " " << id.z() << " ] = " 
>                                 << *reinterpret_cast<float *>(tensor.ptr_to_element(id)) << std::endl;
>                 }
>         });
>         cnt++;
>     }
> #endif
> 
>     Window window;
>     window.use_tensor_dimensions(tensor.info()->tensor_shape());
> 
>     float *dest = pGraphData->outData3;
>     execute_window_loop(window, [&](const Coordinates & id)
>     {
> 		*dest = *reinterpret_cast<float *>(tensor.ptr_to_element(id));
>         dest++;
>     });
> 
> 	clock_gettime(CLOCK_MONOTONIC, &stop);
> 	std::cout << "out_callback time: " << (stop.tv_sec - start.tv_sec) * 1e3 + (stop.tv_nsec - start.tv_nsec) / 1e6 << " ms\n" << std::endl;
> }
> 
> callback_function ultraOut3Func = &ultra_output3;
> 
> void ultra_input(ITensor &tensor, void *obj)
> {
> 	ultra_graph_data_t *pGraphData = (ultra_graph_data_t *) obj;
> 	float *inData = pGraphData->inData;
> 
> 	struct timespec start, stop;
> 	clock_gettime(CLOCK_MONOTONIC, &start);
> 
>     const DataLayout  data_layout  = tensor.info()->data_layout();
> 	bool bgr = false;
> 
>     // Map buffer if creating a CLTensor
>     map(tensor, true);
> 
>     // Stride across channels
>     size_t stride_z = 0;
> 
>     // Iterate through every pixel of the image
>     Window window;
> 
>     window.use_tensor_dimensions(tensor.info()->tensor_shape());
>     Iterator out(&tensor, window);
> 
>     int count = 0;
>     execute_window_loop(window, [&](const Coordinates & id)
>     {
>         *reinterpret_cast<float *>(out.ptr() + 0 * stride_z) = static_cast<float>(((*inData++)-127.5f)*0.0078125f);
>     },
>     out);
>     std::cout << "Count = " << count << std::endl;
> 
>     // Unmap buffer if creating a CLTensor
>     unmap(tensor);
> 	clock_gettime(CLOCK_MONOTONIC, &stop);
> 	std::cout << "in_callback time: " << (stop.tv_sec - start.tv_sec) * 1e3 + (stop.tv_nsec - start.tv_nsec) / 1e6 << " ms\n" << std::endl;
> }
> 
> callback_function ultraInFunc = &ultra_input;
> 
> bool UltraNetModel::do_setup(int argc, char **argv)
> {
>     int mDim = 231;
>     // Parse arguments
>     cmd_parser.parse(argc, argv);
> 
>     // Consume common parameters
>     common_params = consume_common_graph_parameters(common_opts);
> 
>     // Return when help menu is requested
>     if(common_params.help)
>     {
>         cmd_parser.print_help(argv[0]);
>         return false;
>     }
> 
>     //common_params.target = Target::NEON;
>     //common_params.data_layout = DataLayout::NHWC;
>     //common_params.data_type = DataType::F32;
>     //common_params.data_path = "/data/cnn/data_onet";
>     common_params.threads = 6;
> 
>     // Checks
>     ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
>     ARM_COMPUTE_EXIT_ON_MSG(common_params.data_type == DataType::F16 && common_params.target == Target::NEON, "F16 NEON not supported for this graph");
> 
>     // Print parameter values
>     std::cout << common_params << std::endl;
> 
>     // Create model path
>     std::string model_path = "/";
> 
>     // Create input descriptor
>     const TensorShape tensor_shape     = permute_shape(TensorShape(320U, 240U, 3U, 1U), DataLayout::NCHW, common_params.data_layout);
>     TensorDescriptor  input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
>     
>     // Get trainable parameters data path
>     std::string data_path = common_params.data_path;
> 
>     // Add model path to data path
>     if(!data_path.empty())
>     {
>         data_path += model_path;
>     }
> 
>     // Create graph
>     graph   << common_params.target
>             << DepthwiseConvolutionMethod::Optimized3x3 // FIXME(COMPMID-1073): Add heuristics to automatically call the optimized 3x3 method
>             << common_params.fast_math_hint
>             << InputLayer(input_descriptor, get_input_accessor_detection_cust(ultraInFunc, (void*)(&graphData)));
> 
>             get_conv(data_path, "", 16U, 3U, PadStrideInfo(2, 2, 1, 1));
>             get_conv(data_path, "", 16U, 3U, PadStrideInfo(1, 1, 1, 1));
>             get_conv(data_path, "", 32U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 32U, 3U, PadStrideInfo(2, 2, 1, 1));
>             get_conv(data_path, "", 32U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 32U, 3U, PadStrideInfo(1, 1, 1, 1));
>             get_conv(data_path, "", 32U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 32U, 3U, PadStrideInfo(2, 2, 1, 1));
>             get_conv(data_path, "", 64U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 64U, 3U, PadStrideInfo(1, 1, 1, 1));
>             get_conv(data_path, "", 64U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 64U, 3U, PadStrideInfo(1, 1, 1, 1));
>             get_conv(data_path, "", 64U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 64U, 3U, PadStrideInfo(1, 1, 1, 1));
>             get_conv(data_path, "", 64U, 1U, PadStrideInfo(1, 1, 0, 0));
>     
>             SubStream output1(graph);   // 229
>             SubStream output2(graph);   // 229
> 
>             SubStream out10 = get_conv_stream(output1, data_path, "", 64U, 3U, PadStrideInfo(1, 1, 1, 1));
>             SubStream out11 = get_conv_stream(out10, data_path, "", 6U, 1U, PadStrideInfo(1, 1, 0, 0));
> 
>             out11 << PermuteLayer(PermutationVector{3U, 1U, 2U, 0U}, DataLayout::NCHW);   // 243
>             out11 << OutputLayer(get_output_accessor_detection_cust(ultraOut1Func, (void*)(&graphData)));
> 
>             SubStream out20 = get_conv_stream(output2, data_path, "", 64U, 3U, PadStrideInfo(1, 1, 1, 1));
>             SubStream out21 = get_conv_stream(out20, data_path, "", 12U, 1U, PadStrideInfo(1, 1, 0, 0));
>             out21 << PermuteLayer(PermutationVector{3U, 1U, 2U, 0U}, DataLayout::NCHW);   // 257
>             out21 << OutputLayer(get_output_accessor_detection_cust(ultraOut2Func, (void*)(&graphData)));
> 
>             get_conv(data_path, "", 64U, 3U, PadStrideInfo(2, 2, 1, 1));
>             get_conv(data_path, "", 128U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 128U, 3U, PadStrideInfo(1, 1, 1, 1));
>             get_conv(data_path, "", 128U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 128U, 3U, PadStrideInfo(1, 1, 1, 1));
>             get_conv(data_path, "", 128U, 1U, PadStrideInfo(1, 1, 0, 0));   // 275
>             
>             SubStream output3(graph);   // 275
>             SubStream output4(graph);   // 275
> 
>             SubStream out30 = get_conv_stream(output3, data_path, "", 128U, 3U, PadStrideInfo(1, 1, 1, 1));
>             SubStream out31 = get_conv_stream(out30, data_path, "", 4U, 1U, PadStrideInfo(1, 1, 0, 0));
>             out31 << PermuteLayer(PermutationVector{3U, 1U, 2U, 0U}, DataLayout::NCHW);   // 289
>             out31 << OutputLayer(get_output_accessor_detection_cust(ultraOut3Func, (void*)(&graphData)));
> 
>             SubStream out40 = get_conv_stream(output4, data_path, "", 128U, 3U, PadStrideInfo(1, 1, 1, 1));
>             SubStream out41 = get_conv_stream(out40, data_path, "", 8U, 1U, PadStrideInfo(1, 1, 0, 0));
>             out41 << PermuteLayer(PermutationVector{3U, 1U, 2U, 0U}, DataLayout::NCHW);   // 303
>             out41 << OutputLayer(get_output_accessor_detection_cust(ultraOut3Func, (void*)(&graphData)));
> 
>             get_conv(data_path, "", 128U, 3U, PadStrideInfo(2, 2, 1, 1));
>             get_conv(data_path, "", 256U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 256U, 3U, PadStrideInfo(1, 1, 1, 1));
>             get_conv(data_path, "", 256U, 1U, PadStrideInfo(1, 1, 0, 0));
> 
>             SubStream output5(graph);   // 315
>             SubStream output6(graph);   // 315
> 
>             SubStream out50 = get_conv_stream(output5, data_path, "", 256U, 3U, PadStrideInfo(1, 1, 1, 1));
>             SubStream out51 = get_conv_stream(out50, data_path, "", 4U, 1U, PadStrideInfo(1, 1, 0, 0));
>             out51 << PermuteLayer(PermutationVector{3U, 1U, 2U, 0U}, DataLayout::NCHW);   // 329
>             out51 << OutputLayer(get_output_accessor_detection_cust(ultraOut3Func, (void*)(&graphData)));
> 
>             SubStream out60 = get_conv_stream(output6, data_path, "", 256U, 3U, PadStrideInfo(1, 1, 1, 1));
>             SubStream out61 = get_conv_stream(out60, data_path, "", 8U, 1U, PadStrideInfo(1, 1, 0, 0));
>             out61 << PermuteLayer(PermutationVector{3U, 1U, 2U, 0U}, DataLayout::NCHW);   // 343
>             out61 << OutputLayer(get_output_accessor_detection_cust(ultraOut3Func, (void*)(&graphData)));
> 
>             get_conv(data_path, "", 64U, 1U, PadStrideInfo(1, 1, 0, 0));
>             get_conv(data_path, "", 64U, 3U, PadStrideInfo(2, 2, 1, 1));
>             get_conv(data_path, "", 256U, 1U, PadStrideInfo(1, 1, 0, 0));
> 
>             SubStream output7(graph);   // 349
>             
>             SubStream out70 = get_conv_stream(output7, data_path, "", 6U, 3U, PadStrideInfo(1, 1, 1, 1));
>             out70 << PermuteLayer(PermutationVector{3U, 1U, 2U, 0U}, DataLayout::NCHW);   // 361
>             out70 << OutputLayer(get_output_accessor_detection_cust(ultraOut3Func, (void*)(&graphData)));
> 
>             get_conv(data_path, "", 12U, 3U, PadStrideInfo(1, 1, 1, 1));
>             graph << PermuteLayer(PermutationVector{3U, 1U, 2U, 0U}, DataLayout::NCHW);   // 373
>             graph << OutputLayer(get_output_accessor_detection_cust(ultraOut3Func, (void*)(&graphData)));
> 
>     // Finalize graph
>     GraphConfig config;
>     config.num_threads = common_params.threads;
>     config.use_tuner   = common_params.enable_tuner;
>     config.tuner_file  = common_params.tuner_file;
> 
>     graph.finalize(common_params.target, config);
> 
>     return true;
> }
> 
> void UltraNetModel::get_conv(const std::string &data_path, std::string &&param_path,
>             unsigned int channels, unsigned int kernel, PadStrideInfo pad_stride_info)
> {
>     std::string total_path = param_path + "_";
>     
>     graph << ConvolutionLayer(kernel, kernel, channels,
>                     get_weights_accessor(data_path, total_path + "expand_weights_read.npy", DataLayout::NCHW),
>                     get_weights_accessor(data_path, total_path + "expand_BatchNorm_batchnorm_sub.npy"),
>                     pad_stride_info).set_name(param_path + "/expand/Conv")
>             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU, 6.f)).set_name(param_path + "/expand/Relu6");
> }
> 
> SubStream UltraNetModel::get_conv_stream(SubStream stream, const std::string &data_path, std::string &&param_path,
>             unsigned int channels, unsigned int kernel, PadStrideInfo pad_stride_info)
> {
>     std::string total_path = param_path + "_";
>     
>     stream << ConvolutionLayer(kernel, kernel, channels,
>                     get_weights_accessor(data_path, total_path + "expand_weights_read.npy", DataLayout::NCHW),
>                     get_weights_accessor(data_path, total_path + "expand_BatchNorm_batchnorm_sub.npy"),
>                     pad_stride_info).set_name(param_path + "/expand/Conv")
>             << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU, 6.f)).set_name(param_path + "/expand/Relu6");
>     return stream;
> }
> 
> bool UltraNetModel::do_run(float *input, float *output1, float *output2, float *output3)
> {
> 	int ret = 0;
> 	struct timespec start, stop;
> 	clock_gettime(CLOCK_MONOTONIC, &start);
>     // Run graph
>     std::cout << "Starting run " << static_cast<void*>(input) << " " << static_cast<void*>(output1) << std::endl;
> 	graphData.inData = input;
> 	graphData.outData1 = output1;
> 	graphData.outData2 = output2;
> 	graphData.outData3 = output3;
>     graph.run();
> 
>     std::cout << "Ending run" << std::endl;
> 	clock_gettime(CLOCK_MONOTONIC, &stop);
> 	std::cout << "time: " << (stop.tv_sec - start.tv_sec) * 1e3 + (stop.tv_nsec - start.tv_nsec) / 1e6 << " ms\n" << std::endl;
> 	return true;
> }
> 

krgor avatar Apr 12 '20 08:04 krgor

Hi @krgor

Could you please provide a the test source code as a download so that we can run some tests?

morgolock avatar Jun 09 '20 15:06 morgolock

Hi @morgolock ,

Please find them attached. acl_ultra.zip

Thanks, krgor

krgor avatar Jun 14 '20 08:06 krgor

Hi @krgor

I get multiple compiler errors when I try to build acl_ultra.cpp.

In order to help you I'll need a program I can compile without errors. Could you please upload the sources again and make sure there are no compilation errors?

morgolock avatar Sep 21 '20 14:09 morgolock