blob: 742f968a7a2858253305c203a906af0e62075a7e [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 Williams4f55a252020-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 Williams4f55a252020-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 Eilers45274902020-10-15 18:34:43 +010029 bool dequantizeOutput);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010030
Jan Eilers45274902020-10-15 18:34:43 +010031 void operator()(const std::vector<float>& values);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010032
Jan Eilers45274902020-10-15 18:34:43 +010033 void operator()(const std::vector<uint8_t>& values);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010034
Jan Eilers45274902020-10-15 18:34:43 +010035 void operator()(const std::vector<int>& values);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010036
37private:
38 template<typename Container, typename Delegate>
Jan Eilers45274902020-10-15 18:34:43 +010039 void ForEachValue(const Container& c, Delegate delegate);
Francis Murtaghbee4bc92019-06-18 12:30:37 +010040
Sadik Armagan77086282019-09-02 11:46:28 +010041 template<typename T>
Jan Eilers45274902020-10-15 18:34:43 +010042 void WriteToFile(const std::vector<T>& values);
Sadik Armagan77086282019-09-02 11:46:28 +010043
Francis Murtaghbee4bc92019-06-18 12:30:37 +010044 std::string m_OutputBinding;
Jan Eilers45274902020-10-15 18:34:43 +010045 float m_Scale;
46 int m_Offset;
Sadik Armagan77086282019-09-02 11:46:28 +010047 std::string m_OutputTensorFile;
Jan Eilers45274902020-10-15 18:34:43 +010048 bool m_DequantizeOutput;
Francis Murtaghbee4bc92019-06-18 12:30:37 +010049};
50
James Ward6d9f5c52020-09-28 11:56:35 +010051using TContainer = mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>;
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010052using QuantizationParams = std::pair<float, int32_t>;
53
54void PopulateTensorWithData(TContainer& tensorData,
55 unsigned int numElements,
56 const std::string& dataTypeStr,
57 const armnn::Optional<QuantizationParams>& qParams,
Jan Eilers45274902020-10-15 18:34:43 +010058 const armnn::Optional<std::string>& dataFile);
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010059
Jan Eilers45274902020-10-15 18:34:43 +010060/**
61 * Verifies if the given string is a valid path. Reports invalid paths to std::err.
62 * @param file string - A string containing the path to check
63 * @param expectFile bool - If true, checks for a regular file.
64 * @return bool - True if given string is a valid path., false otherwise.
65 * */
66bool ValidatePath(const std::string& file, const bool expectFile);
Aron Virginas-Tarc82c8732019-10-24 17:07:43 +010067
Jan Eilers45274902020-10-15 18:34:43 +010068/**
69 * Verifies if a given vector of strings are valid paths. Reports invalid paths to std::err.
70 * @param fileVec vector of string - A vector of string containing the paths to check
71 * @param expectFile bool - If true, checks for a regular file.
72 * @return bool - True if all given strings are valid paths., false otherwise.
73 * */
Finn Williams4f55a252020-11-20 13:57:53 +000074bool ValidatePaths(const std::vector<std::string>& fileVec, const bool expectFile);
75
76template<typename T, typename TParseElementFunc>
77std::vector<T> ParseArrayImpl(std::istream& stream, TParseElementFunc parseElementFunc, const char* chars = "\t ,:")
78{
79 std::vector<T> result;
80 // Processes line-by-line.
81 std::string line;
82 while (std::getline(stream, line))
83 {
84 std::vector<std::string> tokens = armnn::stringUtils::StringTokenizer(line, chars);
85 for (const std::string& token : tokens)
86 {
87 if (!token.empty()) // See https://stackoverflow.com/questions/10437406/
88 {
89 try
90 {
91 result.push_back(parseElementFunc(token));
92 }
93 catch (const std::exception&)
94 {
95 ARMNN_LOG(error) << "'" << token << "' is not a valid number. It has been ignored.";
96 }
97 }
98 }
99 }
100
101 return result;
102}
103
104template <typename T, typename TParseElementFunc>
105void PopulateTensorWithDataGeneric(std::vector<T>& tensorData,
106 unsigned int numElements,
107 const armnn::Optional<std::string>& dataFile,
108 TParseElementFunc parseFunction)
109{
110 const bool readFromFile = dataFile.has_value() && !dataFile.value().empty();
111
112 std::ifstream inputTensorFile;
113 if (readFromFile)
114 {
115 inputTensorFile = std::ifstream(dataFile.value());
116 }
117
118 tensorData = readFromFile ?
119 ParseArrayImpl<T>(inputTensorFile, parseFunction) :
120 std::vector<T>(numElements, static_cast<T>(0));
121}