MCPcopy Create free account
hub / github.com/alibaba/MNN / checkProbAndOrder

Function checkProbAndOrder

test/op/SoftmaxTest.cpp:56–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54float* outputs[] = {expectedOutput0.data(), expectedOutput1.data(), expectedOutput2.data(), expectedOutput3.data()};
55
56static bool checkProbAndOrder(float* gotOutput, const float* expectedOutput, const int* expectedOrder, int size,
57 std::vector<int> shape = {}, int axis = -1) {
58 float expectedSum = 0, gotSum = 0;
59 std::vector<int> gotOrder(size, 0);
60
61 int outside = 1, inside = 1;
62 for (int i = 0; i < axis; ++i) {
63 outside *= shape[i];
64 }
65 for (int i = axis + 1; i < shape.size(); ++i) {
66 inside *= shape[i];
67 }
68
69 float errorCase = 0;
70 for (int z = 0; z < outside; ++z) {
71 for (int x = 0; x < inside; ++x) {
72 std::vector<int> orderY(shape[axis], 0);
73 float expectedSumY = 0;
74 float gotSumY = 0;
75
76 int xz = x + z * inside * shape[axis];
77 for (int y = 0; y < shape[axis]; ++y) {
78 int idx = xz + y * inside;
79 orderY[y] = idx;
80 expectedSumY += expectedOutput[idx];
81 gotSumY += gotOutput[idx];
82 }
83 sort(orderY.begin(), orderY.end(), [&](const int &a, const int &b) {
84 return gotOutput[a] < gotOutput[b];
85 });
86 float rateY = 0;
87 for (int y = 0; y < shape[axis]; ++y) {
88 if (expectedOrder[(x + z *inside) * shape[axis] + y] == orderY[y]) {
89 rateY += 1;
90 }
91 }
92 rateY /= shape[axis];
93 float pointRate = gotSumY / expectedSumY;
94 if (rateY < 0.5 || pointRate < 0.5 || pointRate > 2.0) {
95 errorCase += 1;
96 }
97 }
98 }
99 if (errorCase / size > 0.03) {
100 MNN_PRINT("softmaxInt8 test on axis = %d, ErrorRate = %f, failed\n", axis, errorCase/size);
101 return false;
102 }
103
104 return true;
105}
106
107static std::vector<float> naiveSoftmax(const float* input, const int outside, const int axis, const int inside) {
108 std::vector<float> output(outside * axis * inside, 0);

Callers 1

runMethod · 0.85

Calls 4

sortFunction · 0.50
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected