1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "DotSerializer.hpp"
7 #include "armnn/utility/StringUtils.hpp"
8
9 #include <sstream>
10 #include <cstring>
11
12 namespace armnn
13 {
14
15 namespace
16 {
Indent(int numSpaces)17 std::string Indent(int numSpaces)
18 {
19 std::stringstream ss;
20 for (int i = 0; i < numSpaces; i++)
21 {
22 ss << " ";
23 }
24 return ss.str();
25 }
26
Escape(std::string s)27 std::string Escape(std::string s)
28 {
29 armnn::stringUtils::StringReplaceAll(s, "<", "\\<");
30 armnn::stringUtils::StringReplaceAll(s, ">", "\\>");
31 return s;
32 }
33
34 } //namespace
35
36
HtmlFont(std::ostream & stream,int fontSize,const char * color,const char * face)37 HtmlFont::HtmlFont(std::ostream& stream, int fontSize, const char *color, const char *face)
38 : DotBase(stream)
39 {
40 GetStream() << "<FONT";
41
42 if (fontSize > -1)
43 {
44 GetStream() << " POINT-SIZE=" << "\"" << fontSize << "\"";
45 }
46
47 if (color && std::strlen(color) != 0)
48 {
49 GetStream() << " COLOR=\"" << color << "\" ";
50 }
51
52 if (face && std::strlen(face) != 0)
53 {
54 GetStream() << " FACE=\"" << face << "\" ";
55 }
56
57 GetStream() << ">";
58 }
59
60
HtmlFont(std::ostream & stream)61 HtmlFont::HtmlFont(std::ostream& stream)
62 : HtmlFont(stream, -1, nullptr, nullptr)
63 {}
64
~HtmlFont()65 HtmlFont::~HtmlFont()
66 {
67 GetStream() << "</FONT>";
68 }
69
70
DotAttributeSet(std::ostream & stream)71 DotAttributeSet::DotAttributeSet(std::ostream& stream)
72 : DotBase(stream)
73 {
74 GetStream() << "[";
75 }
76
~DotAttributeSet()77 DotAttributeSet::~DotAttributeSet()
78 {
79 bool doSpace=false;
80 for (auto&& attrib : m_Attributes)
81 {
82 if (doSpace)
83 {
84 GetStream() << " ";
85 }
86
87 GetStream() << attrib;
88 doSpace=true;
89 }
90
91 GetStream() << "]";
92 }
93
AddAttribute(const std::string & name,const std::stringstream & value)94 DotAttributeSet & DotAttributeSet::AddAttribute(const std::string& name, const std::stringstream& value)
95 {
96 std::stringstream ss;
97 ss << name <<"=" << value.str();
98 m_Attributes.push_back(ss.str());
99 return *this;
100 }
101
AddAttribute(const std::string & name,int value)102 DotAttributeSet & DotAttributeSet::AddAttribute(const std::string& name, int value)
103 {
104 std::stringstream ss;
105 ss << name <<"=" << value;
106 m_Attributes.push_back(ss.str());
107 return *this;
108 }
109
AddAttribute(const std::string & name,const std::string & value)110 DotAttributeSet & DotAttributeSet::AddAttribute(const std::string& name, const std::string& value)
111 {
112 std::stringstream ss;
113 ss << name <<"=\"" << value << "\"";
114 m_Attributes.push_back(ss.str());
115 return *this;
116 }
117
DotEdge(std::ostream & stream,LayerGuid fromNodeId,LayerGuid toNodeId)118 DotEdge::DotEdge(std::ostream& stream, LayerGuid fromNodeId, LayerGuid toNodeId)
119 : DotBase(stream)
120 {
121 std::stringstream ss;
122 ss << Indent(4) << fromNodeId << " -> " << toNodeId << " ";
123 GetStream() << ss.str();
124
125 m_Attributes = std::make_unique<DotAttributeSet>(stream);
126 }
127
~DotEdge()128 DotEdge::~DotEdge()
129 {
130 m_Attributes.reset(nullptr);
131 GetStream() << ";" << std::endl;
132 }
133
134
NodeContent(std::ostream & stream)135 NodeContent::NodeContent(std::ostream& stream)
136 : DotBase(stream)
137 {
138 }
139
SetName(const std::string & name)140 NodeContent & NodeContent::SetName(const std::string & name)
141 {
142 m_Name = name;
143 return *this;
144 }
145
AddContent(const std::string & content)146 NodeContent & NodeContent::AddContent(const std::string & content)
147 {
148 m_Contents.push_back(content);
149 return *this;
150 }
151
~NodeContent()152 NodeContent::~NodeContent()
153 {
154 std::stringstream ss;
155 ss << "label=\"{" << m_Name;
156 if (!m_Contents.empty())
157 {
158 ss << "|";
159 }
160 for (auto & content : m_Contents)
161 {
162 ss << Escape(content);
163 ss << "\\l";
164 }
165 ss << "}\"";
166
167 std::string s;
168 try
169 {
170 // Coverity fix: std::stringstream::str() may throw an exception of type std::length_error.
171 s = ss.str();
172 }
173 catch (const std::exception&) { } // Swallow any exception.
174
175 GetStream() << s;
176 }
177
DotNode(std::ostream & stream,LayerGuid nodeId,const char * label)178 DotNode::DotNode(std::ostream& stream, LayerGuid nodeId, const char* label)
179 : DotBase(stream)
180 {
181 std::stringstream ss;
182 ss << Indent(4) << nodeId;
183
184 GetStream() << ss.str() << " ";
185
186 m_Contents = std::make_unique<NodeContent>(stream);
187 m_Attributes = std::make_unique<DotAttributeSet>(stream);
188
189 if (std::strlen(label) != 0)
190 {
191 m_Contents->SetName(label);
192 }
193 else
194 {
195 m_Contents->SetName("<noname>");
196 }
197 }
198
~DotNode()199 DotNode::~DotNode()
200 {
201 m_Contents.reset(nullptr);
202 m_Attributes.reset(nullptr);
203 GetStream() << ";" << std::endl;
204 }
205
206
DotDefaults(std::ostream & stream,const char * type)207 DotDefaults::DotDefaults(std::ostream& stream, const char* type)
208 : DotBase(stream)
209 {
210 std::stringstream ss;
211 ss << Indent(4) << type;
212
213 GetStream() << ss.str() << " ";
214 m_Attributes = std::make_unique<DotAttributeSet>(stream);
215 }
216
~DotDefaults()217 DotDefaults::~DotDefaults()
218 {
219 m_Attributes.reset(nullptr);
220 GetStream() << ";" << std::endl;
221 }
222
DotGraph(std::ostream & stream,const char * name)223 DotGraph::DotGraph(std::ostream& stream, const char* name)
224 : DotBase(stream)
225 {
226 GetStream() << "digraph " << name << " {" << std::endl;
227 }
228
~DotGraph()229 DotGraph::~DotGraph()
230 {
231 GetStream() << "}" << std::endl;
232 }
233
234 } //namespace armnn
235
236
237