summaryrefslogtreecommitdiff
path: root/compiler/mir/src/DotNodeBuilder.cpp
blob: abe7af63a588bbd5ef258088c265089bbdc874bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "DotNodeBuilder.h"
#include "mir/OpDefs.h"

#include <sstream>

namespace mir
{

template <typename T> static std::string toString(const std::vector<T> &v)
{
  std::stringstream ss;
  ss << "[";
  for (std::size_t i = 0; i < v.size(); ++i)
  {
    if (i != 0)
      ss << ", ";
    ss << v[i];
  }
  return ss.str();
}

DotNodeBuilder::DotNodeBuilder(const Operation &op)
{
  _type_name = getTypeName(op.getType());
  _id = op.getId();

  for (std::size_t i = 0; i < op.getNumInputs(); ++i)
  {
    _in_shapes.push_back(toString(op.getInputShape(i)));
  }

  for (std::size_t i = 0; i < op.getNumOutputs(); ++i)
  {
    _out_shapes.push_back(toString(op.getOutputShape(i)));
  }

  // Get attributes.
  const_cast<Operation &>(op).accept(this);
}

void DotNodeBuilder::visit(ops::AvgPool2DOp &op)
{
  addAttribute("Window size", toString(op.getWindowSize()));
  addAttribute("Strides", toString(op.getStrides()));
  addAttribute("Padding before", toString(op.getPaddingBefore()));
  addAttribute("Padding after", toString(op.getPaddingAfter()));
  addAttribute("Include pad", std::to_string(op.getIncludePad()));
}

void DotNodeBuilder::visit(ops::CappedReluOp &op)
{
  addAttribute("Cap", std::to_string(op.getCap()));
}

void DotNodeBuilder::visit(ops::ConcatOp &op)
{
  addAttribute("Axis", std::to_string(op.getAxis()));
}

void DotNodeBuilder::visit(ops::Conv2DOp &op)
{
  addAttribute("Strides", toString(op.getStrides()));
  addAttribute("Padding before", toString(op.getPaddingBefore()));
  addAttribute("Padding after", toString(op.getPaddingAfter()));
  addAttribute("Num groups", std::to_string(op.getNumGroups()));
  addAttribute("Data format", toString(op.getDataFormat()));
}

void DotNodeBuilder::visit(ops::DepthwiseConv2DOp &op)
{
  addAttribute("Strides", toString(op.getStrides()));
  addAttribute("Padding before", toString(op.getPaddingBefore()));
  addAttribute("Padding after", toString(op.getPaddingAfter()));
  addAttribute("Data format", toString(op.getDataFormat()));
}

void DotNodeBuilder::visit(ops::MaxPool2DOp &op)
{
  addAttribute("Window size", toString(op.getWindowSize()));
  addAttribute("Strides", toString(op.getStrides()));
  addAttribute("Padding before", toString(op.getPaddingBefore()));
  addAttribute("Padding after", toString(op.getPaddingAfter()));
  addAttribute("Data format", toString(op.getDataFormat()));
}

void DotNodeBuilder::visit(ops::SoftmaxOp &op)
{
  addAttribute("Axis", std::to_string(op.getAxis()));
}

void DotNodeBuilder::visit(ops::SliceOp &op)
{
  addAttribute("Starts", toString(op.getStarts()));
  addAttribute("Sizes", toString(op.getSizes()));
}

void DotNodeBuilder::visit(ops::DeConv2DOp &op)
{
  addAttribute("Padding before", toString(op.getPaddingBefore()));
  addAttribute("Padding after", toString(op.getPaddingAfter()));
  addAttribute("Strides", toString(op.getStrides()));
  addAttribute("Data format", toString(op.getDataFormat()));
}

void DotNodeBuilder::visit(ops::EluOp &op) { addAttribute("Alpha", std::to_string(op.getAlpha())); }

void DotNodeBuilder::visit(ops::SqueezeOp &op)
{
  addAttribute("Dims to squeeze", toString(op.getDimsToSqueeze()));
}

void mir::DotNodeBuilder::visit(ops::PadOp &op)
{
  addAttribute("Padding before", toString(op.getPaddingBefore()));
  addAttribute("Padding after", toString(op.getPaddingAfter()));
  addAttribute("Padding value", std::to_string(op.getPaddingValue()));
}

void DotNodeBuilder::visit(ops::ReduceMeanOp &op)
{
  addAttribute("Reduction dims", toString(op.getReductionDims()));
  addAttribute("Keep dims", std::to_string(op.getKeepDims()));
}

void DotNodeBuilder::visit(ops::ResizeOp &op)
{
  assert(op.getMode() == ops::ResizeOp::ResizeMethod::nearestNeighbor);
  (void)op;

  addAttribute("Interpolation mode", "nearestNeighbor");
}

void DotNodeBuilder::visit(ops::TransposeOp &op)
{
  addAttribute("Axis order", toString(op.getAxisOrder()));
}

void DotNodeBuilder::visit(ops::GatherOp &op)
{
  addAttribute("Axis", std::to_string(op.getAxis()));
}

void DotNodeBuilder::visit(mir::ops::LeakyReluOp &op)
{
  addAttribute("Alpha", std::to_string(op.getAlpha()));
}

void DotNodeBuilder::addAttribute(std::string name, std::string val)
{
  this->_attributes.emplace_back(std::move(name), std::move(val));
}

std::string DotNodeBuilder::getLabel() const
{
  std::stringstream ss;

  ss << "{" << _type_name << " | {{";

  for (std::size_t i = 0; i < _in_shapes.size(); ++i)
  {
    if (i != 0)
      ss << " | ";
    ss << "in" << i << ": " << _in_shapes[i];
  }

  ss << " | ";

  for (std::size_t i = 0; i < _out_shapes.size(); ++i)
  {
    if (i != 0)
      ss << " | ";
    ss << "out" << i << ": " << _out_shapes[i];
  }

  ss << "} | {";

  for (std::size_t i = 0; i < _attributes.size(); ++i)
  {
    if (i != 0)
      ss << " | ";
    ss << _attributes[i].first << ": " << _attributes[i].second;
  }

  ss << "}}}";

  return ss.str();
}

} // namespace mir