Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Sadik Armagan | a9c2ce1 | 2020-07-14 10:02:22 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 5 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 6 | #pragma once |
| 7 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 8 | #include <armnn/IRuntime.hpp> |
| 9 | #include <armnn/Types.hpp> |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 10 | #include <armnn/Logging.hpp> |
| 11 | #include <armnn/utility/StringUtils.hpp> |
Francis Murtagh | 40d2741 | 2021-10-28 11:11:35 +0100 | [diff] [blame] | 12 | #include <armnnUtils/TContainer.hpp> |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 13 | |
| 14 | #include <iostream> |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 15 | #include <fstream> |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 16 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 17 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 18 | std::vector<unsigned int> ParseArray(std::istream& stream); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 19 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 20 | /// Splits a given string at every accurance of delimiter into a vector of string |
| 21 | std::vector<std::string> ParseStringList(const std::string& inputString, const char* delimiter); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 22 | |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 23 | struct TensorPrinter |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 24 | { |
Georgios Pinitas | 50311ba | 2020-02-18 13:25:23 +0000 | [diff] [blame] | 25 | TensorPrinter(const std::string& binding, |
| 26 | const armnn::TensorInfo& info, |
| 27 | const std::string& outputTensorFile, |
Jan Eilers | 284b5d1 | 2021-09-07 12:46:15 +0100 | [diff] [blame] | 28 | bool dequantizeOutput, |
| 29 | bool printToConsole = true); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 30 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 31 | void operator()(const std::vector<float>& values); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 32 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 33 | void operator()(const std::vector<uint8_t>& values); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 34 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 35 | void operator()(const std::vector<int>& values); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 36 | |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 37 | void operator()(const std::vector<int8_t>& values); |
| 38 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 39 | private: |
| 40 | template<typename Container, typename Delegate> |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 41 | void ForEachValue(const Container& c, Delegate delegate); |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 42 | |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 43 | template<typename T> |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 44 | void WriteToFile(const std::vector<T>& values); |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 45 | |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 46 | std::string m_OutputBinding; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 47 | float m_Scale; |
| 48 | int m_Offset; |
Sadik Armagan | 7708628 | 2019-09-02 11:46:28 +0100 | [diff] [blame] | 49 | std::string m_OutputTensorFile; |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 50 | bool m_DequantizeOutput; |
Jan Eilers | 284b5d1 | 2021-09-07 12:46:15 +0100 | [diff] [blame] | 51 | bool m_PrintToConsole; |
Francis Murtagh | bee4bc9 | 2019-06-18 12:30:37 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 54 | using QuantizationParams = std::pair<float, int32_t>; |
| 55 | |
Francis Murtagh | 40d2741 | 2021-10-28 11:11:35 +0100 | [diff] [blame] | 56 | void PopulateTensorWithData(armnnUtils::TContainer& tensorData, |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 57 | unsigned int numElements, |
| 58 | const std::string& dataTypeStr, |
| 59 | const armnn::Optional<QuantizationParams>& qParams, |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 60 | const armnn::Optional<std::string>& dataFile); |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 61 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 62 | /** |
| 63 | * Verifies if the given string is a valid path. Reports invalid paths to std::err. |
| 64 | * @param file string - A string containing the path to check |
| 65 | * @param expectFile bool - If true, checks for a regular file. |
| 66 | * @return bool - True if given string is a valid path., false otherwise. |
| 67 | * */ |
| 68 | bool ValidatePath(const std::string& file, const bool expectFile); |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 69 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 70 | /** |
| 71 | * Verifies if a given vector of strings are valid paths. Reports invalid paths to std::err. |
| 72 | * @param fileVec vector of string - A vector of string containing the paths to check |
| 73 | * @param expectFile bool - If true, checks for a regular file. |
| 74 | * @return bool - True if all given strings are valid paths., false otherwise. |
| 75 | * */ |
Finn Williams | 5687018 | 2020-11-20 13:57:53 +0000 | [diff] [blame] | 76 | bool ValidatePaths(const std::vector<std::string>& fileVec, const bool expectFile); |
| 77 | |
| 78 | template<typename T, typename TParseElementFunc> |
| 79 | std::vector<T> ParseArrayImpl(std::istream& stream, TParseElementFunc parseElementFunc, const char* chars = "\t ,:") |
| 80 | { |
| 81 | std::vector<T> result; |
| 82 | // Processes line-by-line. |
| 83 | std::string line; |
| 84 | while (std::getline(stream, line)) |
| 85 | { |
| 86 | std::vector<std::string> tokens = armnn::stringUtils::StringTokenizer(line, chars); |
| 87 | for (const std::string& token : tokens) |
| 88 | { |
| 89 | if (!token.empty()) // See https://stackoverflow.com/questions/10437406/ |
| 90 | { |
| 91 | try |
| 92 | { |
| 93 | result.push_back(parseElementFunc(token)); |
| 94 | } |
| 95 | catch (const std::exception&) |
| 96 | { |
| 97 | ARMNN_LOG(error) << "'" << token << "' is not a valid number. It has been ignored."; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | template <typename T, typename TParseElementFunc> |
| 107 | void PopulateTensorWithDataGeneric(std::vector<T>& tensorData, |
| 108 | unsigned int numElements, |
| 109 | const armnn::Optional<std::string>& dataFile, |
| 110 | TParseElementFunc parseFunction) |
| 111 | { |
| 112 | const bool readFromFile = dataFile.has_value() && !dataFile.value().empty(); |
| 113 | |
| 114 | std::ifstream inputTensorFile; |
| 115 | if (readFromFile) |
| 116 | { |
| 117 | inputTensorFile = std::ifstream(dataFile.value()); |
| 118 | } |
| 119 | |
| 120 | tensorData = readFromFile ? |
| 121 | ParseArrayImpl<T>(inputTensorFile, parseFunction) : |
| 122 | std::vector<T>(numElements, static_cast<T>(0)); |
| 123 | } |