Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "NetworkExecutionUtils.hpp" |
| 7 | |
Rob Hughes | 9542f90 | 2021-07-14 09:48:54 +0100 | [diff] [blame] | 8 | #include <armnnUtils/Filesystem.hpp> |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 9 | #include <iterator> |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 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 | |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 17 | bool CheckInferenceTimeThreshold(const std::chrono::duration<double, std::milli>& duration, |
| 18 | const double& thresholdTime) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 19 | { |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 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) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 24 | { |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 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) |
Jan Eilers | 284b5d1 | 2021-09-07 12:46:15 +0100 | [diff] [blame] | 31 | { |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 32 | std::string errorMessage = "Elapsed inference time is greater than provided threshold time."; |
| 33 | ARMNN_LOG(fatal) << errorMessage; |
| 34 | return false; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 35 | } |
| 36 | } |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 37 | return true; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 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 | |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 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 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 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 | |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 75 | void LogAndThrow(std::string eMsg) |
| 76 | { |
| 77 | ARMNN_LOG(error) << eMsg; |
| 78 | throw armnn::Exception(eMsg); |
| 79 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 80 | |
Colm Donelan | d047262 | 2023-03-06 12:34:54 +0000 | [diff] [blame] | 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 |
| 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 | } |