ArmNN
 25.11
Loading...
Searching...
No Matches
Debug.cpp
Go to the documentation of this file.
1//
2// Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "Debug.hpp"
7#include <common/include/ProfilingGuid.hpp>
9
10#include <BFloat16.hpp>
11#include <Half.hpp>
12
13#include <algorithm>
14#include <iostream>
15#include <iosfwd>
16#include <fstream>
17#include <sys/stat.h>
18
19namespace armnn
20{
21
22template<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)
29{
30 const unsigned int numDims = inputInfo.GetNumDimensions();
31 const unsigned int numElements = inputInfo.GetNumElements();
32 const TensorShape& inputShape = inputInfo.GetShape();
33
34 std::vector<unsigned int> strides(numDims, 0);
35 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
42 os << "{ ";
43 os << "\"layerGuid\": " << guid << ", ";
44 os << "\"layerName\": \"" << layerName << "\", ";
45 os << "\"outputSlot\": " << slotIndex << ", ";
46 os << "\"shape\": ";
47
48 os << "[";
49 for (unsigned int i = 0; i < numDims; i++)
50 {
51 os << inputShape[i];
52 if (i != numDims - 1)
53 {
54 os << ", ";
55 }
56 }
57 os << "], ";
58
59 os << "\"min\": "
60 << static_cast<float>(*std::min_element(inputData, inputData + numElements)) << ", ";
61
62 os << "\"max\": "
63 << static_cast<float>(*std::max_element(inputData, inputData + numElements)) << ", ";
64
65 os << "\"data\": ";
66
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 {
73 os << "[";
74 }
75 }
76
77 os << static_cast<float>(inputData[i]);
78
79 for (unsigned int j = 0; j < numDims; j++)
80 {
81 if ((i + 1) % strides[j] == 0)
82 {
83 os << "]";
84 }
85 }
86
87 if (i != numElements - 1)
88 {
89 os << ", ";
90 }
91 }
92
93 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 {
106#if !defined(ARMNN_DISABLE_FILESYSTEM)
107 fs::path tmpDir = fs::temp_directory_path();
108 std::ofstream out(tmpDir.generic_string() + "/ArmNNIntermediateLayerOutputs/" + layerName + ".numpy");
109 PrintOutput<T>(inputInfo, inputData, guid, layerName, slotIndex, out);
110 out.close();
111#endif
112 }
113 else
114 {
115 PrintOutput<T>(inputInfo, inputData, guid, layerName, slotIndex, std::cout);
116 }
117}
118
119template void Debug<BFloat16>(const TensorInfo& inputInfo,
120 const BFloat16* inputData,
121 LayerGuid guid,
122 const std::string& layerName,
123 unsigned int slotIndex,
124 bool outputsToFile);
125
126template void Debug<Half>(const TensorInfo& inputInfo,
127 const Half* inputData,
128 LayerGuid guid,
129 const std::string& layerName,
130 unsigned int slotIndex,
131 bool outputsToFile);
132
133template void Debug<float>(const TensorInfo& inputInfo,
134 const float* inputData,
135 LayerGuid guid,
136 const std::string& layerName,
137 unsigned int slotIndex,
138 bool outputsToFile);
139
140template void Debug<uint8_t>(const TensorInfo& inputInfo,
141 const uint8_t* inputData,
142 LayerGuid guid,
143 const std::string& layerName,
144 unsigned int slotIndex,
145 bool outputsToFile);
146
147template void Debug<int8_t>(const TensorInfo& inputInfo,
148 const int8_t* inputData,
149 LayerGuid guid,
150 const std::string& layerName,
151 unsigned int slotIndex,
152 bool outputsToFile);
153
154template void Debug<int16_t>(const TensorInfo& inputInfo,
155 const int16_t* inputData,
156 LayerGuid guid,
157 const std::string& layerName,
158 unsigned int slotIndex,
159 bool outputsToFile);
160
161template void Debug<int32_t>(const TensorInfo& inputInfo,
162 const int32_t* inputData,
163 LayerGuid guid,
164 const std::string& layerName,
165 unsigned int slotIndex,
166 bool outputsToFile);
167
168template 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
175} // namespace armnn
arm::pipe::ProfilingGuid LayerGuid
Define LayerGuid type.
Definition Types.hpp:26
const TensorShape & GetShape() const
Definition Tensor.hpp:193
unsigned int GetNumDimensions() const
Definition Tensor.hpp:197
unsigned int GetNumElements() const
Definition Tensor.hpp:198
Copyright (c) 2021 ARM Limited and Contributors.
template void Debug< int16_t >(const TensorInfo &inputInfo, const int16_t *inputData, LayerGuid guid, const std::string &layerName, unsigned int slotIndex, bool outputsToFile)
half_float::half Half
Definition Half.hpp:22
template void Debug< Half >(const TensorInfo &inputInfo, const Half *inputData, LayerGuid guid, const std::string &layerName, unsigned int slotIndex, bool outputsToFile)
template void Debug< uint8_t >(const TensorInfo &inputInfo, const uint8_t *inputData, LayerGuid guid, const std::string &layerName, unsigned int slotIndex, bool outputsToFile)
template void Debug< float >(const TensorInfo &inputInfo, const float *inputData, LayerGuid guid, const std::string &layerName, unsigned int slotIndex, bool outputsToFile)
template void Debug< int8_t >(const TensorInfo &inputInfo, const int8_t *inputData, LayerGuid guid, const std::string &layerName, unsigned int slotIndex, bool outputsToFile)
template void Debug< int64_t >(const TensorInfo &inputInfo, const int64_t *inputData, LayerGuid guid, const std::string &layerName, unsigned int slotIndex, bool outputsToFile)
template void Debug< BFloat16 >(const TensorInfo &inputInfo, const BFloat16 *inputData, LayerGuid guid, const std::string &layerName, unsigned int slotIndex, bool outputsToFile)
template void Debug< int32_t >(const TensorInfo &inputInfo, const int32_t *inputData, LayerGuid guid, const std::string &layerName, unsigned int slotIndex, bool outputsToFile)
void PrintOutput(const TensorInfo &inputInfo, const T *inputData, LayerGuid guid, const std::string &layerName, unsigned int slotIndex, std::ostream &os)
Definition Debug.cpp:23