foss-for-synopsys-dwc-arc-processors / embarc_mli

Machine learning inference library for ARC EM and HS Processors
Other
25 stars 10 forks source link

do you have plan to optimize leaky_relu and tanh op for tflm? #527

Open nyadla-sys opened 2 years ago

nyadla-sys commented 2 years ago

We are using himax board to run our custom model that uses leaky_relu and tanh ops on arc processor and currently we are running on tflm with C reference code and it takes lot of cycles to run inference, so could you please accelerate these ops on TFLM.

nyadla-sys commented 2 years ago

We are in the process of enabling the below kernel for TFLM on HiMax WE1 board as one of our NN models uses the Tanh kernel heavily. So we like to accelerate Tanh on ARC processors. mli_status mli_krn_tanh_fx8(const mli_tensor in, mli_tensor out); Do you plan to support this kernel for TFLM? If not please give us guidance to accelerate this kernel on TFLM for ARC EM9 processor(Himax WE1 board).

nyadla-sys commented 1 year ago

@mfarag13, @JaccovG, @Hakim7267 Please comment on this.. Actually I have ported mli_krn_leaky_relu_fx8(const mli_tensor in, mli_tensor slope_coeffs,mli_tensor * out) to tflm,however it is not outputting expected result. if you are open to provide the feedback, I can share the patch which does leaky_relu acceleration on ARC processor on TFLM.

JaccovG commented 1 year ago

Hi, Feel free to share the patch, I can review it.

nyadla-sys commented 1 year ago

@JaccovG Please refer the attached patch along with changed leaky_relu files https://drive.google.com/drive/folders/1lzRuglfxr4QXm_H2NRj3bwYyux_ZL42t?usp=sharing

nyadla-sys commented 1 year ago

Here is output of one test from tflm leaky_relu_test.cc

Entering to prepare of is_mli_applicable params->alpha 1.0*2^-1

fixed 8: 0x40

tensor->data.int8:0x40

Exiting to prepare of is_mli_applicable Inside LeakyReluEval params->alpha:1.0*2^-1 Converted to Q7 fixed point tensor->data.int8:0x40

Entering EvalMLI

params->alpha:1.0*2^-1 mli tensor of slope coeffs Q7 fixed point :0x40 res:0

Exiting EvalMLI

expected_data[i] (1.02^0) near output_data[i] (1.59999932^1) failed at examples/kernel_add_test/add_test.cc:103 expected_data[i] (1.49999992^1) near output_data[i] (1.14999942^3) failed at examples/kernel_add_test/add_test.cc:103 expected_data[i] (1.02^0) near output_data[i] (1.59999932^-3) failed at examples/kernel_add_test/add_test.cc:103 expected_data[i] (-1.02^-1) near output_data[i] (1.59999932^1) failed at examples/kernel_add_test/add_test.cc:103 expected_data[i] (-1.02^0) near output_data[i] (1.02^-127) failed at examples/kernel_add_test/add_test.cc:103 Testing QuantizedActivationsOpTestLeakyReluInt8_2

nyadla-sys commented 1 year ago

@JaccovG Could you please review patch and let us know your inputs

nyadla-sys commented 1 year ago

@JaccovG Gentle reminder.!

JaccovG commented 1 year ago

I'm not able to access the google drive. could you share it as a github commit? or as a PR?

nyadla-sys commented 1 year ago

@JaccovG https://github.com/usefulsensors/for_synopsys_review/blob/main/0001-UsefulSensors-Leanky_relu-optimization-for-ARC.patch Please review leaky_relu.cc,leaky_relu_common.cc and leaky_relu.h https://github.com/usefulsensors/for_synopsys_review

nyadla-sys commented 1 year ago

@JaccovG Gentle reminder.!

nyadla-sys commented 1 year ago

@JaccovG Gentle reminder.!

JaccovG commented 1 year ago

sorry for my late reply, I was very busy. I had a look at your code, and when you set the slope tensor, you force the exponent to 7. https://github.com/usefulsensors/for_synopsys_review/blob/main/leaky_relu.cc#L91 I don't know the reason for setting it to 7, but maybe the problem is related to how the slope tensor is constructed.

nyadla-sys commented 1 year ago

@JaccovG I have created q7 format for slope tensor,if this is something wrong could you please suggest. What is the correct implementation for this

JaccovG commented 1 year ago

I couldn't quickly find how you did the conversion. it is fine to use q7 format as long as you shift the mantissa to match the exponent of 7. So what you need to check is if the slope value is correctly converted to the fixedpoint value.

nyadla-sys commented 1 year ago

@JaccovG Pls refer here for conversion part
https://github.com/usefulsensors/for_synopsys_review/blob/main/0001-UsefulSensors-Leanky_relu-optimization-for-ARC.patch#L983

nyadla-sys commented 1 year ago

@JaccovG as well pls refer the below code to construct slope_tensor https://github.com/usefulsensors/for_synopsys_review/blob/main/0001-UsefulSensors-Leanky_relu-optimization-for-ARC.patch#L998