CMSIS-NN: Created tests for original CMSIS-NN softmax q7 implementation.
parent
1f87fd50c9
commit
244770716b
@ -0,0 +1,25 @@
|
|||||||
|
#include "Test.h"
|
||||||
|
#include "Pattern.h"
|
||||||
|
class Softmax:public Client::Suite
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Softmax(Testing::testID_t id);
|
||||||
|
void setUp(Testing::testID_t,std::vector<Testing::param_t>& paramsArgs,Client::PatternMgr *mgr);
|
||||||
|
void tearDown(Testing::testID_t,Client::PatternMgr *mgr);
|
||||||
|
private:
|
||||||
|
#include "Softmax_decl.h"
|
||||||
|
|
||||||
|
Client::Pattern<int16_t> dims;
|
||||||
|
Client::Pattern<q7_t> input;
|
||||||
|
|
||||||
|
Client::RefPattern<int16_t> ref;
|
||||||
|
Client::RefPattern<q7_t> samples;
|
||||||
|
|
||||||
|
Client::LocalPattern<int16_t> output;
|
||||||
|
Client::LocalPattern<q7_t> temp;
|
||||||
|
|
||||||
|
int nbSamples;
|
||||||
|
int vecDim;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
@ -0,0 +1,57 @@
|
|||||||
|
import os.path
|
||||||
|
import itertools
|
||||||
|
import Tools
|
||||||
|
import random
|
||||||
|
import numpy as np
|
||||||
|
import scipy.special as sp
|
||||||
|
|
||||||
|
NBTESTSAMPLES = 1000
|
||||||
|
|
||||||
|
def softmax(v):
|
||||||
|
m = sp.softmax(v)
|
||||||
|
return(np.argmax(m)+1)
|
||||||
|
|
||||||
|
def writeTest(config,nb,vecDim):
|
||||||
|
dims=[]
|
||||||
|
inputsA=[]
|
||||||
|
outputs=[]
|
||||||
|
outputsSamples = []
|
||||||
|
|
||||||
|
|
||||||
|
dims.append(NBTESTSAMPLES)
|
||||||
|
dims.append(vecDim)
|
||||||
|
|
||||||
|
|
||||||
|
for _ in range(0,NBTESTSAMPLES):
|
||||||
|
va = np.random.randn(vecDim)
|
||||||
|
va = va / np.sum(va)
|
||||||
|
|
||||||
|
r = sp.softmax(va)
|
||||||
|
outputsSamples += list(r)
|
||||||
|
outputs.append(np.argmax(r)+1)
|
||||||
|
inputsA += list(va)
|
||||||
|
|
||||||
|
|
||||||
|
inputsA=np.array(inputsA)
|
||||||
|
outputs=np.array(outputs)
|
||||||
|
outputsSamples=np.array(outputsSamples)
|
||||||
|
|
||||||
|
config.writeInput(nb, inputsA,"InputA")
|
||||||
|
config.writeInputS16(nb, dims,"Dims")
|
||||||
|
|
||||||
|
config.writeReferenceS16(nb, outputs,"Ref")
|
||||||
|
config.writeReference(nb, outputsSamples,"Samples")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def writeTests(config):
|
||||||
|
writeTest(config,1,15)
|
||||||
|
|
||||||
|
|
||||||
|
PATTERNDIR = os.path.join("Patterns","NN","Softmax",)
|
||||||
|
PARAMDIR = os.path.join("Parameters","NN","Softmax")
|
||||||
|
|
||||||
|
configq7=Tools.Config(PATTERNDIR,PARAMDIR,"q7")
|
||||||
|
|
||||||
|
writeTests(configq7)
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
H
|
||||||
|
2
|
||||||
|
// 1000
|
||||||
|
0x03E8
|
||||||
|
// 15
|
||||||
|
0x000F
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,96 @@
|
|||||||
|
#include "Softmax.h"
|
||||||
|
#include "Error.h"
|
||||||
|
#include "arm_nnfunctions.h"
|
||||||
|
#include "Test.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
int16_t findMaxIndex(q7_t *vec_in, int length)
|
||||||
|
{
|
||||||
|
int16_t currentIndex=0;
|
||||||
|
int16_t i=1;
|
||||||
|
q7_t currentMax=vec_in[0];
|
||||||
|
|
||||||
|
while(i<length)
|
||||||
|
{
|
||||||
|
if (vec_in[i] > currentMax)
|
||||||
|
{
|
||||||
|
currentMax = vec_in[i];
|
||||||
|
currentIndex = i;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return(currentIndex+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int16_t differences(int16_t *pa,int16_t *pb, int length)
|
||||||
|
{
|
||||||
|
int16_t d=0;
|
||||||
|
int i=0;
|
||||||
|
while(i < length)
|
||||||
|
{
|
||||||
|
if (*pa != *pb)
|
||||||
|
{
|
||||||
|
d++;
|
||||||
|
}
|
||||||
|
|
||||||
|
pa++;
|
||||||
|
pb++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Softmax::test_softmax_q7()
|
||||||
|
{
|
||||||
|
const q7_t *vec_in = input.ptr();
|
||||||
|
q7_t *pTmp = temp.ptr();
|
||||||
|
int16_t *pOut = output.ptr();
|
||||||
|
int16_t maxIndex;
|
||||||
|
|
||||||
|
for(int i=0; i <this->nbSamples;i++)
|
||||||
|
{
|
||||||
|
arm_softmax_q7(vec_in, this->vecDim, pTmp );
|
||||||
|
maxIndex=findMaxIndex(pTmp,this->vecDim);
|
||||||
|
*pOut++ = maxIndex;
|
||||||
|
|
||||||
|
vec_in += this->vecDim;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Nb diffs : %d\n",differences(ref.ptr(),output.ptr(),this->nbSamples));
|
||||||
|
|
||||||
|
ASSERT_EQ(output,ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Softmax::setUp(Testing::testID_t id,std::vector<Testing::param_t>& paramsArgs,Client::PatternMgr *mgr)
|
||||||
|
{
|
||||||
|
|
||||||
|
switch(id)
|
||||||
|
{
|
||||||
|
case Softmax::TEST_SOFTMAX_Q7_1:
|
||||||
|
ref.reload(Softmax::REF1_S16_ID,mgr);
|
||||||
|
dims.reload(Softmax::DIMS1_S16_ID,mgr);
|
||||||
|
input.reload(Softmax::INPUT1_Q7_ID,mgr);
|
||||||
|
|
||||||
|
const int16_t *pDims=dims.ptr();
|
||||||
|
|
||||||
|
this->nbSamples = pDims[0];
|
||||||
|
this->vecDim = pDims[1];
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
output.create(ref.nbSamples(),Softmax::OUTPUT_S16_ID,mgr);
|
||||||
|
temp.create(this->vecDim,Softmax::TEMP_Q7_ID,mgr);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Softmax::tearDown(Testing::testID_t id,Client::PatternMgr *mgr)
|
||||||
|
{
|
||||||
|
//output.dump(mgr);
|
||||||
|
//temp.dump(mgr);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue