blob: f4f9792fc16480502f725bbf1cdc2c4c6177b8d4 [file] [log] [blame]
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// 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 {
Keith Davisf63b4572022-10-19 14:53:05 +0100106 fs::path tmpDir = fs::temp_directory_path();
107 std::ofstream out(tmpDir.generic_string() + "/ArmNNIntermediateLayerOutputs/" + layerName + ".numpy");
Keith Davis15f9c682022-10-14 15:50:33 +0100108 PrintOutput<T>(inputInfo, inputData, guid, layerName, slotIndex, out);
Keith Davisf63b4572022-10-19 14:53:05 +0100109 out.close();
Keith Davis15f9c682022-10-14 15:50:33 +0100110 }
111 else
112 {
113 PrintOutput<T>(inputInfo, inputData, guid, layerName, slotIndex, std::cout);
114 }
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000115}
116
Narumol Prangnawarat403a1852020-03-12 14:24:13 +0000117template void Debug<BFloat16>(const TensorInfo& inputInfo,
Keith Davis15f9c682022-10-14 15:50:33 +0100118 const BFloat16* inputData,
119 LayerGuid guid,
120 const std::string& layerName,
121 unsigned int slotIndex,
122 bool outputsToFile);
Narumol Prangnawarat403a1852020-03-12 14:24:13 +0000123
Aron Virginas-Tardb1a2832019-11-12 16:15:11 +0000124template void Debug<Half>(const TensorInfo& inputInfo,
125 const Half* inputData,
126 LayerGuid guid,
127 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100128 unsigned int slotIndex,
129 bool outputsToFile);
Aron Virginas-Tardb1a2832019-11-12 16:15:11 +0000130
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000131template void Debug<float>(const TensorInfo& inputInfo,
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000132 const float* inputData,
Nattapat Chaimanowong964e9552019-03-26 11:03:26 +0000133 LayerGuid guid,
134 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100135 unsigned int slotIndex,
136 bool outputsToFile);
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000137
138template void Debug<uint8_t>(const TensorInfo& inputInfo,
Nattapat Chaimanowongcfdcadf2018-12-06 11:54:33 +0000139 const uint8_t* inputData,
Nattapat Chaimanowong964e9552019-03-26 11:03:26 +0000140 LayerGuid guid,
141 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100142 unsigned int slotIndex,
143 bool outputsToFile);
Nattapat Chaimanowong964e9552019-03-26 11:03:26 +0000144
Keith Davis5204aa82020-01-27 15:24:59 +0000145template void Debug<int8_t>(const TensorInfo& inputInfo,
Keith Davis15f9c682022-10-14 15:50:33 +0100146 const int8_t* inputData,
147 LayerGuid guid,
148 const std::string& layerName,
149 unsigned int slotIndex,
150 bool outputsToFile);
Keith Davis5204aa82020-01-27 15:24:59 +0000151
Narumol Prangnawarat47cfee92019-07-04 10:29:00 +0100152template void Debug<int16_t>(const TensorInfo& inputInfo,
153 const int16_t* inputData,
154 LayerGuid guid,
155 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100156 unsigned int slotIndex,
157 bool outputsToFile);
Aron Virginas-Tardb1a2832019-11-12 16:15:11 +0000158
Narumol Prangnawaratd2d917d2020-01-09 10:16:39 +0000159template void Debug<int32_t>(const TensorInfo& inputInfo,
160 const int32_t* inputData,
161 LayerGuid guid,
162 const std::string& layerName,
Keith Davis15f9c682022-10-14 15:50:33 +0100163 unsigned int slotIndex,
164 bool outputsToFile);
Narumol Prangnawaratd2d917d2020-01-09 10:16:39 +0000165
Matteo Martincigh49124022019-01-11 13:25:59 +0000166} // namespace armnn