ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

Problem with GEMM (NEGEMMLowp function, 8-bit) - very low performance

Open atrah22 opened this issue 9 years ago • 9 comments

Hello, I tried to benchmark NEGEMMLowp (8-bit) for different Matrix parameters on Xiaomi Redmi Pro (10 cores). The performance is very low compared to Google's gemmlowp optimzied for ARM NEON. Can anyone provide an example code showing the way to efficiently use NEGEMMLowp function? I provided my code below. Please, suggest me if there is any issue with the code, and/or gemm function, and/or optimziation.

I ran the "benchmark_meta_gemm.cc (21.8ms)" and "benchmark.cc (9.95ms)" from "https://github.com/google/gemmlowp" for Matrix size M=3136; N=192; K=576. Whereas, it takes 2575.92 ms when NEGEMMLowp is used for the same matrix size.

` double time() { timespec t; clock_gettime(CLOCK_REALTIME, &t); return t.tv_sec + 1e-9 * t.tv_nsec; }

void main_neon_gemm(int argc, const char **argv) {

Tensor    src1, src2, out1;

constexpr uint16_t M[] = { 49, 128, 160, 196, 196, 196, 3136, 12544, 1000, 1000, 1000, 1000}; constexpr uint16_t N[] = { 256, 508, 832, 160, 204, 256, 192, 64, 1000, 1000, 1000, 1000}; constexpr uint16_t K[] = { 832, 1, 49, 528, 864, 1152, 576, 147, 1, 10, 100, 1000};

int pointer =0;

if (argc > 1) {
  pointer= atoi(argv[1]);
  std::cout << "Argv(int)=" << pointer <<"\n\n";
}
else {
    pointer=2;  
    std:: cout << "Argv(int) Default=" << pointer << "\n\n";
}

const TensorShape src1_shape(M[pointer],K[pointer]);
const TensorShape src2_shape(K[pointer],N[pointer]);
const TensorShape out1_shape(M[pointer],N[pointer]);

src1.allocator()->init(TensorInfo(src1_shape, Format::U8));
src2.allocator()->init(TensorInfo(src2_shape, Format::U8));
out1.allocator()->init(TensorInfo(out1_shape, Format::U8));

// Initialize just the dimensions and format of the temporary and destination images:
NEGEMMLowp gemm;

gemm.configure(&src2, &src1, &out1, 0, 0, 0, 1, 24);

// Now that the padding requirements are known we can allocate the images:
src1.allocator()->allocate();
src2.allocator()->allocate();
out1.allocator()->allocate();

float avg =3;
double totalTime = 0.0;
for ( int i=0; i<avg; i++)
{
    double st = time();
    gemm.run();
    double totalTemp = (time() - st);
    totalTime = totalTime + totalTemp;
}

double tT= totalTime*1000;
tT = tT/avg;
std::cout << "Matrix size= " << M[pointer] << " * " << N[pointer] << " * " << K[pointer] << "\t" << tT << "ms\n\n";

}

int main(int argc, const char **argv) { return test_helpers::run_example(argc, argv, main_neon_gemm); } `

atrah22 avatar Apr 05 '17 04:04 atrah22

Did you try to compile this library with debug=0 asserts=0?

sundw2014 avatar Apr 05 '17 08:04 sundw2014

Yes @sundw2014. I just compiled the library with debug=0 asserts=0 options. The performance now improved by a lot. Its 151.37ms. However, its still 6.95x slower than the "benchmark_meta_gemm.cc (21.8ms)" from "https://github.com/google/gemmlowp".

The "benchmark_meta_gemm.cc" has similar input and output variables as like the NEGEMMLowp. The precision and the method is also same as like eight_bit_int_gemm function used in "benchmark_meta_gemm.cc". So is the performance gap is due to low level optimization? I was hoping to see similar and/or better performance with ARM compute library atleast incase of gemm than google's gemm function.

atrah22 avatar Apr 05 '17 08:04 atrah22

GEMMlowp was optimised by ARM for selected CPU micro-architectures and cache sizes using hand written assembly. Currently the ARM Compute Library only uses NEON intrinsics in order to remain compatible with as many devices as possible which is why you see a performance gap between the two for this particular function.

AnthonyBarbier avatar Apr 05 '17 10:04 AnthonyBarbier

Thankyou for the reply, @AnthonyARM. Can I know what are the ARM BigCPU cores for which it is optimized? Or Is ther any ARM CPU mirco-architecture specifically for which this NEGEMMLowp function will have superior performance? Google's gemmlowp is also optimzied for NEON with hand written assembly so that it can be used in different ARM cores containing NEON. I also tried benchmarking for S7 and the performance gap is similar. Anyway, thankyou for the insight and quick reply.

atrah22 avatar Apr 06 '17 00:04 atrah22

NEGEMMLowp is optimised for all ARM CPUs which support Neon, so naturally it wont run as fast as optimisations written in assembly for just one or two CPUs like the code in Google's gemmlowp.

All the functions in the ARM Compute Library support multithreading and run the kernels on as many threads as CPU's cores are available on the device. You can see this in the function run() method: https://github.com/ARM-software/ComputeLibrary/blob/master/src/runtime/NEON/functions/NEGEMMLowp.cpp#L83

morgolock avatar Apr 06 '17 08:04 morgolock

Hi @atrah22 and nice to "virtually" meet you :) I was looking at the code shared in order to reproduce the issue and I was wondering if the following dimensions for the Matrix A and Matrix B were correct:

const TensorShape src1_shape(M[pointer],K[pointer]); // Matrix A const TensorShape src2_shape(K[pointer],N[pointer]); // Matrix B const TensorShape out1_shape(M[pointer],N[pointer]);

so

Matrix A Columns = M; Matrix A Rows = K;

Matrix B Colums = K; Matrix B Rows = N;

Matrix out Colums = M Matrix out Rows = N

basically the first value passed to TensorShape() is the number of columns whilst the second one is the number of rows.

I'm asking this because based on the "blas" nomenclature (https://www.math.utah.edu/software/lapack/lapack-blas/sgemm.html), "M" should be the number of rows of matrix A, "N" should be the number of colums of matrix B and "K" should be the number of columns of the matrix A.

I do not think this can affect "considerably" the performance of the benchmark M=3136; N=192; K=576 anyway.

One thing I can suggest is to force manually the number of threads and to evaluate the execution time varying the number of threads.

You can use: arm_compute::Scheduler::get().force_number_of_threads(num_threads); in order to force the number of threads. https://github.com/ARM-software/ComputeLibrary/blob/master/arm_compute/runtime/CPP/CPPScheduler.h#L47

This evaluation at least could help you to understand how many cores are you actually using.

In order to help your investigation further, could you answer the following questions, please?

  1. Do you know if the DVFS is disabled?
  2. Did you set the CPU frequency to the maximum value?
  3. Could you share the full build command?
  4. Could share the execution time for each run?

Hope this can help you.

Thanks, Gian Marco

gmiodice avatar Apr 06 '17 17:04 gmiodice

Hello @gmiodice , good to see your reply and interest. **** When I place the matrix to configure, I put "src2" as the first matrix. "gemm.configure(&src2, &src1, &out1, 0, 0, 0, 1, 24);" Also, rechanging it and putting "gemm.configure(&src1, &src2, &out1, 0, 0, 0, 1, 24);" and rearranging columns and rows as you mentioned makes no change in performance. **** All the cores are working in parallel that means are used. I checked it with ARM DS-5. Hence, multi-threading is using max number of threads. Answers:

  1. DVFS is disabled as frequency is fixed to maximum
  2. Answered in above :)
  3. CXX=g++ CC=gcc scons Werror=1 -j8 debug=0 asserts=0 neon=1 opencl=0 os=android arch=arm64-v8a
  4. Results: (NEGEMMLowp) Matrix size= 49 * 256 * 832 1.56379ms Matrix size= 128 * 508 * 1 0.327031ms Matrix size= 160 * 832 * 49 1.07733ms Matrix size= 196 * 160 * 528 1.69071ms Matrix size= 196 * 204 * 864 3.76066ms Matrix size= 196 * 256 * 1152 5.84912ms Matrix size= 3136 * 192 * 576 30.8735ms Matrix size= 12544 * 64 * 147 18.8625ms Matrix size= 1000 * 1000 * 1 1.8971ms Matrix size= 1000 * 1000 * 10 2.68841ms Matrix size= 1000 * 1000 * 100 12.0682ms Matrix size= 1000 * 1000 * 1000 73.1828ms

("benchmark_meta_test.cc" of google/gemmlowp) Matrix size= 128 * 508 * 1 0.20ms Matrix size= 3136 * 192 * 576 13.2ms Matrix size= 12544 * 64 * 147 5ms Matrix size= 1000 * 1000 * 1000 42ms

atrah22 avatar Apr 07 '17 01:04 atrah22

Is it addressed yet.

kzos avatar Sep 06 '17 13:09 kzos

Closing as this issue has to do with a very old version of the library. Please try with the newest version and reopen if you still have any questions.

mpekatsoula avatar Sep 17 '18 16:09 mpekatsoula