summaryrefslogtreecommitdiff
path: root/runtime/neurun/core/src/dumper/dot/DotDumper.cc
blob: 44313a6579504b4385b101b1974c2c11e51e8731 (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
/*
 * 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 <fstream>
#include <unordered_map>

#include "DotDumper.h"
#include "DotBuilder.h"
#include "DotSubgraphInfo.h"
#include "ir/OpSequence.h"
#include "ir/OperationIndexMap.h"
#include "backend/Backend.h"
#include "backend/BackendManager.h"
#include "backend/IConfig.h"

namespace neurun
{
namespace dumper
{
namespace dot
{

void DotDumper::dump(const std::string &tag)
{
  if (_level == Level::OFF)
  {
    return;
  }

  neurun::dumper::dot::DotBuilder dot_builder;

  auto &operations = _graph.operations();
  auto &operands = _graph.operands();

  ir::OperationIndexMap<std::unique_ptr<Operation>> operation_nodes;
  std::unordered_map<ir::OperandIndex, std::unique_ptr<Operand>> operand_nodes;

  operations.iterate([&](const ir::OperationIndex &index, const ir::Operation &op) {
    auto node = nnfw::cpp14::make_unique<Operation>(index, op);

    for (auto output : op.getOutputs())
    {
      using neurun::dumper::dot::Operand;
      auto child = std::make_shared<Operand>(output, Operand::Type::MODEL_OUTPUT);
      node->addEdge(child);
    }

    operation_nodes.emplace(index, std::move(node));
  });

  auto backend_to_fillcolor = [](const backend::Backend *backend) {
    static const auto map = []() {
      std::unordered_map<const backend::Backend *, std::string> ret;
      uint32_t index = 1; // Start from 1 to avoid 0(red) which is too dark :(
      for (const auto backend : backend::BackendManager::get().getAll())
      {
        ret.emplace(backend, Node::BG_COLORS[index]);
        index = (index + 1) % (sizeof(Node::BG_COLORS) / sizeof(Node::BG_COLORS[0]));
      }
      return ret;
    }();

    auto itr = map.find(backend);
    if (itr == map.end())
    {
      return Node::DEFAULT_FILLCOLOR;
    }
    else
    {
      return itr->second;
    }
  };

  util::Set<ir::OperandIndex> shown_operand_set;

  operands.iterate([&](const ir::OperandIndex &index, const ir::Operand &object) {
    bool showing_cond = false;
    if (_level == Level::ALL)
    {
      showing_cond = true;
    }
    else
    {
      showing_cond = !object.isConstant();
    }
    if (object.isConstant() || _graph.getInputs().contains(index))
    {
      showing_cond = showing_cond && (object.getUses().size() > 0);
    }
    if (showing_cond)
    {
      shown_operand_set.add(index);

      auto type = [&]() {
        using neurun::dumper::dot::Operand;
        if (_graph.getInputs().contains(index))
          return Operand::Type::MODEL_INPUT;
        if (_graph.getOutputs().contains(index))
          return Operand::Type::MODEL_OUTPUT;
        return Operand::Type::INTERNAL;
      }();

      auto lower_info = _graph.getLowerInfo(index);
      auto node = nnfw::cpp14::make_unique<Operand>(index, type);

      {
        // Display LowerInfo attributes
        std::string label = std::to_string(index.value());
        std::string fillcolor = "";
        if (lower_info)
        {
          const auto &def_factors = lower_info->def_factors();
          if (def_factors.size() > 0)
          {
            label += "\\n[";
            label += def_factors.getOnlyElement().backend()->config()->id();
            label += "]";

            fillcolor = backend_to_fillcolor(lower_info->def_factors().getOnlyElement().backend());
          }
        }
        node->setAttribute("label", label);
        node->setAttribute("fillcolor", fillcolor);
      }

      for (auto operation_index : object.getUses().list())
      {
        auto &operation = operations.at(operation_index);
        auto child = std::make_shared<Operation>(operation_index, operation);
        node->addEdge(child);
      }

      operand_nodes.emplace(index, std::move(node));
    }
  });

  const auto subgraphs = _graph.subgraphs();
  if (subgraphs)
  {
    subgraphs->iterate([&](const ir::SubgraphIndex &index, const ir::OpSequence &op_seq) {
      const auto lower_info = _graph.getLowerInfo(index);
      auto fillcolor = backend_to_fillcolor(lower_info->backend());
      std::string label =
          std::to_string(index.value()) + " [" + lower_info->backend()->config()->id() + "]";
      DotSubgraphInfo subgraph_info{index, op_seq, shown_operand_set};
      subgraph_info.label(label);
      subgraph_info.fillcolor(fillcolor);
      dot_builder.addSubgraph(subgraph_info);

      // Set fillcolor of all operations in the op_seq
      for (const auto &op : op_seq.operations())
      {
        auto found = operation_nodes.find(op.index);
        if (found != operation_nodes.end())
        {
          auto &&op = found->second;
          op->setAttribute("fillcolor", fillcolor);
        }
      }
    });
  }

  for (const auto &e : operation_nodes)
    dot_builder.update(*e.second);
  for (const auto &e : operand_nodes)
    dot_builder.update(*e.second);

  // Dump to file
  {
    std::string file_name;
    file_name += tag;
    file_name += ".dot";
    std::filebuf fb;

    fb.open(file_name, std::ios::out);
    std::ostream os(&fb);

    dot_builder.writeDot(os);

    fb.close();
  }
}

} // namespace dot
} // namespace dumper
} // namespace neurun