blob: bc2868ab35c46e792f330c24e1c5dc43f4bb757b [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +00002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
Francis Murtaghbee4bc92019-06-18 12:30:37 +01003// SPDX-License-Identifier: MIT
4//
Francis Murtaghbee4bc92019-06-18 12:30:37 +01005
Jan Eilers45274902020-10-15 18:34:43 +01006#pragma once
7
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +00008#include <armnn/IRuntime.hpp>
9#include <armnn/Types.hpp>
Finn Williams56870182020-11-20 13:57:53 +000010#include <armnn/Logging.hpp>
11#include <armnn/utility/StringUtils.hpp>
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +000012#include <armnnUtils/TContainer.hpp>
Francis Murtaghbee4bc92019-06-18 12:30:37 +010013
14#include <iostream>
Finn Williams56870182020-11-20 13:57:53 +000015#include <fstream>
Francis Murtaghbee4bc92019-06-18 12:30:37 +010016
Francis Murtaghbee4bc92019-06-18 12:30:37 +010017
Jan Eilers45274902020-10-15 18:34:43 +010018std::vector<unsigned int> ParseArray(std::istream& stream);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010019
Jan Eilers45274902020-10-15 18:34:43 +010020/// Splits a given string at every accurance of delimiter into a vector of string
21std::vector<std::string> ParseStringList(const std::string& inputString, const char* delimiter);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010022
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +000023struct TensorPrinter
Francis Murtaghbee4bc92019-06-18 12:30:37 +010024{
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +000025 TensorPrinter(const std::string& binding,
26 const armnn::TensorInfo& info,
27 const std::string& outputTensorFile,
28 bool dequantizeOutput,
29 bool printToConsole = true);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010030
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +000031 void operator()(const std::vector<float>& values);
32
33 void operator()(const std::vector<uint8_t>& values);
34
35 void operator()(const std::vector<int>& values);
36
37 void operator()(const std::vector<int8_t>& values);
38
39private:
40 template<typename Container, typename Delegate>
41 void ForEachValue(const Container& c, Delegate delegate);
42
43 template<typename T>
44 void WriteToFile(const std::vector<T>& values);
45
46 std::string m_OutputBinding;
47 float m_Scale;
48 int m_Offset;
49 std::string m_OutputTensorFile;
50 bool m_DequantizeOutput;
51 bool m_PrintToConsole;
52};
53
54using QuantizationParams = std::pair<float, int32_t>;
55
56void PopulateTensorWithData(armnnUtils::TContainer& tensorData,
57 unsigned int numElements,
58 const std::string& dataTypeStr,
59 const armnn::Optional<QuantizationParams>& qParams,
60 const armnn::Optional<std::string>& dataFile);
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010061
Jan Eilers45274902020-10-15 18:34:43 +010062/**
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 * */
68bool ValidatePath(const std::string& file, const bool expectFile);
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010069
Jan Eilers45274902020-10-15 18:34:43 +010070/**
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 Williams56870182020-11-20 13:57:53 +000076bool ValidatePaths(const std::vector<std::string>& fileVec, const bool expectFile);
77
78template<typename T, typename TParseElementFunc>
79std::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
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +0000106template <typename T, typename TParseElementFunc>
107void PopulateTensorWithDataGeneric(std::vector<T>& tensorData,
108 unsigned int numElements,
109 const armnn::Optional<std::string>& dataFile,
110 TParseElementFunc parseFunction)
Finn Williams56870182020-11-20 13:57:53 +0000111{
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +0000112 const bool readFromFile = dataFile.has_value() && !dataFile.value().empty();
Finn Williams56870182020-11-20 13:57:53 +0000113
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +0000114 std::ifstream inputTensorFile;
115 if (readFromFile)
Finn Williams56870182020-11-20 13:57:53 +0000116 {
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +0000117 inputTensorFile = std::ifstream(dataFile.value());
Finn Williams56870182020-11-20 13:57:53 +0000118 }
119
Nikhil Raj Arm1a7f0332022-07-05 09:29:18 +0000120 tensorData = readFromFile ?
121 ParseArrayImpl<T>(inputTensorFile, parseFunction) :
122 std::vector<T>(numElements, static_cast<T>(0));
123}