xref: /aosp_15_r20/external/armnn/tests/NetworkExecutionUtils/NetworkExecutionUtils.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "NetworkExecutionUtils.hpp"
7 
8 #include <armnnUtils/Filesystem.hpp>
9 #include <iterator>
ParseStringList(const std::string & inputString,const char * delimiter)10 std::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 
CheckInferenceTimeThreshold(const std::chrono::duration<double,std::milli> & duration,const double & thresholdTime)17 bool CheckInferenceTimeThreshold(const std::chrono::duration<double, std::milli>& duration,
18                                  const double& thresholdTime)
19 {
20     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)
24     {
25         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)
31         {
32             std::string errorMessage = "Elapsed inference time is greater than provided threshold time.";
33             ARMNN_LOG(fatal) << errorMessage;
34             return false;
35         }
36     }
37     return true;
38 }
39 
ValidatePath(const std::string & file,const bool expectFile)40 bool 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 
ParseArray(std::istream & stream)55 std::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 
ValidatePaths(const std::vector<std::string> & fileVec,const bool expectFile)62 bool 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 
LogAndThrow(std::string eMsg)75 void LogAndThrow(std::string eMsg)
76 {
77     ARMNN_LOG(error) << eMsg;
78     throw armnn::Exception(eMsg);
79 }
80 
81 /// 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
ComputeByteLevelRMSE(const void * expected,const void * actual,const size_t size)86 double 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 }
99