blob: 0abda4f8ee659af8cfd1d29326b658ca6a7bbfd6 [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
Sadik Armagana9c2ce12020-07-14 10:02:22 +01002// 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
Jan Eilers45274902020-10-15 18:34:43 +01008#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>
Francis Murtaghbee4bc92019-06-18 12:30:37 +010012
James Ward6d9f5c52020-09-28 11:56:35 +010013#include <mapbox/variant.hpp>
Francis Murtaghbee4bc92019-06-18 12:30:37 +010014
15#include <iostream>
Finn Williams56870182020-11-20 13:57:53 +000016#include <fstream>
Francis Murtaghbee4bc92019-06-18 12:30:37 +010017
Francis Murtaghbee4bc92019-06-18 12:30:37 +010018
Jan Eilers45274902020-10-15 18:34:43 +010019std::vector<unsigned int> ParseArray(std::istream& stream);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010020
Jan Eilers45274902020-10-15 18:34:43 +010021/// Splits a given string at every accurance of delimiter into a vector of string
22std::vector<std::string> ParseStringList(const std::string& inputString, const char* delimiter);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010023
James Ward6d9f5c52020-09-28 11:56:35 +010024struct TensorPrinter
Francis Murtaghbee4bc92019-06-18 12:30:37 +010025{
Georgios Pinitas50311ba2020-02-18 13:25:23 +000026 TensorPrinter(const std::string& binding,
27 const armnn::TensorInfo& info,
28 const std::string& outputTensorFile,
Jan Eilers284b5d12021-09-07 12:46:15 +010029 bool dequantizeOutput,
30 bool printToConsole = true);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010031
Jan Eilers45274902020-10-15 18:34:43 +010032 void operator()(const std::vector<float>& values);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010033
Jan Eilers45274902020-10-15 18:34:43 +010034 void operator()(const std::vector<uint8_t>& values);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010035
Jan Eilers45274902020-10-15 18:34:43 +010036 void operator()(const std::vector<int>& values);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010037
Finn Williamsf806c4d2021-02-22 15:13:12 +000038 void operator()(const std::vector<int8_t>& values);
39
Francis Murtaghbee4bc92019-06-18 12:30:37 +010040private:
41 template<typename Container, typename Delegate>
Jan Eilers45274902020-10-15 18:34:43 +010042 void ForEachValue(const Container& c, Delegate delegate);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010043
Sadik Armagan77086282019-09-02 11:46:28 +010044 template<typename T>
Jan Eilers45274902020-10-15 18:34:43 +010045 void WriteToFile(const std::vector<T>& values);
Sadik Armagan77086282019-09-02 11:46:28 +010046
Francis Murtaghbee4bc92019-06-18 12:30:37 +010047 std::string m_OutputBinding;
Jan Eilers45274902020-10-15 18:34:43 +010048 float m_Scale;
49 int m_Offset;
Sadik Armagan77086282019-09-02 11:46:28 +010050 std::string m_OutputTensorFile;
Jan Eilers45274902020-10-15 18:34:43 +010051 bool m_DequantizeOutput;
Jan Eilers284b5d12021-09-07 12:46:15 +010052 bool m_PrintToConsole;
Francis Murtaghbee4bc92019-06-18 12:30:37 +010053};
54
Finn Williamsf806c4d2021-02-22 15:13:12 +000055using TContainer =
56 mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>, std::vector<int8_t>>;
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010057using QuantizationParams = std::pair<float, int32_t>;
58
59void PopulateTensorWithData(TContainer& tensorData,
60 unsigned int numElements,
61 const std::string& dataTypeStr,
62 const armnn::Optional<QuantizationParams>& qParams,
Jan Eilers45274902020-10-15 18:34:43 +010063 const armnn::Optional<std::string>& dataFile);
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010064
Jan Eilers45274902020-10-15 18:34:43 +010065/**
66 * Verifies if the given string is a valid path. Reports invalid paths to std::err.
67 * @param file string - A string containing the path to check
68 * @param expectFile bool - If true, checks for a regular file.
69 * @return bool - True if given string is a valid path., false otherwise.
70 * */
71bool ValidatePath(const std::string& file, const bool expectFile);
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010072
Jan Eilers45274902020-10-15 18:34:43 +010073/**
74 * Verifies if a given vector of strings are valid paths. Reports invalid paths to std::err.
75 * @param fileVec vector of string - A vector of string containing the paths to check
76 * @param expectFile bool - If true, checks for a regular file.
77 * @return bool - True if all given strings are valid paths., false otherwise.
78 * */
Finn Williams56870182020-11-20 13:57:53 +000079bool ValidatePaths(const std::vector<std::string>& fileVec, const bool expectFile);
80
81template<typename T, typename TParseElementFunc>
82std::vector<T> ParseArrayImpl(std::istream& stream, TParseElementFunc parseElementFunc, const char* chars = "\t ,:")
83{
84 std::vector<T> result;
85 // Processes line-by-line.
86 std::string line;
87 while (std::getline(stream, line))
88 {
89 std::vector<std::string> tokens = armnn::stringUtils::StringTokenizer(line, chars);
90 for (const std::string& token : tokens)
91 {
92 if (!token.empty()) // See https://stackoverflow.com/questions/10437406/
93 {
94 try
95 {
96 result.push_back(parseElementFunc(token));
97 }
98 catch (const std::exception&)
99 {
100 ARMNN_LOG(error) << "'" << token << "' is not a valid number. It has been ignored.";
101 }
102 }
103 }
104 }
105
106 return result;
107}
108
109template <typename T, typename TParseElementFunc>
110void PopulateTensorWithDataGeneric(std::vector<T>& tensorData,
111 unsigned int numElements,
112 const armnn::Optional<std::string>& dataFile,
113 TParseElementFunc parseFunction)
114{
115 const bool readFromFile = dataFile.has_value() && !dataFile.value().empty();
116
117 std::ifstream inputTensorFile;
118 if (readFromFile)
119 {
120 inputTensorFile = std::ifstream(dataFile.value());
121 }
122
123 tensorData = readFromFile ?
124 ParseArrayImpl<T>(inputTensorFile, parseFunction) :
125 std::vector<T>(numElements, static_cast<T>(0));
126}