summaryrefslogtreecommitdiff
path: root/compiler/luci/import/src/PostImport.cpp
blob: 63b16bb95310a8de946aef2c0d8a96fe30885b56 (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * Copyright (c) 2020 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 "PostImport.h"

#include "luci/Import/CircleReader.h"

#include <luci/IR/CircleNodes.h>
#include <luci/IR/CircleDialect.h>
#include <luci/IR/CircleNodeVisitor.h>
#include <luci/Log.h>

#include <loco.h>
#include <oops/InternalExn.h>

namespace
{

/**
 * @brief  FixInterGraphNodes will fix inter graph connections for each Nodes
 */
class FixInterGraphNodes final : public luci::CircleNodeMutableVisitor<void>
{
public:
  FixInterGraphNodes(const luci::Module *m, const luci::CircleReader &r) : _module(m), _reader(r) {}

  /**
   * @note  This will set Graph* to every CircleIf nodes 'else' and 'then'
   */
  void visit(luci::CircleIf *node) final
  {
    LOGGER(l);
    INFO(l) << "CircleIf " << node->name() << std::endl;

    auto then_branch = node->then_branch();
    auto else_branch = node->else_branch();
    auto num_graphs = static_cast<int32_t>(_module->size());
    (void)num_graphs;

    assert(num_graphs > 0);
    assert(then_branch >= 0 && then_branch < num_graphs);
    assert(else_branch >= 0 && else_branch < num_graphs);

    auto then_graph = _module->graph(then_branch);
    auto else_graph = _module->graph(else_branch);
    assert(then_graph != nullptr);
    assert(else_graph != nullptr);

    node->then_graph(then_graph);
    node->else_graph(else_graph);
  }

  void visit(luci::CircleWhile *node) final
  {
    LOGGER(l);
    INFO(l) << "CircleWhile " << node->name() << std::endl;

    auto cond_branch = node->cond_branch();
    auto body_branch = node->body_branch();
    auto num_graphs = static_cast<int32_t>(_module->size());
    (void)num_graphs;

    assert(num_graphs > 0);
    assert(cond_branch >= 0 && cond_branch < num_graphs);
    assert(body_branch >= 0 && body_branch < num_graphs);

    auto cond_graph = _module->graph(cond_branch);
    auto body_graph = _module->graph(body_branch);
    assert(cond_graph != nullptr);
    assert(body_graph != nullptr);

    node->cond_graph(cond_graph);
    node->body_graph(body_graph);
  }

  void visit(luci::CircleNode *) final
  {
    // DO NOTHING
  }

private:
  const luci::Module *_module;
  const luci::CircleReader &_reader;
};

/**
 * @brief  FixInterGraph will fix inter graph connections
 */
class FixInterGraph final
{
public:
  void run(loco::Graph *g, const luci::Module *m, const luci::CircleReader &r)
  {
    for (auto node : loco::postorder_traversal(loco::output_nodes(g)))
    {
      if (recognize(node->dialect()))
      {
        auto cn = loco::must_cast<luci::CircleNode *>(node);

        fix(cn, m, r);
      }
    }
  }

private:
  bool recognize(const loco::Dialect *dialect) { return (dialect == luci::CircleDialect::get()); }

  void fix(luci::CircleNode *node, const luci::Module *module, const luci::CircleReader &reader)
  {
    FixInterGraphNodes fix(module, reader);
    node->accept(&fix);
  }
};

} // namespace

namespace
{
/**
 * @brief  ValidateNodeProp will validate inter graph connections for each Nodes.
 * @note   In here, only loco::GraphInput and loco::GraphOutput are validated,
 *         since this class is for checking inter graph connections.
 *         CircleNodes such as CircleInput and CircleOutput will be validated at later steps.
 */
class ValidateNodeProp final : public luci::CircleNodeMutableVisitor<void>
{
public:
  ValidateNodeProp(const luci::Module *m, const luci::CircleReader &r) : _module(m), _reader(r) {}

  /**
   * @note  Validate CircleIf node 'else' and 'then' graph input/output count
   *        shape and type
   */
  void visit(luci::CircleIf *node) final
  {
    LOGGER(l);
    INFO(l) << "CircleIf " << node->name() << std::endl;

    auto then_graph = node->then_graph();
    auto else_graph = node->else_graph();
    assert(then_graph != nullptr);
    assert(else_graph != nullptr);

    // TODO support for differnt shape; but how?
    // NODE Shape/Type inference assume below conditions

    // Check both "then" and "else" subgraph outputs are same in count
    auto then_outputs = loco::output_nodes(then_graph); // CircleOutput nodes
    auto else_outputs = loco::output_nodes(else_graph);
    if (then_outputs.size() != else_outputs.size())
    {
      INTERNAL_EXN("CircleIf THEN and ELSE Graph are not same in size");
    }

    // check outputs have same shape and dtype
    auto then_graph_outputs = then_graph->outputs(); // loco::GraphOutput items
    auto else_graph_outputs = else_graph->outputs();
    for (size_t idx = 0; idx < then_outputs.size(); ++idx)
    {
      auto then_out = loco::must_cast<luci::CircleOutput *>(then_outputs.at(idx));
      auto else_out = loco::must_cast<luci::CircleOutput *>(else_outputs.at(idx));

      auto then_graph_output = then_graph_outputs->at(then_out->index());
      auto else_graph_output = else_graph_outputs->at(else_out->index());
      if (then_graph_output->shape()->rank() != else_graph_output->shape()->rank())
      {
        INTERNAL_EXN_V("CircleIf THEN and ELSE Graph Output rank mismatch ", idx);
      }
      for (uint32_t i = 0; i < then_graph_output->shape()->rank(); ++i)
      {
        if (then_graph_output->shape()->dim(i).known() &&
            else_graph_output->shape()->dim(i).known() &&
            then_graph_output->shape()->dim(i).value() !=
              else_graph_output->shape()->dim(i).value())
        {
          INTERNAL_EXN_V("CircleIf THEN and ELSE Graph Output dimension mismatch ", idx);
        }
      }
      if (then_graph_output->dtype() != else_graph_output->dtype())
      {
        INTERNAL_EXN_V("CircleIf THEN and ELSE Graph Output type mismatch ", idx);
      }
    }
  }

  /**
   * @note  Validate CircleWhile node 'cond' and 'body' graph input/output count
   *        shape and type
   */
  void visit(luci::CircleWhile *node) final
  {
    LOGGER(l);
    INFO(l) << "CircleWhile " << node->name() << std::endl;

    auto cond_graph = node->cond_graph();
    auto body_graph = node->body_graph();
    assert(cond_graph != nullptr);
    assert(body_graph != nullptr);

    // Check input of "cond" and input/output of "body" subgraph have the same size
    auto cond_inputs = loco::input_nodes(cond_graph);
    auto cond_outputs = loco::output_nodes(cond_graph);
    auto body_inputs = loco::input_nodes(body_graph);
    auto body_outputs = loco::output_nodes(body_graph);
    if (cond_inputs.size() != body_outputs.size())
    {
      INTERNAL_EXN("CircleWhile COND input and BODY output have different sizes");
    }
    if (cond_inputs.size() != body_inputs.size())
    {
      INTERNAL_EXN("CircleWhile COND input and BODY input have different sizes");
    }
    if (cond_outputs.size() != 1)
    {
      INTERNAL_EXN("CircleWhile COND output must have size 1");
    }
    auto cond_out = loco::must_cast<luci::CircleOutput *>(cond_outputs.at(0));
    if (cond_out->dtype() != loco::DataType::BOOL)
    {
      INTERNAL_EXN("CircleWhile COND output must have bool type");
    }

    // input of "cond" and input/output of "body" subgraph must have the same shape and type
    // First we compare input of "cond" with input of "body"
    auto cond_graph_inputs = cond_graph->inputs();
    auto body_graph_inputs = body_graph->inputs();
    for (size_t idx = 0; idx < cond_inputs.size(); ++idx)
    {
      auto cond_in = loco::must_cast<luci::CircleInput *>(cond_inputs.at(idx));
      auto body_in = loco::must_cast<luci::CircleInput *>(body_inputs.at(idx));

      auto cond_graph_input = cond_graph_inputs->at(cond_in->index());
      auto body_graph_input = body_graph_inputs->at(body_in->index());
      if (cond_graph_input->shape()->rank() != body_graph_input->shape()->rank())
      {
        INTERNAL_EXN_V("CircleWhile COND input and BODY input rank mismatch ", idx);
      }
      for (uint32_t i = 0; i < cond_graph_input->shape()->rank(); ++i)
      {
        if (cond_graph_input->shape()->dim(i).known() &&
            body_graph_input->shape()->dim(i).known() &&
            cond_graph_input->shape()->dim(i).value() != body_graph_input->shape()->dim(i).value())
        {
          INTERNAL_EXN_V("CircleWhile COND input and BODY input dimension mismatch ", idx);
        }
      }
      if (cond_graph_input->dtype() != body_graph_input->dtype())
      {
        INTERNAL_EXN_V("CircleWhile COND input and BODY input type mismatch ", idx);
      }
    }

    // Next we compare input of "cond" with output of "body"
    auto body_graph_outputs = body_graph->outputs();
    for (size_t idx = 0; idx < cond_inputs.size(); ++idx)
    {
      auto cond_in = loco::must_cast<luci::CircleInput *>(cond_inputs.at(idx));
      auto body_out = loco::must_cast<luci::CircleOutput *>(body_outputs.at(idx));

      auto cond_graph_input = cond_graph_inputs->at(cond_in->index());
      auto body_graph_output = body_graph_outputs->at(body_out->index());
      if (cond_graph_input->shape()->rank() != body_graph_output->shape()->rank())
      {
        INTERNAL_EXN_V("CircleWhile COND input and BODY output rank mismatch ", idx);
      }
      for (uint32_t i = 0; i < cond_graph_input->shape()->rank(); ++i)
      {
        if (cond_graph_input->shape()->dim(i).known() &&
            body_graph_output->shape()->dim(i).known() &&
            cond_graph_input->shape()->dim(i).value() != body_graph_output->shape()->dim(i).value())
        {
          INTERNAL_EXN_V("CircleWhile COND input and BODY output dimension mismatch ", idx);
        }
      }
      if (cond_graph_input->dtype() != body_graph_output->dtype())
      {
        INTERNAL_EXN_V("CircleWhile COND input and BODY output type mismatch ", idx);
      }
    }
  }

  void visit(luci::CircleNode *) final
  {
    // DO NOTHING
  }

private:
  const luci::Module *_module;
  const luci::CircleReader &_reader;
};

/**
 * @brief  ValidateGraphProp will validate inter graph node properties
 */
class ValidateGraphProp final
{
public:
  void run(loco::Graph *g, const luci::Module *m, const luci::CircleReader &r)
  {
    for (auto node : loco::postorder_traversal(loco::output_nodes(g)))
    {
      if (recognize(node->dialect()))
      {
        auto cn = loco::must_cast<luci::CircleNode *>(node);

        eval(cn, m, r);
      }
    }
  }

private:
  bool recognize(const loco::Dialect *dialect) { return (dialect == luci::CircleDialect::get()); }

  void eval(luci::CircleNode *node, const luci::Module *module, const luci::CircleReader &reader)
  {
    ValidateNodeProp val(module, reader);
    node->accept(&val);
  }
};

} // namespace

namespace luci
{

/**
 * @brief  Do post import actions
 */
void post_import_graph(luci::Module *module, const luci::CircleReader &reader)
{
  LOGGER(l);

  auto count = module->size();

  for (size_t s = 0; s < count; ++s)
  {
    auto g = module->graph(s);
    assert(g != nullptr);

    INFO(l) << "--- FixInterGraph " << g->name() << "-------------------------";
    FixInterGraph fix;
    fix.run(g, module, reader);
  }

  for (size_t s = 0; s < count; ++s)
  {
    auto g = module->graph(s);
    assert(g != nullptr);

    INFO(l) << "--- ValidateGraphProp " << g->name() << "---------------------";
    ValidateGraphProp prop;
    prop.run(g, module, reader);
  }

  INFO(l) << "--- post_import_graph done -------------------------------------";
}

} // namespace luci