ArmNN
 25.11
Loading...
Searching...
No Matches
JsonPrinter.cpp
Go to the documentation of this file.
1//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "JsonPrinter.hpp"
7
8#include <iomanip>
9#include <iostream>
10#include <sstream>
11
12namespace armnn
13{
14
16{
17 if (object.GetType() == JsonObjectType::Event)
18 {
19 // Increase the Id for new events. This ensures a new event has a unique ID and any measurements belonging
20 // to the event have the same id. This id is appended to the name during the call to PrintLabel() below.
21 id++;
22 }
23
24 if (object.GetType() != JsonObjectType::ExecObjectDesc)
25 {
26 PrintLabel(object.m_Label, id);
27 if (object.m_Guid.has_value())
28 {
29 PrintGuid(object.m_Guid.value());
30 }
31 PrintType(object.m_Type);
32 }
33
34 if (!object.m_Measurements.empty() || !object.m_Children.empty())
35 {
38 }
39 if (object.GetType() == JsonObjectType::Measurement)
40 {
41 PrintMeasurementsList(object.m_Measurements);
44 PrintUnit(object.m_Unit);
45 }
46 else if (object.GetType() == JsonObjectType::ExecObjectDesc)
47 {
48 // Add details opening
50 PrintTabs();
51 m_OutputStream << std::quoted("Graph") << ":[";
53
54 // Fill details body
55 for (std::string stringLine : object.m_LayerDetailsList)
56 {
57 PrintTabs();
58 m_OutputStream << stringLine;
60 }
61
62 // Close out details
63 PrintTabs();
64 object.IsDetailsOnlyEnabled() ? m_OutputStream << "]" : m_OutputStream << "],";
65
68 }
69 if (!object.m_Children.empty())
70 {
71 for (unsigned int childIndex = 0; childIndex < object.m_Children.size(); ++childIndex)
72 {
73 PrintJsonChildObject(object.m_Children[childIndex], id);
74 // Only print separator and new line if current child is not the last element.
75 if (&object.m_Children[childIndex] != &object.m_Children.back())
76 {
79 }
80 }
81 }
82 if (object.GetType() != JsonObjectType::ExecObjectDesc)
83 {
86 }
87}
88
89std::string JsonPrinter::MakeKey(const std::string& label, size_t id)
90{
91 std::stringstream ss;
92 ss << label << std::string("_#") << id;
93 return ss.str();
94}
95
96void JsonPrinter::PrintLabel(const std::string& label, size_t id)
97{
98 PrintTabs();
99 m_OutputStream << R"(")" << MakeKey(label, id) << R"(": {)" << std::endl;
101}
102
104{
105 PrintTabs();
106 m_OutputStream << R"("unit": ")";
107 m_OutputStream << armnn::Measurement::ToString(unit);
108 m_OutputStream << R"(")";
109}
110
112{
113 auto ToString = [](armnn::JsonObjectType type)
114 {
115 switch (type)
116 {
118 {
119 return "Measurement";
120 }
122 {
123 return "Event";
124 }
126 {
127 return "Operator Description";
128 }
129 default:
130 {
131 return "Unknown";
132 }
133 }
134 };
135 PrintTabs();
136 m_OutputStream << R"("type": ")";
137 m_OutputStream << ToString(type);
138 m_OutputStream << R"(")";
139}
140
141void JsonPrinter::PrintGuid(arm::pipe::ProfilingGuid guid)
142{
143 PrintTabs();
144 m_OutputStream << std::quoted("GUID") << ": " << std::quoted(std::to_string(guid)) << "," << std::endl;
145}
146
147void JsonPrinter::PrintMeasurementsList(const std::vector<double>& measurementsVector)
148{
149 if (measurementsVector.empty())
150 {
151 return;
152 }
153
154 PrintTabs();
155 m_OutputStream << R"("raw": [)" << std::endl;
157 PrintTabs();
158 auto iter = measurementsVector.begin();
159 m_OutputStream << *iter;
160 for (iter = std::next(iter); iter != measurementsVector.end(); ++iter)
161 {
162 m_OutputStream << "," << std::endl;
163 PrintTabs();
164 m_OutputStream << *iter;
165 }
166 m_OutputStream << std::endl;
168 PrintTabs();
169 m_OutputStream << "]";
170}
171
172} // namespace armnn
void PrintMeasurementsList(const std::vector< double > &measurementsVector)
void PrintType(armnn::JsonObjectType type)
void PrintUnit(armnn::Measurement::Unit unit)
void PrintLabel(const std::string &label, size_t id)
void PrintGuid(arm::pipe::ProfilingGuid guid)
void PrintJsonChildObject(const JsonChildObject &object, size_t &id)
void PrintSeparator()
Definition JsonUtils.hpp:70
void DecrementNumberOfTabs()
Definition JsonUtils.hpp:32
void IncrementNumberOfTabs()
Definition JsonUtils.hpp:41
Copyright (c) 2021 ARM Limited and Contributors.
static const char * ToString(Unit unit)