blob: 6f777e3ce8fb37fff90134d99c9ea3226e35c9fa [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)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010039{
40}
41
42DriverOptions::DriverOptions(const std::vector<armnn::BackendId>& backends, bool fp16Enabled)
43 : m_Backends(backends)
telsoa01ce3e84a2018-08-31 09:31:35 +010044 , m_VerboseLogging(false)
45 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010046 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010047 , m_EnableGpuProfiling(false)
Nikhil Raj77605822018-09-03 11:25:56 +010048 , m_fp16Enabled(fp16Enabled)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010049 , m_FastMathEnabled(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010050{
51}
52
53DriverOptions::DriverOptions(int argc, char** argv)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010054 : m_VerboseLogging(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010055 , m_ClTunedParametersMode(armnn::IGpuAccTunedParameters::Mode::UseTunedParameters)
Ruomei Yan689c6ee2019-04-25 17:48:41 +010056 , m_ClTuningLevel(armnn::IGpuAccTunedParameters::TuningLevel::Rapid)
telsoa01ce3e84a2018-08-31 09:31:35 +010057 , m_EnableGpuProfiling(false)
58 , m_fp16Enabled(false)
Mike Kelly7ed56dd2020-09-30 20:22:56 +010059 , m_FastMathEnabled(false)
Mike Kelly6df71fd2020-10-13 17:50:05 +010060 , m_ShouldExit(false)
telsoa01ce3e84a2018-08-31 09:31:35 +010061{
telsoa01ce3e84a2018-08-31 09:31:35 +010062 std::string unsupportedOperationsAsString;
63 std::string clTunedParametersModeAsString;
Ruomei Yan689c6ee2019-04-25 17:48:41 +010064 std::string clTuningLevelAsString;
Colm Donelan0cc61782020-10-06 21:02:21 +010065 std::vector<std::string> backends;
Mike Kelly6df71fd2020-10-13 17:50:05 +010066 bool showHelp;
67 bool showVersion;
telsoa01ce3e84a2018-08-31 09:31:35 +010068
Mike Kelly6df71fd2020-10-13 17:50:05 +010069 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 Donelan0cc61782020-10-06 21:02:21 +010072 try
73 {
74 optionsDesc.add_options()
Mike Kelly6df71fd2020-10-13 17:50:05 +010075
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 Donelan0cc61782020-10-06 21:02:21 +010080 ("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))
telsoa01ce3e84a2018-08-31 09:31:35 +010083
Colm Donelan0cc61782020-10-06 21:02:21 +010084 ("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(""))
telsoa01ce3e84a2018-08-31 09:31:35 +010087
Mike Kelly6df71fd2020-10-13 17:50:05 +010088 ("f,fp16-enabled", "Enables support for relaxed computation from Float32 to Float16",
89 cxxopts::value<bool>(m_fp16Enabled)->default_value("false"))
Kevin Mayabc95d02020-05-15 15:34:03 +010090
Mike Kelly6df71fd2020-10-13 17:50:05 +010091 ("h,help", "Show this help",
92 cxxopts::value<bool>(showHelp)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +010093
Colm Donelan0cc61782020-10-06 21:02:21 +010094 ("m,cl-tuned-parameters-mode",
telsoa01ce3e84a2018-08-31 09:31:35 +010095 "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 Donelan0cc61782020-10-06 21:02:21 +010098 "the file accordingly.",
99 cxxopts::value<std::string>(clTunedParametersModeAsString)->default_value("UseTunedParameters"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100100
Mike Kelly6df71fd2020-10-13 17:50:05 +0100101 ("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 Donelan0cc61782020-10-06 21:02:21 +0100105 ("o,cl-tuning-level",
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100106 "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 Donelan0cc61782020-10-06 21:02:21 +0100109 "rapid: only 3 lws values should be tested for each kernel ",
110 cxxopts::value<std::string>(clTuningLevelAsString)->default_value("rapid"))
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100111
Colm Donelan0cc61782020-10-06 21:02:21 +0100112 ("p,gpu-profiling", "Turns GPU profiling on",
113 cxxopts::value<bool>(m_EnableGpuProfiling)->default_value("false"))
telsoa01ce3e84a2018-08-31 09:31:35 +0100114
Mike Kelly6df71fd2020-10-13 17:50:05 +0100115 ("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 Donelan0cc61782020-10-06 21:02:21 +0100130 }
131 catch (const std::exception& e)
132 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100133 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 Donelan0cc61782020-10-06 21:02:21 +0100137 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100138
telsoa01ce3e84a2018-08-31 09:31:35 +0100139 try
140 {
Colm Donelan0cc61782020-10-06 21:02:21 +0100141 cxxopts::ParseResult result = optionsDesc.parse(argc, argv);
telsoa01ce3e84a2018-08-31 09:31:35 +0100142 }
Colm Donelan0cc61782020-10-06 21:02:21 +0100143 catch (const cxxopts::OptionException& e)
telsoa01ce3e84a2018-08-31 09:31:35 +0100144 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100145 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 Kellyc24bb032020-10-20 15:29:19 +0100149 m_ShouldExit = true;
150 m_ExitCode = EXIT_FAILURE;
151 return;
Mike Kelly6df71fd2020-10-13 17:50:05 +0100152 }
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;
telsoa01ce3e84a2018-08-31 09:31:35 +0100169 }
170
Colm Donelan0cc61782020-10-06 21:02:21 +0100171 // Convert the string backend names into backendId's.
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100172 m_Backends.reserve(backends.size());
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100173 for (auto&& backend : backends)
telsoa01ce3e84a2018-08-31 09:31:35 +0100174 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100175 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;
telsoa01ce3e84a2018-08-31 09:31:35 +0100188 }
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 Yan689c6ee2019-04-25 17:48:41 +0100226
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 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100244 }
245}
246
247} // namespace armnn_driver