blob: c30a4406a747b5908c03c1bb40381f52b26fc158 [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)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010041{
42}
43
44DriverOptions::DriverOptions(const std::vector<armnn::BackendId>& backends, bool fp16Enabled)
45 : m_Backends(backends)
telsoa01ce3e84a2018-08-31 09:31:35 +010046 , m_VerboseLogging(false)
47 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010048 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010049 , m_EnableGpuProfiling(false)
Nikhil Raj77605822018-09-03 11:25:56 +010050 , m_fp16Enabled(fp16Enabled)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010051 , m_FastMathEnabled(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000052 , m_ShouldExit(false)
53 , m_SaveCachedNetwork(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010054{
55}
56
57DriverOptions::DriverOptions(int argc, char** argv)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010058 : m_VerboseLogging(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010059 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010060 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010061 , m_EnableGpuProfiling(false)
62 , m_fp16Enabled(false)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010063 , m_FastMathEnabled(false)
Mike Kelly6df71fd2020-10-13 17:50:05 +010064 , m_ShouldExit(false)
Sadik Armaganf36e10b2021-01-11 16:34:01 +000065 , m_SaveCachedNetwork(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010066{
telsoa01ce3e84a2018-08-31 09:31:35 +010067 std::string unsupportedOperationsAsString;
68 std::string clTunedParametersModeAsString;
Ruomei Yan689c6ee2019-04-25 17:48:41 +010069 std::string clTuningLevelAsString;
Colm Donelan0cc61782020-10-06 21:02:21 +010070 std::vector<std::string> backends;
Mike Kelly6df71fd2020-10-13 17:50:05 +010071 bool showHelp;
72 bool showVersion;
telsoa01ce3e84a2018-08-31 09:31:35 +010073
Mike Kelly6df71fd2020-10-13 17:50:05 +010074 cxxopts::Options optionsDesc(argv[0], "ArmNN Android NN driver for the Android Neural Networks API. The Android NN "
75 "driver will convert Android NNAPI requests and delegate them to available "
76 "ArmNN backends.");
Colm Donelan0cc61782020-10-06 21:02:21 +010077 try
78 {
79 optionsDesc.add_options()
Mike Kelly6df71fd2020-10-13 17:50:05 +010080
81 ("a,enable-fast-math", "Enables fast_math options in backends that support it. Using the fast_math flag can "
82 "lead to performance improvements but may result in reduced or different precision.",
83 cxxopts::value<bool>(m_FastMathEnabled)->default_value("false"))
84
Colm Donelan0cc61782020-10-06 21:02:21 +010085 ("c,compute",
86 "Comma separated list of backends to run layers on. Examples of possible values are: CpuRef, CpuAcc, GpuAcc",
87 cxxopts::value<std::vector<std::string>>(backends))
telsoa01ce3e84a2018-08-31 09:31:35 +010088
Colm Donelan0cc61782020-10-06 21:02:21 +010089 ("d,request-inputs-and-outputs-dump-dir",
90 "If non-empty, the directory where request inputs and outputs should be dumped",
91 cxxopts::value<std::string>(m_RequestInputsAndOutputsDumpDir)->default_value(""))
telsoa01ce3e84a2018-08-31 09:31:35 +010092
Mike Kelly6df71fd2020-10-13 17:50:05 +010093 ("f,fp16-enabled", "Enables support for relaxed computation from Float32 to Float16",
94 cxxopts::value<bool>(m_fp16Enabled)->default_value("false"))
Kevin Mayabc95d02020-05-15 15:34:03 +010095
Mike Kelly6df71fd2020-10-13 17:50:05 +010096 ("h,help", "Show this help",
97 cxxopts::value<bool>(showHelp)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +010098
Colm Donelan0cc61782020-10-06 21:02:21 +010099 ("m,cl-tuned-parameters-mode",
telsoa01ce3e84a2018-08-31 09:31:35 +0100100 "If 'UseTunedParameters' (the default), will read CL tuned parameters from the file specified by "
101 "--cl-tuned-parameters-file. "
102 "If 'UpdateTunedParameters', will also find the optimum parameters when preparing new networks and update "
Colm Donelan0cc61782020-10-06 21:02:21 +0100103 "the file accordingly.",
104 cxxopts::value<std::string>(clTunedParametersModeAsString)->default_value("UseTunedParameters"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100105
Mike Kelly6df71fd2020-10-13 17:50:05 +0100106 ("n,service-name",
107 "If non-empty, the driver service name to be registered",
108 cxxopts::value<std::string>(m_ServiceName)->default_value("armnn"))
109
Colm Donelan0cc61782020-10-06 21:02:21 +0100110 ("o,cl-tuning-level",
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100111 "exhaustive: all lws values are tested "
112 "normal: reduced number of lws values but enough to still have the performance really close to the "
113 "exhaustive approach "
Colm Donelan0cc61782020-10-06 21:02:21 +0100114 "rapid: only 3 lws values should be tested for each kernel ",
115 cxxopts::value<std::string>(clTuningLevelAsString)->default_value("rapid"))
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100116
Colm Donelan0cc61782020-10-06 21:02:21 +0100117 ("p,gpu-profiling", "Turns GPU profiling on",
118 cxxopts::value<bool>(m_EnableGpuProfiling)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100119
Sadik Armaganf36e10b2021-01-11 16:34:01 +0000120 ("q,cached-network-file", "If non-empty, the given file will be used to load/save cached network. "
121 "If save-cached-network option is given will save the cached network to given file."
122 "If save-cached-network option is not given will load the cached network from given "
123 "file.",
124 cxxopts::value<std::string>(m_CachedNetworkFilePath)->default_value(""))
125
126 ("s,save-cached-network", "Enables saving the cached network to the file given with cached-network-file option."
127 " See also --cached-network-file",
128 cxxopts::value<bool>(m_SaveCachedNetwork)->default_value("false"))
129
Mike Kelly6df71fd2020-10-13 17:50:05 +0100130 ("t,cl-tuned-parameters-file",
131 "If non-empty, the given file will be used to load/save CL tuned parameters. "
132 "See also --cl-tuned-parameters-mode",
133 cxxopts::value<std::string>(m_ClTunedParametersFile)->default_value(""))
134
135 ("u,unsupported-operations",
136 "If non-empty, a comma-separated list of operation indices which the driver will forcibly "
137 "consider unsupported",
138 cxxopts::value<std::string>(unsupportedOperationsAsString)->default_value(""))
139
140 ("v,verbose-logging", "Turns verbose logging on",
141 cxxopts::value<bool>(m_VerboseLogging)->default_value("false"))
142
143 ("V,version", "Show version information",
144 cxxopts::value<bool>(showVersion)->default_value("false"));
Colm Donelan0cc61782020-10-06 21:02:21 +0100145 }
146 catch (const std::exception& e)
147 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100148 ALOGE("An error occurred attempting to construct options: %s", e.what());
149 std::cout << "An error occurred attempting to construct options: %s" << std::endl;
150 m_ExitCode = EXIT_FAILURE;
151 return;
Colm Donelan0cc61782020-10-06 21:02:21 +0100152 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100153
telsoa01ce3e84a2018-08-31 09:31:35 +0100154 try
155 {
Colm Donelan0cc61782020-10-06 21:02:21 +0100156 cxxopts::ParseResult result = optionsDesc.parse(argc, argv);
telsoa01ce3e84a2018-08-31 09:31:35 +0100157 }
Colm Donelan0cc61782020-10-06 21:02:21 +0100158 catch (const cxxopts::OptionException& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100159 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100160 ALOGW("An exception occurred attempting to parse program options: %s", e.what());
161 std::cout << optionsDesc.help() << std::endl
162 << "An exception occurred while parsing program options: " << std::endl
163 << e.what() << std::endl;
Mike Kellyc24bb032020-10-20 15:29:19 +0100164 m_ShouldExit = true;
165 m_ExitCode = EXIT_FAILURE;
166 return;
Mike Kelly6df71fd2020-10-13 17:50:05 +0100167 }
168 if (showHelp)
169 {
170 ALOGW("Showing help and exiting");
171 std::cout << optionsDesc.help() << std::endl;
172 m_ShouldExit = true;
173 m_ExitCode = EXIT_SUCCESS;
174 return;
175 }
176 if (showVersion)
177 {
178 ALOGW("Showing version and exiting");
179 std::cout << "ArmNN Android NN driver for the Android Neural Networks API.\n"
180 "ArmNN v" << ARMNN_VERSION << std::endl;
181 m_ShouldExit = true;
182 m_ExitCode = EXIT_SUCCESS;
183 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100184 }
185
Colm Donelan0cc61782020-10-06 21:02:21 +0100186 // Convert the string backend names into backendId's.
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100187 m_Backends.reserve(backends.size());
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100188 for (auto&& backend : backends)
telsoa01ce3e84a2018-08-31 09:31:35 +0100189 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100190 m_Backends.emplace_back(backend);
191 }
192
193 // If no backends have been specified then the default value is GpuAcc.
194 if (backends.empty())
195 {
196 ALOGE("No backends have been specified:");
197 std::cout << optionsDesc.help() << std::endl
198 << "Unable to start:" << std::endl
199 << "No backends have been specified" << std::endl;
200 m_ShouldExit = true;
201 m_ExitCode = EXIT_FAILURE;
202 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100203 }
204
205 if (!unsupportedOperationsAsString.empty())
206 {
207 std::istringstream argStream(unsupportedOperationsAsString);
208
209 std::string s;
210 while (!argStream.eof())
211 {
212 std::getline(argStream, s, ',');
213 try
214 {
215 unsigned int operationIdx = std::stoi(s);
216 m_ForcedUnsupportedOperations.insert(operationIdx);
217 }
218 catch (const std::invalid_argument&)
219 {
220 ALOGW("Ignoring invalid integer argument in -u/--unsupported-operations value: %s", s.c_str());
221 }
222 }
223 }
224
225 if (!m_ClTunedParametersFile.empty())
226 {
227 // The mode is only relevant if the file path has been provided
228 if (clTunedParametersModeAsString == "UseTunedParameters")
229 {
230 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UseTunedParameters;
231 }
232 else if (clTunedParametersModeAsString == "UpdateTunedParameters")
233 {
234 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters;
235 }
236 else
237 {
238 ALOGW("Requested unknown cl-tuned-parameters-mode '%s'. Defaulting to UseTunedParameters",
239 clTunedParametersModeAsString.c_str());
240 }
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100241
242 if (clTuningLevelAsString == "exhaustive")
243 {
244 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Exhaustive;
245 }
246 else if (clTuningLevelAsString == "normal")
247 {
248 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Normal;
249 }
250 else if (clTuningLevelAsString == "rapid")
251 {
252 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Rapid;
253 }
254 else
255 {
256 ALOGW("Requested unknown cl-tuner-mode '%s'. Defaulting to rapid",
257 clTuningLevelAsString.c_str());
258 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100259 }
260}
261
262} // namespace armnn_driver