blob: 6168c9d0383232d6e36f7005b609daa1e53307f2 [file] [log] [blame]
Kevin May42477c12020-03-26 13:34:14 +00001//
2// Copyright © 2020 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ArmnnDriverImpl.hpp"
7#include "../ArmnnPreparedModel_1_3.hpp"
8#include "../ModelToINetworkConverter.hpp"
9#include "../SystemPropertiesUtils.hpp"
10
11#include <log/log.h>
12
13namespace
14{
Kevin May42477c12020-03-26 13:34:14 +000015const char *g_RelaxedFloat32toFloat16PerformanceExecTime = "ArmNN.relaxedFloat32toFloat16Performance.execTime";
16const char *g_RelaxedFloat32toFloat16PerformancePowerUsage = "ArmNN.relaxedFloat32toFloat16Performance.powerUsage";
17
Kevin May2eaa1192020-04-15 16:50:57 +010018const char *g_ifPerformanceExecTime = "ArmNN.ifPerformance.execTime";
19const char *g_ifPerformancePowerUsage = "ArmNN.ifPerformance.powerUsage";
20
21const char *g_whilePerformanceExecTime = "ArmNN.whilePerformance.execTime";
22const char *g_whilePerformancePowerUsage = "ArmNN.whilePerformance.powerUsage";
23
Kevin May42477c12020-03-26 13:34:14 +000024const char *g_OperandTypeTensorFloat32PerformanceExecTime = "Armnn.operandTypeTensorFloat32Performance.execTime";
25const char *g_OperandTypeTensorFloat32PerformancePowerUsage = "Armnn.operandTypeTensorFloat32Performance.powerUsage";
26
27const char *g_OperandTypeFloat32PerformanceExecTime = "Armnn.operandTypeFloat32Performance.execTime";
28const char *g_OperandTypeFloat32PerformancePowerUsage = "Armnn.operandTypeFloat32Performance.powerUsage";
29
30const char *g_OperandTypeTensorFloat16PerformanceExecTime = "Armnn.operandTypeTensorFloat16Performance.execTime";
31const char *g_OperandTypeTensorFloat16PerformancePowerUsage = "Armnn.operandTypeTensorFloat16Performance.powerUsage";
32
33const char *g_OperandTypeFloat16PerformanceExecTime = "Armnn.operandTypeFloat16Performance.execTime";
34const char *g_OperandTypeFloat16PerformancePowerUsage = "Armnn.operandTypeFloat16Performance.powerUsage";
35
36const char *g_OperandTypeTensorQuant8AsymmPerformanceExecTime =
37 "Armnn.operandTypeTensorQuant8AsymmPerformance.execTime";
38const char *g_OperandTypeTensorQuant8AsymmPerformancePowerUsage =
39 "Armnn.operandTypeTensorQuant8AsymmPerformance.powerUsage";
40
41const char *g_OperandTypeTensorQuant8AsymmSignedPerformanceExecTime =
42 "Armnn.operandTypeTensorQuant8AsymmSignedPerformance.execTime";
43const char *g_OperandTypeTensorQuant8AsymmSignedPerformancePowerUsage =
44 "Armnn.operandTypeTensorQuant8AsymmSignedPerformance.powerUsage";
45
46const char *g_OperandTypeTensorQuant16SymmPerformanceExecTime =
47 "Armnn.operandTypeTensorQuant16SymmPerformance.execTime";
48const char *g_OperandTypeTensorQuant16SymmPerformancePowerUsage =
49 "Armnn.operandTypeTensorQuant16SymmPerformance.powerUsage";
50
51const char *g_OperandTypeTensorQuant8SymmPerformanceExecTime =
52 "Armnn.operandTypeTensorQuant8SymmPerformance.execTime";
53const char *g_OperandTypeTensorQuant8SymmPerformancePowerUsage =
54 "Armnn.operandTypeTensorQuant8SymmPerformance.powerUsage";
55
56const char *g_OperandTypeTensorQuant8SymmPerChannelPerformanceExecTime =
57 "Armnn.operandTypeTensorQuant8SymmPerChannelPerformance.execTime";
58const char *g_OperandTypeTensorQuant8SymmPerChannelPerformancePowerUsage =
59 "Armnn.operandTypeTensorQuant8SymmPerChannelPerformance.powerUsage";
60
61
62const char *g_OperandTypeTensorInt32PerformanceExecTime = "Armnn.operandTypeTensorInt32Performance.execTime";
63const char *g_OperandTypeTensorInt32PerformancePowerUsage = "Armnn.operandTypeTensorInt32Performance.powerUsage";
64
65const char *g_OperandTypeInt32PerformanceExecTime = "Armnn.operandTypeInt32Performance.execTime";
66const char *g_OperandTypeInt32PerformancePowerUsage = "Armnn.operandTypeInt32Performance.powerUsage";
67
68
69void NotifyCallbackAndCheck(const sp<V1_3::IPreparedModelCallback>& callback,
70 V1_3::ErrorStatus errorStatus,
71 const sp<V1_3::IPreparedModel>& preparedModelPtr)
72{
73 Return<void> returned = callback->notify_1_3(errorStatus, preparedModelPtr);
74 // This check is required, if the callback fails and it isn't checked it will bring down the service
75 if (!returned.isOk())
76 {
77 ALOGE("ArmnnDriverImpl::prepareModel: hidl callback failed to return properly: %s ",
78 returned.description().c_str());
79 }
80}
81
82Return<V1_3::ErrorStatus> FailPrepareModel(V1_3::ErrorStatus error,
83 const std::string& message,
84 const sp<V1_3::IPreparedModelCallback>& callback)
85{
86 ALOGW("ArmnnDriverImpl::prepareModel: %s", message.c_str());
87 NotifyCallbackAndCheck(callback, error, nullptr);
88 return error;
89}
90
91} // anonymous namespace
92
93namespace armnn_driver
94{
95namespace hal_1_3
96{
97
98Return<V1_3::ErrorStatus> ArmnnDriverImpl::prepareArmnnModel_1_3(
99 const armnn::IRuntimePtr& runtime,
100 const armnn::IGpuAccTunedParametersPtr& clTunedParameters,
101 const DriverOptions& options,
102 const V1_3::Model& model,
103 const sp<V1_3::IPreparedModelCallback>& cb,
Narumol Prangnawaratcad4e912020-06-02 12:07:43 +0100104 bool float32ToFloat16,
105 V1_3::Priority priority)
Kevin May42477c12020-03-26 13:34:14 +0000106{
107 ALOGV("ArmnnDriverImpl::prepareArmnnModel_1_3()");
108
109 if (cb.get() == nullptr)
110 {
111 ALOGW("ArmnnDriverImpl::prepareModel: Invalid callback passed to prepareModel");
112 return V1_3::ErrorStatus::INVALID_ARGUMENT;
113 }
114
115 if (!runtime)
116 {
117 return FailPrepareModel(V1_3::ErrorStatus::DEVICE_UNAVAILABLE, "Device unavailable", cb);
118 }
119
120 if (!android::nn::validateModel(model))
121 {
122 return FailPrepareModel(V1_3::ErrorStatus::INVALID_ARGUMENT, "Invalid model passed as input", cb);
123 }
124
125 // Deliberately ignore any unsupported operations requested by the options -
126 // at this point we're being asked to prepare a model that we've already declared support for
127 // and the operation indices may be different to those in getSupportedOperations anyway.
128 std::set<unsigned int> unsupportedOperations;
129 ModelToINetworkConverter<HalPolicy> modelConverter(options.GetBackends(),
130 model,
131 unsupportedOperations);
132
133 if (modelConverter.GetConversionResult() != ConversionResult::Success)
134 {
135 FailPrepareModel(V1_3::ErrorStatus::GENERAL_FAILURE, "ModelToINetworkConverter failed", cb);
136 return V1_3::ErrorStatus::NONE;
137 }
138
139 // Optimize the network
140 armnn::IOptimizedNetworkPtr optNet(nullptr, nullptr);
141 armnn::OptimizerOptions OptOptions;
142 OptOptions.m_ReduceFp32ToFp16 = float32ToFloat16;
143
144 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 }
153 catch (std::exception& e)
154 {
155 std::stringstream message;
156 message << "Exception (" << e.what() << ") caught from optimize.";
157 FailPrepareModel(V1_3::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
158 return V1_3::ErrorStatus::NONE;
159 }
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 }
170 FailPrepareModel(V1_3::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
171 return V1_3::ErrorStatus::NONE;
172 }
173
174 // Export the optimized network graph to a dot file if an output dump directory
175 // has been specified in the drivers' arguments.
176 std::string dotGraphFileName = ExportNetworkGraphToDotFile(*optNet,
177 options.GetRequestInputsAndOutputsDumpDir());
178
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 {
185 return FailPrepareModel(V1_3::ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb);
186 }
187 }
188 catch (std::exception& e)
189 {
190 std::stringstream message;
191 message << "Exception (" << e.what()<< ") caught from LoadNetwork.";
192 FailPrepareModel(V1_3::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
193 return V1_3::ErrorStatus::NONE;
194 }
195
196 // 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
202 std::unique_ptr<ArmnnPreparedModel_1_3<hal_1_3::HalPolicy>> preparedModel(
203 new ArmnnPreparedModel_1_3<hal_1_3::HalPolicy>(
204 netId,
205 runtime.get(),
206 model,
207 options.GetRequestInputsAndOutputsDumpDir(),
Narumol Prangnawaratcad4e912020-06-02 12:07:43 +0100208 options.IsGpuProfilingEnabled(),
209 priority));
Kevin May42477c12020-03-26 13:34:14 +0000210
211 // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if
212 // this is enabled) before the first 'real' inference which removes the overhead of the first inference.
213 if (!preparedModel->ExecuteWithDummyInputs())
214 {
215 return FailPrepareModel(V1_3::ErrorStatus::GENERAL_FAILURE, "Network could not be executed", cb);
216 }
217
218 if (clTunedParameters &&
219 options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters)
220 {
221 // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file.
222 try
223 {
224 clTunedParameters->Save(options.GetClTunedParametersFile().c_str());
225 }
226 catch (std::exception& error)
227 {
228 ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s",
229 options.GetClTunedParametersFile().c_str(), error.what());
230 }
231 }
232
233 NotifyCallbackAndCheck(cb, V1_3::ErrorStatus::NONE, preparedModel.release());
234
235 return V1_3::ErrorStatus::NONE;
236}
237
238Return<void> ArmnnDriverImpl::getCapabilities_1_3(const armnn::IRuntimePtr& runtime,
239 V1_3::IDevice::getCapabilities_1_3_cb cb)
240{
241 ALOGV("hal_1_3::ArmnnDriverImpl::getCapabilities()");
242
243 V1_3::Capabilities capabilities;
244
245 float defaultValue = .1f;
246
247 if (runtime)
248 {
249 capabilities.relaxedFloat32toFloat16PerformanceScalar.execTime =
250 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformanceExecTime, defaultValue);
251
Kevin May2eaa1192020-04-15 16:50:57 +0100252 capabilities.relaxedFloat32toFloat16PerformanceScalar.powerUsage =
253 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformancePowerUsage, defaultValue);
254
255 capabilities.relaxedFloat32toFloat16PerformanceTensor.execTime =
256 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformanceExecTime, defaultValue);
257
Kevin May42477c12020-03-26 13:34:14 +0000258 capabilities.relaxedFloat32toFloat16PerformanceTensor.powerUsage =
259 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformancePowerUsage, defaultValue);
260
Kevin May2eaa1192020-04-15 16:50:57 +0100261 capabilities.ifPerformance.execTime =
262 ParseSystemProperty(g_ifPerformanceExecTime, defaultValue);
263
264 capabilities.ifPerformance.powerUsage =
265 ParseSystemProperty(g_ifPerformancePowerUsage, defaultValue);
266
267 capabilities.whilePerformance.execTime =
268 ParseSystemProperty(g_whilePerformanceExecTime, defaultValue);
269
270 capabilities.whilePerformance.powerUsage =
271 ParseSystemProperty(g_whilePerformancePowerUsage, defaultValue);
272
Kevin May42477c12020-03-26 13:34:14 +0000273 // Set the base value for all operand types
274 capabilities.operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_3>({FLT_MAX, FLT_MAX});
275
276 // Load supported operand types
277 update(&capabilities.operandPerformance, V1_3::OperandType::TENSOR_FLOAT32,
278 {
279 .execTime = ParseSystemProperty(g_OperandTypeTensorFloat32PerformanceExecTime, defaultValue),
280 .powerUsage = ParseSystemProperty(g_OperandTypeTensorFloat32PerformancePowerUsage, defaultValue)
281 });
282
283 update(&capabilities.operandPerformance, V1_3::OperandType::FLOAT32,
284 {
285 .execTime = ParseSystemProperty(g_OperandTypeFloat32PerformanceExecTime, defaultValue),
286 .powerUsage = ParseSystemProperty(g_OperandTypeFloat32PerformancePowerUsage, defaultValue)
287 });
288
289 update(&capabilities.operandPerformance, V1_3::OperandType::TENSOR_FLOAT16,
290 {
291 .execTime = ParseSystemProperty(g_OperandTypeTensorFloat16PerformanceExecTime, defaultValue),
292 .powerUsage = ParseSystemProperty(g_OperandTypeTensorFloat16PerformancePowerUsage, defaultValue)
293 });
294
295 update(&capabilities.operandPerformance, V1_3::OperandType::FLOAT16,
296 {
297 .execTime = ParseSystemProperty(g_OperandTypeFloat16PerformanceExecTime, defaultValue),
298 .powerUsage = ParseSystemProperty(g_OperandTypeFloat16PerformancePowerUsage, defaultValue)
299 });
300
301 update(&capabilities.operandPerformance, V1_3::OperandType::TENSOR_QUANT8_ASYMM,
302 {
303 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant8AsymmPerformanceExecTime, defaultValue),
304 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant8AsymmPerformancePowerUsage, defaultValue)
305 });
306
307 update(&capabilities.operandPerformance, V1_3::OperandType::TENSOR_QUANT8_SYMM,
308 {
309 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerformanceExecTime, defaultValue),
310 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerformancePowerUsage, defaultValue)
311 });
312 update(&capabilities.operandPerformance, V1_3::OperandType::TENSOR_QUANT8_ASYMM_SIGNED,
313 {
314 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant8AsymmSignedPerformanceExecTime,
315 defaultValue),
316 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant8AsymmSignedPerformancePowerUsage,
317 defaultValue)
318 });
319
320 update(&capabilities.operandPerformance, V1_3::OperandType::TENSOR_QUANT16_SYMM,
321 {
322 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant16SymmPerformanceExecTime, defaultValue),
323 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant16SymmPerformancePowerUsage, defaultValue)
324 });
325
326 update(&capabilities.operandPerformance, V1_3::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL,
327 {
328 .execTime =
329 ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerChannelPerformanceExecTime, defaultValue),
330 .powerUsage =
331 ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerChannelPerformancePowerUsage, defaultValue)
332 });
333
334 update(&capabilities.operandPerformance, V1_3::OperandType::TENSOR_INT32,
335 {
336 .execTime = ParseSystemProperty(g_OperandTypeTensorInt32PerformanceExecTime, defaultValue),
337 .powerUsage = ParseSystemProperty(g_OperandTypeTensorInt32PerformancePowerUsage, defaultValue)
338 });
339
340 update(&capabilities.operandPerformance, V1_3::OperandType::INT32,
341 {
342 .execTime = ParseSystemProperty(g_OperandTypeInt32PerformanceExecTime, defaultValue),
343 .powerUsage = ParseSystemProperty(g_OperandTypeInt32PerformancePowerUsage, defaultValue)
344 });
345
346 cb(V1_3::ErrorStatus::NONE, capabilities);
347 }
348 else
349 {
Kevin May2eaa1192020-04-15 16:50:57 +0100350 capabilities.relaxedFloat32toFloat16PerformanceScalar.execTime = 0;
351 capabilities.relaxedFloat32toFloat16PerformanceScalar.powerUsage = 0;
352 capabilities.relaxedFloat32toFloat16PerformanceTensor.execTime = 0;
353 capabilities.relaxedFloat32toFloat16PerformanceTensor.powerUsage = 0;
354 capabilities.ifPerformance.execTime = 0;
355 capabilities.ifPerformance.powerUsage = 0;
356 capabilities.whilePerformance.execTime = 0;
357 capabilities.whilePerformance.powerUsage = 0;
Kevin May42477c12020-03-26 13:34:14 +0000358
359 // Set the base value for all operand types
360 capabilities.operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_3>({0.f, 0.0f});
361
362 cb(V1_3::ErrorStatus::DEVICE_UNAVAILABLE, capabilities);
363 }
364
365 return Void();
366}
367
368} // namespace hal_1_3
369} // namespace armnn_driver