diff --git a/Testing/CMakeLists.txt b/Testing/CMakeLists.txt index 53396b6b..5e8a1658 100644 --- a/Testing/CMakeLists.txt +++ b/Testing/CMakeLists.txt @@ -93,6 +93,7 @@ set(TESTSRC testmain.cpp Source/FullyConnectedBench.cpp Source/Pooling.cpp Source/PoolingBench.cpp + Source/Softmax.cpp GeneratedSource/TestDesc.cpp ) diff --git a/Testing/Include/Softmax.h b/Testing/Include/Softmax.h new file mode 100755 index 00000000..401e9403 --- /dev/null +++ b/Testing/Include/Softmax.h @@ -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& paramsArgs,Client::PatternMgr *mgr); + void tearDown(Testing::testID_t,Client::PatternMgr *mgr); + private: + #include "Softmax_decl.h" + + Client::Pattern dims; + Client::Pattern input; + + Client::RefPattern ref; + Client::RefPattern samples; + + Client::LocalPattern output; + Client::LocalPattern temp; + + int nbSamples; + int vecDim; + + + }; \ No newline at end of file diff --git a/Testing/PatternGeneration/Softmax.py b/Testing/PatternGeneration/Softmax.py new file mode 100755 index 00000000..2e0929a5 --- /dev/null +++ b/Testing/PatternGeneration/Softmax.py @@ -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) diff --git a/Testing/PatternGeneration/Tools.py b/Testing/PatternGeneration/Tools.py index 8254b421..a8821aca 100755 --- a/Testing/PatternGeneration/Tools.py +++ b/Testing/PatternGeneration/Tools.py @@ -62,6 +62,9 @@ def to_q7(v): r = -0x080 return ("0x%s" % format(struct.unpack(' + +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 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 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& 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); + } diff --git a/Testing/desc.txt b/Testing/desc.txt index 3022d864..39d203a6 100644 --- a/Testing/desc.txt +++ b/Testing/desc.txt @@ -641,6 +641,23 @@ group Root { arm_avgpool_s8 Test 8:test_avgpool_s8 } } + + suite Softmax { + class = Softmax + folder = SoftmaxQ7 + + Pattern INPUT1_Q7_ID : InputA1_q7.txt + Pattern DIMS1_S16_ID : Dims1_s16.txt + Pattern REF1_S16_ID : Ref1_s16.txt + Pattern SAMPLES1_Q7_ID : Samples1_q7.txt + + Output OUTPUT_S16_ID : Output + Output TEMP_Q7_ID : Temp + + Functions { + arm_softmax_q7 Test 1:test_softmax_q7 + } + } } group NN Benchmarks