ArmNN
 21.08
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 
12 namespace armnn
13 {
14 
15 void JsonPrinter::PrintJsonChildObject(const JsonChildObject& object, size_t& id)
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  {
37  PrintNewLine();
38  }
39  if (object.GetType() == JsonObjectType::Measurement)
40  {
41  PrintMeasurementsList(object.m_Measurements);
43  PrintNewLine();
44  PrintUnit(object.m_Unit);
45  }
46  else if (object.GetType() == JsonObjectType::ExecObjectDesc)
47  {
48  for (std::string stringLine : object.m_LayerDetailsList)
49  {
50  PrintTabs();
51  m_OutputStream << stringLine;
52  PrintNewLine();
53  }
54  }
55  if (!object.m_Children.empty())
56  {
57  for (unsigned int childIndex = 0; childIndex < object.m_Children.size(); ++childIndex)
58  {
59  PrintJsonChildObject(object.m_Children[childIndex], id);
60  // Only print separator and new line if current child is not the last element.
61  if (&object.m_Children[childIndex] != &object.m_Children.back())
62  {
64  PrintNewLine();
65  }
66  }
67  }
68  if (object.GetType() != JsonObjectType::ExecObjectDesc)
69  {
70  PrintNewLine();
71  PrintFooter();
72  }
73 }
74 
75 std::string JsonPrinter::MakeKey(const std::string& label, size_t id)
76 {
77  std::stringstream ss;
78  ss << label << std::string("_#") << id;
79  return ss.str();
80 }
81 
82 void JsonPrinter::PrintLabel(const std::string& label, size_t id)
83 {
84  PrintTabs();
85  m_OutputStream << R"(")" << MakeKey(label, id) << R"(": {)" << std::endl;
87 }
88 
90 {
91  PrintTabs();
92  m_OutputStream << R"("unit": ")";
93  m_OutputStream << armnn::Measurement::ToString(unit);
94  m_OutputStream << R"(")";
95 }
96 
98 {
99  auto ToString = [](armnn::JsonObjectType type)
100  {
101  switch (type)
102  {
104  {
105  return "Measurement";
106  }
108  {
109  return "Event";
110  }
112  {
113  return "Operator Description";
114  }
115  default:
116  {
117  return "Unknown";
118  }
119  }
120  };
121  PrintTabs();
122  m_OutputStream << R"("type": ")";
123  m_OutputStream << ToString(type);
124  m_OutputStream << R"(")";
125 }
126 
127 void JsonPrinter::PrintGuid(armnn::profiling::ProfilingGuid guid)
128 {
129  PrintTabs();
130  m_OutputStream << std::quoted("GUID") << ": " << std::quoted(std::to_string(guid)) << "," << std::endl;
131 }
132 
133 void JsonPrinter::PrintMeasurementsList(const std::vector<double>& measurementsVector)
134 {
135  if (measurementsVector.empty())
136  {
137  return;
138  }
139 
140  PrintTabs();
141  m_OutputStream << R"("raw": [)" << std::endl;
143  PrintTabs();
144  auto iter = measurementsVector.begin();
145  m_OutputStream << *iter;
146  for (iter = std::next(iter); iter != measurementsVector.end(); ++iter)
147  {
148  m_OutputStream << "," << std::endl;
149  PrintTabs();
150  m_OutputStream << *iter;
151  }
152  m_OutputStream << std::endl;
154  PrintTabs();
155  m_OutputStream << "]";
156 }
157 
158 } // namespace armnn
static const char * ToString(Unit unit)
Definition: Instrument.hpp:23
Copyright (c) 2021 ARM Limited and Contributors.
void PrintGuid(armnn::profiling::ProfilingGuid guid)
void DecrementNumberOfTabs()
Definition: JsonUtils.hpp:32
void PrintFooter()
Definition: JsonUtils.hpp:51
void PrintMeasurementsList(const std::vector< double > &measurementsVector)
void PrintType(armnn::JsonObjectType type)
Definition: JsonPrinter.cpp:97
void PrintJsonChildObject(const JsonChildObject &object, size_t &id)
Definition: JsonPrinter.cpp:15
void PrintNewLine()
Definition: JsonUtils.hpp:46
void PrintSeparator()
Definition: JsonUtils.hpp:70
void PrintLabel(const std::string &label, size_t id)
Definition: JsonPrinter.cpp:82
void IncrementNumberOfTabs()
Definition: JsonUtils.hpp:41
JsonObjectType
Definition: JsonPrinter.hpp:20
void PrintUnit(armnn::Measurement::Unit unit)
Definition: JsonPrinter.cpp:89