Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ExecuteNetworkParams.hpp" |
| 7 | |
| 8 | #include "NetworkExecutionUtils/NetworkExecutionUtils.hpp" |
| 9 | #include <InferenceModel.hpp> |
| 10 | #include <armnn/Logging.hpp> |
| 11 | |
| 12 | #include <fmt/format.h> |
| 13 | |
| 14 | bool IsModelBinary(const std::string& modelFormat) |
| 15 | { |
| 16 | // Parse model binary flag from the model-format string we got from the command-line |
| 17 | if (modelFormat.find("binary") != std::string::npos) |
| 18 | { |
| 19 | return true; |
| 20 | } |
| 21 | else if (modelFormat.find("txt") != std::string::npos || modelFormat.find("text") != std::string::npos) |
| 22 | { |
| 23 | return false; |
| 24 | } |
| 25 | else |
| 26 | { |
| 27 | throw armnn::InvalidArgumentException(fmt::format("Unknown model format: '{}'. " |
| 28 | "Please include 'binary' or 'text'", |
| 29 | modelFormat)); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | void CheckModelFormat(const std::string& modelFormat) |
| 34 | { |
| 35 | // Forward to implementation based on the parser type |
| 36 | if (modelFormat.find("armnn") != std::string::npos) |
| 37 | { |
| 38 | #if defined(ARMNN_SERIALIZER) |
| 39 | #else |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 40 | throw armnn::InvalidArgumentException("Can't run model in armnn format without a " |
| 41 | "built with serialization support."); |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 42 | #endif |
| 43 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 44 | else if (modelFormat.find("onnx") != std::string::npos) |
| 45 | { |
| 46 | #if defined(ARMNN_ONNX_PARSER) |
| 47 | #else |
| 48 | throw armnn::InvalidArgumentException("Can't run model in onnx format without a " |
| 49 | "built with Onnx parser support."); |
| 50 | #endif |
| 51 | } |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 52 | else if (modelFormat.find("tflite") != std::string::npos) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 53 | { |
| 54 | #if defined(ARMNN_TF_LITE_PARSER) |
| 55 | if (!IsModelBinary(modelFormat)) |
| 56 | { |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 57 | throw armnn::InvalidArgumentException(fmt::format("Unknown model format: '{}'. Only 'binary' " |
| 58 | "format supported for tflite files", |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 59 | modelFormat)); |
| 60 | } |
Sadik Armagan | 5d03e31 | 2020-11-17 16:43:56 +0000 | [diff] [blame] | 61 | #elif defined(ARMNN_TFLITE_DELEGATE) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 62 | #else |
| 63 | throw armnn::InvalidArgumentException("Can't run model in tflite format without a " |
| 64 | "built with Tensorflow Lite parser support."); |
| 65 | #endif |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | throw armnn::InvalidArgumentException(fmt::format("Unknown model format: '{}'. " |
Kevin May | eb03e0f | 2021-04-28 16:16:22 +0100 | [diff] [blame] | 70 | "Please include 'tflite' or 'onnx'", |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 71 | modelFormat)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void CheckClTuningParameter(const int& tuningLevel, |
| 76 | const std::string& tuningPath, |
| 77 | const std::vector<armnn::BackendId> computeDevices) |
| 78 | { |
| 79 | if (!tuningPath.empty()) |
| 80 | { |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 81 | if (tuningLevel == 0) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 82 | { |
| 83 | ARMNN_LOG(info) << "Using cl tuning file: " << tuningPath << "\n"; |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 84 | if (!ValidatePath(tuningPath, true)) |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 85 | { |
| 86 | throw armnn::InvalidArgumentException("The tuning path is not valid"); |
| 87 | } |
| 88 | } |
| 89 | else if ((1 <= tuningLevel) && (tuningLevel <= 3)) |
| 90 | { |
| 91 | ARMNN_LOG(info) << "Starting execution to generate a cl tuning file: " << tuningPath << "\n" |
| 92 | << "Tuning level in use: " << tuningLevel << "\n"; |
| 93 | } |
| 94 | else if ((0 < tuningLevel) || (tuningLevel > 3)) |
| 95 | { |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 96 | throw armnn::InvalidArgumentException(fmt::format("The tuning level {} is not valid.", |
| 97 | tuningLevel)); |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Ensure that a GpuAcc is enabled. Otherwise no tuning data are used or genereted |
| 101 | // Only warn if it's not enabled |
| 102 | auto it = std::find(computeDevices.begin(), computeDevices.end(), "GpuAcc"); |
| 103 | if (it == computeDevices.end()) |
| 104 | { |
| 105 | ARMNN_LOG(warning) << "To use Cl Tuning the compute device GpuAcc needs to be active."; |
| 106 | } |
| 107 | } |
| 108 | |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void ExecuteNetworkParams::ValidateParams() |
| 112 | { |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 113 | // Set to true if it is preferred to throw an exception rather than use ARMNN_LOG |
| 114 | bool throwExc = false; |
| 115 | |
| 116 | try |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 117 | { |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 118 | if (m_DynamicBackendsPath == "") |
Francis Murtagh | bf18a26 | 2020-10-27 15:20:40 +0000 | [diff] [blame] | 119 | { |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 120 | // Check compute devices are valid unless they are dynamically loaded at runtime |
| 121 | std::string invalidBackends; |
| 122 | if (!CheckRequestedBackendsAreValid(m_ComputeDevices, armnn::Optional<std::string&>(invalidBackends))) |
| 123 | { |
| 124 | ARMNN_LOG(fatal) << "The list of preferred devices contains invalid backend IDs: " |
| 125 | << invalidBackends; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | CheckClTuningParameter(m_TuningLevel, m_TuningPath, m_ComputeDevices); |
| 130 | |
| 131 | if (m_EnableBf16TurboMode && m_EnableFp16TurboMode) |
| 132 | { |
| 133 | ARMNN_LOG(fatal) << "BFloat16 and Float16 turbo mode cannot be enabled at the same time."; |
| 134 | } |
| 135 | |
| 136 | m_IsModelBinary = IsModelBinary(m_ModelFormat); |
| 137 | |
| 138 | CheckModelFormat(m_ModelFormat); |
| 139 | |
| 140 | // Check input tensor shapes |
| 141 | if ((m_InputTensorShapes.size() != 0) && |
| 142 | (m_InputTensorShapes.size() != m_InputNames.size())) |
| 143 | { |
| 144 | ARMNN_LOG(fatal) << "input-name and input-tensor-shape must have the same amount of elements. "; |
| 145 | } |
| 146 | |
| 147 | if (m_InputTensorDataFilePaths.size() != 0) |
| 148 | { |
| 149 | if (!ValidatePaths(m_InputTensorDataFilePaths, true)) |
| 150 | { |
| 151 | ARMNN_LOG(fatal) << "One or more input data file paths are not valid. "; |
| 152 | } |
| 153 | |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 154 | if (!m_Concurrent && m_InputTensorDataFilePaths.size() != m_InputNames.size()) |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 155 | { |
| 156 | ARMNN_LOG(fatal) << "input-name and input-tensor-data must have the same amount of elements. "; |
| 157 | } |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 158 | |
| 159 | if (m_InputTensorDataFilePaths.size() < m_SimultaneousIterations * m_InputNames.size()) |
| 160 | { |
| 161 | ARMNN_LOG(fatal) << "There is not enough input data for " << m_SimultaneousIterations << " execution."; |
| 162 | } |
| 163 | if (m_InputTensorDataFilePaths.size() > m_SimultaneousIterations * m_InputNames.size()) |
| 164 | { |
| 165 | ARMNN_LOG(fatal) << "There is more input data for " << m_SimultaneousIterations << " execution."; |
| 166 | } |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if ((m_OutputTensorFiles.size() != 0) && |
| 170 | (m_OutputTensorFiles.size() != m_OutputNames.size())) |
| 171 | { |
| 172 | ARMNN_LOG(fatal) << "output-name and write-outputs-to-file must have the same amount of elements. "; |
| 173 | } |
| 174 | |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 175 | if ((m_OutputTensorFiles.size() != 0) |
| 176 | && m_OutputTensorFiles.size() != m_SimultaneousIterations * m_OutputNames.size()) |
| 177 | { |
| 178 | ARMNN_LOG(fatal) << "There is not enough output data for " << m_SimultaneousIterations << " execution."; |
| 179 | } |
| 180 | |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 181 | if (m_InputTypes.size() == 0) |
| 182 | { |
| 183 | //Defaults the value of all inputs to "float" |
| 184 | m_InputTypes.assign(m_InputNames.size(), "float"); |
| 185 | } |
| 186 | else if ((m_InputTypes.size() != 0) && |
| 187 | (m_InputTypes.size() != m_InputNames.size())) |
| 188 | { |
| 189 | ARMNN_LOG(fatal) << "input-name and input-type must have the same amount of elements."; |
| 190 | } |
| 191 | |
| 192 | if (m_OutputTypes.size() == 0) |
| 193 | { |
| 194 | //Defaults the value of all outputs to "float" |
| 195 | m_OutputTypes.assign(m_OutputNames.size(), "float"); |
| 196 | } |
| 197 | else if ((m_OutputTypes.size() != 0) && |
| 198 | (m_OutputTypes.size() != m_OutputNames.size())) |
| 199 | { |
| 200 | ARMNN_LOG(fatal) << "output-name and output-type must have the same amount of elements."; |
| 201 | } |
| 202 | |
| 203 | // Check that threshold time is not less than zero |
| 204 | if (m_ThresholdTime < 0) |
| 205 | { |
| 206 | ARMNN_LOG(fatal) << "Threshold time supplied as a command line argument is less than zero."; |
Francis Murtagh | bf18a26 | 2020-10-27 15:20:40 +0000 | [diff] [blame] | 207 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 208 | } |
Keith Davis | cb8e350 | 2020-11-12 10:27:19 +0000 | [diff] [blame] | 209 | catch (std::string& exc) |
| 210 | { |
| 211 | if (throwExc) |
| 212 | { |
| 213 | throw armnn::InvalidArgumentException(exc); |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | std::cout << exc; |
| 218 | exit(EXIT_FAILURE); |
| 219 | } |
| 220 | } |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 221 | // Check turbo modes |
Jan Eilers | 4527490 | 2020-10-15 18:34:43 +0100 | [diff] [blame] | 222 | |
| 223 | // Warn if ExecuteNetwork will generate dummy input data |
| 224 | if (m_GenerateTensorData) |
| 225 | { |
| 226 | ARMNN_LOG(warning) << "No input files provided, input tensors will be filled with 0s."; |
| 227 | } |
| 228 | } |