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