blob: 42f7ea9e148843fcbe6940e691d84feb48a51bea [file] [log] [blame]
telsoa01ce3e84a2018-08-31 09:31:35 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa01ce3e84a2018-08-31 09:31:35 +01004//
5
6#define LOG_TAG "ArmnnDriver"
7
8#include "DriverOptions.hpp"
9#include "Utils.hpp"
10
Mike Kelly6df71fd2020-10-13 17:50:05 +010011#include <armnn/Version.hpp>
telsoa01ce3e84a2018-08-31 09:31:35 +010012#include <log/log.h>
13#include "SystemPropertiesUtils.hpp"
14
15#include <OperationsUtils.h>
16
Colm Donelan0cc61782020-10-06 21:02:21 +010017#include <cxxopts/cxxopts.hpp>
telsoa01ce3e84a2018-08-31 09:31:35 +010018
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010019#include <algorithm>
telsoa01ce3e84a2018-08-31 09:31:35 +010020#include <cassert>
21#include <functional>
22#include <string>
23#include <sstream>
24
25using namespace android;
26using namespace std;
27
28namespace armnn_driver
29{
30
Nikhil Raj77605822018-09-03 11:25:56 +010031DriverOptions::DriverOptions(armnn::Compute computeDevice, bool fp16Enabled)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010032 : m_Backends({computeDevice})
33 , m_VerboseLogging(false)
34 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010035 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010036 , m_EnableGpuProfiling(false)
37 , m_fp16Enabled(fp16Enabled)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010038 , m_FastMathEnabled(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000039 , m_ShouldExit(false)
40 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000041 , m_NumberOfThreads(0)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010042{
43}
44
45DriverOptions::DriverOptions(const std::vector<armnn::BackendId>& backends, bool fp16Enabled)
46 : m_Backends(backends)
telsoa01ce3e84a2018-08-31 09:31:35 +010047 , m_VerboseLogging(false)
48 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010049 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010050 , m_EnableGpuProfiling(false)
Nikhil Raj77605822018-09-03 11:25:56 +010051 , m_fp16Enabled(fp16Enabled)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010052 , m_FastMathEnabled(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000053 , m_ShouldExit(false)
54 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000055 , m_NumberOfThreads(0)
telsoa01ce3e84a2018-08-31 09:31:35 +010056{
57}
58
59DriverOptions::DriverOptions(int argc, char** argv)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010060 : m_VerboseLogging(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010061 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010062 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010063 , m_EnableGpuProfiling(false)
64 , m_fp16Enabled(false)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010065 , m_FastMathEnabled(false)
Mike Kelly6df71fd2020-10-13 17:50:05 +010066 , m_ShouldExit(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000067 , m_SaveCachedNetwork(false)
Matthew Sloyancd639c92021-02-11 16:57:38 +000068 , m_NumberOfThreads(0)
telsoa01ce3e84a2018-08-31 09:31:35 +010069{
telsoa01ce3e84a2018-08-31 09:31:35 +010070 std::string unsupportedOperationsAsString;
71 std::string clTunedParametersModeAsString;
Ruomei Yan689c6ee2019-04-25 17:48:41 +010072 std::string clTuningLevelAsString;
Colm Donelan0cc61782020-10-06 21:02:21 +010073 std::vector<std::string> backends;
Mike Kelly6df71fd2020-10-13 17:50:05 +010074 bool showHelp;
75 bool showVersion;
telsoa01ce3e84a2018-08-31 09:31:35 +010076
Mike Kelly6df71fd2020-10-13 17:50:05 +010077 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 Donelan0cc61782020-10-06 21:02:21 +010080 try
81 {
82 optionsDesc.add_options()
Mike Kelly6df71fd2020-10-13 17:50:05 +010083
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 Donelan0cc61782020-10-06 21:02:21 +010088 ("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))
telsoa01ce3e84a2018-08-31 09:31:35 +010091
Colm Donelan0cc61782020-10-06 21:02:21 +010092 ("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(""))
telsoa01ce3e84a2018-08-31 09:31:35 +010095
Mike Kelly6df71fd2020-10-13 17:50:05 +010096 ("f,fp16-enabled", "Enables support for relaxed computation from Float32 to Float16",
97 cxxopts::value<bool>(m_fp16Enabled)->default_value("false"))
Kevin Mayabc95d02020-05-15 15:34:03 +010098
Mike Kelly6df71fd2020-10-13 17:50:05 +010099 ("h,help", "Show this help",
100 cxxopts::value<bool>(showHelp)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100101
Colm Donelan0cc61782020-10-06 21:02:21 +0100102 ("m,cl-tuned-parameters-mode",
telsoa01ce3e84a2018-08-31 09:31:35 +0100103 "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 Donelan0cc61782020-10-06 21:02:21 +0100106 "the file accordingly.",
107 cxxopts::value<std::string>(clTunedParametersModeAsString)->default_value("UseTunedParameters"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100108
Finn Williamsf5ca16c2021-02-12 14:26:23 +0000109 ("g,mlgo-cl-tuned-parameters-file",
110 "If non-empty, the given file will be used to load/save MLGO CL tuned parameters. ",
111 cxxopts::value<std::string>(m_ClMLGOTunedParametersFile)->default_value(""))
112
Mike Kelly6df71fd2020-10-13 17:50:05 +0100113 ("n,service-name",
114 "If non-empty, the driver service name to be registered",
115 cxxopts::value<std::string>(m_ServiceName)->default_value("armnn"))
116
Colm Donelan0cc61782020-10-06 21:02:21 +0100117 ("o,cl-tuning-level",
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100118 "exhaustive: all lws values are tested "
119 "normal: reduced number of lws values but enough to still have the performance really close to the "
120 "exhaustive approach "
Colm Donelan0cc61782020-10-06 21:02:21 +0100121 "rapid: only 3 lws values should be tested for each kernel ",
122 cxxopts::value<std::string>(clTuningLevelAsString)->default_value("rapid"))
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100123
Colm Donelan0cc61782020-10-06 21:02:21 +0100124 ("p,gpu-profiling", "Turns GPU profiling on",
125 cxxopts::value<bool>(m_EnableGpuProfiling)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100126
Sadik Armaganf36e10b2021-01-11 16:34:01 +0000127 ("q,cached-network-file", "If non-empty, the given file will be used to load/save cached network. "
128 "If save-cached-network option is given will save the cached network to given file."
129 "If save-cached-network option is not given will load the cached network from given "
130 "file.",
131 cxxopts::value<std::string>(m_CachedNetworkFilePath)->default_value(""))
132
133 ("s,save-cached-network", "Enables saving the cached network to the file given with cached-network-file option."
134 " See also --cached-network-file",
135 cxxopts::value<bool>(m_SaveCachedNetwork)->default_value("false"))
136
Matthew Sloyancd639c92021-02-11 16:57:38 +0000137 ("number-of-threads",
138 "Assign the number of threads used by the CpuAcc backend. "
139 "Input value must be between 1 and 64. "
140 "Default is set to 0 (Backend will decide number of threads to use).",
141 cxxopts::value<unsigned int>(m_NumberOfThreads)->default_value("0"))
142
Mike Kelly6df71fd2020-10-13 17:50:05 +0100143 ("t,cl-tuned-parameters-file",
144 "If non-empty, the given file will be used to load/save CL tuned parameters. "
145 "See also --cl-tuned-parameters-mode",
146 cxxopts::value<std::string>(m_ClTunedParametersFile)->default_value(""))
147
148 ("u,unsupported-operations",
149 "If non-empty, a comma-separated list of operation indices which the driver will forcibly "
150 "consider unsupported",
151 cxxopts::value<std::string>(unsupportedOperationsAsString)->default_value(""))
152
153 ("v,verbose-logging", "Turns verbose logging on",
154 cxxopts::value<bool>(m_VerboseLogging)->default_value("false"))
155
156 ("V,version", "Show version information",
157 cxxopts::value<bool>(showVersion)->default_value("false"));
Colm Donelan0cc61782020-10-06 21:02:21 +0100158 }
159 catch (const std::exception& e)
160 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100161 ALOGE("An error occurred attempting to construct options: %s", e.what());
162 std::cout << "An error occurred attempting to construct options: %s" << std::endl;
163 m_ExitCode = EXIT_FAILURE;
164 return;
Colm Donelan0cc61782020-10-06 21:02:21 +0100165 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100166
telsoa01ce3e84a2018-08-31 09:31:35 +0100167 try
168 {
Colm Donelan0cc61782020-10-06 21:02:21 +0100169 cxxopts::ParseResult result = optionsDesc.parse(argc, argv);
telsoa01ce3e84a2018-08-31 09:31:35 +0100170 }
Colm Donelan0cc61782020-10-06 21:02:21 +0100171 catch (const cxxopts::OptionException& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100172 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100173 ALOGW("An exception occurred attempting to parse program options: %s", e.what());
174 std::cout << optionsDesc.help() << std::endl
175 << "An exception occurred while parsing program options: " << std::endl
176 << e.what() << std::endl;
Mike Kellyc24bb032020-10-20 15:29:19 +0100177 m_ShouldExit = true;
178 m_ExitCode = EXIT_FAILURE;
179 return;
Mike Kelly6df71fd2020-10-13 17:50:05 +0100180 }
181 if (showHelp)
182 {
183 ALOGW("Showing help and exiting");
184 std::cout << optionsDesc.help() << std::endl;
185 m_ShouldExit = true;
186 m_ExitCode = EXIT_SUCCESS;
187 return;
188 }
189 if (showVersion)
190 {
191 ALOGW("Showing version and exiting");
192 std::cout << "ArmNN Android NN driver for the Android Neural Networks API.\n"
193 "ArmNN v" << ARMNN_VERSION << std::endl;
194 m_ShouldExit = true;
195 m_ExitCode = EXIT_SUCCESS;
196 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100197 }
198
Colm Donelan0cc61782020-10-06 21:02:21 +0100199 // Convert the string backend names into backendId's.
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100200 m_Backends.reserve(backends.size());
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100201 for (auto&& backend : backends)
telsoa01ce3e84a2018-08-31 09:31:35 +0100202 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100203 m_Backends.emplace_back(backend);
204 }
205
206 // If no backends have been specified then the default value is GpuAcc.
207 if (backends.empty())
208 {
209 ALOGE("No backends have been specified:");
210 std::cout << optionsDesc.help() << std::endl
211 << "Unable to start:" << std::endl
212 << "No backends have been specified" << std::endl;
213 m_ShouldExit = true;
214 m_ExitCode = EXIT_FAILURE;
215 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100216 }
217
218 if (!unsupportedOperationsAsString.empty())
219 {
220 std::istringstream argStream(unsupportedOperationsAsString);
221
222 std::string s;
223 while (!argStream.eof())
224 {
225 std::getline(argStream, s, ',');
226 try
227 {
228 unsigned int operationIdx = std::stoi(s);
229 m_ForcedUnsupportedOperations.insert(operationIdx);
230 }
231 catch (const std::invalid_argument&)
232 {
233 ALOGW("Ignoring invalid integer argument in -u/--unsupported-operations value: %s", s.c_str());
234 }
235 }
236 }
237
238 if (!m_ClTunedParametersFile.empty())
239 {
240 // The mode is only relevant if the file path has been provided
241 if (clTunedParametersModeAsString == "UseTunedParameters")
242 {
243 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UseTunedParameters;
244 }
245 else if (clTunedParametersModeAsString == "UpdateTunedParameters")
246 {
247 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters;
248 }
249 else
250 {
251 ALOGW("Requested unknown cl-tuned-parameters-mode '%s'. Defaulting to UseTunedParameters",
252 clTunedParametersModeAsString.c_str());
253 }
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100254
255 if (clTuningLevelAsString == "exhaustive")
256 {
257 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Exhaustive;
258 }
259 else if (clTuningLevelAsString == "normal")
260 {
261 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Normal;
262 }
263 else if (clTuningLevelAsString == "rapid")
264 {
265 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Rapid;
266 }
267 else
268 {
269 ALOGW("Requested unknown cl-tuner-mode '%s'. Defaulting to rapid",
270 clTuningLevelAsString.c_str());
271 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100272 }
273}
274
275} // namespace armnn_driver