blob: 4571fe0ea5a17d9f5ddae3321924269ef5151b87 [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
133 std::vector<std::string> errMessages;
134 try
135 {
136 optNet = armnn::Optimize(*modelConverter.GetINetwork(),
137 options.GetBackends(),
138 runtime->GetDeviceSpec(),
139 OptOptions,
140 errMessages);
141 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000142 catch (std::exception &e)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100143 {
144 std::stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000145 message << "Exception (" << e.what() << ") caught from optimize.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000146 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
147 return V1_0::ErrorStatus::NONE;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100148 }
149
150 // Check that the optimized network is valid.
151 if (!optNet)
152 {
153 std::stringstream message;
154 message << "Invalid optimized network";
155 for (const std::string& msg : errMessages)
156 {
157 message << "\n" << msg;
158 }
Kevin Mayec1e5b82020-02-26 17:00:39 +0000159 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
160 return V1_0::ErrorStatus::NONE;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100161 }
162
163 // Export the optimized network graph to a dot file if an output dump directory
164 // has been specified in the drivers' arguments.
Jim Flynn829ad302019-12-13 14:43:24 +0000165 std::string dotGraphFileName = ExportNetworkGraphToDotFile(*optNet,
166 options.GetRequestInputsAndOutputsDumpDir());
Mike Kellyb5fdf382019-06-11 16:35:25 +0100167
168 // Load it into the runtime.
169 armnn::NetworkId netId = 0;
170 try
171 {
172 if (runtime->LoadNetwork(netId, move(optNet)) != armnn::Status::Success)
173 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000174 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be loaded", cb);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100175 }
176 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000177 catch (std::exception& e)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100178 {
179 std::stringstream message;
Derek Lambertib9cb8442019-11-28 13:34:48 +0000180 message << "Exception (" << e.what()<< ") caught from LoadNetwork.";
Kevin Mayec1e5b82020-02-26 17:00:39 +0000181 FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, message.str(), cb);
182 return V1_0::ErrorStatus::NONE;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100183 }
184
Jim Flynn829ad302019-12-13 14:43:24 +0000185 // Now that we have a networkId for the graph rename the dump file to use it
186 // so that we can associate the graph file and the input/output tensor dump files
187 RenameGraphDotFile(dotGraphFileName,
188 options.GetRequestInputsAndOutputsDumpDir(),
189 netId);
190
Mike Kellyb5fdf382019-06-11 16:35:25 +0100191 std::unique_ptr<ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>> preparedModel(
192 new ArmnnPreparedModel_1_2<hal_1_2::HalPolicy>(
193 netId,
194 runtime.get(),
195 model,
196 options.GetRequestInputsAndOutputsDumpDir(),
197 options.IsGpuProfilingEnabled()));
198
199 // Run a single 'dummy' inference of the model. This means that CL kernels will get compiled (and tuned if
200 // this is enabled) before the first 'real' inference which removes the overhead of the first inference.
201 if (!preparedModel->ExecuteWithDummyInputs())
202 {
Kevin Mayec1e5b82020-02-26 17:00:39 +0000203 return FailPrepareModel(V1_0::ErrorStatus::GENERAL_FAILURE, "Network could not be executed", cb);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100204 }
205
206 if (clTunedParameters &&
207 options.GetClTunedParametersMode() == armnn::IGpuAccTunedParameters::Mode::UpdateTunedParameters)
208 {
209 // Now that we've done one inference the CL kernel parameters will have been tuned, so save the updated file.
210 try
211 {
212 clTunedParameters->Save(options.GetClTunedParametersFile().c_str());
213 }
Derek Lambertib9cb8442019-11-28 13:34:48 +0000214 catch (std::exception& error)
Mike Kellyb5fdf382019-06-11 16:35:25 +0100215 {
216 ALOGE("ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file '%s': %s",
217 options.GetClTunedParametersFile().c_str(), error.what());
218 }
219 }
220
Kevin Mayec1e5b82020-02-26 17:00:39 +0000221 NotifyCallbackAndCheck(cb, V1_0::ErrorStatus::NONE, preparedModel.release());
Mike Kellyb5fdf382019-06-11 16:35:25 +0100222
Kevin Mayec1e5b82020-02-26 17:00:39 +0000223 return V1_0::ErrorStatus::NONE;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100224}
225
226Return<void> ArmnnDriverImpl::getCapabilities_1_2(const armnn::IRuntimePtr& runtime,
227 V1_2::IDevice::getCapabilities_1_2_cb cb)
228{
229 ALOGV("hal_1_2::ArmnnDriverImpl::getCapabilities()");
230
231 V1_2::Capabilities capabilities;
232
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100233 float defaultValue = .1f;
234
Mike Kellyb5fdf382019-06-11 16:35:25 +0100235 if (runtime)
236 {
237 capabilities.relaxedFloat32toFloat16PerformanceScalar.execTime =
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100238 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformanceExecTime, defaultValue);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100239
Kevin May2eaa1192020-04-15 16:50:57 +0100240 capabilities.relaxedFloat32toFloat16PerformanceScalar.powerUsage =
241 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformancePowerUsage, defaultValue);
242
243 capabilities.relaxedFloat32toFloat16PerformanceTensor.execTime =
244 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformanceExecTime, defaultValue);
245
FinnWilliamsArmdf655ee2019-07-24 16:04:18 +0100246 capabilities.relaxedFloat32toFloat16PerformanceTensor.powerUsage =
247 ParseSystemProperty(g_RelaxedFloat32toFloat16PerformancePowerUsage, defaultValue);
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100248
249 // Set the base value for all operand types
Kevin Mayec1e5b82020-02-26 17:00:39 +0000250 #ifdef ARMNN_ANDROID_R
251 capabilities.operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_2>({FLT_MAX, FLT_MAX});
252 #else
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100253 capabilities.operandPerformance = nonExtensionOperandPerformance({FLT_MAX, FLT_MAX});
Kevin Mayec1e5b82020-02-26 17:00:39 +0000254 #endif
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100255
256 // Load supported operand types
Kevin Mayec1e5b82020-02-26 17:00:39 +0000257 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_FLOAT32,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100258 {
259 .execTime = ParseSystemProperty(g_OperandTypeTensorFloat32PerformanceExecTime, defaultValue),
260 .powerUsage = ParseSystemProperty(g_OperandTypeTensorFloat32PerformancePowerUsage, defaultValue)
261 });
262
Kevin Mayec1e5b82020-02-26 17:00:39 +0000263 update(&capabilities.operandPerformance, V1_2::OperandType::FLOAT32,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100264 {
265 .execTime = ParseSystemProperty(g_OperandTypeFloat32PerformanceExecTime, defaultValue),
266 .powerUsage = ParseSystemProperty(g_OperandTypeFloat32PerformancePowerUsage, defaultValue)
267 });
268
Kevin Mayec1e5b82020-02-26 17:00:39 +0000269 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_FLOAT16,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100270 {
271 .execTime = ParseSystemProperty(g_OperandTypeTensorFloat16PerformanceExecTime, defaultValue),
272 .powerUsage = ParseSystemProperty(g_OperandTypeTensorFloat16PerformancePowerUsage, defaultValue)
273 });
274
Kevin Mayec1e5b82020-02-26 17:00:39 +0000275 update(&capabilities.operandPerformance, V1_2::OperandType::FLOAT16,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100276 {
277 .execTime = ParseSystemProperty(g_OperandTypeFloat16PerformanceExecTime, defaultValue),
278 .powerUsage = ParseSystemProperty(g_OperandTypeFloat16PerformancePowerUsage, defaultValue)
279 });
280
Kevin Mayec1e5b82020-02-26 17:00:39 +0000281 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_QUANT8_ASYMM,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100282 {
283 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant8AsymmPerformanceExecTime, defaultValue),
284 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant8AsymmPerformancePowerUsage, defaultValue)
285 });
286
Kevin Mayec1e5b82020-02-26 17:00:39 +0000287 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_QUANT8_SYMM,
Pablo Tellofb45e2f2019-10-18 16:51:57 +0100288 {
289 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerformanceExecTime, defaultValue),
290 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerformancePowerUsage, defaultValue)
291 });
292
Kevin Mayec1e5b82020-02-26 17:00:39 +0000293 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_QUANT16_SYMM,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100294 {
295 .execTime = ParseSystemProperty(g_OperandTypeTensorQuant16SymmPerformanceExecTime, defaultValue),
296 .powerUsage = ParseSystemProperty(g_OperandTypeTensorQuant16SymmPerformancePowerUsage, defaultValue)
297 });
298
Kevin Mayec1e5b82020-02-26 17:00:39 +0000299 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL,
Kevin May87cb7612019-11-11 17:30:35 +0000300 {
301 .execTime =
302 ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerChannelPerformanceExecTime, defaultValue),
303 .powerUsage =
304 ParseSystemProperty(g_OperandTypeTensorQuant8SymmPerChannelPerformancePowerUsage, defaultValue)
305 });
306
Kevin Mayec1e5b82020-02-26 17:00:39 +0000307 update(&capabilities.operandPerformance, V1_2::OperandType::TENSOR_INT32,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100308 {
309 .execTime = ParseSystemProperty(g_OperandTypeTensorInt32PerformanceExecTime, defaultValue),
310 .powerUsage = ParseSystemProperty(g_OperandTypeTensorInt32PerformancePowerUsage, defaultValue)
311 });
312
Kevin Mayec1e5b82020-02-26 17:00:39 +0000313 update(&capabilities.operandPerformance, V1_2::OperandType::INT32,
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100314 {
315 .execTime = ParseSystemProperty(g_OperandTypeInt32PerformanceExecTime, defaultValue),
316 .powerUsage = ParseSystemProperty(g_OperandTypeInt32PerformancePowerUsage, defaultValue)
317 });
Mike Kellyb5fdf382019-06-11 16:35:25 +0100318
Kevin Mayec1e5b82020-02-26 17:00:39 +0000319 cb(V1_0::ErrorStatus::NONE, capabilities);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100320 }
321 else
322 {
Kevin May2eaa1192020-04-15 16:50:57 +0100323 capabilities.relaxedFloat32toFloat16PerformanceScalar.execTime = 0;
324 capabilities.relaxedFloat32toFloat16PerformanceScalar.powerUsage = 0;
325 capabilities.relaxedFloat32toFloat16PerformanceTensor.execTime = 0;
326 capabilities.relaxedFloat32toFloat16PerformanceTensor.powerUsage = 0;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100327
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100328 // Set the base value for all operand types
Kevin Mayec1e5b82020-02-26 17:00:39 +0000329 #ifdef ARMNN_ANDROID_R
330 capabilities.operandPerformance = nonExtensionOperandPerformance<HalVersion::V1_2>({0.f, 0.0f});
331 #else
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100332 capabilities.operandPerformance = nonExtensionOperandPerformance({0.f, 0.0f});
Kevin Mayec1e5b82020-02-26 17:00:39 +0000333 #endif
Ferran Balaguerd7c8eb92019-07-01 13:37:44 +0100334
Kevin Mayec1e5b82020-02-26 17:00:39 +0000335 cb(V1_0::ErrorStatus::DEVICE_UNAVAILABLE, capabilities);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100336 }
337
338 return Void();
339}
340
341} // namespace hal_1_2
Kevin Mayec1e5b82020-02-26 17:00:39 +0000342} // namespace armnn_driver