summaryrefslogtreecommitdiff
path: root/runtimes/neurun/src/graph/Graph.cc
blob: 832e2b887a8ee5ce58d73c1f07f6bbe59cd6a2d3 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * 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 "Graph.h"

#include <algorithm>
#include <bitset>

#include "util/logging.h"
#include "verifier/Verifier.h"
#include "cpp14/memory.h"
#include "linear/Linear.h"
#include "operation/LowerInfo.h"
#include "operand/LowerInfo.h"
#include "operand/Shape4DConvert.h"
#include "compiler/BackendResolver.h"
#include "backend/interface/IConfig.h"
#include "pass/PermutationInsertionPass.h"
#include "pass/PermutationEliminationPass.h"

namespace neurun
{
namespace graph
{

Graph::Graph(void) = default;

Graph::~Graph(void) = default;

model::operand::Index Graph::addOperand(const model::operand::Shape &shape,
                                        const model::operand::TypeInfo &type)
{
  return _model->operands.append(shape, type);
}

model::operation::Index Graph::addOperation(std::unique_ptr<model::operation::Node> &&node)
{
  assert(isBuildingPhase());
  return _model->operations.append(std::move(node));
}

void Graph::setOperandValue(const model::operand::Index &ind,
                            std::unique_ptr<model::operand::Data> &&data)
{
  assert(isBuildingPhase());
  assert(_model->operands.exist(ind));
  _model->operands.at(ind).data(std::move(data));
}

void Graph::addInput(const model::operand::Index &ind)
{
  assert(isBuildingPhase());
  _model->inputs.append(ind);
}

void Graph::addOutput(const model::operand::Index &ind)
{
  assert(isBuildingPhase());
  _model->outputs.append(ind);
}

void Graph::finishBuilding(void)
{
  assert(isBuildingPhase());
  _phase = Phase::MODEL;

  // Initialize operand use-def
  initializeUseDef();

  // Call graph verifications for the MODEL phase
  {
    assert(verifier::DAGChecker().verify(*this));
    assert(verifier::EdgeConsistencyChecker().verify(*this));
  }
}

void Graph::lower(void)
{
  assert(_phase == Phase::MODEL);

  // Lower
  {
    // operand::LowerInfo holder
    std::unordered_map<model::operand::Index, std::unique_ptr<operand::LowerInfo>>
        operands_lower_info;

    _model->operands.iterate([&](const model::operand::Index &index,
                                 const model::operand::Object &object) {
      operands_lower_info[index] =
          nnfw::cpp14::make_unique<operand::LowerInfo>(graph::operand::asShape4D(object.shape()));
    });

    _backend_resolver = nnfw::cpp14::make_unique<compiler::BackendResolver>(_model->operands);

    _model->operations.iterate(
        [&](const model::operation::Index &index, model::operation::Node &node) {
          auto backend = _backend_resolver->getBackend(typeid(node));

          // Operation LowerInfo
          setLowerInfo(index, nnfw::cpp14::make_unique<graph::operation::LowerInfo>(backend));

          // LowerInfo for in/output operands
          for (auto operand : node.getInputs())
          {
            auto &&lower_info = operands_lower_info.at(operand);
            lower_info->addUseBackend(backend);
          }
          for (auto operand : node.getOutputs())
          {
            auto &&lower_info = operands_lower_info.at(operand);
            lower_info->addDefBackend(backend);
          }
        });

    // Add def backend to model input/output operand as default backend
    for (auto index : getInputs())
    {
      auto &&lower_info = operands_lower_info.at(index);
      lower_info->addDefBackend(_backend_resolver->getDefaultBackend());
    }

    for (auto index : getOutputs())
    {
      auto &&lower_info = operands_lower_info.at(index);
      lower_info->addUseBackend(_backend_resolver->getDefaultBackend());
    }

    // Add DefBackend constants same as UseBackend
    // NOTE This assumes a constant operand is used by only one operation
    _model->operations.iterate([&](const model::operation::Index &, model::operation::Node &node) {
      // LowerInfo for input operands
      for (auto operand : node.getInputs())
      {
        auto &&lower_info = operands_lower_info.at(operand);
        if (lower_info->def_backends().empty())
        {
          lower_info->addDefBackend(lower_info->use_backends().getOnlyElement());
        }
      }
    });

    // Set LowerInfo for each operand from the operand::LowerInfo holder
    _model->operands.iterate([&](const model::operand::Index &index,
                                 model::operand::Object &object) {
      object.lower_info(std::move(operands_lower_info[index]));

      // Dump operand LowerInfo
      // TODO Extract this dumping procedure to be reusable
      if (!object.lower_info()->def_backends().empty() ||
          !object.lower_info()->use_backends().empty())
      {
        auto backends_to_string = [](const operand::BackendSet &backends) {
          std::string str;
          for (auto backend : backends)
          {
            str += backend->config()->id();
            str += " ";
          }
          return "{ " + str + "}";
        };

        auto operation_index_to_string = [](const model::operation::IndexList &operations) {
          std::string str;
          for (auto op : operations.list())
          {
            str += std::to_string(op.value());
            str += " ";
          }
          return "{ " + str + "}";
        };

        const auto &lower_info = object.lower_info();
        const auto &shape = object.shape();
        const auto &lower_shape = lower_info->shape();
        std::string def_ops = operation_index_to_string(object.getDef());
        std::string use_ops = operation_index_to_string(object.getUses());
        std::string def_layouts = backends_to_string(lower_info->def_backends());
        std::string use_layouts = backends_to_string(lower_info->use_backends());
        VERBOSE(Lower) << "* Operand #" << index.value() << " LowerInfo" << std::endl;
        VERBOSE(Lower) << "  - Shape           : { " << shape.dim(0) << " "
                       << (shape.rank() > 1 ? shape.dim(1) : 0) << " "
                       << (shape.rank() > 2 ? shape.dim(2) : 0) << " "
                       << (shape.rank() > 3 ? shape.dim(3) : 0) << " "
                       << "}" << std::endl;
        VERBOSE(Lower) << "  - Def Operations  : " << def_ops << std::endl;
        VERBOSE(Lower) << "  - Use Operations  : " << use_ops << std::endl;
        VERBOSE(Lower) << "  - Lower Info" << std::endl;
        VERBOSE(Lower) << "    - 4D Shape (NHWC) : { " << lower_shape.n() << " " << lower_shape.h()
                       << " " << lower_shape.w() << " " << lower_shape.c() << " "
                       << "}" << std::endl;
        VERBOSE(Lower) << "    - Def Backends    : " << def_layouts << std::endl;
        VERBOSE(Lower) << "    - Use Backends    : " << use_layouts << std::endl;
      }
    });
  }

  // Run PermutationInsertionPass
  {
    pass::PermutationInsertionPass pi_pass(*this);
    pi_pass.run();
    pass::PermutationEliminationPass pe_pass(*this);
    pe_pass.run();
  }

  // Graph verifications for the LOWERED phase
  {
    assert(verifier::DAGChecker().verify(*this));
    assert(verifier::EdgeConsistencyChecker().verify(*this));
  }
}

std::unique_ptr<linear::Linear> Graph::linearize(void)
{
  assert(_phase == Phase::MODEL);

  auto linear = nnfw::cpp14::make_unique<linear::Linear>(*this);

  // TODO Move the operations and operands to linear object
  return std::move(linear);
}

void Graph::initializeUseDef()
{
  operations().iterate(
      [&](const model::operation::Index &index, const model::operation::Node &node) -> void {
        auto outputs = node.getOutputs();
        for (auto output : outputs)
        {
          operands().at(output).appendDef(index);
        }

        auto inputs = node.getInputs();
        for (auto input : inputs)
        {
          operands().at(input).appendUse(index);
        }
      });
}

const operation::LowerInfo *Graph::getLowerInfo(const model::operation::Index &index) const
{
  auto itr = _operation_lower_info.find(index);
  if (itr == _operation_lower_info.end())
    return nullptr;
  return itr->second.get();
}

void Graph::setLowerInfo(const model::operation::Index &index,
                         std::unique_ptr<operation::LowerInfo> &&lower_info)
{
  _operation_lower_info.insert(std::make_pair(index, std::move(lower_info)));
}

} // namespace graph
} // namespace neurun

namespace neurun
{
namespace graph
{

// Explicit instantiations to have implementation in the source file.

template class Graph::DefaultIterator<true>;
template class Graph::DefaultIterator<false>;

template class Graph::PostDfsIterator<true>;
template class Graph::PostDfsIterator<false>;

//
// Graph::DefaultIterator
//

template <bool is_const>
void Graph::DefaultIterator<is_const>::iterate(GraphRef graph, const IterFn &fn) const
{
  graph.operations().iterate(
      [&](const model::operation::Index &index, NodeRef node) -> void { fn(index, node); });
}

//
// Graph::PostDfsIterator
//

template <bool is_const>
void Graph::PostDfsIterator<is_const>::iterate(GraphRef graph, const IterFn &fn) const
{
  assert(!graph.isBuildingPhase()); // Restrict iteration condition

  std::unordered_map<model::operation::Index, bool> visited;
  graph.operations().iterate(
      [&](const model::operation::Index &index, NodeRef) { visited[index] = false; });

  std::function<void(const model::operation::Index &, NodeRef)> dfs_recursive =
      [&](const model::operation::Index &index, NodeRef node) -> void {
    if (visited[index])
      return;
    visited[index] = true;

    for (auto output : node.getOutputs())
    {
      const auto &operand = graph.operands().at(output);
      for (const auto &use : operand.getUses().list())
      {
        dfs_recursive(use, graph.operations().at(use));
      }
    }

    fn(index, node);
  };

  graph.operations().iterate(dfs_recursive);

  // All of the operations(nodes) must have been visited.
  assert(std::all_of(
      visited.begin(), visited.end(),
      [](const std::pair<const model::operation::Index, bool> &v) { return v.second; }));
}

} // namespace graph
} // namespace neurun