summaryrefslogtreecommitdiff
path: root/compiler/luci/service/src/Validate.cpp
blob: 65b82c2b4f0213fc34ab612c48b8f5eccd638695 (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
/*
 * 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 "luci/Service/Validate.h"

#include <luci/IR/Nodes/CircleOutput.h>
#include <luci/Log.h>

#include <loco/IR/NodeShape.h>
#include <loco/Service/ShapeInference.h>
#include <loco/Service/TypeInference.h>

#include <cassert>
#include <vector>

namespace
{

/**
 * @brief  returns a node that is CircleOutput with index is out_index in nodes
 */
luci::CircleOutput *find_node(std::vector<loco::Node *> nodes, loco::GraphOutputIndex out_index)
{
  for (auto node : nodes)
  {
    auto circle_output = dynamic_cast<luci::CircleOutput *>(node);
    if (circle_output != nullptr)
    {
      if (circle_output->indexed() && circle_output->index() == out_index)
        return circle_output;
    }
  }
  return nullptr;
}

bool validate_shape_type(loco::Graph *g)
{
  LOGGER(l);

  auto output_nodes = loco::output_nodes(g);

  auto count = g->outputs()->size();
  for (uint32_t out = 0; out < count; ++out)
  {
    auto graph_out = g->outputs()->at(out);
    auto out_index = graph_out->index();

    auto circle_output = find_node(output_nodes, out_index);
    assert(circle_output != nullptr);
    assert(circle_output->from() != nullptr);
    auto circle_node = dynamic_cast<luci::CircleNode *>(circle_output->from());
    assert(circle_node != nullptr);
    assert(loco::shape_known(circle_node));

    // check if output node shape is same as graph output shape
    auto co_shape = loco::shape_get(circle_node);
    auto go_tensor_shape = graph_out->shape();
    assert(go_tensor_shape);
    auto go_shape = loco::NodeShape(*go_tensor_shape);
    if (!(co_shape == go_shape))
    {
      INFO(l) << "Shape for #" << out_index << " not same " << std::endl;
      return false;
    }

    // check if data type match
    assert(loco::dtype_known(circle_node));
    if (graph_out->dtype() != loco::dtype_get(circle_node))
    {
      INFO(l) << "Type for #" << out_index << " not same " << std::endl;
      return false;
    }
  }

  return true;
}

} // namespace

namespace luci
{

bool validate(loco::Graph *g)
{
  if (!loco::valid(g))
    return false;

  if (!validate_shape_type(g))
    return false;

  // TODO add more validation

  return true;
}

} // namespace luci