blob: 564dd7ae5c1ce9147cbbaa5f38b4525494490f52 [file] [log] [blame]
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +00001//
John Mcloughlin4cf29d62023-09-25 14:10:32 +01002// Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +00003// SPDX-License-Identifier: MIT
4//
Aron Virginas-Tardb1a2832019-11-12 16:15:11 +00005
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +00006#include "Debug.hpp"
Nikhil Raj7dcc6972021-04-30 15:44:24 +01007#include <common/include/ProfilingGuid.hpp>
Keith Davis15f9c682022-10-14 15:50:33 +01008#include <armnnUtils/Filesystem.hpp>
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +00009
Narumol Prangnawarat403a1852020-03-12 14:24:13 +000010#include <BFloat16.hpp>
Aron Virginas-Tardb1a2832019-11-12 16:15:11 +000011#include <Half.hpp>
12
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000013#include <algorithm>
14#include <iostream>
Keith Davis15f9c682022-10-14 15:50:33 +010015#include <iosfwd>
16#include <fstream>
17#include <sys/stat.h>
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000018
19namespace armnn
20{
21
Keith Davis15f9c682022-10-14 15:50:33 +010022template<typename T>
23void PrintOutput(const TensorInfo& inputInfo,
24 const T* inputData,
25 LayerGuid guid,
26 const std::string& layerName,
27 unsigned int slotIndex,
28 std::ostream& os)
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000029{
30 const unsigned int numDims = inputInfo.GetNumDimensions();
31 const unsigned int numElements = inputInfo.GetNumElements();
32 const TensorShape& inputShape = inputInfo.GetShape();
33
Rob Hughes9e10c2b2019-07-23 15:37:19 +010034 std::vector<unsigned int> strides(numDims, 0);
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000035 strides[numDims - 1] = inputShape[numDims - 1];
36
37 for (unsigned int i = 2; i <= numDims; i++)
38 {
39 strides[numDims - i] = strides[numDims - i + 1] * inputShape[numDims - i];
40 }
41
Keith Davis15f9c682022-10-14 15:50:33 +010042 os << "{ ";
43 os << "\"layerGuid\": " << guid << ", ";
44 os << "\"layerName\": \"" << layerName << "\", ";
45 os << "\"outputSlot\": " << slotIndex << ", ";
46 os << "\"shape\": ";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000047
Keith Davis15f9c682022-10-14 15:50:33 +010048 os << "[";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000049 for (unsigned int i = 0; i < numDims; i++)
50 {
Keith Davis15f9c682022-10-14 15:50:33 +010051 os << inputShape[i];
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000052 if (i != numDims - 1)
53 {
Keith Davis15f9c682022-10-14 15:50:33 +010054 os << ", ";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000055 }
56 }
Keith Davis15f9c682022-10-14 15:50:33 +010057 os << "], ";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000058
Keith Davis15f9c682022-10-14 15:50:33 +010059 os << "\"min\": "
60 << static_cast<float>(*std::min_element(inputData, inputData + numElements)) << ", ";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000061
Keith Davis15f9c682022-10-14 15:50:33 +010062 os << "\"max\": "
63 << static_cast<float>(*std::max_element(inputData, inputData + numElements)) << ", ";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000064
Keith Davis15f9c682022-10-14 15:50:33 +010065 os << "\"data\": ";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000066
67 for (unsigned int i = 0; i < numElements; i++)
68 {
69 for (unsigned int j = 0; j < numDims; j++)
70 {
71 if (i % strides[j] == 0)
72 {
Keith Davis15f9c682022-10-14 15:50:33 +010073 os << "[";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000074 }
75 }
76
Keith Davis15f9c682022-10-14 15:50:33 +010077 os << static_cast<float>(inputData[i]);
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000078
79 for (unsigned int j = 0; j < numDims; j++)
80 {
Keith Davis15f9c682022-10-14 15:50:33 +010081 if ((i + 1) % strides[j] == 0)
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000082 {
Keith Davis15f9c682022-10-14 15:50:33 +010083 os << "]";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000084 }
85 }
86
87 if (i != numElements - 1)
88 {
Keith Davis15f9c682022-10-14 15:50:33 +010089 os << ", ";
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +000090 }
91 }
92
Keith Davis15f9c682022-10-14 15:50:33 +010093 os << " }" << std::endl;
94}
95
96template<typename T>
97void Debug(const TensorInfo& inputInfo,
98 const T* inputData,
99 LayerGuid guid,
100 const std::string& layerName,
101 unsigned int slotIndex,
102 bool outputsToFile)
103{
104 if (outputsToFile)
105 {
Ryan OSheaa3dc95e2023-03-20 11:10:40 +0000106#if !defined(ARMNN_DISABLE_FILESYSTEM)
Keith Davisf63b4572022-10-19 14:53:05 +0100107 fs::path tmpDir = fs::temp_directory_path();
108 std::ofstream out(tmpDir.generic_string() + "/ArmNNIntermediateLayerOutputs/" + layerName + ".numpy");
Keith Davis15f9c682022-10-14 15:50:33 +0100109 PrintOutput<T>(inputInfo, inputData, guid, layerName, slotIndex, out);
Keith Davisf63b4572022-10-19 14:53:05 +0100110 out.close();
Ryan OSheaa3dc95e2023-03-20 11:10:40 +0000111#endif
Keith Davis15f9c682022-10-14 15:50:33 +0100112 }
113 else
114 {
115 PrintOutput<T>(inputInfo, inputData, guid, layerName, slotIndex, std::cout);
116 }
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000117}
118
Narumol Prangnawarat403a1852020-03-12 14:24:13 +0000119template void Debug<BFloat16>(const TensorInfo& inputInfo,
Keith Davis15f9c682022-10-14 15:50:33 +0100120 const BFloat16* inputData,
121 LayerGuid guid,
122 const std::string& layerName,
123 unsigned int slotIndex,
124 bool outputsToFile);
Narumol Prangnawarat403a1852020-03-12 14:24:13 +0000125
Aron Virginas-Tardb1a2832019-11-12 16:15:11 +0000126template void Debug<Half>(const TensorInfo& inputInfo,
127 const Half* inputData,
128 LayerGuid guid,
129 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100130 unsigned int slotIndex,
131 bool outputsToFile);
Aron Virginas-Tardb1a2832019-11-12 16:15:11 +0000132
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000133template void Debug<float>(const TensorInfo& inputInfo,
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000134 const float* inputData,
Nattapat Chaimanowong964e9552019-03-26 11:03:26 +0000135 LayerGuid guid,
136 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100137 unsigned int slotIndex,
138 bool outputsToFile);
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000139
140template void Debug<uint8_t>(const TensorInfo& inputInfo,
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000141 const uint8_t* inputData,
Nattapat Chaimanowong964e9552019-03-26 11:03:26 +0000142 LayerGuid guid,
143 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100144 unsigned int slotIndex,
145 bool outputsToFile);
Nattapat Chaimanowong964e9552019-03-26 11:03:26 +0000146
Keith Davis5204aa82020-01-27 15:24:59 +0000147template void Debug<int8_t>(const TensorInfo& inputInfo,
Keith Davis15f9c682022-10-14 15:50:33 +0100148 const int8_t* inputData,
149 LayerGuid guid,
150 const std::string& layerName,
151 unsigned int slotIndex,
152 bool outputsToFile);
Keith Davis5204aa82020-01-27 15:24:59 +0000153
Narumol Prangnawarat47cfee92019-07-04 10:29:00 +0100154template void Debug<int16_t>(const TensorInfo& inputInfo,
155 const int16_t* inputData,
156 LayerGuid guid,
157 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100158 unsigned int slotIndex,
159 bool outputsToFile);
Aron Virginas-Tardb1a2832019-11-12 16:15:11 +0000160
Narumol Prangnawaratd2d917d2020-01-09 10:16:39 +0000161template void Debug<int32_t>(const TensorInfo& inputInfo,
162 const int32_t* inputData,
163 LayerGuid guid,
164 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100165 unsigned int slotIndex,
166 bool outputsToFile);
Narumol Prangnawaratd2d917d2020-01-09 10:16:39 +0000167
John Mcloughlin4cf29d62023-09-25 14:10:32 +0100168template void Debug<int64_t>(const TensorInfo& inputInfo,
169 const int64_t* inputData,
170 LayerGuid guid,
171 const std::string& layerName,
172 unsigned int slotIndex,
173 bool outputsToFile);
174
Matteo Martincigh49124022019-01-11 13:25:59 +0000175} // namespace armnn