Comments (7)
Hi @0xyd,
CMSIS-NN should be compatible with TFLM and TFL reference kernels. Tensorflow lite (TFL) also supports other kernels other than the reference kernels, for example optimized kernels. They might sometimes differ slightly and CMSIS-NN is only compatible with the reference kernels. Sometimes even TFL and TFLM reference kernels might differ for some operator.
It seems you were able to run the model with TFLM with CMSIS-NN kernels?
If yes are you able to run the model with TFLM reference kernels?
The output should match.
One way to make sure it is a fair match could be to use the network tester example:
https://github.com/tensorflow/tflite-micro/tree/main/tensorflow/lite/micro/examples/network_tester
It requires converting the model to tflite file format and then in source file format with xxd.
from cmsis-nn.
Hello, thank you for your reply. I actually don't know what "run the model with TFLM with CMSIS-NN kernels" means. But my approach is based on C code generation: I first build a model in tensorflow, convert it into a quantized model in tensorflow-lite, and parse its computational graph to get the parameters I require. My platform is a bit restricted so I have to use its tool chain for building models from C.
from cmsis-nn.
CMSIS-NN is a library so the recommended way is to run a model with Tensorflow Lite Micro (TFLM), which then use CMSIS-NN kernels for any operators supported by CMSIS-NN. Calling CMSIS-NN directly is cumbersome and error prone. Since you would need to calculate many parameters and multipliers/shifts manually. Maybe this is what you are doing?
What I meant was that you could use xxd to convert your model in tflite format into: https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/examples/network_tester/network_model.h and then run the network tester example, with and without CMSIS-N kernels, print and compare the output for each:
make -f tensorflow/lite/micro/tools/make/Makefile OPTIMIZED_KERNEL_DIR=cmsis_nn TARGET=cortex_m_corstone_300 TARGET_ARCH=cortex-m55 test_network_tester_test
make -f tensorflow/lite/micro/tools/make/Makefile TARGET=cortex_m_corstone_300 TARGET_ARCH=cortex-m55 test_network_tester_test
Or if you could provide a script that can generate your model in tflite format, I could check it.
from cmsis-nn.
Yes, my code calculates the multipliers/shifts based on the quantization parameters stored in the Tensorflow-Lite model. So far, convolution, maxpooling, avgpooling, fully-connected, and softmax work fine. (The output of these operations are identical to the TF Lite models) The link you provide is testing with the depthwise convolution which I haven't checked yet. So what you suggest is to follow a similar way to build an example with element-wise add operator?
The way I generate the element-wise add code is very similar to here. As my previous Residual block shows, there are two input tensors for the KL.Add
: One is the output of the first convolution layer and the other one is the last convolution layer. The KL.Add
has one output tensor in the end. Using the testing code as the instruction, I implement the code to calculate the multipliers/shifts as following:
inputScale_1 = inputTensor_1['quantization']['scale']
inputZeroPoint_1 = -1 * inputTensor_1['quantization']['zeroPoint']
inputScale_2 = inputTensor_2['quantization']['scale']
inputZeroPoint_2 = -1 * inputTensor_2['quantization']['zeroPoint']
doubleMaxInputScale = max(inputScale_1, inputScale_2) * 2
inputMultiplier_1, shift_1 = math.frexp(inputScale_1 / doubleMaxInputScale)
inputMultiplier_1 = round(inputMultiplier_1 * (1 << 31))
inputMultiplier_2, shift_2 = math.frexp(inputScale_2 / doubleMaxInputScale)
inputMultiplier_2 = round(inputMultiplier_2 * (1 << 31))
outputScale = outputTensor['quantization']['scale']
outputZeroPoint = outputTensor['quantization']['zeroPoint']
leftShift = 20
realOutputScale = doubleMaxInputScale / ((1 << leftShift) * outputScale)
outputMultiplier, outputShift = math.frexp(realOutputScale)
outputMultiplier = round(outputMultiplier * (1 << 31))
The model is quantized with INT8 scheme, so I use the arm_elementwise_add_s8
in my CMSIS-NN code. Because I just mimic the testing code, several things I am not so sure are:
- What is leftShift and why is it 20?
- Why do we need to find the maximum input scale of two input tensors? Why do we need to double it?
- Why do we need to calibrate the output scale?
I check the CMSIS-NN paper but I still coudn't figure it out. It will be nice if you know how this mechanism is implemented in TFLM with CMSIS-NN together.
For the tflite model, you can just copy the code at the beginning of the thread and quantize it with INT8 scheme.
from cmsis-nn.
What I am suggesting is based on your first question (I am wondering is the function proved to be compatible with tensorflow-lite or do I miss something?) is some way to verify the generated model to be bit exact between Tensorflow Lite and TFLM+CMSIS-NN kernels. I am pretty sure that there is no diff by looking at the code you provided. However, another way to do is apart from what I already suggested might be to use this script, which will extract multipliers etc for a given model so that CMSIS-NN can use that information and run the model without TFLM: https://github.com/ARM-software/CMSIS-NN/blob/main/Tests/UnitTest/model_extractor.py
Perhaps this will give you some insight related your own script, as the scripts (model_extractor.py and yours) should generate the same output basically.
The short answer to your questions, 1-3 is that if that is not done, CMSIS-NN will not be bit exact against Tensorflow reference kernels.
from cmsis-nn.
Hi, I check the script you provide and "ADD" is not included in the script so the script is not helpful for me to check if I make ADD operation correctly or not. (The script will just skip the ADD apparently)
Since arm_elementwise_add_s8 is already existed in commit here, I expect the function has been tested with tensorflow-lite. But maybe it is not tested when input is 3 dimension feature maps?
from cmsis-nn.
Can't really see a difference between TFLM reference and CMSIS-NN kernels related input dimension, or perhaps you ADD requires broadcast, in that case CMSIS-NN does not support it: https://github.com/tensorflow/tflite-micro/blob/2f2c744672f176d3b774cb105402ba705922ca7e/tensorflow/lite/micro/kernels/cmsis_nn/add.cc#L136
I can't say for sure if it is a bug or something not supported, or something with your setup. If you can provide a script that generates a tflite file with the ADD, I could verify it.
from cmsis-nn.
Related Issues (20)
- missing arm_s8_to_s16_unordered_with_offset.c when I included the pack HOT 3
- Output diff for new lstm-operator
- Confusing doc on `arm_nn_requantize`
- MVE Conv 1 x N does not handle all configurations HOT 1
- Unit tests fails to build with -O0
- Possible undefined behavior for `arm_nn_requantize` when compiling with `CMSIS_NN_USE_SINGLE_ROUNDING` HOT 9
- Inconsistency of CMSIS-NN Quantization Method(Q-format) with ARM Documentation HOT 4
- Inconsistency of CMSIS-NN Quantization Method(Q-format) with ARM Documentation HOT 2
- No MVE support Conv2D int16
- Move test platform to other FVPs HOT 6
- IAR compiler issues HOT 1
- Is there a roadmap? HOT 3
- Does the MVEI version of CMSIS NN have implementations of q15 and q7? HOT 1
- Vector of variable length used in arm_convolve_1x1_s8_fast HOT 1
- Documentation warning HOT 1
- What does CMSIS exactly refer to? HOT 1
- Occur an ASM error when compiling arm_nn_mat_mul_core_4x_s8 HOT 11
- reserved identifier violation HOT 6
- Is there some easy to get started guide? HOT 1
- Include/Internal/arm_nn_compiler.h: undefined __ARM_FEATURE_MVE HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cmsis-nn.