blob: 6f9cdf87bcad397e84a0ed4f881f6998255f96b9 [file] [log] [blame]
Jan Eilers45274902020-10-15 18:34:43 +01001//
Teresa Charlin83b42912022-07-07 14:24:59 +01002// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
Jan Eilers45274902020-10-15 18:34:43 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "NetworkExecutionUtils.hpp"
7
Rob Hughes9542f902021-07-14 09:48:54 +01008#include <armnnUtils/Filesystem.hpp>
Teresa Charlin83b42912022-07-07 14:24:59 +01009#include <iterator>
Jan Eilers45274902020-10-15 18:34:43 +010010std::vector<std::string> ParseStringList(const std::string& inputString, const char* delimiter)
11{
12 std::stringstream stream(inputString);
13 return ParseArrayImpl<std::string>(stream, [](const std::string& s) {
14 return armnn::stringUtils::StringTrimCopy(s); }, delimiter);
15}
16
Teresa Charlin83b42912022-07-07 14:24:59 +010017bool CheckInferenceTimeThreshold(const std::chrono::duration<double, std::milli>& duration,
18 const double& thresholdTime)
Jan Eilers45274902020-10-15 18:34:43 +010019{
Teresa Charlin83b42912022-07-07 14:24:59 +010020 ARMNN_LOG(info) << "Inference time: " << std::setprecision(2)
21 << std::fixed << duration.count() << " ms\n";
22 // If thresholdTime == 0.0 (default), then it hasn't been supplied at command line
23 if (thresholdTime != 0.0)
Jan Eilers45274902020-10-15 18:34:43 +010024 {
Teresa Charlin83b42912022-07-07 14:24:59 +010025 ARMNN_LOG(info) << "Threshold time: " << std::setprecision(2)
26 << std::fixed << thresholdTime << " ms";
27 auto thresholdMinusInference = thresholdTime - duration.count();
28 ARMNN_LOG(info) << "Threshold time - Inference time: " << std::setprecision(2)
29 << std::fixed << thresholdMinusInference << " ms" << "\n";
30 if (thresholdMinusInference < 0)
Jan Eilers284b5d12021-09-07 12:46:15 +010031 {
Teresa Charlin83b42912022-07-07 14:24:59 +010032 std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
33 ARMNN_LOG(fatal) << errorMessage;
34 return false;
Jan Eilers45274902020-10-15 18:34:43 +010035 }
36 }
Teresa Charlin83b42912022-07-07 14:24:59 +010037 return true;
Jan Eilers45274902020-10-15 18:34:43 +010038}
39
40bool ValidatePath(const std::string& file, const bool expectFile)
41{
42 if (!fs::exists(file))
43 {
44 std::cerr << "Given file path '" << file << "' does not exist" << std::endl;
45 return false;
46 }
47 if (!fs::is_regular_file(file) && expectFile)
48 {
49 std::cerr << "Given file path '" << file << "' is not a regular file" << std::endl;
50 return false;
51 }
52 return true;
53}
54
Teresa Charlin83b42912022-07-07 14:24:59 +010055std::vector<unsigned int> ParseArray(std::istream& stream)
56{
57 return ParseArrayImpl<unsigned int>(
58 stream,
59 [](const std::string& s) { return armnn::numeric_cast<unsigned int>(std::stoi(s)); });
60}
61
Jan Eilers45274902020-10-15 18:34:43 +010062bool ValidatePaths(const std::vector<std::string>& fileVec, const bool expectFile)
63{
64 bool allPathsValid = true;
65 for (auto const& file : fileVec)
66 {
67 if(!ValidatePath(file, expectFile))
68 {
69 allPathsValid = false;
70 }
71 }
72 return allPathsValid;
73}
74
Teresa Charlin83b42912022-07-07 14:24:59 +010075void LogAndThrow(std::string eMsg)
76{
77 ARMNN_LOG(error) << eMsg;
78 throw armnn::Exception(eMsg);
79}
Jan Eilers45274902020-10-15 18:34:43 +010080
Colm Doneland0472622023-03-06 12:34:54 +000081/// Compute the root-mean-square error (RMSE) at a byte level between two tensors of the same size.
82/// @param expected
83/// @param actual
84/// @param size size of the tensor in bytes.
85/// @return float the RMSE
86double ComputeByteLevelRMSE(const void* expected, const void* actual, const size_t size)
87{
88 const uint8_t* byteExpected = reinterpret_cast<const uint8_t*>(expected);
89 const uint8_t* byteActual = reinterpret_cast<const uint8_t*>(actual);
90
91 double errorSum = 0;
92 for (unsigned int i = 0; i < size; i++)
93 {
94 int difference = byteExpected[i] - byteActual[i];
95 errorSum += std::pow(difference, 2);
96 }
97 return std::sqrt(errorSum/armnn::numeric_cast<double>(size));
98}