Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compiler / luci / pass / src / MigrateLegacyShapeDtypePass.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "luci/Pass/MigrateLegacyShapeDtypePass.h"
18
19 #include <loco/Service/ShapeInference.h>
20 #include <loco/Service/TypeInference.h>
21
22 #include <luci/IR/CircleNodes.h>
23
24 #include <loco.h>
25
26 namespace
27 {
28
29 bool has_same_shape(luci::CircleNode *node, loco::TensorShape shape)
30 {
31   if (node->rank() != shape.rank())
32     return false;
33
34   for (uint32_t i = 0; i < shape.rank(); ++i)
35     if (!(node->dim(i) == shape.dim(i)))
36       return false;
37
38   return true;
39 }
40
41 } // namespace
42
43 namespace luci
44 {
45
46 bool MigrateLegacyShapeDtypePass::run(luci::Module *m)
47 {
48   bool changed = false;
49
50   for (size_t g = 0; g < m->size(); ++g)
51   {
52     if (run(m->graph(g)))
53       changed = true;
54   }
55
56   return changed;
57 }
58
59 bool MigrateLegacyShapeDtypePass::run(loco::Graph *g)
60 {
61   bool changed = false;
62
63   for (auto node : loco::all_nodes(g))
64   {
65     auto circle_node = loco::must_cast<luci::CircleNode *>(node);
66     if (loco::shape_known(node))
67     {
68       auto loco_shape = loco::shape_get(node).as<loco::TensorShape>();
69
70       assert(circle_node->shape_signature().rank() == 0 ||
71              circle_node->shape_signature().rank() == loco_shape.rank());
72
73       // When shape of loco is copied to circle node, ShapeSignature should be applied.
74       loco::TensorShape new_shape;
75       new_shape.rank(loco_shape.rank());
76       for (uint32_t i = 0; i < loco_shape.rank(); ++i)
77       {
78         if (circle_node->shape_signature().rank() > 0 &&
79             circle_node->shape_signature().dim(i) == -1)
80           new_shape.dim(i) = 1;
81         else
82           new_shape.dim(i) = loco_shape.dim(i);
83       }
84
85       if (circle_node->shape_status() == luci::ShapeStatus::UNDEFINED ||
86           !has_same_shape(circle_node, new_shape))
87       {
88         circle_node->rank(new_shape.rank());
89         for (uint32_t i = 0; i < new_shape.rank(); ++i)
90           circle_node->dim(i) = new_shape.dim(i);
91
92         if (circle_node->shape_status() == luci::ShapeStatus::UNDEFINED)
93           circle_node->shape_status(luci::ShapeStatus::VALID);
94
95         changed = true;
96       }
97     }
98
99     if (loco::dtype_known(node))
100     {
101       if (loco::dtype_get(node) != circle_node->dtype())
102       {
103         circle_node->dtype(loco::dtype_get(node));
104         changed = true;
105       }
106     }
107   }
108
109   return changed;
110 }
111
112 } // namespace luci