blob: 246d641579ab97a89c22248caedd52adc649e0b0 [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa015307bc12018-03-09 13:51:08 +00004//
5
6#define LOG_TAG "ArmnnDriver"
7
8#include "Utils.hpp"
9
Mike Kelly3c673942019-07-25 09:26:06 +010010#include <Half.hpp>
telsoa015307bc12018-03-09 13:51:08 +000011#include <Permute.hpp>
12
telsoa015307bc12018-03-09 13:51:08 +000013#include <cassert>
14#include <cinttypes>
telsoa015307bc12018-03-09 13:51:08 +000015
16using namespace android;
telsoa01ce3e84a2018-08-31 09:31:35 +010017using namespace android::hardware;
telsoa015307bc12018-03-09 13:51:08 +000018using namespace android::hidl::memory::V1_0;
19
20namespace armnn_driver
21{
22const armnn::PermutationVector g_DontPermute{};
23
24namespace
25{
26
27template <typename T>
28void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorShape& inTensorShape, const void* input,
29 void* output, const armnn::PermutationVector& mappings)
30{
31 const auto inputData = static_cast<const T*>(input);
32 const auto outputData = static_cast<T*>(output);
33
Matteo Martincigh2c444fc2019-01-07 10:18:47 +000034 armnnUtils::Permute(armnnUtils::Permuted(inTensorShape, mappings), mappings, inputData, outputData, sizeof(T));
telsoa015307bc12018-03-09 13:51:08 +000035}
36
37} // anonymous namespace
38
39void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorInfo& tensor, const void* input, void* output,
40 const armnn::PermutationVector& mappings)
41{
42 assert(tensor.GetNumDimensions() == 4U);
43
44 switch(tensor.GetDataType())
45 {
Mike Kelly3c673942019-07-25 09:26:06 +010046 case armnn::DataType::Float16:
47 SwizzleAndroidNn4dTensorToArmNn<armnn::Half>(tensor.GetShape(), input, output, mappings);
48 break;
telsoa015307bc12018-03-09 13:51:08 +000049 case armnn::DataType::Float32:
50 SwizzleAndroidNn4dTensorToArmNn<float>(tensor.GetShape(), input, output, mappings);
51 break;
52 case armnn::DataType::QuantisedAsymm8:
53 SwizzleAndroidNn4dTensorToArmNn<uint8_t>(tensor.GetShape(), input, output, mappings);
54 break;
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +000055 case armnn::DataType::QuantizedSymm8PerAxis:
56 SwizzleAndroidNn4dTensorToArmNn<int8_t>(tensor.GetShape(), input, output, mappings);
57 break;
telsoa015307bc12018-03-09 13:51:08 +000058 default:
59 ALOGW("Unknown armnn::DataType for swizzling");
60 assert(0);
61 }
62}
63
64void* GetMemoryFromPool(DataLocation location, const std::vector<android::nn::RunTimePoolInfo>& memPools)
65{
66 // find the location within the pool
67 assert(location.poolIndex < memPools.size());
68
surmeh01deb3bdb2018-07-05 12:06:04 +010069 const android::nn::RunTimePoolInfo& memPool = memPools[location.poolIndex];
70
71 // Type android::nn::RunTimePoolInfo has changed between Android O and Android P, where
72 // "buffer" has been made private and must be accessed via the accessor method "getBuffer".
Mike Kellyb5fdf382019-06-11 16:35:25 +010073#if defined(ARMNN_ANDROID_P) || defined(ARMNN_ANDROID_Q) // Use the new Android implementation.
surmeh01deb3bdb2018-07-05 12:06:04 +010074 uint8_t* memPoolBuffer = memPool.getBuffer();
75#else // Fallback to the old Android O implementation.
76 uint8_t* memPoolBuffer = memPool.buffer;
77#endif
78
79 uint8_t* memory = memPoolBuffer + location.offset;
telsoa015307bc12018-03-09 13:51:08 +000080
81 return memory;
82}
83
Matthew Bentham912b3622019-05-03 15:49:14 +010084armnn::TensorInfo GetTensorInfoForOperand(const V1_0::Operand& operand)
telsoa015307bc12018-03-09 13:51:08 +000085{
86 armnn::DataType type;
87
88 switch (operand.type)
89 {
Matthew Bentham912b3622019-05-03 15:49:14 +010090 case V1_0::OperandType::TENSOR_FLOAT32:
telsoa015307bc12018-03-09 13:51:08 +000091 type = armnn::DataType::Float32;
92 break;
Matthew Bentham912b3622019-05-03 15:49:14 +010093 case V1_0::OperandType::TENSOR_QUANT8_ASYMM:
telsoa015307bc12018-03-09 13:51:08 +000094 type = armnn::DataType::QuantisedAsymm8;
95 break;
Matthew Bentham912b3622019-05-03 15:49:14 +010096 case V1_0::OperandType::TENSOR_INT32:
telsoa015307bc12018-03-09 13:51:08 +000097 type = armnn::DataType::Signed32;
98 break;
99 default:
Mike Kellyb5fdf382019-06-11 16:35:25 +0100100 throw UnsupportedOperand<V1_0::OperandType>(operand.type);
telsoa015307bc12018-03-09 13:51:08 +0000101 }
102
103 armnn::TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type);
104
105 ret.SetQuantizationScale(operand.scale);
106 ret.SetQuantizationOffset(operand.zeroPoint);
107
108 return ret;
109}
110
Mike Kellyb5fdf382019-06-11 16:35:25 +0100111#ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2
112
113armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand)
114{
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000115 using namespace armnn;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100116
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000117 DataType type;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100118 switch (operand.type)
119 {
120 case V1_2::OperandType::TENSOR_FLOAT32:
121 type = armnn::DataType::Float32;
122 break;
Mike Kelly3c673942019-07-25 09:26:06 +0100123 case V1_2::OperandType::TENSOR_FLOAT16:
124 type = armnn::DataType::Float16;
125 break;
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000126 case V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
127 type = armnn::DataType::QuantizedSymm8PerAxis;
128 break;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100129 case V1_2::OperandType::TENSOR_QUANT8_ASYMM:
130 type = armnn::DataType::QuantisedAsymm8;
131 break;
132 case V1_2::OperandType::TENSOR_QUANT16_SYMM:
133 type = armnn::DataType::QuantisedSymm16;
134 break;
135 case V1_2::OperandType::TENSOR_INT32:
136 type = armnn::DataType::Signed32;
137 break;
138 default:
139 throw UnsupportedOperand<V1_2::OperandType>(operand.type);
140 }
141
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000142 TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type);
143 if (type == DataType::QuantizedSymm8PerAxis)
144 {
145 // ExtraParams is expected to be of type channelQuant
146 BOOST_ASSERT(operand.extraParams.getDiscriminator() ==
147 V1_2::Operand::ExtraParams::hidl_discriminator::channelQuant);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100148
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000149 auto perAxisQuantParams = operand.extraParams.channelQuant();
150
151 ret.SetQuantizationScales(perAxisQuantParams.scales);
152 ret.SetQuantizationDim(MakeOptional<unsigned int>(perAxisQuantParams.channelDim));
153 }
154 else
155 {
156 ret.SetQuantizationScale(operand.scale);
157 ret.SetQuantizationOffset(operand.zeroPoint);
158 }
Mike Kellyb5fdf382019-06-11 16:35:25 +0100159
160 return ret;
161}
162
163#endif
164
Matthew Bentham912b3622019-05-03 15:49:14 +0100165std::string GetOperandSummary(const V1_0::Operand& operand)
telsoa015307bc12018-03-09 13:51:08 +0000166{
167 return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " +
168 toString(operand.type);
169}
170
Mike Kellyb5fdf382019-06-11 16:35:25 +0100171#ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2
172
173std::string GetOperandSummary(const V1_2::Operand& operand)
174{
175 return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " +
176 toString(operand.type);
177}
178
179#endif
180
telsoa015307bc12018-03-09 13:51:08 +0000181using DumpElementFunction = void (*)(const armnn::ConstTensor& tensor,
182 unsigned int elementIndex,
183 std::ofstream& fileStream);
184
185namespace
186{
187template <typename ElementType, typename PrintableType = ElementType>
188void DumpTensorElement(const armnn::ConstTensor& tensor, unsigned int elementIndex, std::ofstream& fileStream)
189{
190 const ElementType* elements = reinterpret_cast<const ElementType*>(tensor.GetMemoryArea());
191 fileStream << static_cast<PrintableType>(elements[elementIndex]) << ",";
192}
193
194constexpr const char* MemoryLayoutString(const armnn::ConstTensor& tensor)
195{
196 const char* str = "";
197
198 switch (tensor.GetNumDimensions())
199 {
200 case 4: { str = "(BHWC) "; break; }
201 case 3: { str = "(HWC) "; break; }
202 case 2: { str = "(HW) "; break; }
203 default: { str = ""; break; }
204 }
205
206 return str;
207}
208} // namespace
209
210void DumpTensor(const std::string& dumpDir,
211 const std::string& requestName,
212 const std::string& tensorName,
213 const armnn::ConstTensor& tensor)
214{
215 // The dump directory must exist in advance.
216 const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.dump") % dumpDir % requestName % tensorName);
217
218 std::ofstream fileStream;
219 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
220
221 if (!fileStream.good())
222 {
223 ALOGW("Could not open file %s for writing", fileName.c_str());
224 return;
225 }
226
227 DumpElementFunction dumpElementFunction = nullptr;
228
229 switch (tensor.GetDataType())
230 {
231 case armnn::DataType::Float32:
232 {
233 dumpElementFunction = &DumpTensorElement<float>;
234 break;
235 }
236 case armnn::DataType::QuantisedAsymm8:
237 {
238 dumpElementFunction = &DumpTensorElement<uint8_t, uint32_t>;
239 break;
240 }
241 case armnn::DataType::Signed32:
242 {
243 dumpElementFunction = &DumpTensorElement<int32_t>;
244 break;
245 }
246 default:
247 {
248 dumpElementFunction = nullptr;
249 }
250 }
251
252 if (dumpElementFunction != nullptr)
253 {
254 const unsigned int numDimensions = tensor.GetNumDimensions();
255
256 const unsigned int batch = (numDimensions == 4) ? tensor.GetShape()[numDimensions - 4] : 1;
257
258 const unsigned int height = (numDimensions >= 3)
259 ? tensor.GetShape()[numDimensions - 3]
260 : (numDimensions >= 2) ? tensor.GetShape()[numDimensions - 2] : 1;
261
262 const unsigned int width = (numDimensions >= 3)
263 ? tensor.GetShape()[numDimensions - 2]
264 : (numDimensions >= 1) ? tensor.GetShape()[numDimensions - 1] : 0;
265
266 const unsigned int channels = (numDimensions >= 3) ? tensor.GetShape()[numDimensions - 1] : 1;
267
268 fileStream << "# Number of elements " << tensor.GetNumElements() << std::endl;
269 fileStream << "# Dimensions " << MemoryLayoutString(tensor);
270 fileStream << "[" << tensor.GetShape()[0];
271 for (unsigned int d = 1; d < numDimensions; d++)
272 {
273 fileStream << "," << tensor.GetShape()[d];
274 }
275 fileStream << "]" << std::endl;
276
277 for (unsigned int e = 0, b = 0; b < batch; ++b)
278 {
279 if (numDimensions >= 4)
280 {
281 fileStream << "# Batch " << b << std::endl;
282 }
283 for (unsigned int c = 0; c < channels; c++)
284 {
285 if (numDimensions >= 3)
286 {
287 fileStream << "# Channel " << c << std::endl;
288 }
289 for (unsigned int h = 0; h < height; h++)
290 {
291 for (unsigned int w = 0; w < width; w++, e += channels)
292 {
293 (*dumpElementFunction)(tensor, e, fileStream);
294 }
295 fileStream << std::endl;
296 }
297 e -= channels - 1;
298 if (c < channels)
299 {
300 e -= ((height * width) - 1) * channels;
301 }
302 }
303 fileStream << std::endl;
304 }
305 fileStream << std::endl;
306 }
307 else
308 {
309 fileStream << "Cannot dump tensor elements: Unsupported data type "
310 << static_cast<unsigned int>(tensor.GetDataType()) << std::endl;
311 }
312
313 if (!fileStream.good())
314 {
315 ALOGW("An error occurred when writing to file %s", fileName.c_str());
316 }
317}
318
telsoa01ce3e84a2018-08-31 09:31:35 +0100319void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled,
320 const std::string& dumpDir,
321 armnn::NetworkId networkId,
322 const armnn::IProfiler* profiler)
323{
324 // Check if profiling is required.
325 if (!gpuProfilingEnabled)
326 {
327 return;
328 }
329
330 // The dump directory must exist in advance.
331 if (dumpDir.empty())
332 {
333 return;
334 }
335
336 BOOST_ASSERT(profiler);
337
338 // Set the name of the output profiling file.
339 const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.json")
340 % dumpDir
341 % std::to_string(networkId)
342 % "profiling");
343
344 // Open the ouput file for writing.
345 std::ofstream fileStream;
346 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
347
348 if (!fileStream.good())
349 {
350 ALOGW("Could not open file %s for writing", fileName.c_str());
351 return;
352 }
353
354 // Write the profiling info to a JSON file.
355 profiler->Print(fileStream);
356}
357
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +0100358bool IsDynamicTensor(const armnn::TensorInfo& outputInfo)
359{
360 // Dynamic tensors have at least one 0-sized dimension
361 return outputInfo.GetNumElements() == 0u;
362}
363
telsoa015307bc12018-03-09 13:51:08 +0000364} // namespace armnn_driver