From 2d9956162dd002a41f7fb4fa6753195d33524c7f Mon Sep 17 00:00:00 2001 From: David Monahan Date: Mon, 1 Nov 2021 10:16:37 +0000 Subject: IVGCVSW-6359 Added support for Float16 (Half) to Execute Network * Allows the user to specify float16 as a datatype * Does not contain support for float16 on the TfLiteDelegate via ExecuteNetwork Signed-off-by: David Monahan Change-Id: Icba56feedab32662e2cf671cc46ada899cf40c6c --- include/armnnUtils/TContainer.hpp | 13 +++++++--- tests/ExecuteNetwork/ExecuteNetwork.cpp | 15 ++++++++++- .../ExecuteNetworkProgramOptions.cpp | 4 +-- tests/InferenceTest.inl | 8 ++++++ .../NetworkExecutionUtils.cpp | 29 ++++++++++++++++++++++ .../NetworkExecutionUtils.hpp | 2 ++ 6 files changed, 64 insertions(+), 7 deletions(-) diff --git a/include/armnnUtils/TContainer.hpp b/include/armnnUtils/TContainer.hpp index a55f9df488..d2a868ac18 100644 --- a/include/armnnUtils/TContainer.hpp +++ b/include/armnnUtils/TContainer.hpp @@ -6,15 +6,20 @@ #pragma once #include +#include #include namespace armnnUtils { -// Standard definition of TContainer used by ArmNN, use this definition or add alternative definitions here instead of -// defining your own. - using TContainer = - mapbox::util::variant, std::vector, std::vector, std::vector>; +// Standard declaration of TContainer used by ArmNN +// Changes to this declaration constitute an api/abi break, new types should be added as a separate declaration and +// merged on the next planned api/abi update. +using TContainer = mapbox::util::variant, + std::vector, + std::vector, + std::vector, + std::vector>; } // namespace armnnUtils diff --git a/tests/ExecuteNetwork/ExecuteNetwork.cpp b/tests/ExecuteNetwork/ExecuteNetwork.cpp index a0a08d31b0..0d5271158b 100644 --- a/tests/ExecuteNetwork/ExecuteNetwork.cpp +++ b/tests/ExecuteNetwork/ExecuteNetwork.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #if defined(ARMNN_SERIALIZER) #include "armnnDeserializer/IDeserializer.hpp" @@ -484,7 +485,7 @@ int MainImpl(const ExecuteNetworkParams& params, armnn::DataType type = model.GetOutputBindingInfo(outputIdx).second.GetDataType(); switch (type) { - // --output-type only supports float, int, qasymms8 or qasymmu8. + // --output-type only supports float, float16, int, qasymms8 or qasymmu8. case armnn::DataType::Float32: if (params.m_OutputTypes[outputIdx].compare("float") != 0) { @@ -493,6 +494,14 @@ int MainImpl(const ExecuteNetworkParams& params, ". This may cause unexpected problems or random failures."; } break; + case armnn::DataType::Float16: + if (params.m_OutputTypes[outputIdx].compare("float16") != 0) + { + ARMNN_LOG(warning) << "Model output index: " << outputIdx << " has data type Float16. The " << + "corresponding --output-type is " << params.m_OutputTypes[outputIdx] << + ". This may cause unexpected problems or random failures."; + } + break; case armnn::DataType::QAsymmU8: if (params.m_OutputTypes[outputIdx].compare("qasymmu8") != 0) { @@ -530,6 +539,10 @@ int MainImpl(const ExecuteNetworkParams& params, { outputDataContainers.push_back(std::vector(model.GetOutputSize(i))); } + else if (params.m_OutputTypes[i].compare("float16") == 0) + { + outputDataContainers.push_back(std::vector(model.GetOutputSize(i))); + } else if (params.m_OutputTypes[i].compare("int") == 0) { outputDataContainers.push_back(std::vector(model.GetOutputSize(i))); diff --git a/tests/ExecuteNetwork/ExecuteNetworkProgramOptions.cpp b/tests/ExecuteNetwork/ExecuteNetworkProgramOptions.cpp index 8ee66cf64b..25dbe91455 100644 --- a/tests/ExecuteNetwork/ExecuteNetworkProgramOptions.cpp +++ b/tests/ExecuteNetwork/ExecuteNetworkProgramOptions.cpp @@ -294,13 +294,13 @@ ProgramOptions::ProgramOptions() : m_CxxOptions{"ExecuteNetwork", ("y,input-type", "The type of the input tensors in the network separated by comma. " "If unset, defaults to \"float\" for all defined inputs. " - "Accepted values (float, int, qasymms8 or qasymmu8).", + "Accepted values (float, float16, int, qasymms8 or qasymmu8).", cxxopts::value()) ("z,output-type", "The type of the output tensors in the network separated by comma. " "If unset, defaults to \"float\" for all defined outputs. " - "Accepted values (float, int, qasymms8 or qasymmu8).", + "Accepted values (float, float16, int, qasymms8 or qasymmu8).", cxxopts::value()) ("T,tflite-executor", diff --git a/tests/InferenceTest.inl b/tests/InferenceTest.inl index b6087c5e5a..94dbfe78b8 100644 --- a/tests/InferenceTest.inl +++ b/tests/InferenceTest.inl @@ -67,6 +67,14 @@ struct ClassifierResultProcessor }); } + void operator()(const std::vector& values) + { + SortPredictions(values, [](armnn::Half value) + { + return value; + }); + } + void operator()(const std::vector& values) { SortPredictions(values, [](int8_t value) diff --git a/tests/NetworkExecutionUtils/NetworkExecutionUtils.cpp b/tests/NetworkExecutionUtils/NetworkExecutionUtils.cpp index 6c74aaa6ed..00ed55caaf 100644 --- a/tests/NetworkExecutionUtils/NetworkExecutionUtils.cpp +++ b/tests/NetworkExecutionUtils/NetworkExecutionUtils.cpp @@ -33,6 +33,15 @@ auto ParseDataArray(std::istream& stream) return ParseArrayImpl(stream, [](const std::string& s) { return std::stof(s); }); } +template<> +auto ParseDataArray(std::istream& stream) +{ + return ParseArrayImpl(stream, [](const std::string& s) + { + return armnn::Half(std::stof(s)); + }); +} + template<> auto ParseDataArray(std::istream& stream) { @@ -139,6 +148,20 @@ void TensorPrinter::operator()(const std::vector& values) WriteToFile(values); } +void TensorPrinter::operator()(const std::vector& values) +{ + if (m_PrintToConsole) + { + std::cout << m_OutputBinding << ": "; + ForEachValue(values, [](armnn::Half value) + { + printf("%f ", static_cast(value)); + }); + printf("\n"); + } + WriteToFile(values); +} + void TensorPrinter::operator()(const std::vector& values) { if(m_DequantizeOutput) @@ -261,6 +284,12 @@ void PopulateTensorWithData(armnnUtils::TContainer& tensorData, GenerateDummyTensorData(numElements); } } + else if (dataTypeStr.compare("float16") == 0) + { + tensorData = readFromFile ? + ParseDataArray(inputTensorFile) : + GenerateDummyTensorData(numElements); + } else if (dataTypeStr.compare("int") == 0) { tensorData = readFromFile ? diff --git a/tests/NetworkExecutionUtils/NetworkExecutionUtils.hpp b/tests/NetworkExecutionUtils/NetworkExecutionUtils.hpp index bc2868ab35..8cd5c5b310 100644 --- a/tests/NetworkExecutionUtils/NetworkExecutionUtils.hpp +++ b/tests/NetworkExecutionUtils/NetworkExecutionUtils.hpp @@ -36,6 +36,8 @@ struct TensorPrinter void operator()(const std::vector& values); + void operator()(const std::vector& values); + private: template void ForEachValue(const Container& c, Delegate delegate); -- cgit v1.2.1