telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | 93e4898 | 2018-09-05 13:05:09 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #define LOG_TAG "ArmnnDriver" |
| 7 | |
| 8 | #include "DriverOptions.hpp" |
| 9 | #include "Utils.hpp" |
| 10 | |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 11 | #include <armnn/Version.hpp> |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 12 | #include <log/log.h> |
| 13 | #include "SystemPropertiesUtils.hpp" |
| 14 | |
| 15 | #include <OperationsUtils.h> |
| 16 | |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 17 | #include <cxxopts/cxxopts.hpp> |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 18 | |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 19 | #include <algorithm> |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 20 | #include <cassert> |
| 21 | #include <functional> |
| 22 | #include <string> |
| 23 | #include <sstream> |
| 24 | |
| 25 | using namespace android; |
| 26 | using namespace std; |
| 27 | |
| 28 | namespace armnn_driver |
| 29 | { |
| 30 | |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 31 | DriverOptions::DriverOptions(armnn::Compute computeDevice, bool fp16Enabled) |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 32 | : m_Backends({computeDevice}) |
| 33 | , m_VerboseLogging(false) |
| 34 | , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters) |
Ruomei Yan | 689c6ee | 2019-04-25 17:48:41 +0100 | [diff] [blame] | 35 | , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid) |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 36 | , m_EnableGpuProfiling(false) |
| 37 | , m_fp16Enabled(fp16Enabled) |
Mike Kelly | 7ed56dd | 2020-09-30 20:22:56 +0100 | [diff] [blame] | 38 | , m_FastMathEnabled(false) |
Sadik Armagan | f36e10b | 2021-01-11 16:34:01 +0000 | [diff] [blame] | 39 | , m_ShouldExit(false) |
| 40 | , m_SaveCachedNetwork(false) |
Matthew Sloyan | cd639c9 | 2021-02-11 16:57:38 +0000 | [diff] [blame] | 41 | , m_NumberOfThreads(0) |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | DriverOptions::DriverOptions(const std::vector<armnn::BackendId>& backends, bool fp16Enabled) |
| 46 | : m_Backends(backends) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 47 | , m_VerboseLogging(false) |
| 48 | , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters) |
Ruomei Yan | 689c6ee | 2019-04-25 17:48:41 +0100 | [diff] [blame] | 49 | , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 50 | , m_EnableGpuProfiling(false) |
Nikhil Raj | 7760582 | 2018-09-03 11:25:56 +0100 | [diff] [blame] | 51 | , m_fp16Enabled(fp16Enabled) |
Mike Kelly | 7ed56dd | 2020-09-30 20:22:56 +0100 | [diff] [blame] | 52 | , m_FastMathEnabled(false) |
Sadik Armagan | f36e10b | 2021-01-11 16:34:01 +0000 | [diff] [blame] | 53 | , m_ShouldExit(false) |
| 54 | , m_SaveCachedNetwork(false) |
Matthew Sloyan | cd639c9 | 2021-02-11 16:57:38 +0000 | [diff] [blame] | 55 | , m_NumberOfThreads(0) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | DriverOptions::DriverOptions(int argc, char** argv) |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 60 | : m_VerboseLogging(false) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 61 | , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters) |
Ruomei Yan | 689c6ee | 2019-04-25 17:48:41 +0100 | [diff] [blame] | 62 | , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 63 | , m_EnableGpuProfiling(false) |
| 64 | , m_fp16Enabled(false) |
Mike Kelly | 7ed56dd | 2020-09-30 20:22:56 +0100 | [diff] [blame] | 65 | , m_FastMathEnabled(false) |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 66 | , m_ShouldExit(false) |
Sadik Armagan | f36e10b | 2021-01-11 16:34:01 +0000 | [diff] [blame] | 67 | , m_SaveCachedNetwork(false) |
Matthew Sloyan | cd639c9 | 2021-02-11 16:57:38 +0000 | [diff] [blame] | 68 | , m_NumberOfThreads(0) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 69 | { |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 70 | std::string unsupportedOperationsAsString; |
| 71 | std::string clTunedParametersModeAsString; |
Ruomei Yan | 689c6ee | 2019-04-25 17:48:41 +0100 | [diff] [blame] | 72 | std::string clTuningLevelAsString; |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 73 | std::vector<std::string> backends; |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 74 | bool showHelp; |
| 75 | bool showVersion; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 76 | |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 77 | cxxopts::Options optionsDesc(argv[0], "ArmNN Android NN driver for the Android Neural Networks API. The Android NN " |
| 78 | "driver will convert Android NNAPI requests and delegate them to available " |
| 79 | "ArmNN backends."); |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 80 | try |
| 81 | { |
| 82 | optionsDesc.add_options() |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 83 | |
| 84 | ("a,enable-fast-math", "Enables fast_math options in backends that support it. Using the fast_math flag can " |
| 85 | "lead to performance improvements but may result in reduced or different precision.", |
| 86 | cxxopts::value<bool>(m_FastMathEnabled)->default_value("false")) |
| 87 | |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 88 | ("c,compute", |
| 89 | "Comma separated list of backends to run layers on. Examples of possible values are: CpuRef, CpuAcc, GpuAcc", |
| 90 | cxxopts::value<std::vector<std::string>>(backends)) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 91 | |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 92 | ("d,request-inputs-and-outputs-dump-dir", |
| 93 | "If non-empty, the directory where request inputs and outputs should be dumped", |
| 94 | cxxopts::value<std::string>(m_RequestInputsAndOutputsDumpDir)->default_value("")) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 95 | |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 96 | ("f,fp16-enabled", "Enables support for relaxed computation from Float32 to Float16", |
| 97 | cxxopts::value<bool>(m_fp16Enabled)->default_value("false")) |
Kevin May | abc95d0 | 2020-05-15 15:34:03 +0100 | [diff] [blame] | 98 | |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 99 | ("h,help", "Show this help", |
| 100 | cxxopts::value<bool>(showHelp)->default_value("false")) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 101 | |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 102 | ("m,cl-tuned-parameters-mode", |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 103 | "If 'UseTunedParameters' (the default), will read CL tuned parameters from the file specified by " |
| 104 | "--cl-tuned-parameters-file. " |
| 105 | "If 'UpdateTunedParameters', will also find the optimum parameters when preparing new networks and update " |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 106 | "the file accordingly.", |
| 107 | cxxopts::value<std::string>(clTunedParametersModeAsString)->default_value("UseTunedParameters")) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 108 | |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 109 | ("n,service-name", |
| 110 | "If non-empty, the driver service name to be registered", |
| 111 | cxxopts::value<std::string>(m_ServiceName)->default_value("armnn")) |
| 112 | |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 113 | ("o,cl-tuning-level", |
Ruomei Yan | 689c6ee | 2019-04-25 17:48:41 +0100 | [diff] [blame] | 114 | "exhaustive: all lws values are tested " |
| 115 | "normal: reduced number of lws values but enough to still have the performance really close to the " |
| 116 | "exhaustive approach " |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 117 | "rapid: only 3 lws values should be tested for each kernel ", |
| 118 | cxxopts::value<std::string>(clTuningLevelAsString)->default_value("rapid")) |
Ruomei Yan | 689c6ee | 2019-04-25 17:48:41 +0100 | [diff] [blame] | 119 | |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 120 | ("p,gpu-profiling", "Turns GPU profiling on", |
| 121 | cxxopts::value<bool>(m_EnableGpuProfiling)->default_value("false")) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 122 | |
Sadik Armagan | f36e10b | 2021-01-11 16:34:01 +0000 | [diff] [blame] | 123 | ("q,cached-network-file", "If non-empty, the given file will be used to load/save cached network. " |
| 124 | "If save-cached-network option is given will save the cached network to given file." |
| 125 | "If save-cached-network option is not given will load the cached network from given " |
| 126 | "file.", |
| 127 | cxxopts::value<std::string>(m_CachedNetworkFilePath)->default_value("")) |
| 128 | |
| 129 | ("s,save-cached-network", "Enables saving the cached network to the file given with cached-network-file option." |
| 130 | " See also --cached-network-file", |
| 131 | cxxopts::value<bool>(m_SaveCachedNetwork)->default_value("false")) |
| 132 | |
Matthew Sloyan | cd639c9 | 2021-02-11 16:57:38 +0000 | [diff] [blame] | 133 | ("number-of-threads", |
| 134 | "Assign the number of threads used by the CpuAcc backend. " |
| 135 | "Input value must be between 1 and 64. " |
| 136 | "Default is set to 0 (Backend will decide number of threads to use).", |
| 137 | cxxopts::value<unsigned int>(m_NumberOfThreads)->default_value("0")) |
| 138 | |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 139 | ("t,cl-tuned-parameters-file", |
| 140 | "If non-empty, the given file will be used to load/save CL tuned parameters. " |
| 141 | "See also --cl-tuned-parameters-mode", |
| 142 | cxxopts::value<std::string>(m_ClTunedParametersFile)->default_value("")) |
| 143 | |
| 144 | ("u,unsupported-operations", |
| 145 | "If non-empty, a comma-separated list of operation indices which the driver will forcibly " |
| 146 | "consider unsupported", |
| 147 | cxxopts::value<std::string>(unsupportedOperationsAsString)->default_value("")) |
| 148 | |
| 149 | ("v,verbose-logging", "Turns verbose logging on", |
| 150 | cxxopts::value<bool>(m_VerboseLogging)->default_value("false")) |
| 151 | |
| 152 | ("V,version", "Show version information", |
| 153 | cxxopts::value<bool>(showVersion)->default_value("false")); |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 154 | } |
| 155 | catch (const std::exception& e) |
| 156 | { |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 157 | ALOGE("An error occurred attempting to construct options: %s", e.what()); |
| 158 | std::cout << "An error occurred attempting to construct options: %s" << std::endl; |
| 159 | m_ExitCode = EXIT_FAILURE; |
| 160 | return; |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 161 | } |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 162 | |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 163 | try |
| 164 | { |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 165 | cxxopts::ParseResult result = optionsDesc.parse(argc, argv); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 166 | } |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 167 | catch (const cxxopts::OptionException& e) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 168 | { |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 169 | ALOGW("An exception occurred attempting to parse program options: %s", e.what()); |
| 170 | std::cout << optionsDesc.help() << std::endl |
| 171 | << "An exception occurred while parsing program options: " << std::endl |
| 172 | << e.what() << std::endl; |
Mike Kelly | c24bb03 | 2020-10-20 15:29:19 +0100 | [diff] [blame] | 173 | m_ShouldExit = true; |
| 174 | m_ExitCode = EXIT_FAILURE; |
| 175 | return; |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 176 | } |
| 177 | if (showHelp) |
| 178 | { |
| 179 | ALOGW("Showing help and exiting"); |
| 180 | std::cout << optionsDesc.help() << std::endl; |
| 181 | m_ShouldExit = true; |
| 182 | m_ExitCode = EXIT_SUCCESS; |
| 183 | return; |
| 184 | } |
| 185 | if (showVersion) |
| 186 | { |
| 187 | ALOGW("Showing version and exiting"); |
| 188 | std::cout << "ArmNN Android NN driver for the Android Neural Networks API.\n" |
| 189 | "ArmNN v" << ARMNN_VERSION << std::endl; |
| 190 | m_ShouldExit = true; |
| 191 | m_ExitCode = EXIT_SUCCESS; |
| 192 | return; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Colm Donelan | 0cc6178 | 2020-10-06 21:02:21 +0100 | [diff] [blame] | 195 | // Convert the string backend names into backendId's. |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 196 | m_Backends.reserve(backends.size()); |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 197 | for (auto&& backend : backends) |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 198 | { |
Mike Kelly | 6df71fd | 2020-10-13 17:50:05 +0100 | [diff] [blame] | 199 | m_Backends.emplace_back(backend); |
| 200 | } |
| 201 | |
| 202 | // If no backends have been specified then the default value is GpuAcc. |
| 203 | if (backends.empty()) |
| 204 | { |
| 205 | ALOGE("No backends have been specified:"); |
| 206 | std::cout << optionsDesc.help() << std::endl |
| 207 | << "Unable to start:" << std::endl |
| 208 | << "No backends have been specified" << std::endl; |
| 209 | m_ShouldExit = true; |
| 210 | m_ExitCode = EXIT_FAILURE; |
| 211 | return; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | if (!unsupportedOperationsAsString.empty()) |
| 215 | { |
| 216 | std::istringstream argStream(unsupportedOperationsAsString); |
| 217 | |
| 218 | std::string s; |
| 219 | while (!argStream.eof()) |
| 220 | { |
| 221 | std::getline(argStream, s, ','); |
| 222 | try |
| 223 | { |
| 224 | unsigned int operationIdx = std::stoi(s); |
| 225 | m_ForcedUnsupportedOperations.insert(operationIdx); |
| 226 | } |
| 227 | catch (const std::invalid_argument&) |
| 228 | { |
| 229 | ALOGW("Ignoring invalid integer argument in -u/--unsupported-operations value: %s", s.c_str()); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if (!m_ClTunedParametersFile.empty()) |
| 235 | { |
| 236 | // The mode is only relevant if the file path has been provided |
| 237 | if (clTunedParametersModeAsString == "UseTunedParameters") |
| 238 | { |
| 239 | m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UseTunedParameters; |
| 240 | } |
| 241 | else if (clTunedParametersModeAsString == "UpdateTunedParameters") |
| 242 | { |
| 243 | m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters; |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | ALOGW("Requested unknown cl-tuned-parameters-mode '%s'. Defaulting to UseTunedParameters", |
| 248 | clTunedParametersModeAsString.c_str()); |
| 249 | } |
Ruomei Yan | 689c6ee | 2019-04-25 17:48:41 +0100 | [diff] [blame] | 250 | |
| 251 | if (clTuningLevelAsString == "exhaustive") |
| 252 | { |
| 253 | m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Exhaustive; |
| 254 | } |
| 255 | else if (clTuningLevelAsString == "normal") |
| 256 | { |
| 257 | m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Normal; |
| 258 | } |
| 259 | else if (clTuningLevelAsString == "rapid") |
| 260 | { |
| 261 | m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Rapid; |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | ALOGW("Requested unknown cl-tuner-mode '%s'. Defaulting to rapid", |
| 266 | clTuningLevelAsString.c_str()); |
| 267 | } |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | } // namespace armnn_driver |