blob: 6d713e0671412d48f736d7d98f644686fe2fa511 [file] [log] [blame]
Mike Kellyb5fdf382019-06-11 16:35:25 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ArmnnDriverImpl.hpp"
7#include "../ArmnnPreparedModel_1_2.hpp"
8#include "../ModelToINetworkConverter.hpp"
9#include "../SystemPropertiesUtils.hpp"
10
11#include <log/log.h>
12
13namespace
14{
15
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +010016const char *g_RelaxedFloat32toFloat16PerformanceExecTime = "ArmNN.relaxedFloat32toFloat16Performance.execTime";
FinnWilliamsArmdf655ee2019-07-24 16:04:18 +010017const char *g_RelaxedFloat32toFloat16PerformancePowerUsage = "ArmNN.relaxedFloat32toFloat16Performance.powerUsage";
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +010018
19const char *g_OperandTypeTensorFloat32PerformanceExecTime = "Armnn.operandTypeTensorFloat32Performance.execTime";
20const char *g_OperandTypeTensorFloat32PerformancePowerUsage = "Armnn.operandTypeTensorFloat32Performance.powerUsage";
21
22const char *g_OperandTypeFloat32PerformanceExecTime = "Armnn.operandTypeFloat32Performance.execTime";
23const char *g_OperandTypeFloat32PerformancePowerUsage = "Armnn.operandTypeFloat32Performance.powerUsage";
24
25const char *g_OperandTypeTensorFloat16PerformanceExecTime = "Armnn.operandTypeTensorFloat16Performance.execTime";
26const char *g_OperandTypeTensorFloat16PerformancePowerUsage = "Armnn.operandTypeTensorFloat16Performance.powerUsage";
27
28const char *g_OperandTypeFloat16PerformanceExecTime = "Armnn.operandTypeFloat16Performance.execTime";
29const char *g_OperandTypeFloat16PerformancePowerUsage = "Armnn.operandTypeFloat16Performance.powerUsage";
30
31const char *g_OperandTypeTensorQuant8AsymmPerformanceExecTime =
32 "Armnn.operandTypeTensorQuant8AsymmPerformance.execTime";
33const char *g_OperandTypeTensorQuant8AsymmPerformancePowerUsage =
34 "Armnn.operandTypeTensorQuant8AsymmPerformance.powerUsage";
35
36const char *g_OperandTypeTensorQuant16SymmPerformanceExecTime =
37 "Armnn.operandTypeTensorQuant16SymmPerformance.execTime";
38const char *g_OperandTypeTensorQuant16SymmPerformancePowerUsage =
39 "Armnn.operandTypeTensorQuant16SymmPerformance.powerUsage";
40
Pablo Tellofb45e2f2019-10-18 16:51:57 +010041const char *g_OperandTypeTensorQuant8SymmPerformanceExecTime =
42 "Armnn.operandTypeTensorQuant8SymmPerformance.execTime";
43const char *g_OperandTypeTensorQuant8SymmPerformancePowerUsage =
44 "Armnn.operandTypeTensorQuant8SymmPerformance.powerUsage";
45
Kevin May87cb7612019-11-11 17:30:35 +000046const char *g_OperandTypeTensorQuant8SymmPerChannelPerformanceExecTime =
47 "Armnn.operandTypeTensorQuant8SymmPerChannelPerformance.execTime";
48const char *g_OperandTypeTensorQuant8SymmPerChannelPerformancePowerUsage =
49 "Armnn.operandTypeTensorQuant8SymmPerChannelPerformance.powerUsage";
50
Pablo Tellofb45e2f2019-10-18 16:51:57 +010051
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +010052const char *g_OperandTypeTensorInt32PerformanceExecTime = "Armnn.operandTypeTensorInt32Performance.execTime";
53const char *g_OperandTypeTensorInt32PerformancePowerUsage = "Armnn.operandTypeTensorInt32Performance.powerUsage";
54
55const char *g_OperandTypeInt32PerformanceExecTime = "Armnn.operandTypeInt32Performance.execTime";
56const char *g_OperandTypeInt32PerformancePowerUsage = "Armnn.operandTypeInt32Performance.powerUsage";
57
58
Mike Kellyb5fdf382019-06-11 16:35:25 +010059void NotifyCallbackAndCheck(const sp<V1_2::IPreparedModelCallback>& callback,
Kevin Mayec1e5b82020-02-26 17:00:39 +000060 V1_0::ErrorStatus errorStatus,
Mike Kellyb5fdf382019-06-11 16:35:25 +010061 const sp<V1_2::IPreparedModel>& preparedModelPtr)
62{
Ferran Balaguerb2397fd2019-07-25 12:12:39 +010063 Return<void> returned = callback->notify_1_2(errorStatus, preparedModelPtr);
Mike Kellyb5fdf382019-06-11 16:35:25 +010064 // This check is required, if the callback fails and it isn't checked it will bring down the service
65 if (!returned.isOk())
66 {
67 ALOGE("ArmnnDriverImpl::prepareModel: hidl callback failed to return properly: %s ",
68 returned.description().c_str());
69 }
70}
71
Kevin Mayec1e5b82020-02-26 17:00:39 +000072Return<V1_0::ErrorStatus> FailPrepareModel(V1_0::ErrorStatus error,
73 const std::string& message,
74 const sp<V1_2::IPreparedModelCallback>& callback)
Mike Kellyb5fdf382019-06-11 16:35:25 +010075{
76 ALOGW("ArmnnDriverImpl::prepareModel: %s", message.c_str());
77 NotifyCallbackAndCheck(callback, error, nullptr);
78 return error;
79}
80
81} // anonymous namespace
82
83namespace armnn_driver
84{
85namespace hal_1_2
86{
87
Kevin Mayec1e5b82020-02-26 17:00:39 +000088Return<V1_0::ErrorStatus> ArmnnDriverImpl::prepareArmnnModel_1_2(
89 const armnn::IRuntimePtr& runtime,
90 const armnn::IGpuAccTunedParametersPtr& clTunedParameters,
91 const DriverOptions& options,
92 const V1_2::Model& model,
93 const sp<V1_2::IPreparedModelCallback>& cb,
94 bool float32ToFloat16)
Mike Kellyb5fdf382019-06-11 16:35:25 +010095{
Matteo Martincigh0bd89a82019-07-02 16:53:10 +010096 ALOGV("ArmnnDriverImpl::prepareArmnnModel_1_2()");
Mike Kellyb5fdf382019-06-11 16:35:25 +010097
98 if (cb.get() == nullptr)
99 {
100 ALOGW("ArmnnDriverImpl::prepareModel: Invalid callback passed to prepareModel");
Kevin Mayec1e5b82020-02-26 17:00:39 +0000101 return V1_0::ErrorStatus::INVALID_ARGUMENT;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100102 }
103
104 if (!runtime)
105 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000106 return FailPrepareModel(V1_0::ErrorStatus::DEVICE_UNAVAILABLE, "Device unavailable", cb);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100107 }
108
109 if (!android::nn::validateModel(model))
110 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000111 return FailPrepareModel(V1_0::ErrorStatus::INVALID_ARGUMENT, "Invalid model passed as input", cb);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100112 }
113
114 // Deliberately ignore any unsupported operations requested by the options -
115 // at this point we're being asked to prepare a model that we've already declared support for
116 // and the operation indices may be different to those in getSupportedOperations anyway.
117 std::set<unsigned int> unsupportedOperations;
118 ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(),
119 model,
120 unsupportedOperations);
121
122 if (modelConverter.GetConversionResult() != ConversionResult::Success)
123 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000124 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "ModelToINetworkConverter failed", cb);
125 return V1_0::ErrorStatus::NONE;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100126 }
127
128 // Optimize the network
129 armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr);
130 armnn::OptimizerOptions OptOptions;
131 OptOptions.m_ReduceFp32ToFp16 = float32ToFloat16;
132
Mike Kelly7ed56dd2020-09-30 20:22:56 +0100133 armnn::BackendOptions gpuAcc("GpuAcc",
134 {
135 { "FastMathEnabled", options.IsFastMathEnabled() }
136 });
137 armnn::BackendOptions cpuAcc("CpuAcc",
138 {
139 { "FastMathEnabled", options.IsFastMathEnabled() }
140 });
141 OptOptions.m_ModelOptions.push_back(gpuAcc);
142 OptOptions.m_ModelOptions.push_back(cpuAcc);
143
Mike Kellyb5fdf382019-06-11 16:35:25 +0100144 std::vector<std::string> errMessages;
145 try
146 {
147 optNet = armnn::Optimize(*modelConverter.GetINetwork(),
148 options.GetBackends(),
149 runtime->GetDeviceSpec(),
150 OptOptions,
151 errMessages);
152 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000153 catch (std::exception &e)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100154 {
155 std::stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000156 message << "Exception (" << e.what() << ") caught from optimize.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000157 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
158 return V1_0::ErrorStatus::NONE;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100159 }
160
161 // Check that the optimized network is valid.
162 if (!optNet)
163 {
164 std::stringstream message;
165 message << "Invalid optimized network";
166 for (const std::string& msg : errMessages)
167 {
168 message << "\n" << msg;
169 }
Kevin Mayec1e5b82020-02-26 17:00:39 +0000170 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
171 return V1_0::ErrorStatus::NONE;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100172 }
173
174 // Export the optimized network graph to a dot file if an output dump directory
175 // has been specified in the drivers' arguments.
Jim Flynn829ad302019-12-13 14:43:24 +0000176 std::string dotGraphFileName = ExportNetworkGraphToDotFile(*optNet,
177 options.GetRequestInputsAndOutputsDumpDir());
Mike Kellyb5fdf382019-06-11 16:35:25 +0100178
179 // Load it into the runtime.
180 armnn::NetworkId netId = 0;
181 try
182 {
183 if (runtime->LoadNetwork(netId, move(optNet)) != armnn::Status::Success)
184 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000185 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100186 }
187 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000188 catch (std::exception& e)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100189 {
190 std::stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000191 message << "Exception (" << e.what()<< ") caught from LoadNetwork.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000192 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
193 return V1_0::ErrorStatus::NONE;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100194 }
195
Jim Flynn829ad302019-12-13 14:43:24 +0000196 // Now that we have a networkId for the graph rename the dump file to use it
197 // so that we can associate the graph file and the input/output tensor dump files
198 RenameGraphDotFile(dotGraphFileName,
199 options.GetRequestInputsAndOutputsDumpDir(),
200 netId);
201
Mike Kellyb5fdf382019-06-11 16:35:25 +0100202 std::unique_ptr<ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>> preparedModel(
203 new ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>(
204 netId,
205 runtime.get(),
206 model,
207 options.GetRequestInputsAndOutputsDumpDir(),
208 options.IsGpuProfilingEnabled()));
209
210 // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if
211 // this is enabled) before the first 'real' inference which removes the overhead of the first inference.
212 if (!preparedModel->ExecuteWithDummyInputs())
213 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000214 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be executed", cb);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100215 }
216
217 if (clTunedParameters &&
218 options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters)
219 {
220 // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file.
221 try
222 {
223 clTunedParameters->Save(options.GetClTunedParametersFile().c_str());
224 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000225 catch (std::exception& error)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100226 {
227 ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s",
228 options.GetClTunedParametersFile().c_str(), error.what());
229 }
230 }
231
Kevin Mayec1e5b82020-02-26 17:00:39 +0000232 NotifyCallbackAndCheck(cb, V1_0::ErrorStatus::NONE, preparedModel.release());
Mike Kellyb5fdf382019-06-11 16:35:25 +0100233
Kevin Mayec1e5b82020-02-26 17:00:39 +0000234 return V1_0::ErrorStatus::NONE;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100235}
236
237Return<void> ArmnnDriverImpl::getCapabilities_1_2(const armnn::IRuntimePtr& runtime,
238 V1_2::IDevice::getCapabilities_1_2_cb cb)
239{
240 ALOGV("hal_1_2::ArmnnDriverImpl::getCapabilities()");
241
242 V1_2::Capabilities capabilities;
243
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100244 float defaultValue = .1f;
245
Mike Kellyb5fdf382019-06-11 16:35:25 +0100246 if (runtime)
247 {
248 capabilities.relaxedFloat32toFloat16PerformanceScalar.execTime =
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100249 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformanceExecTime, defaultValue);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100250
Kevin May2eaa1192020-04-15 16:50:57 +0100251 capabilities.relaxedFloat32toFloat16PerformanceScalar.powerUsage =
252 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformancePowerUsage, defaultValue);
253
254 capabilities.relaxedFloat32toFloat16PerformanceTensor.execTime =
255 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformanceExecTime, defaultValue);
256
FinnWilliamsArmdf655ee2019-07-24 16:04:18 +0100257 capabilities.relaxedFloat32toFloat16PerformanceTensor.powerUsage =
258 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformancePowerUsage, defaultValue);
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100259
260 // Set the base value for all operand types
Kevin Mayec1e5b82020-02-26 17:00:39 +0000261 #ifdef ARMNN_ANDROID_R
262 capabilities.operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_2>({FLT_MAX, FLT_MAX});
263 #else
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100264 capabilities.operandPerformance = nonExtensionOperandPerformance({FLT_MAX, FLT_MAX});
Kevin Mayec1e5b82020-02-26 17:00:39 +0000265 #endif
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100266
267 // Load supported operand types
Kevin Mayec1e5b82020-02-26 17:00:39 +0000268 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_FLOAT32,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100269 {
270 .execTime = ParseSystemProperty(g_OperandTypeTensorFloat32PerformanceExecTime, defaultValue),
271 .powerUsage = ParseSystemProperty(g_OperandTypeTensorFloat32PerformancePowerUsage, defaultValue)
272 });
273
Kevin Mayec1e5b82020-02-26 17:00:39 +0000274 update(&capabilities.operandPerformance, V1_2::OperandType::FLOAT32,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100275 {
276 .execTime = ParseSystemProperty(g_OperandTypeFloat32PerformanceExecTime, defaultValue),
277 .powerUsage = ParseSystemProperty(g_OperandTypeFloat32PerformancePowerUsage, defaultValue)
278 });
279
Kevin Mayec1e5b82020-02-26 17:00:39 +0000280 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_FLOAT16,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100281 {
282 .execTime = ParseSystemProperty(g_OperandTypeTensorFloat16PerformanceExecTime, defaultValue),
283 .powerUsage = ParseSystemProperty(g_OperandTypeTensorFloat16PerformancePowerUsage, defaultValue)
284 });
285
Kevin Mayec1e5b82020-02-26 17:00:39 +0000286 update(&capabilities.operandPerformance, V1_2::OperandType::FLOAT16,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100287 {
288 .execTime = ParseSystemProperty(g_OperandTypeFloat16PerformanceExecTime, defaultValue),
289 .powerUsage = ParseSystemProperty(g_OperandTypeFloat16PerformancePowerUsage, defaultValue)
290 });
291
Kevin Mayec1e5b82020-02-26 17:00:39 +0000292 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_QUANT8_ASYMM,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100293 {
294 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant8AsymmPerformanceExecTime, defaultValue),
295 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant8AsymmPerformancePowerUsage, defaultValue)
296 });
297
Kevin Mayec1e5b82020-02-26 17:00:39 +0000298 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_QUANT8_SYMM,
Pablo Tellofb45e2f2019-10-18 16:51:57 +0100299 {
300 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerformanceExecTime, defaultValue),
301 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerformancePowerUsage, defaultValue)
302 });
303
Kevin Mayec1e5b82020-02-26 17:00:39 +0000304 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_QUANT16_SYMM,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100305 {
306 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant16SymmPerformanceExecTime, defaultValue),
307 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant16SymmPerformancePowerUsage, defaultValue)
308 });
309
Kevin Mayec1e5b82020-02-26 17:00:39 +0000310 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL,
Kevin May87cb7612019-11-11 17:30:35 +0000311 {
312 .execTime =
313 ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerChannelPerformanceExecTime, defaultValue),
314 .powerUsage =
315 ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerChannelPerformancePowerUsage, defaultValue)
316 });
317
Kevin Mayec1e5b82020-02-26 17:00:39 +0000318 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_INT32,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100319 {
320 .execTime = ParseSystemProperty(g_OperandTypeTensorInt32PerformanceExecTime, defaultValue),
321 .powerUsage = ParseSystemProperty(g_OperandTypeTensorInt32PerformancePowerUsage, defaultValue)
322 });
323
Kevin Mayec1e5b82020-02-26 17:00:39 +0000324 update(&capabilities.operandPerformance, V1_2::OperandType::INT32,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100325 {
326 .execTime = ParseSystemProperty(g_OperandTypeInt32PerformanceExecTime, defaultValue),
327 .powerUsage = ParseSystemProperty(g_OperandTypeInt32PerformancePowerUsage, defaultValue)
328 });
Mike Kellyb5fdf382019-06-11 16:35:25 +0100329
Kevin Mayec1e5b82020-02-26 17:00:39 +0000330 cb(V1_0::ErrorStatus::NONE, capabilities);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100331 }
332 else
333 {
Kevin May2eaa1192020-04-15 16:50:57 +0100334 capabilities.relaxedFloat32toFloat16PerformanceScalar.execTime = 0;
335 capabilities.relaxedFloat32toFloat16PerformanceScalar.powerUsage = 0;
336 capabilities.relaxedFloat32toFloat16PerformanceTensor.execTime = 0;
337 capabilities.relaxedFloat32toFloat16PerformanceTensor.powerUsage = 0;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100338
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100339 // Set the base value for all operand types
Kevin Mayec1e5b82020-02-26 17:00:39 +0000340 #ifdef ARMNN_ANDROID_R
341 capabilities.operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_2>({0.f, 0.0f});
342 #else
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100343 capabilities.operandPerformance = nonExtensionOperandPerformance({0.f, 0.0f});
Kevin Mayec1e5b82020-02-26 17:00:39 +0000344 #endif
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100345
Kevin Mayec1e5b82020-02-26 17:00:39 +0000346 cb(V1_0::ErrorStatus::DEVICE_UNAVAILABLE, capabilities);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100347 }
348
349 return Void();
350}
351
352} // namespace hal_1_2
Kevin Mayec1e5b82020-02-26 17:00:39 +0000353} // namespace armnn_driver