From 77605826a353981d41f0ee346850d411770535f8 Mon Sep 17 00:00:00 2001 From: Nikhil Raj Date: Mon, 3 Sep 2018 11:25:56 +0100 Subject: IVGCVSW-1713 Create a minimum unit test to compare the results before and after passing the FP16 flag in the Android-nn-driver Change-Id: If8d4ca12421c3bee2526eec98f11d393af822373 --- 1.0/HalPolicy.hpp | 1 + 1.1/HalPolicy.hpp | 1 + ArmnnPreparedModel.cpp | 2 +- Convolution2D.cpp | 110 ++++++++++++++++++++++++++++++++++++++ DriverOptions.cpp | 4 +- DriverOptions.hpp | 2 +- test/1.0/Convolution2D.cpp | 42 +++++++++++++++ test/1.1/Convolution2D.cpp | 52 ++++++++++++++++++ test/Android.mk | 5 +- test/Convolution2D.cpp | 110 -------------------------------------- test/Convolution2D.hpp | 129 +++++++++++++++++++++++++++++++++++++++++++++ test/DriverTestHelpers.cpp | 79 +++++++-------------------- test/DriverTestHelpers.hpp | 90 ++++++++++++++++++++++++++----- test/FullyConnected.cpp | 12 ++--- test/GenericLayerTests.cpp | 2 +- 15 files changed, 443 insertions(+), 198 deletions(-) create mode 100644 Convolution2D.cpp create mode 100644 test/1.0/Convolution2D.cpp create mode 100644 test/1.1/Convolution2D.cpp delete mode 100644 test/Convolution2D.cpp create mode 100644 test/Convolution2D.hpp diff --git a/1.0/HalPolicy.hpp b/1.0/HalPolicy.hpp index c596075b..fe41d073 100644 --- a/1.0/HalPolicy.hpp +++ b/1.0/HalPolicy.hpp @@ -21,6 +21,7 @@ class HalPolicy public: using Model = V1_0::Model; using Operation = V1_0::Operation; + using OperationType = V1_0::OperationType; using getSupportedOperations_cb = V1_0::IDevice::getSupportedOperations_cb; static bool ConvertOperation(const Operation& operation, const Model& model, ConversionData& data); diff --git a/1.1/HalPolicy.hpp b/1.1/HalPolicy.hpp index 3722d49d..5efe813b 100644 --- a/1.1/HalPolicy.hpp +++ b/1.1/HalPolicy.hpp @@ -19,6 +19,7 @@ class HalPolicy public: using Model = V1_1::Model; using Operation = V1_1::Operation; + using OperationType = V1_1::OperationType; using getSupportedOperations_cb = V1_1::IDevice::getSupportedOperations_1_1_cb; static bool ConvertOperation(const Operation& operation, const Model& model, ConversionData& data); diff --git a/ArmnnPreparedModel.cpp b/ArmnnPreparedModel.cpp index e4a8b147..85251ef1 100644 --- a/ArmnnPreparedModel.cpp +++ b/ArmnnPreparedModel.cpp @@ -305,4 +305,4 @@ template class ArmnnPreparedModel; template class ArmnnPreparedModel; #endif -} // namespace armnn_driver \ No newline at end of file +} // namespace armnn_driver diff --git a/Convolution2D.cpp b/Convolution2D.cpp new file mode 100644 index 00000000..a8491955 --- /dev/null +++ b/Convolution2D.cpp @@ -0,0 +1,110 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// +#include "../DriverTestHelpers.hpp" +#include +#include + +#include + +BOOST_AUTO_TEST_SUITE(Convolution2DTests) + +using namespace android::hardware; +using namespace driverTestHelpers; +using namespace armnn_driver; + +namespace +{ + +void PaddingTestImpl(android::nn::PaddingScheme paddingScheme) +{ + auto driver = std::make_unique(DriverOptions(armnn::Compute::CpuRef)); + V1_0::Model model = {}; + + uint32_t outSize = paddingScheme == android::nn::kPaddingSame ? 2 : 1; + + // add operands + float weightValue[] = {1, -1, 0, 1}; + float biasValue[] = {0}; + + AddInputOperand(model, hidl_vec{1, 2, 3, 1}); + AddTensorOperand(model, hidl_vec{1, 2, 2, 1}, weightValue); + AddTensorOperand(model, hidl_vec{1}, biasValue); + AddIntOperand(model, (int32_t)paddingScheme); // padding + AddIntOperand(model, 2); // stride x + AddIntOperand(model, 2); // stride y + AddIntOperand(model, 0); // no activation + AddOutputOperand(model, hidl_vec{1, 1, outSize, 1}); + + // make the convolution operation + model.operations.resize(1); + model.operations[0].type = V1_0::OperationType::CONV_2D; + model.operations[0].inputs = hidl_vec{0, 1, 2, 3, 4, 5, 6}; + model.operations[0].outputs = hidl_vec{7}; + + // make the prepared model + android::sp preparedModel = PrepareModel(model, *driver); + + // construct the request + DataLocation inloc = {}; + inloc.poolIndex = 0; + inloc.offset = 0; + inloc.length = 6 * sizeof(float); + RequestArgument input = {}; + input.location = inloc; + input.dimensions = hidl_vec{}; + + DataLocation outloc = {}; + outloc.poolIndex = 1; + outloc.offset = 0; + outloc.length = outSize * sizeof(float); + RequestArgument output = {}; + output.location = outloc; + output.dimensions = hidl_vec{}; + + Request request = {}; + request.inputs = hidl_vec{input}; + request.outputs = hidl_vec{output}; + + + // set the input data (matching source test) + float indata[] = {4, 1, 0, 3, -1, 2}; + AddPoolAndSetData(6, request, indata); + + // add memory for the output + android::sp outMemory = AddPoolAndGetData(outSize, request); + float* outdata = static_cast(static_cast(outMemory->getPointer())); + + // run the execution + Execute(preparedModel, request); + + // check the result + if (paddingScheme == android::nn::kPaddingValid) + { + BOOST_TEST(outdata[0] == 2); + } + else if (paddingScheme == android::nn::kPaddingSame) + { + BOOST_TEST(outdata[0] == 2); + BOOST_TEST(outdata[1] == 0); + } + else + { + BOOST_TEST(false); + } +} + +} // namespace + +BOOST_AUTO_TEST_CASE(ConvValidPadding) +{ + PaddingTestImpl(android::nn::kPaddingValid); +} + +BOOST_AUTO_TEST_CASE(ConvSamePadding) +{ + PaddingTestImpl(android::nn::kPaddingSame); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/DriverOptions.cpp b/DriverOptions.cpp index 65945d89..10919a7b 100644 --- a/DriverOptions.cpp +++ b/DriverOptions.cpp @@ -27,12 +27,12 @@ using namespace std; namespace armnn_driver { -DriverOptions::DriverOptions(armnn::Compute computeDevice) +DriverOptions::DriverOptions(armnn::Compute computeDevice, bool fp16Enabled) : m_ComputeDevice(computeDevice) , m_VerboseLogging(false) , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters) , m_EnableGpuProfiling(false) - , m_fp16Enabled(false) + , m_fp16Enabled(fp16Enabled) { } diff --git a/DriverOptions.hpp b/DriverOptions.hpp index c30ef6db..7271ac16 100644 --- a/DriverOptions.hpp +++ b/DriverOptions.hpp @@ -16,7 +16,7 @@ namespace armnn_driver class DriverOptions { public: - DriverOptions(armnn::Compute computeDevice); + DriverOptions(armnn::Compute computeDevice, bool fp16Enabled = false); DriverOptions(int argc, char** argv); DriverOptions(DriverOptions&& other) = default; diff --git a/test/1.0/Convolution2D.cpp b/test/1.0/Convolution2D.cpp new file mode 100644 index 00000000..9a5d2393 --- /dev/null +++ b/test/1.0/Convolution2D.cpp @@ -0,0 +1,42 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "../DriverTestHelpers.hpp" +#include "../Convolution2D.hpp" +#include "../../1.0/HalPolicy.hpp" + +#include +#include + +#include + +BOOST_AUTO_TEST_SUITE(Convolution2DTests) + +using namespace android::hardware; +using namespace driverTestHelpers; +using namespace armnn_driver; + +namespace driverTestHelpers +{ + +void SetModelFp16Flag(V1_0::Model&, bool) +{ + // Nothing to do, the V1_0::Model does not support fp16 precision relaxation. + // This function is used for compatibility only. +} + +} // namespace driverTestHelpers + +BOOST_AUTO_TEST_CASE(ConvValidPadding_Hal_1_0) +{ + PaddingTestImpl(android::nn::kPaddingValid); +} + +BOOST_AUTO_TEST_CASE(ConvSamePadding_Hal_1_0) +{ + PaddingTestImpl(android::nn::kPaddingSame); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/1.1/Convolution2D.cpp b/test/1.1/Convolution2D.cpp new file mode 100644 index 00000000..32d5018c --- /dev/null +++ b/test/1.1/Convolution2D.cpp @@ -0,0 +1,52 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "../DriverTestHelpers.hpp" +#include "../Convolution2D.hpp" +#include "../../1.1/HalPolicy.hpp" + +#include +#include + +#include + +BOOST_AUTO_TEST_SUITE(Convolution2DTests) + +using namespace android::hardware; +using namespace driverTestHelpers; +using namespace armnn_driver; + +namespace driverTestHelpers +{ + +void SetModelFp16Flag(V1_1::Model& model, bool fp16Enabled) +{ + // Set the fp16 flag in the given model + model.relaxComputationFloat32toFloat16 = fp16Enabled; +} + +} // namespace driverTestHelpers + +BOOST_AUTO_TEST_CASE(ConvValidPadding_Hal_1_1) +{ + PaddingTestImpl(android::nn::kPaddingValid); +} + +BOOST_AUTO_TEST_CASE(ConvSamePadding_Hal_1_1) +{ + PaddingTestImpl(android::nn::kPaddingSame); +} + +BOOST_AUTO_TEST_CASE(ConvValidPaddingFp16Flag_Hal_1_1) +{ + PaddingTestImpl(android::nn::kPaddingValid, true); +} + +BOOST_AUTO_TEST_CASE(ConvSamePaddingFp16Flag_Hal_1_1) +{ + PaddingTestImpl(android::nn::kPaddingSame, true); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/Android.mk b/test/Android.mk index cab874c3..c38b92e8 100644 --- a/test/Android.mk +++ b/test/Android.mk @@ -44,10 +44,10 @@ LOCAL_CFLAGS+= \ endif # PLATFORM_VERSION == 9 LOCAL_SRC_FILES := \ + 1.0/Convolution2D.cpp \ Tests.cpp \ UtilsTests.cpp \ Concurrent.cpp \ - Convolution2D.cpp \ FullyConnected.cpp \ GenericLayerTests.cpp \ DriverTestHelpers.cpp \ @@ -118,10 +118,11 @@ LOCAL_CFLAGS := \ -DARMNN_ANDROID_NN_V1_1 LOCAL_SRC_FILES := \ + 1.0/Convolution2D.cpp \ + 1.1/Convolution2D.cpp \ Tests.cpp \ UtilsTests.cpp \ Concurrent.cpp \ - Convolution2D.cpp \ FullyConnected.cpp \ GenericLayerTests.cpp \ DriverTestHelpers.cpp \ diff --git a/test/Convolution2D.cpp b/test/Convolution2D.cpp deleted file mode 100644 index 3f097b86..00000000 --- a/test/Convolution2D.cpp +++ /dev/null @@ -1,110 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// -#include "DriverTestHelpers.hpp" -#include -#include - -#include - -BOOST_AUTO_TEST_SUITE(Convolution2DTests) - -using namespace android::hardware; -using namespace driverTestHelpers; -using namespace armnn_driver; - -namespace -{ - -void PaddingTestImpl(android::nn::PaddingScheme paddingScheme) -{ - auto driver = std::make_unique(DriverOptions(armnn::Compute::CpuRef)); - neuralnetworks::V1_0::Model model = {}; - - uint32_t outSize = paddingScheme == android::nn::kPaddingSame ? 2 : 1; - - // add operands - float weightValue[] = {1, -1, 0, 1}; - float biasValue[] = {0}; - - AddInputOperand(model, hidl_vec{1, 2, 3, 1}); - AddTensorOperand(model, hidl_vec{1, 2, 2, 1}, weightValue); - AddTensorOperand(model, hidl_vec{1}, biasValue); - AddIntOperand(model, (int32_t)paddingScheme); // padding - AddIntOperand(model, 2); // stride x - AddIntOperand(model, 2); // stride y - AddIntOperand(model, 0); // no activation - AddOutputOperand(model, hidl_vec{1, 1, outSize, 1}); - - // make the convolution operation - model.operations.resize(1); - model.operations[0].type = neuralnetworks::V1_0::OperationType::CONV_2D; - model.operations[0].inputs = hidl_vec{0, 1, 2, 3, 4, 5, 6}; - model.operations[0].outputs = hidl_vec{7}; - - // make the prepared model - android::sp preparedModel = PrepareModel(model, *driver); - - // construct the request - DataLocation inloc = {}; - inloc.poolIndex = 0; - inloc.offset = 0; - inloc.length = 6 * sizeof(float); - RequestArgument input = {}; - input.location = inloc; - input.dimensions = hidl_vec{}; - - DataLocation outloc = {}; - outloc.poolIndex = 1; - outloc.offset = 0; - outloc.length = outSize * sizeof(float); - RequestArgument output = {}; - output.location = outloc; - output.dimensions = hidl_vec{}; - - Request request = {}; - request.inputs = hidl_vec{input}; - request.outputs = hidl_vec{output}; - - - // set the input data (matching source test) - float indata[] = {4, 1, 0, 3, -1, 2}; - AddPoolAndSetData(6, request, indata); - - // add memory for the output - android::sp outMemory = AddPoolAndGetData(outSize, request); - float* outdata = static_cast(static_cast(outMemory->getPointer())); - - // run the execution - Execute(preparedModel, request); - - // check the result - if (paddingScheme == android::nn::kPaddingValid) - { - BOOST_TEST(outdata[0] == 2); - } - else if (paddingScheme == android::nn::kPaddingSame) - { - BOOST_TEST(outdata[0] == 2); - BOOST_TEST(outdata[1] == 0); - } - else - { - BOOST_TEST(false); - } -} - -} // namespace - -BOOST_AUTO_TEST_CASE(ConvValidPadding) -{ - PaddingTestImpl(android::nn::kPaddingValid); -} - -BOOST_AUTO_TEST_CASE(ConvSamePadding) -{ - PaddingTestImpl(android::nn::kPaddingSame); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/test/Convolution2D.hpp b/test/Convolution2D.hpp new file mode 100644 index 00000000..ff417d96 --- /dev/null +++ b/test/Convolution2D.hpp @@ -0,0 +1,129 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "DriverTestHelpers.hpp" + +#include +#include + +#include + +BOOST_AUTO_TEST_SUITE(Convolution2DTests) + +using namespace android::hardware; +using namespace driverTestHelpers; +using namespace armnn_driver; + +namespace driverTestHelpers +{ + +void SetModelFp16Flag(V1_0::Model& model, bool fp16Enabled); + +#ifdef ARMNN_ANDROID_NN_V1_1 +void SetModelFp16Flag(V1_1::Model& model, bool fp16Enabled); +#endif + +template +void PaddingTestImpl(android::nn::PaddingScheme paddingScheme, bool fp16Enabled = false) +{ + using HalModel = typename HalPolicy::Model; + using HalOperationType = typename HalPolicy::OperationType; + + auto driver = std::make_unique(DriverOptions(armnn::Compute::GpuAcc, fp16Enabled)); + HalModel model = {}; + + uint32_t outSize = paddingScheme == android::nn::kPaddingSame ? 2 : 1; + + // add operands + float weightValue[] = {1.f, -1.f, 0.f, 1.f}; + float biasValue[] = {0.f}; + + AddInputOperand(model, hidl_vec{1, 2, 3, 1}); + AddTensorOperand(model, hidl_vec{1, 2, 2, 1}, weightValue); + AddTensorOperand(model, hidl_vec{1}, biasValue); + AddIntOperand(model, (int32_t)paddingScheme); // padding + AddIntOperand(model, 2); // stride x + AddIntOperand(model, 2); // stride y + AddIntOperand(model, 0); // no activation + AddOutputOperand(model, hidl_vec{1, 1, outSize, 1}); + + // make the convolution operation + model.operations.resize(1); + model.operations[0].type = HalOperationType::CONV_2D; + model.operations[0].inputs = hidl_vec{0, 1, 2, 3, 4, 5, 6}; + model.operations[0].outputs = hidl_vec{7}; + + // make the prepared model + SetModelFp16Flag(model, fp16Enabled); + android::sp preparedModel = PrepareModel(model, *driver); + + // construct the request + DataLocation inloc = {}; + inloc.poolIndex = 0; + inloc.offset = 0; + inloc.length = 6 * sizeof(float); + RequestArgument input = {}; + input.location = inloc; + input.dimensions = hidl_vec{}; + + DataLocation outloc = {}; + outloc.poolIndex = 1; + outloc.offset = 0; + outloc.length = outSize * sizeof(float); + RequestArgument output = {}; + output.location = outloc; + output.dimensions = hidl_vec{}; + + Request request = {}; + request.inputs = hidl_vec{input}; + request.outputs = hidl_vec{output}; + + // set the input data (matching source test) + float indata[] = {1024.25f, 1.f, 0.f, 3.f, -1, -1024.25f}; + AddPoolAndSetData(6, request, indata); + + // add memory for the output + android::sp outMemory = AddPoolAndGetData(outSize, request); + float* outdata = reinterpret_cast(static_cast(outMemory->getPointer())); + + // run the execution + Execute(preparedModel, request); + + // check the result + switch (paddingScheme) + { + case android::nn::kPaddingValid: + if (fp16Enabled) + { + BOOST_TEST(outdata[0] == 1022.f); + } + else + { + BOOST_TEST(outdata[0] == 1022.25f); + } + break; + case android::nn::kPaddingSame: + if (fp16Enabled) + { + BOOST_TEST(outdata[0] == 1022.f); + BOOST_TEST(outdata[1] == 0.f); + } + else + { + BOOST_TEST(outdata[0] == 1022.25f); + BOOST_TEST(outdata[1] == 0.f); + } + break; + default: + BOOST_TEST(false); + break; + } +} + +} // namespace driverTestHelpers + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/DriverTestHelpers.cpp b/test/DriverTestHelpers.cpp index 11154912..ded24592 100644 --- a/test/DriverTestHelpers.cpp +++ b/test/DriverTestHelpers.cpp @@ -109,70 +109,32 @@ void AddPoolAndSetData(uint32_t size, Request& request, const float* data) memcpy(dst, data, size * sizeof(float)); } -void AddOperand(neuralnetworks::V1_0::Model& model, const Operand& op) -{ - model.operands.resize(model.operands.size() + 1); - model.operands[model.operands.size() - 1] = op; -} - -void AddIntOperand(neuralnetworks::V1_0::Model& model, int32_t value) -{ - DataLocation location = {}; - location.offset = model.operandValues.size(); - location.length = sizeof(int32_t); - - Operand op = {}; - op.type = OperandType::INT32; - op.dimensions = hidl_vec{}; - op.lifetime = OperandLifeTime::CONSTANT_COPY; - op.location = location; - - model.operandValues.resize(model.operandValues.size() + location.length); - *reinterpret_cast(&model.operandValues[location.offset]) = value; - - AddOperand(model, op); -} - -void AddInputOperand(neuralnetworks::V1_0::Model& model, - hidl_vec dimensions, - neuralnetworks::V1_0::OperandType operandType) -{ - Operand op = {}; - op.type = operandType; - op.dimensions = dimensions; - op.lifetime = OperandLifeTime::MODEL_INPUT; - - AddOperand(model, op); - - model.inputIndexes.resize(model.inputIndexes.size() + 1); - model.inputIndexes[model.inputIndexes.size() - 1] = model.operands.size() - 1; -} - -void AddOutputOperand(neuralnetworks::V1_0::Model& model, - hidl_vec dimensions, - neuralnetworks::V1_0::OperandType operandType) +android::sp PrepareModelWithStatus(const neuralnetworks::V1_0::Model& model, + armnn_driver::ArmnnDriver& driver, + ErrorStatus& prepareStatus, + ErrorStatus expectedStatus) { - Operand op = {}; - op.type = operandType; - op.scale = operandType == neuralnetworks::V1_0::OperandType::TENSOR_QUANT8_ASYMM ? 1.f / 255.f : 0.f; - op.dimensions = dimensions; - op.lifetime = OperandLifeTime::MODEL_OUTPUT; - - AddOperand(model, op); + android::sp cb(new PreparedModelCallback()); + driver.prepareModel(model, cb); - model.outputIndexes.resize(model.outputIndexes.size() + 1); - model.outputIndexes[model.outputIndexes.size() - 1] = model.operands.size() - 1; + prepareStatus = cb->GetErrorStatus(); + BOOST_TEST(prepareStatus == expectedStatus); + if (expectedStatus == ErrorStatus::NONE) + { + BOOST_TEST((cb->GetPreparedModel() != nullptr)); + } + return cb->GetPreparedModel(); } +#if defined(ARMNN_ANDROID_NN_V1_1) // Using ::android::hardware::neuralnetworks::V1_1. -android::sp PrepareModelWithStatus(const neuralnetworks::V1_0::Model& model, +android::sp PrepareModelWithStatus(const neuralnetworks::V1_1::Model& model, armnn_driver::ArmnnDriver& driver, - ErrorStatus & prepareStatus, + ErrorStatus& prepareStatus, ErrorStatus expectedStatus) { - android::sp cb(new PreparedModelCallback()); - driver.prepareModel(model, cb); + driver.prepareModel_1_1(model, neuralnetworks::V1_1::ExecutionPreference::LOW_POWER, cb); prepareStatus = cb->GetErrorStatus(); BOOST_TEST(prepareStatus == expectedStatus); @@ -183,12 +145,7 @@ android::sp PrepareModelWithStatus(const neuralnetworks::V1_0::M return cb->GetPreparedModel(); } -android::sp PrepareModel(const neuralnetworks::V1_0::Model& model, - armnn_driver::ArmnnDriver& driver) -{ - ErrorStatus prepareStatus = ErrorStatus::NONE; - return PrepareModelWithStatus(model, driver, prepareStatus); -} +#endif ErrorStatus Execute(android::sp preparedModel, const Request& request, diff --git a/test/DriverTestHelpers.hpp b/test/DriverTestHelpers.hpp index 03dbeb98..ce09ee68 100644 --- a/test/DriverTestHelpers.hpp +++ b/test/DriverTestHelpers.hpp @@ -10,6 +10,7 @@ #include "../ArmnnDriver.hpp" #include +#include namespace android { @@ -72,9 +73,31 @@ android::sp AddPoolAndGetData(uint32_t size, Request& request); void AddPoolAndSetData(uint32_t size, Request& request, const float* data); -void AddOperand(::android::hardware::neuralnetworks::V1_0::Model& model, const Operand& op); +template +void AddOperand(HalModel& model, const Operand& op) +{ + model.operands.resize(model.operands.size() + 1); + model.operands[model.operands.size() - 1] = op; +} + +template +void AddIntOperand(HalModel& model, int32_t value) +{ + DataLocation location = {}; + location.offset = model.operandValues.size(); + location.length = sizeof(int32_t); + + Operand op = {}; + op.type = OperandType::INT32; + op.dimensions = hidl_vec{}; + op.lifetime = OperandLifeTime::CONSTANT_COPY; + op.location = location; -void AddIntOperand(::android::hardware::neuralnetworks::V1_0::Model& model, int32_t value); + model.operandValues.resize(model.operandValues.size() + location.length); + *reinterpret_cast(&model.operandValues[location.offset]) = value; + + AddOperand(model, op); +} template OperandType TypeToOperandType(); @@ -85,8 +108,8 @@ OperandType TypeToOperandType(); template<> OperandType TypeToOperandType(); -template -void AddTensorOperand(::android::hardware::neuralnetworks::V1_0::Model& model, +template +void AddTensorOperand(HalModel& model, hidl_vec dimensions, T* values, OperandType operandType = OperandType::TENSOR_FLOAT32) @@ -113,28 +136,67 @@ void AddTensorOperand(::android::hardware::neuralnetworks::V1_0::Model& model, *(reinterpret_cast(&model.operandValues[location.offset]) + i) = values[i]; } - AddOperand(model, op); + AddOperand(model, op); } -void AddInputOperand(::android::hardware::neuralnetworks::V1_0::Model& model, +template +void AddInputOperand(HalModel& model, hidl_vec dimensions, - ::android::hardware::neuralnetworks::V1_0::OperandType operandType = OperandType::TENSOR_FLOAT32); + OperandType operandType = OperandType::TENSOR_FLOAT32) +{ + Operand op = {}; + op.type = operandType; + op.dimensions = dimensions; + op.lifetime = OperandLifeTime::MODEL_INPUT; -void AddOutputOperand(::android::hardware::neuralnetworks::V1_0::Model& model, + AddOperand(model, op); + + model.inputIndexes.resize(model.inputIndexes.size() + 1); + model.inputIndexes[model.inputIndexes.size() - 1] = model.operands.size() - 1; +} + +template +void AddOutputOperand(HalModel& model, hidl_vec dimensions, - ::android::hardware::neuralnetworks::V1_0::OperandType operandType = OperandType::TENSOR_FLOAT32); + OperandType operandType = OperandType::TENSOR_FLOAT32) +{ + Operand op = {}; + op.type = operandType; + op.scale = operandType == OperandType::TENSOR_QUANT8_ASYMM ? 1.f / 255.f : 0.f; + op.dimensions = dimensions; + op.lifetime = OperandLifeTime::MODEL_OUTPUT; -android::sp PrepareModel(const ::android::hardware::neuralnetworks::V1_0::Model& model, - armnn_driver::ArmnnDriver& driver); + AddOperand(model, op); + + model.outputIndexes.resize(model.outputIndexes.size() + 1); + model.outputIndexes[model.outputIndexes.size() - 1] = model.operands.size() - 1; +} android::sp PrepareModelWithStatus(const ::android::hardware::neuralnetworks::V1_0::Model& model, armnn_driver::ArmnnDriver& driver, - ErrorStatus & prepareStatus, - ErrorStatus expectedStatus=ErrorStatus::NONE); + ErrorStatus& prepareStatus, + ErrorStatus expectedStatus = ErrorStatus::NONE); + +#if defined(ARMNN_ANDROID_NN_V1_1) // Using ::android::hardware::neuralnetworks::V1_1. + +android::sp PrepareModelWithStatus(const ::android::hardware::neuralnetworks::V1_1::Model& model, + armnn_driver::ArmnnDriver& driver, + ErrorStatus& prepareStatus, + ErrorStatus expectedStatus = ErrorStatus::NONE); + +#endif + +template +android::sp PrepareModel(const HalModel& model, + armnn_driver::ArmnnDriver& driver) +{ + ErrorStatus prepareStatus = ErrorStatus::NONE; + return PrepareModelWithStatus(model, driver, prepareStatus); +} ErrorStatus Execute(android::sp preparedModel, const Request& request, - ErrorStatus expectedStatus=ErrorStatus::NONE); + ErrorStatus expectedStatus = ErrorStatus::NONE); android::sp ExecuteNoWait(android::sp preparedModel, const Request& request); diff --git a/test/FullyConnected.cpp b/test/FullyConnected.cpp index 85e58d02..5c204ca7 100644 --- a/test/FullyConnected.cpp +++ b/test/FullyConnected.cpp @@ -19,7 +19,7 @@ BOOST_AUTO_TEST_CASE(FullyConnected) // but that uses slightly weird dimensions which I don't think we need to support for now auto driver = std::make_unique(DriverOptions(armnn::Compute::CpuRef)); - neuralnetworks::V1_0::Model model = {}; + V1_0::Model model = {}; // add operands int32_t actValue = 0; @@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(FullyConnected) // make the fully connected operation model.operations.resize(1); - model.operations[0].type = neuralnetworks::V1_0::OperationType::FULLY_CONNECTED; + model.operations[0].type = V1_0::OperationType::FULLY_CONNECTED; model.operations[0].inputs = hidl_vec{0, 1, 2, 3}; model.operations[0].outputs = hidl_vec{4}; @@ -90,7 +90,7 @@ BOOST_AUTO_TEST_CASE(TestFullyConnected4dInput) sup = supported; }; - neuralnetworks::V1_0::Model model = {}; + V1_0::Model model = {}; // operands int32_t actValue = 0; @@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE(TestFullyConnected4dInput) model.operations.resize(1); - model.operations[0].type = neuralnetworks::V1_0::OperationType::FULLY_CONNECTED; + model.operations[0].type = V1_0::OperationType::FULLY_CONNECTED; model.operations[0].inputs = hidl_vec{0,1,2,3}; model.operations[0].outputs = hidl_vec{4}; @@ -177,7 +177,7 @@ BOOST_AUTO_TEST_CASE(TestFullyConnected4dInputReshape) sup = supported; }; - neuralnetworks::V1_0::Model model = {}; + V1_0::Model model = {}; // operands int32_t actValue = 0; @@ -200,7 +200,7 @@ BOOST_AUTO_TEST_CASE(TestFullyConnected4dInputReshape) model.operations.resize(1); - model.operations[0].type = neuralnetworks::V1_0::OperationType::FULLY_CONNECTED; + model.operations[0].type = V1_0::OperationType::FULLY_CONNECTED; model.operations[0].inputs = hidl_vec{0,1,2,3}; model.operations[0].outputs = hidl_vec{4}; diff --git a/test/GenericLayerTests.cpp b/test/GenericLayerTests.cpp index fd58a584..c66854f4 100644 --- a/test/GenericLayerTests.cpp +++ b/test/GenericLayerTests.cpp @@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(UnsupportedLayerContinueOnFailure) // during mem pool mapping we properly report an error to the framework via a callback BOOST_AUTO_TEST_CASE(ModelToINetworkConverterMemPoolFail) { - auto driver = std::make_unique(armnn::Compute::CpuRef); + auto driver = std::make_unique(DriverOptions(armnn::Compute::CpuRef)); ErrorStatus errorStatus; std::vector supported; -- cgit v1.2.1