From 976ad3e257c4a68e809f5a7fe3b7a99bb3f1fba4 Mon Sep 17 00:00:00 2001 From: Ellen Norris-Thompson Date: Wed, 21 Aug 2019 15:21:14 +0100 Subject: IVGCVSW-3613 Refactoring Android NN driver for QuantisedLstm test * Templated AddPoolAndGetData and AddPoolAndSetData to accept different types * Moved scale and offset to arguments of Add*Operand functions Signed-off-by: Ellen Norris-Thompson Change-Id: Iaf4c2d5f4183ea54799bedae20950b23b914a727 --- test/1.1/Mean.cpp | 2 +- test/1.1/Transpose.cpp | 2 +- test/Concat.cpp | 2 +- test/Concurrent.cpp | 4 ++-- test/Convolution2D.hpp | 2 +- test/DriverTestHelpers.cpp | 27 ----------------------- test/DriverTestHelpers.hpp | 55 ++++++++++++++++++++++++++++++++++++++-------- test/FullyConnected.cpp | 8 +++---- test/GenericLayerTests.cpp | 3 ++- test/Lstm.hpp | 8 +++---- 10 files changed, 62 insertions(+), 51 deletions(-) diff --git a/test/1.1/Mean.cpp b/test/1.1/Mean.cpp index 6e96d84b..529371e1 100644 --- a/test/1.1/Mean.cpp +++ b/test/1.1/Mean.cpp @@ -86,7 +86,7 @@ void MeanTestImpl(const TestTensor& input, AddPoolAndSetData(input.GetNumElements(), request, input.GetData()); // Add memory for the output - android::sp outMemory = AddPoolAndGetData(expectedOutput.GetNumElements(), request); + android::sp outMemory = AddPoolAndGetData(expectedOutput.GetNumElements(), request); const float* outputData = static_cast(static_cast(outMemory->getPointer())); ErrorStatus execStatus = Execute(preparedModel, request); diff --git a/test/1.1/Transpose.cpp b/test/1.1/Transpose.cpp index f2c77b3f..1b30aa6b 100644 --- a/test/1.1/Transpose.cpp +++ b/test/1.1/Transpose.cpp @@ -86,7 +86,7 @@ void TransposeTestImpl(const TestTensor & inputs, int32_t perm[], inputs.GetData()); // add memory for the output - android::sp outMemory = AddPoolAndGetData(expectedOutputTensor.GetNumElements(), request); + android::sp outMemory = AddPoolAndGetData(expectedOutputTensor.GetNumElements(), request); float* outdata = static_cast(static_cast(outMemory->getPointer())); auto execStatus = Execute(preparedModel, request); diff --git a/test/Concat.cpp b/test/Concat.cpp index 02d66cb8..9beb67bd 100644 --- a/test/Concat.cpp +++ b/test/Concat.cpp @@ -124,7 +124,7 @@ ConcatTestImpl(const std::vector & inputs, } // add memory for the output - android::sp outMemory = AddPoolAndGetData(expectedOutputTensor.GetNumElements(), request); + android::sp outMemory = AddPoolAndGetData(expectedOutputTensor.GetNumElements(), request); float* outdata = static_cast(static_cast(outMemory->getPointer())); // run the execution diff --git a/test/Concurrent.cpp b/test/Concurrent.cpp index 87ac2e80..9fe6f46e 100644 --- a/test/Concurrent.cpp +++ b/test/Concurrent.cpp @@ -84,9 +84,9 @@ BOOST_AUTO_TEST_CASE(ConcurrentExecute) requests[i].outputs = hidl_vec{output}; // set the input data (matching source test) float indata[] = {2, 32, 16}; - AddPoolAndSetData(3, requests[i], indata); + AddPoolAndSetData(3, requests[i], indata); // add memory for the output - outMemory[i] = AddPoolAndGetData(1, requests[i]); + outMemory[i] = AddPoolAndGetData(1, requests[i]); outdata[i] = static_cast(static_cast(outMemory[i]->getPointer())); } diff --git a/test/Convolution2D.hpp b/test/Convolution2D.hpp index 46b46549..180f57e2 100644 --- a/test/Convolution2D.hpp +++ b/test/Convolution2D.hpp @@ -102,7 +102,7 @@ void PaddingTestImpl(android::nn::PaddingScheme paddingScheme, bool fp16Enabled AddPoolAndSetData(6, request, indata); // add memory for the output - android::sp outMemory = AddPoolAndGetData(outSize, request); + android::sp outMemory = AddPoolAndGetData(outSize, request); float* outdata = reinterpret_cast(static_cast(outMemory->getPointer())); // run the execution diff --git a/test/DriverTestHelpers.cpp b/test/DriverTestHelpers.cpp index 4c26174b..3a3c98ff 100644 --- a/test/DriverTestHelpers.cpp +++ b/test/DriverTestHelpers.cpp @@ -102,33 +102,6 @@ hidl_memory allocateSharedMemory(int64_t size) return memory; } -android::sp AddPoolAndGetData(uint32_t size, Request& request) -{ - hidl_memory pool; - - android::sp allocator = IAllocator::getService("ashmem"); - allocator->allocate(sizeof(float) * size, [&](bool success, const hidl_memory& mem) { - BOOST_TEST(success); - pool = mem; - }); - - request.pools.resize(request.pools.size() + 1); - request.pools[request.pools.size() - 1] = pool; - - android::sp mapped = mapMemory(pool); - mapped->update(); - return mapped; -} - -void AddPoolAndSetData(uint32_t size, Request& request, const float* data) -{ - android::sp memory = AddPoolAndGetData(size, request); - - float* dst = static_cast(static_cast(memory->getPointer())); - - memcpy(dst, data, size * sizeof(float)); -} - android::sp PrepareModelWithStatus(const V1_0::Model& model, armnn_driver::ArmnnDriver& driver, ErrorStatus& prepareStatus, diff --git a/test/DriverTestHelpers.hpp b/test/DriverTestHelpers.hpp index c6f3f1fe..9da02603 100644 --- a/test/DriverTestHelpers.hpp +++ b/test/DriverTestHelpers.hpp @@ -99,9 +99,34 @@ private: hidl_memory allocateSharedMemory(int64_t size); -android::sp AddPoolAndGetData(uint32_t size, Request& request); +template +android::sp AddPoolAndGetData(uint32_t size, Request& request) +{ + hidl_memory pool; + + android::sp allocator = IAllocator::getService("ashmem"); + allocator->allocate(sizeof(T) * size, [&](bool success, const hidl_memory& mem) { + BOOST_TEST(success); + pool = mem; + }); + + request.pools.resize(request.pools.size() + 1); + request.pools[request.pools.size() - 1] = pool; + + android::sp mapped = mapMemory(pool); + mapped->update(); + return mapped; +} -void AddPoolAndSetData(uint32_t size, Request& request, const float* data); +template +void AddPoolAndSetData(uint32_t size, Request& request, const T* data) +{ + android::sp memory = AddPoolAndGetData(size, request); + + T* dst = static_cast(static_cast(memory->getPointer())); + + memcpy(dst, data, size * sizeof(T)); +} template& dimensions, const T* values, HalOperandType operandType = HalOperandType::TENSOR_FLOAT32, - HalOperandLifeTime operandLifeTime = HalOperandLifeTime::CONSTANT_COPY) + HalOperandLifeTime operandLifeTime = HalOperandLifeTime::CONSTANT_COPY, + double scale = 0.f, + int offset = 0) { using HalOperand = typename HalPolicy::Operand; @@ -197,6 +224,8 @@ void AddTensorOperand(HalModel& model, HalOperand op = {}; op.type = operandType; op.dimensions = dimensions; + op.scale = scale; + op.zeroPoint = offset; op.lifetime = HalOperandLifeTime::CONSTANT_COPY; op.location = location; @@ -218,9 +247,11 @@ void AddTensorOperand(HalModel& model, const hidl_vec& dimensions, const std::vector& values, HalOperandType operandType = HalPolicy::OperandType::TENSOR_FLOAT32, - HalOperandLifeTime operandLifeTime = HalOperandLifeTime::CONSTANT_COPY) + HalOperandLifeTime operandLifeTime = HalOperandLifeTime::CONSTANT_COPY, + double scale = 0.f, + int offset = 0) { - AddTensorOperand(model, dimensions, values.data(), operandType, operandLifeTime); + AddTensorOperand(model, dimensions, values.data(), operandType, operandLifeTime, scale, offset); } template void AddInputOperand(HalModel& model, const hidl_vec& dimensions, - HalOperandType operandType = HalOperandType::TENSOR_FLOAT32) + HalOperandType operandType = HalOperandType::TENSOR_FLOAT32, + double scale = 0.f, + int offset = 0) { using HalOperand = typename HalPolicy::Operand; using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; HalOperand op = {}; op.type = operandType; - op.scale = operandType == HalOperandType::TENSOR_QUANT8_ASYMM ? 1.f / 255.f : 0.f; + op.scale = scale; + op.zeroPoint = offset; op.dimensions = dimensions; op.lifetime = HalOperandLifeTime::MODEL_INPUT; @@ -250,14 +284,17 @@ template void AddOutputOperand(HalModel& model, const hidl_vec& dimensions, - HalOperandType operandType = HalOperandType::TENSOR_FLOAT32) + HalOperandType operandType = HalOperandType::TENSOR_FLOAT32, + double scale = 0.f, + int offset = 0) { using HalOperand = typename HalPolicy::Operand; using HalOperandLifeTime = typename HalPolicy::OperandLifeTime; HalOperand op = {}; op.type = operandType; - op.scale = operandType == HalOperandType::TENSOR_QUANT8_ASYMM ? 1.f / 255.f : 0.f; + op.scale = scale; + op.zeroPoint = offset; op.dimensions = dimensions; op.lifetime = HalOperandLifeTime::MODEL_OUTPUT; diff --git a/test/FullyConnected.cpp b/test/FullyConnected.cpp index de885153..ec4fcbda 100644 --- a/test/FullyConnected.cpp +++ b/test/FullyConnected.cpp @@ -70,10 +70,10 @@ BOOST_AUTO_TEST_CASE(FullyConnected) // set the input data (matching source test) float indata[] = {2, 32, 16}; - AddPoolAndSetData(3, request, indata); + AddPoolAndSetData(3, request, indata); // add memory for the output - android::sp outMemory = AddPoolAndGetData(1, request); + android::sp outMemory = AddPoolAndGetData(1, request); float* outdata = static_cast(static_cast(outMemory->getPointer())); // run the execution @@ -153,7 +153,7 @@ BOOST_AUTO_TEST_CASE(TestFullyConnected4dInput) AddPoolAndSetData(8, request, indata); // add memory for the output - android::sp outMemory = AddPoolAndGetData(8, request); + android::sp outMemory = AddPoolAndGetData(8, request); float* outdata = static_cast(static_cast(outMemory->getPointer())); // run the execution @@ -240,7 +240,7 @@ BOOST_AUTO_TEST_CASE(TestFullyConnected4dInputReshape) AddPoolAndSetData(8, request, indata); // add memory for the output - android::sp outMemory = AddPoolAndGetData(8, request); + android::sp outMemory = AddPoolAndGetData(8, request); float* outdata = static_cast(static_cast(outMemory->getPointer())); // run the execution diff --git a/test/GenericLayerTests.cpp b/test/GenericLayerTests.cpp index 6b51fb93..3b11b726 100644 --- a/test/GenericLayerTests.cpp +++ b/test/GenericLayerTests.cpp @@ -188,7 +188,8 @@ BOOST_AUTO_TEST_CASE(UnsupportedLayerContinueOnFailure) AddOutputOperand(model, hidl_vec{1, 1, 3, 4}); AddOutputOperand(model, hidl_vec{1, 1, 3, 4}, - HalPolicy::OperandType::TENSOR_QUANT8_ASYMM); + HalPolicy::OperandType::TENSOR_QUANT8_ASYMM, + 1.f / 225.f); // Fully connected is supported AddInputOperand(model, hidl_vec{1, 3}); diff --git a/test/Lstm.hpp b/test/Lstm.hpp index 3d9bf77f..6032f1c2 100644 --- a/test/Lstm.hpp +++ b/test/Lstm.hpp @@ -372,12 +372,12 @@ void LstmTestImpl(const hidl_vec& inputDimensions, AddPoolAndSetData(cellStateInValue.size(), request, cellStateInValue.data()); // add memory for the outputs - AddPoolAndGetData(scratchBufferValue.size(), request); - android::sp outputStateOutMemory = AddPoolAndGetData(outputStateOutValue.size(), request); + AddPoolAndGetData(scratchBufferValue.size(), request); + android::sp outputStateOutMemory = AddPoolAndGetData(outputStateOutValue.size(), request); float* outputStateOutData = static_cast(static_cast(outputStateOutMemory->getPointer())); - android::sp cellStateOutMemory = AddPoolAndGetData(cellStateOutValue.size(), request); + android::sp cellStateOutMemory = AddPoolAndGetData(cellStateOutValue.size(), request); float* cellStateOutData = static_cast(static_cast(cellStateOutMemory->getPointer())); - android::sp outputMemory = AddPoolAndGetData(outputValue.size(), request); + android::sp outputMemory = AddPoolAndGetData(outputValue.size(), request); float* outputData = static_cast(static_cast(outputMemory->getPointer())); // make the prepared model and run the execution -- cgit v1.2.1