blob: 414fc93d589dae92be52a9bacf5946494fc028ec [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;
149 }
150 if (showHelp)
151 {
152 ALOGW("Showing help and exiting");
153 std::cout << optionsDesc.help() << std::endl;
154 m_ShouldExit = true;
155 m_ExitCode = EXIT_SUCCESS;
156 return;
157 }
158 if (showVersion)
159 {
160 ALOGW("Showing version and exiting");
161 std::cout << "ArmNN Android NN driver for the Android Neural Networks API.\n"
162 "ArmNN v" << ARMNN_VERSION << std::endl;
163 m_ShouldExit = true;
164 m_ExitCode = EXIT_SUCCESS;
165 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100166 }
167
Colm Donelan0cc61782020-10-06 21:02:21 +0100168 // Convert the string backend names into backendId's.
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100169 m_Backends.reserve(backends.size());
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +0100170 for (auto&& backend : backends)
telsoa01ce3e84a2018-08-31 09:31:35 +0100171 {
Mike Kelly6df71fd2020-10-13 17:50:05 +0100172 m_Backends.emplace_back(backend);
173 }
174
175 // If no backends have been specified then the default value is GpuAcc.
176 if (backends.empty())
177 {
178 ALOGE("No backends have been specified:");
179 std::cout << optionsDesc.help() << std::endl
180 << "Unable to start:" << std::endl
181 << "No backends have been specified" << std::endl;
182 m_ShouldExit = true;
183 m_ExitCode = EXIT_FAILURE;
184 return;
telsoa01ce3e84a2018-08-31 09:31:35 +0100185 }
186
187 if (!unsupportedOperationsAsString.empty())
188 {
189 std::istringstream argStream(unsupportedOperationsAsString);
190
191 std::string s;
192 while (!argStream.eof())
193 {
194 std::getline(argStream, s, ',');
195 try
196 {
197 unsigned int operationIdx = std::stoi(s);
198 m_ForcedUnsupportedOperations.insert(operationIdx);
199 }
200 catch (const std::invalid_argument&)
201 {
202 ALOGW("Ignoring invalid integer argument in -u/--unsupported-operations value: %s", s.c_str());
203 }
204 }
205 }
206
207 if (!m_ClTunedParametersFile.empty())
208 {
209 // The mode is only relevant if the file path has been provided
210 if (clTunedParametersModeAsString == "UseTunedParameters")
211 {
212 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UseTunedParameters;
213 }
214 else if (clTunedParametersModeAsString == "UpdateTunedParameters")
215 {
216 m_ClTunedParametersMode = armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters;
217 }
218 else
219 {
220 ALOGW("Requested unknown cl-tuned-parameters-mode '%s'. Defaulting to UseTunedParameters",
221 clTunedParametersModeAsString.c_str());
222 }
Ruomei Yan689c6ee2019-04-25 17:48:41 +0100223
224 if (clTuningLevelAsString == "exhaustive")
225 {
226 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Exhaustive;
227 }
228 else if (clTuningLevelAsString == "normal")
229 {
230 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Normal;
231 }
232 else if (clTuningLevelAsString == "rapid")
233 {
234 m_ClTuningLevel = armnn::IGpuAccTunedParameters::TuningLevel::Rapid;
235 }
236 else
237 {
238 ALOGW("Requested unknown cl-tuner-mode '%s'. Defaulting to rapid",
239 clTuningLevelAsString.c_str());
240 }
telsoa01ce3e84a2018-08-31 09:31:35 +0100241 }
242}
243
244} // namespace armnn_driver