blob: 3f9b76ee0b9c3ca07a87dc61d3e976aaad34c719 [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
Mike Kelly6df71fd2020-10-13 17:50:05 +0100109 ("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 Donelan0cc61782020-10-06 21:02:21 +0100113 ("o,cl-tuning-level",
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100114 "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 Donelan0cc61782020-10-06 21:02:21 +0100117 "rapid: only 3 lws values should be tested for each kernel ",
118 cxxopts::value<std::string>(clTuningLevelAsString)->default_value("rapid"))
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100119
Colm Donelan0cc61782020-10-06 21:02:21 +0100120 ("p,gpu-profiling", "Turns GPU profiling on",
121 cxxopts::value<bool>(m_EnableGpuProfiling)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100122
Sadik Armaganf36e10b2021-01-11 16:34:01 +0000123 ("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 Sloyancd639c92021-02-11 16:57:38 +0000133 ("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 Kelly6df71fd2020-10-13 17:50:05 +0100139 ("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 Donelan0cc61782020-10-06 21:02:21 +0100154 }
155 catch (const std::exception& e)
156 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100157 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 Donelan0cc61782020-10-06 21:02:21 +0100161 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100162
telsoa01ce3e84a2018-08-31 09:31:35 +0100163 try
164 {
Colm Donelan0cc61782020-10-06 21:02:21 +0100165 cxxopts::ParseResult result = optionsDesc.parse(argc, argv);
telsoa01ce3e84a2018-08-31 09:31:35 +0100166 }
Colm Donelan0cc61782020-10-06 21:02:21 +0100167 catch (const cxxopts::OptionException& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100168 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100169 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 Kellyc24bb032020-10-20 15:29:19 +0100173 m_ShouldExit = true;
174 m_ExitCode = EXIT_FAILURE;
175 return;
Mike Kelly6df71fd2020-10-13 17:50:05 +0100176 }
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;
telsoa01ce3e84a2018-08-31 09:31:35 +0100193 }
194
Colm Donelan0cc61782020-10-06 21:02:21 +0100195 // Convert the string backend names into backendId's.
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100196 m_Backends.reserve(backends.size());
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100197 for (auto&& backend : backends)
telsoa01ce3e84a2018-08-31 09:31:35 +0100198 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100199 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;
telsoa01ce3e84a2018-08-31 09:31:35 +0100212 }
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 Yan689c6ee2019-04-25 17:48:41 +0100250
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 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100268 }
269}
270
271} // namespace armnn_driver