espressif / esp-nn

Optimised Neural Network functions for Espressif chipsets
Apache License 2.0
125 stars 22 forks source link

Abnormal results from optimized 'elementwise add' operator #6

Closed Unbinilium closed 10 months ago

Unbinilium commented 10 months ago

Hello, I've noticed that after enabling the acceleration option for esp-nn, the elementwise add operator is returning abnormal results in some cases. Based on various test results, I personally suspect that there might be alignment issues with the stack usage of the elementwise add operation. Since I'm not familiar with the Xtensa architecture and ESP EE instruction set, I raise this issue seeking for help.

Environment:

Reproduce Details:

Additional Information:

If you need any other information that might be helpful, I'll try to provide, thanks.

vikramdattu commented 10 months ago

Hello @Unbinilium thanks for reporting the issue. Can you please confirm for which particular input size/s to the depth wise the issue occurs? Is it possible to share the model (or a smaller/untrained model if that's not possible) along with changes in the code, which can be used to re-produce the issue?

Thanks in advance.

Unbinilium commented 10 months ago

@vikramdattu Hi, sorry the name of the add operator I mentioned earlier should be corrected to elementwise add, it located in the basic_math folder of esp-nn.

And our model is a single-class YOLO object detection model (model.zip), quantified to int8 in a TFLite flatbuffer. It takes input of [192, 192, 3] and produces an output of [1, 2268, 6]. The issue we are observing most directly is that in the [1, 2268, :1] channel, some value seems to be smaller than normal, while values in the other channels appear to be normal.

Screenshot 2023-08-11 at 00 16 39

However, the actual situation might be more complex since there are other add operations before obtaining this value. What I can confirm is that everything is normal when using the non-accelerated ANSI C version elementwise add.

Hope the above information will help you find the issue.

vikramdattu commented 10 months ago

Hi @Unbinilium thank you agin for the detailed input. Please find the patch attached, which skips optimizations for add and mul when inputs are unaligned. In future, we will think of handling un-aligned cases as well for these two. But for now, since, these two ops are less often used and take up, only a % of total model, the path to selectively enabling vector optimization is taken.

0001-Bugfix-elementwise-add-mul-mismatch-when-unaligned-i.patch

Do share your observations after applying the patch.

Unbinilium commented 10 months ago

Thanks for your timely reply. I will test whether the patch solves the problem as soon as possible.

Unbinilium commented 10 months ago

After applied the patch, we tested in some cases and found that the issue we previously encountered no longer appears. Thanks again for your prompt patch.