Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <iomanip> |
| 9 | |
| 10 | #include "armnn/Types.hpp" |
| 11 | #include "armnn/TypesUtils.hpp" |
| 12 | #include "armnn/backends/WorkloadInfo.hpp" |
| 13 | |
| 14 | #include "SerializeLayerParameters.hpp" |
| 15 | #include "JsonUtils.hpp" |
| 16 | |
| 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | /// ProfilingDetails class records any details associated with the operator and passes on for outputting to the user |
| 21 | class ProfilingDetails : public JsonUtils |
| 22 | { |
| 23 | public: |
| 24 | /// Constructor |
Keith Davis | de68d7d | 2021-11-03 11:02:29 +0000 | [diff] [blame] | 25 | ProfilingDetails() : JsonUtils(m_ProfilingDetails), m_DetailsExist(false), m_PrintSeparator(false) |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 26 | {} |
| 27 | |
| 28 | /// Destructor |
| 29 | ~ProfilingDetails() noexcept |
| 30 | {} |
| 31 | |
| 32 | /// Add to the ProfilingDetails |
| 33 | template<typename DescriptorType> |
| 34 | void AddDetailsToString(const std::string& workloadName, |
| 35 | const DescriptorType& desc, |
Keith Davis | 5a64f22 | 2021-08-04 10:35:20 +0100 | [diff] [blame] | 36 | const WorkloadInfo& infos, |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame^] | 37 | const arm::pipe::ProfilingGuid guid) |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 38 | { |
Keith Davis | ec22ad2 | 2021-10-22 14:17:19 +0100 | [diff] [blame] | 39 | // Once details exist, we can assume we're on the second iteration of details |
| 40 | if (m_DetailsExist) |
| 41 | { |
| 42 | PrintSeparator(); |
| 43 | PrintNewLine(); |
| 44 | } |
| 45 | |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 46 | PrintHeader(); |
Keith Davis | de68d7d | 2021-11-03 11:02:29 +0000 | [diff] [blame] | 47 | PrintTabs(); |
| 48 | m_ProfilingDetails << std::quoted("Name") << ": " << std::quoted(workloadName); |
| 49 | PrintSeparator(); |
| 50 | PrintNewLine(); |
Keith Davis | 5a64f22 | 2021-08-04 10:35:20 +0100 | [diff] [blame] | 51 | PrintTabs(); |
| 52 | m_ProfilingDetails << std::quoted("GUID") << ": " << std::quoted(std::to_string(guid)); |
| 53 | PrintSeparator(); |
| 54 | PrintNewLine(); |
| 55 | |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 56 | // Print tensor infos and related data types |
| 57 | PrintInfos(infos.m_InputTensorInfos, "Input"); |
| 58 | |
| 59 | PrintInfos(infos.m_OutputTensorInfos, "Output"); |
| 60 | |
| 61 | if ( infos.m_BiasTensorInfo.has_value()) |
| 62 | { |
| 63 | PrintInfo(infos.m_BiasTensorInfo.value(), "Bias"); |
| 64 | } |
Mike Kelly | 80211a4 | 2021-08-12 14:18:05 +0100 | [diff] [blame] | 65 | if ( infos.m_WeightsTensorInfo.has_value()) |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 66 | { |
| 67 | PrintInfo(infos.m_WeightsTensorInfo.value(), "Weights"); |
| 68 | } |
| 69 | if ( infos.m_ConvolutionMethod.has_value()) |
| 70 | { |
| 71 | PrintTabs(); |
| 72 | |
| 73 | m_ProfilingDetails << std::quoted("Convolution Method") << ": " |
| 74 | << std::quoted(infos.m_ConvolutionMethod.value()); |
| 75 | |
| 76 | PrintSeparator(); |
| 77 | PrintNewLine(); |
| 78 | } |
| 79 | |
| 80 | ParameterStringifyFunction extractParams = [this](const std::string& name, const std::string& value) { |
Keith Davis | de68d7d | 2021-11-03 11:02:29 +0000 | [diff] [blame] | 81 | if (m_PrintSeparator) |
| 82 | { |
| 83 | PrintSeparator(); |
| 84 | PrintNewLine(); |
| 85 | } |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 86 | PrintTabs(); |
| 87 | m_ProfilingDetails << std::quoted(name) << " : " << std::quoted(value); |
Keith Davis | de68d7d | 2021-11-03 11:02:29 +0000 | [diff] [blame] | 88 | m_PrintSeparator = true; |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | StringifyLayerParameters<DescriptorType>::Serialize(extractParams, desc); |
| 92 | |
Keith Davis | de68d7d | 2021-11-03 11:02:29 +0000 | [diff] [blame] | 93 | PrintNewLine(); |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 94 | PrintFooter(); |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 95 | |
| 96 | m_DetailsExist = true; |
Keith Davis | de68d7d | 2021-11-03 11:02:29 +0000 | [diff] [blame] | 97 | m_PrintSeparator = false; |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /// Get the ProfilingDetails |
| 101 | /// \return the ProfilingDetails |
| 102 | std::string GetProfilingDetails() const |
| 103 | { |
| 104 | return m_ProfilingDetails.str(); |
| 105 | } |
| 106 | |
| 107 | bool DetailsExist() |
| 108 | { |
| 109 | return m_DetailsExist; |
| 110 | } |
| 111 | |
| 112 | private: |
| 113 | // Print tensor infos and related data types |
| 114 | void PrintInfo(const TensorInfo& info, const std::string& ioString) |
| 115 | { |
| 116 | const std::vector<TensorInfo> infoVect{ info }; |
| 117 | PrintInfos(infoVect, ioString); |
| 118 | } |
| 119 | |
| 120 | void PrintInfos(const std::vector<TensorInfo>& infos, const std::string& ioString) |
| 121 | { |
| 122 | for ( size_t i = 0; i < infos.size(); i++ ) |
| 123 | { |
| 124 | auto shape = infos[i].GetShape(); |
| 125 | PrintTabs(); |
| 126 | |
| 127 | m_ProfilingDetails << std::quoted(ioString + " " + std::to_string(i)) << ": "; |
| 128 | |
| 129 | PrintHeader(); |
| 130 | PrintTabs(); |
| 131 | |
| 132 | // Shape |
| 133 | m_ProfilingDetails << std::quoted("Shape") << ": \"["; |
| 134 | for ( unsigned int dim = 0; dim < shape.GetNumDimensions(); dim++ ) |
| 135 | { |
| 136 | shape.GetNumDimensions() == dim + 1 ? |
| 137 | m_ProfilingDetails << shape[dim] << "]\"" : // true |
| 138 | m_ProfilingDetails << shape[dim] << ","; // false |
| 139 | } |
| 140 | |
| 141 | PrintSeparator(); |
| 142 | PrintNewLine(); |
| 143 | |
| 144 | // Data Type |
| 145 | PrintTabs(); |
| 146 | m_ProfilingDetails << std::quoted("DataType") << ": " |
| 147 | << std::quoted(GetDataTypeName(infos[i].GetDataType())); |
| 148 | |
| 149 | PrintSeparator(); |
| 150 | PrintNewLine(); |
| 151 | |
| 152 | // Number of Dimensions |
| 153 | PrintTabs(); |
| 154 | m_ProfilingDetails << std::quoted("Num Dims") << ": " |
| 155 | << std::quoted(std::to_string(shape.GetNumDimensions())); |
| 156 | |
| 157 | |
| 158 | // Close out the scope |
| 159 | PrintNewLine(); |
| 160 | PrintFooter(); |
| 161 | PrintSeparator(); |
| 162 | PrintNewLine(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /// Stores ProfilingDetails |
| 167 | std::ostringstream m_ProfilingDetails; |
| 168 | bool m_DetailsExist; |
Keith Davis | de68d7d | 2021-11-03 11:02:29 +0000 | [diff] [blame] | 169 | bool m_PrintSeparator; |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame] | 170 | |
| 171 | }; |
| 172 | |
| 173 | } // namespace armnn |