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 <ellen.norris-thompson@arm.com>
Change-Id: Iaf4c2d5f4183ea54799bedae20950b23b914a727
diff --git a/test/1.1/Mean.cpp b/test/1.1/Mean.cpp
index 6e96d84..529371e 100644
--- a/test/1.1/Mean.cpp
+++ b/test/1.1/Mean.cpp
@@ -86,7 +86,7 @@
     AddPoolAndSetData(input.GetNumElements(), request, input.GetData());
 
     // Add memory for the output
-    android::sp<IMemory> outMemory = AddPoolAndGetData(expectedOutput.GetNumElements(), request);
+    android::sp<IMemory> outMemory = AddPoolAndGetData<float>(expectedOutput.GetNumElements(), request);
     const float* outputData = static_cast<const float*>(static_cast<void*>(outMemory->getPointer()));
 
     ErrorStatus execStatus = Execute(preparedModel, request);
diff --git a/test/1.1/Transpose.cpp b/test/1.1/Transpose.cpp
index f2c77b3..1b30aa6 100644
--- a/test/1.1/Transpose.cpp
+++ b/test/1.1/Transpose.cpp
@@ -86,7 +86,7 @@
                       inputs.GetData());
 
     // add memory for the output
-    android::sp<IMemory> outMemory = AddPoolAndGetData(expectedOutputTensor.GetNumElements(), request);
+    android::sp<IMemory> outMemory = AddPoolAndGetData<float>(expectedOutputTensor.GetNumElements(), request);
     float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer()));
 
     auto execStatus = Execute(preparedModel, request);
diff --git a/test/Concat.cpp b/test/Concat.cpp
index 02d66cb..9beb67b 100644
--- a/test/Concat.cpp
+++ b/test/Concat.cpp
@@ -124,7 +124,7 @@
     }
 
     // add memory for the output
-    android::sp<IMemory> outMemory = AddPoolAndGetData(expectedOutputTensor.GetNumElements(), request);
+    android::sp<IMemory> outMemory = AddPoolAndGetData<float>(expectedOutputTensor.GetNumElements(), request);
     float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer()));
 
     // run the execution
diff --git a/test/Concurrent.cpp b/test/Concurrent.cpp
index 87ac2e8..9fe6f46 100644
--- a/test/Concurrent.cpp
+++ b/test/Concurrent.cpp
@@ -84,9 +84,9 @@
         requests[i].outputs = hidl_vec<RequestArgument>{output};
         // set the input data (matching source test)
         float indata[] = {2, 32, 16};
-        AddPoolAndSetData(3, requests[i], indata);
+        AddPoolAndSetData<float>(3, requests[i], indata);
         // add memory for the output
-        outMemory[i] = AddPoolAndGetData(1, requests[i]);
+        outMemory[i] = AddPoolAndGetData<float>(1, requests[i]);
         outdata[i] = static_cast<float*>(static_cast<void*>(outMemory[i]->getPointer()));
     }
 
diff --git a/test/Convolution2D.hpp b/test/Convolution2D.hpp
index 46b4654..180f57e 100644
--- a/test/Convolution2D.hpp
+++ b/test/Convolution2D.hpp
@@ -102,7 +102,7 @@
     AddPoolAndSetData(6, request, indata);
 
     // add memory for the output
-    android::sp<IMemory> outMemory = AddPoolAndGetData(outSize, request);
+    android::sp<IMemory> outMemory = AddPoolAndGetData<float>(outSize, request);
     float* outdata = reinterpret_cast<float*>(static_cast<void*>(outMemory->getPointer()));
 
     // run the execution
diff --git a/test/DriverTestHelpers.cpp b/test/DriverTestHelpers.cpp
index 4c26174..3a3c98f 100644
--- a/test/DriverTestHelpers.cpp
+++ b/test/DriverTestHelpers.cpp
@@ -102,33 +102,6 @@
     return memory;
 }
 
-android::sp<IMemory> AddPoolAndGetData(uint32_t size, Request& request)
-{
-    hidl_memory pool;
-
-    android::sp<IAllocator> 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<IMemory> mapped = mapMemory(pool);
-    mapped->update();
-    return mapped;
-}
-
-void AddPoolAndSetData(uint32_t size, Request& request, const float* data)
-{
-    android::sp<IMemory> memory = AddPoolAndGetData(size, request);
-
-    float* dst = static_cast<float*>(static_cast<void*>(memory->getPointer()));
-
-    memcpy(dst, data, size * sizeof(float));
-}
-
 android::sp<V1_0::IPreparedModel> PrepareModelWithStatus(const V1_0::Model& model,
                                                          armnn_driver::ArmnnDriver& driver,
                                                          ErrorStatus& prepareStatus,
diff --git a/test/DriverTestHelpers.hpp b/test/DriverTestHelpers.hpp
index c6f3f1f..9da0260 100644
--- a/test/DriverTestHelpers.hpp
+++ b/test/DriverTestHelpers.hpp
@@ -99,9 +99,34 @@
 
 hidl_memory allocateSharedMemory(int64_t size);
 
-android::sp<IMemory> AddPoolAndGetData(uint32_t size, Request& request);
+template<typename T>
+android::sp<IMemory> AddPoolAndGetData(uint32_t size, Request& request)
+{
+    hidl_memory pool;
 
-void AddPoolAndSetData(uint32_t size, Request& request, const float* data);
+    android::sp<IAllocator> 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<IMemory> mapped = mapMemory(pool);
+    mapped->update();
+    return mapped;
+}
+
+template<typename T>
+void AddPoolAndSetData(uint32_t size, Request& request, const T* data)
+{
+    android::sp<IMemory> memory = AddPoolAndGetData<T>(size, request);
+
+    T* dst = static_cast<T*>(static_cast<void*>(memory->getPointer()));
+
+    memcpy(dst, data, size * sizeof(T));
+}
 
 template<typename HalPolicy,
          typename HalModel   = typename HalPolicy::Model,
@@ -176,7 +201,9 @@
                       const hidl_vec<uint32_t>& 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 @@
     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 @@
                       const hidl_vec<uint32_t>& dimensions,
                       const std::vector<T>& 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<HalPolicy, T>(model, dimensions, values.data(), operandType, operandLifeTime);
+    AddTensorOperand<HalPolicy, T>(model, dimensions, values.data(), operandType, operandLifeTime, scale, offset);
 }
 
 template<typename HalPolicy,
@@ -228,14 +259,17 @@
          typename HalOperandType = typename HalPolicy::OperandType>
 void AddInputOperand(HalModel& model,
                      const hidl_vec<uint32_t>& 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 @@
          typename HalOperandType = typename HalPolicy::OperandType>
 void AddOutputOperand(HalModel& model,
                       const hidl_vec<uint32_t>& 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 de88515..ec4fcbd 100644
--- a/test/FullyConnected.cpp
+++ b/test/FullyConnected.cpp
@@ -70,10 +70,10 @@
 
     // set the input data (matching source test)
     float indata[] = {2, 32, 16};
-    AddPoolAndSetData(3, request, indata);
+    AddPoolAndSetData<float>(3, request, indata);
 
     // add memory for the output
-    android::sp<IMemory> outMemory = AddPoolAndGetData(1, request);
+    android::sp<IMemory> outMemory = AddPoolAndGetData<float>(1, request);
     float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer()));
 
     // run the execution
@@ -153,7 +153,7 @@
     AddPoolAndSetData(8, request, indata);
 
     // add memory for the output
-    android::sp<IMemory> outMemory = AddPoolAndGetData(8, request);
+    android::sp<IMemory> outMemory = AddPoolAndGetData<float>(8, request);
     float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer()));
 
     // run the execution
@@ -240,7 +240,7 @@
     AddPoolAndSetData(8, request, indata);
 
     // add memory for the output
-    android::sp<IMemory> outMemory = AddPoolAndGetData(8, request);
+    android::sp<IMemory> outMemory = AddPoolAndGetData<float>(8, request);
     float* outdata = static_cast<float*>(static_cast<void*>(outMemory->getPointer()));
 
     // run the execution
diff --git a/test/GenericLayerTests.cpp b/test/GenericLayerTests.cpp
index 6b51fb9..3b11b72 100644
--- a/test/GenericLayerTests.cpp
+++ b/test/GenericLayerTests.cpp
@@ -188,7 +188,8 @@
     AddOutputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 1, 3, 4});
     AddOutputOperand<HalPolicy>(model,
                                 hidl_vec<uint32_t>{1, 1, 3, 4},
-                                HalPolicy::OperandType::TENSOR_QUANT8_ASYMM);
+                                HalPolicy::OperandType::TENSOR_QUANT8_ASYMM,
+                                1.f / 225.f);
 
     // Fully connected is supported
     AddInputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 3});
diff --git a/test/Lstm.hpp b/test/Lstm.hpp
index 3d9bf77..6032f1c 100644
--- a/test/Lstm.hpp
+++ b/test/Lstm.hpp
@@ -372,12 +372,12 @@
     AddPoolAndSetData(cellStateInValue.size(), request, cellStateInValue.data());
 
     // add memory for the outputs
-    AddPoolAndGetData(scratchBufferValue.size(), request);
-    android::sp<IMemory> outputStateOutMemory = AddPoolAndGetData(outputStateOutValue.size(), request);
+    AddPoolAndGetData<float>(scratchBufferValue.size(), request);
+    android::sp<IMemory> outputStateOutMemory = AddPoolAndGetData<float>(outputStateOutValue.size(), request);
     float* outputStateOutData = static_cast<float*>(static_cast<void*>(outputStateOutMemory->getPointer()));
-    android::sp<IMemory> cellStateOutMemory = AddPoolAndGetData(cellStateOutValue.size(), request);
+    android::sp<IMemory> cellStateOutMemory = AddPoolAndGetData<float>(cellStateOutValue.size(), request);
     float* cellStateOutData = static_cast<float*>(static_cast<void*>(cellStateOutMemory->getPointer()));
-    android::sp<IMemory> outputMemory = AddPoolAndGetData(outputValue.size(), request);
+    android::sp<IMemory> outputMemory = AddPoolAndGetData<float>(outputValue.size(), request);
     float* outputData = static_cast<float*>(static_cast<void*>(outputMemory->getPointer()));
 
     // make the prepared model and run the execution