// // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // #pragma once #include #include #include #include #include #include #include std::vector ParseArray(std::istream& stream); /// Splits a given string at every accurance of delimiter into a vector of string std::vector ParseStringList(const std::string& inputString, const char* delimiter); struct TensorPrinter { TensorPrinter(const std::string& binding, const armnn::TensorInfo& info, const std::string& outputTensorFile, bool dequantizeOutput, bool printToConsole = true); void operator()(const std::vector& values); void operator()(const std::vector& values); void operator()(const std::vector& values); void operator()(const std::vector& values); private: template void ForEachValue(const Container& c, Delegate delegate); template void WriteToFile(const std::vector& values); std::string m_OutputBinding; float m_Scale; int m_Offset; std::string m_OutputTensorFile; bool m_DequantizeOutput; bool m_PrintToConsole; }; using QuantizationParams = std::pair; void PopulateTensorWithData(armnnUtils::TContainer& tensorData, unsigned int numElements, const std::string& dataTypeStr, const armnn::Optional& qParams, const armnn::Optional& dataFile); /** * Verifies if the given string is a valid path. Reports invalid paths to std::err. * @param file string - A string containing the path to check * @param expectFile bool - If true, checks for a regular file. * @return bool - True if given string is a valid path., false otherwise. * */ bool ValidatePath(const std::string& file, const bool expectFile); /** * Verifies if a given vector of strings are valid paths. Reports invalid paths to std::err. * @param fileVec vector of string - A vector of string containing the paths to check * @param expectFile bool - If true, checks for a regular file. * @return bool - True if all given strings are valid paths., false otherwise. * */ bool ValidatePaths(const std::vector& fileVec, const bool expectFile); template std::vector ParseArrayImpl(std::istream& stream, TParseElementFunc parseElementFunc, const char* chars = "\t ,:") { std::vector result; // Processes line-by-line. std::string line; while (std::getline(stream, line)) { std::vector tokens = armnn::stringUtils::StringTokenizer(line, chars); for (const std::string& token : tokens) { if (!token.empty()) // See https://stackoverflow.com/questions/10437406/ { try { result.push_back(parseElementFunc(token)); } catch (const std::exception&) { ARMNN_LOG(error) << "'" << token << "' is not a valid number. It has been ignored."; } } } } return result; } template void PopulateTensorWithDataGeneric(std::vector& tensorData, unsigned int numElements, const armnn::Optional& dataFile, TParseElementFunc parseFunction) { const bool readFromFile = dataFile.has_value() && !dataFile.value().empty(); std::ifstream inputTensorFile; if (readFromFile) { inputTensorFile = std::ifstream(dataFile.value()); } tensorData = readFromFile ? ParseArrayImpl(inputTensorFile, parseFunction) : std::vector(numElements, static_cast(0)); }