Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Problem statement
arm_softmax_s16 returns twice the TFLM result for the nine-element row below. A one-element row also returns -32768 instead of 32767.
These results violate the probability encoding and normalization contract and can affect consumers that use confidence thresholds.
The tested CMSIS-NN commit is 71cbe3d5a686c875c88a4d112633b13fae94b5a2. The reference is the unmodified TFLM SoftmaxInt16 at 230c9d9531bd9b08d445b4f2ed0760756ffd5f78. Both cases use the generic C implementation with default quantization options.
The public contract states: “Supported framework: TensorFlow Lite micro (bit-accurate)”. The README selects TFLM behavior when TFL and TFLM differ. The reference normalization and output steps widen the normalization arithmetic, clamp the reciprocal input, and clamp the final output to [0,32767].
All cases below use one row, multiplier 1073741824, and the documented 513-entry LUTs. Both failing calls return ARM_CMSIS_NN_SUCCESS (0).
| Case | Input | Shift | Expected TFLM output | Observed CMSIS-NN output |
|---|---|---|---|---|
| Normalization boundary | Eight 32767 values, then -10176 |
1 | Eight 4095 values, then 6 |
Eight 8190 values, then 12 |
| Adjacent control | Eight 32767 values, then -10177 |
1 | Eight 4095 values, then 6 |
Matches |
| One-class row | [0] |
0 | [32767] |
[-32768] |
| Two-class control | [0,0] |
0 | [16384,16384] |
Matches |
TensorFlow CPU 2.20.0 produced unedited int16 SOFTMAX version-2 models with shapes [1,9], [1,1], and [1,2]. Beta is 1. Both tensor zero points are 0. The output scale is 1/32768. The converted input scales are 0.00015259021893143654 for the nine-class case and 0.00007629510946571827 for the singleton and two-class control. Pinned TFLM preparation derives the multiplier and shifts above from this metadata. It also generates LUTs that match the CMSIS-NN test tables used below. These prepared values are runtime results, not fields serialized by the converter.
At the normalization assignments, the nine-class row has an exponential sum of 262143 and headroom 14. The rounding numerator is 2147475456 + 8192 = 2147483648, which cannot fit in int32. The widened calculation gives normalized sum 131072 and centered reciprocal input 32768, which must clamp to 32767. The direct int16 narrowing instead feeds -32768 to the reciprocal lookup on both tested compilers. Target-only UBSan identifies the signed addition overflow at line 90. Signed overflow is undefined in C. The arrays above describe the tested compiler results.
For the singleton, normalization produces centered reciprocal input 32744 without the preceding overflow. The rounded output is 32768. The final assignment converts it directly to int16. Both tested compilers produce -32768. This out-of-range conversion is implementation-defined. The TFLM reference clamps it to 32767.
Diagnostic scratch copies isolate the two assignments. Widening normalization and clamping the reciprocal input corrects the nine-class case. Clamping only the final output corrects the singleton. Both changes together match both reference results and preserve both controls.
Steps To Reproduce
The C driver below was source-checked against the pinned sources but was not compiled during drafting.
Save this driver as softmax_s16_repro.c in an empty working directory. It uses the pinned upstream test LUT headers. The example driver is CC0-1.0. The upstream headers retain their Apache-2.0 license.
// SPDX-License-Identifier: CC0-1.0
#include <stdint.h>
#include <stdio.h>
#include "arm_nnfunctions.h"
#include "exp_lut_data.h"
#include "one_by_one_lut_data.h"
static int run(const char *name, const int16_t *input, int depth, int shift,
const int16_t *expected) {
int16_t output[9] = {0};
const cmsis_nn_softmax_lut_s16 luts = {
softmax_s16_exp_lut, softmax_s16_one_by_one_lut};
const arm_cmsis_nn_status status =
arm_softmax_s16(input, 1, depth, 1073741824, shift, &luts, output);
int mismatch = status != ARM_CMSIS_NN_SUCCESS;
printf("%s: status=%d output=[", name, (int)status);
for (int i = 0; i < depth; ++i) {
printf("%s%d", i ? "," : "", (int)output[i]);
mismatch |= output[i] != expected[i];
}
printf("] %s\n", mismatch ? "MISMATCH" : "PASS");
return mismatch;
}
int main(void) {
int16_t row[9] = {32767,32767,32767,32767,32767,32767,32767,32767,-10177};
const int16_t normalized[9] = {4095,4095,4095,4095,4095,4095,4095,4095,6};
const int16_t zero[2] = {0,0};
const int16_t pair[2] = {16384,16384};
const int16_t singleton[1] = {32767};
int mismatch = run("normalize-control", row, 9, 1, normalized);
row[8] = -10176;
mismatch |= run("normalize-boundary", row, 9, 1, normalized);
mismatch |= run("singleton-control", zero, 2, 0, pair);
mismatch |= run("singleton", zero, 1, 0, singleton);
return mismatch ? 1 : 0;
}Run on a Linux host with GCC or Clang:
git clone https://github.com/ARM-software/CMSIS-NN.git CMSIS-NN
git -C CMSIS-NN checkout --detach 71cbe3d5a686c875c88a4d112633b13fae94b5a2
gcc -std=c11 -O2 -I CMSIS-NN/Include \
-I CMSIS-NN/Tests/UnitTest/TestCases/Common/Softmax \
softmax_s16_repro.c CMSIS-NN/Source/SoftmaxFunctions/arm_softmax_s16.c \
-o softmax_s16_gcc
./softmax_s16_gcc
clang -std=c11 -O2 -I CMSIS-NN/Include \
-I CMSIS-NN/Tests/UnitTest/TestCases/Common/Softmax \
softmax_s16_repro.c CMSIS-NN/Source/SoftmaxFunctions/arm_softmax_s16.c \
-o softmax_s16_clang
./softmax_s16_clangThe driver prints each result and returns 1 when any output differs from the expected array. Both controls should print PASS. The two boundary cases should print MISMATCH on the tested source. No architecture macros, intrinsic stubs, or target edits are needed.
The executed results reported here are recorded runs of the original Python reproducers and the independent verifier adapter. The verified reproductions used x86_64 Linux, Python 3.12.3, GCC/G++ 13.3.0, and Clang/Clang++ 18.1.3. GCC -O0 and -O2, and Clang -O2, produced the recorded arrays. The reference was rebuilt with both C++ compilers. Actual TFLM preparation, the TFLite reference interpreter, and independent integer calculations agreed on both expected arrays.
Testing covers the generic C function and these pins. A directed sweep found 128 normalization differences for tails [-10176,-10049] with the other eight inputs fixed at 32767. The remaining tails matched TFLM. Singleton endpoint inputs and two-row cases also reproduced the output difference. The 66,430-case component check found only these two recorded families, with all Python/TFLM comparisons equal.
The full library was not built for the host. The README documents that host builds are not supported out of the box. Arm hardware, DSP/MVE configurations, optional single-rounding and inline-assembly modes, other operators, other commits, and shift 31 were not tested.
Found by ChipLab's AI verification agents and independently reproduced.
ChipLab ref: CLAB-2026-185
Activity
mert-ogul commentedon Oct 12, 2026
Thanks for the detailed report and repro cases. I'd like to submit the fix if nobody's on it.
My plan is widen the normalization arithmetic in arm_softmax_s16.c so the shift can't overflow int32, clamp the reciprocal LUT input, and saturate the final output to int16 so the one-class case returns 32767 like TFLM does. I'll add the four vectors from the issue to the softmax_s16 unit tests and bump the file's version and date.
I can build and check it on the host, but I may not be able to run the Corstone-300 FVP tests myself. Is it OK if I open the PR and ask for help with that part, as the README suggests?
vera-rubin commentedon Oct 15, 2026
We've already opened #249 for the normalization and one-class cases, with a request for help with the Arm/FVP tests.
Fix arm_softmax_s16 normalization and output saturation (ARM-software…