Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "QuantizationDataSet.hpp" |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 7 | |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 8 | #include <fmt/format.h> |
| 9 | |
| 10 | #include <armnn/utility/StringUtils.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 11 | #include <armnn/utility/IgnoreUnused.hpp> |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 12 | #include <Filesystem.hpp> |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 13 | |
| 14 | namespace armnnQuantizer |
| 15 | { |
| 16 | |
| 17 | QuantizationDataSet::QuantizationDataSet() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | QuantizationDataSet::QuantizationDataSet(const std::string csvFilePath): |
| 22 | m_QuantizationInputs(), |
| 23 | m_CsvFilePath(csvFilePath) |
| 24 | { |
| 25 | ParseCsvFile(); |
| 26 | } |
| 27 | |
| 28 | void AddInputData(unsigned int passId, |
| 29 | armnn::LayerBindingId bindingId, |
| 30 | const std::string& inputFilePath, |
| 31 | std::map<unsigned int, QuantizationInput>& passIdToQuantizationInput) |
| 32 | { |
| 33 | auto iterator = passIdToQuantizationInput.find(passId); |
| 34 | if (iterator == passIdToQuantizationInput.end()) |
| 35 | { |
| 36 | QuantizationInput input(passId, bindingId, inputFilePath); |
| 37 | passIdToQuantizationInput.emplace(passId, input); |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | auto existingQuantizationInput = iterator->second; |
| 42 | existingQuantizationInput.AddEntry(bindingId, inputFilePath); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | QuantizationDataSet::~QuantizationDataSet() |
| 47 | { |
| 48 | } |
| 49 | |
Finn Williams | b454c5c | 2021-02-09 15:56:23 +0000 | [diff] [blame] | 50 | |
| 51 | /// Visitor class implementation to gather the TensorInfo for LayerBindingID for creation of ConstTensor for Refine. |
| 52 | |
| 53 | void InputLayerStrategy::ExecuteStrategy(const armnn::IConnectableLayer* layer, |
| 54 | const armnn::BaseDescriptor& descriptor, |
| 55 | const std::vector<armnn::ConstTensor>& constants, |
| 56 | const char* name, |
| 57 | const armnn::LayerBindingId id) |
| 58 | { |
| 59 | armnn::IgnoreUnused(name, descriptor, constants); |
| 60 | |
| 61 | m_TensorInfos.emplace(id, layer->GetOutputSlot(0).GetTensorInfo()); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | armnn::TensorInfo InputLayerStrategy::GetTensorInfo(armnn::LayerBindingId layerBindingId) |
| 68 | { |
| 69 | auto iterator = m_TensorInfos.find(layerBindingId); |
| 70 | if (iterator != m_TensorInfos.end()) |
| 71 | { |
| 72 | return m_TensorInfos.at(layerBindingId); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | throw armnn::Exception("Could not retrieve tensor info for binding ID " + std::to_string(layerBindingId)); |
| 77 | } |
| 78 | } |
| 79 | |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 80 | void InputLayerVisitor::VisitInputLayer(const armnn::IConnectableLayer* layer, |
| 81 | armnn::LayerBindingId id, |
| 82 | const char* name) |
| 83 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 84 | armnn::IgnoreUnused(name); |
Narumol Prangnawarat | cd05f3e | 2019-05-10 17:19:58 +0100 | [diff] [blame] | 85 | m_TensorInfos.emplace(id, layer->GetOutputSlot(0).GetTensorInfo()); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | armnn::TensorInfo InputLayerVisitor::GetTensorInfo(armnn::LayerBindingId layerBindingId) |
| 89 | { |
| 90 | auto iterator = m_TensorInfos.find(layerBindingId); |
| 91 | if (iterator != m_TensorInfos.end()) |
| 92 | { |
| 93 | return m_TensorInfos.at(layerBindingId); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | throw armnn::Exception("Could not retrieve tensor info for binding ID " + std::to_string(layerBindingId)); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 102 | unsigned int GetPassIdFromCsvRow(std::vector<std::string> tokens, unsigned int lineIndex) |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 103 | { |
| 104 | unsigned int passId; |
| 105 | try |
| 106 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 107 | passId = static_cast<unsigned int>(std::stoi(tokens[0])); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 108 | } |
Matthew Bentham | 31b2e13 | 2019-05-22 17:20:55 +0100 | [diff] [blame] | 109 | catch (const std::invalid_argument&) |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 110 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 111 | throw armnn::ParseException(fmt::format("Pass ID [{}] is not correct format on CSV row {}", |
| 112 | tokens[0], lineIndex)); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 113 | } |
| 114 | return passId; |
| 115 | } |
| 116 | |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 117 | armnn::LayerBindingId GetBindingIdFromCsvRow(std::vector<std::string> tokens, unsigned int lineIndex) |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 118 | { |
| 119 | armnn::LayerBindingId bindingId; |
| 120 | try |
| 121 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 122 | bindingId = std::stoi(tokens[1]); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 123 | } |
Matthew Bentham | 31b2e13 | 2019-05-22 17:20:55 +0100 | [diff] [blame] | 124 | catch (const std::invalid_argument&) |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 125 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 126 | throw armnn::ParseException(fmt::format("Binding ID [{}] is not correct format on CSV row {}", |
| 127 | tokens[1], lineIndex)); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 128 | } |
| 129 | return bindingId; |
| 130 | } |
| 131 | |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 132 | std::string GetFileNameFromCsvRow(std::vector<std::string> tokens, unsigned int lineIndex) |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 133 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 134 | std::string fileName = armnn::stringUtils::StringTrim(tokens[2]); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 135 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 136 | if (!fs::exists(fileName)) |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 137 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 138 | throw armnn::ParseException(fmt::format("File [{}] provided on CSV row {} does not exist.", |
| 139 | fileName, lineIndex)); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | if (fileName.empty()) |
| 143 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 144 | throw armnn::ParseException(fmt::format("Filename cannot be empty on CSV row {} ", lineIndex)); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 145 | } |
| 146 | return fileName; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | void QuantizationDataSet::ParseCsvFile() |
| 151 | { |
| 152 | std::map<unsigned int, QuantizationInput> passIdToQuantizationInput; |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 153 | |
| 154 | if (m_CsvFilePath == "") |
| 155 | { |
| 156 | throw armnn::Exception("CSV file not specified."); |
| 157 | } |
| 158 | |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 159 | std::ifstream inf (m_CsvFilePath.c_str()); |
| 160 | std::string line; |
| 161 | std::vector<std::string> tokens; |
| 162 | unsigned int lineIndex = 0; |
| 163 | |
| 164 | if (!inf) |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 165 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 166 | throw armnn::Exception(fmt::format("CSV file {} not found.", m_CsvFilePath)); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 167 | } |
| 168 | |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 169 | while (getline(inf, line)) |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 170 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 171 | tokens = armnn::stringUtils::StringTokenizer(line, ","); |
| 172 | |
| 173 | if (tokens.size() != 3) |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 174 | { |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 175 | throw armnn::Exception(fmt::format("CSV file [{}] does not have correct number of entries" \ |
| 176 | "on line {}. Expected 3 entries but was {}.", |
| 177 | m_CsvFilePath, lineIndex, tokens.size())); |
| 178 | |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 179 | } |
| 180 | |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 181 | unsigned int passId = GetPassIdFromCsvRow(tokens, lineIndex); |
| 182 | armnn::LayerBindingId bindingId = GetBindingIdFromCsvRow(tokens, lineIndex); |
| 183 | std::string rawFileName = GetFileNameFromCsvRow(tokens, lineIndex); |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 184 | |
| 185 | AddInputData(passId, bindingId, rawFileName, passIdToQuantizationInput); |
James Ward | 5ea9f31 | 2020-10-29 16:19:02 +0000 | [diff] [blame] | 186 | |
| 187 | ++lineIndex; |
Nina Drozd | 59e15b0 | 2019-04-25 15:45:20 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (passIdToQuantizationInput.empty()) |
| 191 | { |
| 192 | throw armnn::Exception("Could not parse CSV file."); |
| 193 | } |
| 194 | |
| 195 | // Once all entries in CSV file are parsed successfully and QuantizationInput map is populated, populate |
| 196 | // QuantizationInputs iterator for easier access and clear the map |
| 197 | for (auto itr = passIdToQuantizationInput.begin(); itr != passIdToQuantizationInput.end(); ++itr) |
| 198 | { |
| 199 | m_QuantizationInputs.emplace_back(itr->second); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | } |