Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / luci / import / src / Nodes / CircleReshape.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/Import/Nodes/CircleReshape.h"
18
19 #include <luci/IR/Nodes/CircleConst.h>
20 #include <luci/IR/Nodes/CircleReshape.h>
21
22 namespace luci
23 {
24
25 bool CircleReshapeGraphBuilder::validate(const ValidateArgs &args) const
26 {
27   if (args.op.inputs.size() != 1 && args.op.inputs.size() != 2)
28     return false;
29
30   if (args.op.outputs.size() != 1)
31     return false;
32
33   return true;
34 }
35
36 static void setup_shape_attribute(const std::vector<int32_t> &shape, CircleReshape *node)
37 {
38   node->newShape()->rank(shape.size());
39   for (uint32_t i = 0; i < shape.size(); ++i)
40   {
41     node->newShape()->dim(i) = shape[i];
42   }
43 }
44
45 static CircleNode *create_shape_node(const std::vector<int32_t> &shape, loco::Graph *graph)
46 {
47   auto *shape_node = graph->nodes()->create<luci::CircleConst>();
48   shape_node->dtype(loco::DataType::S32);
49   shape_node->rank(1);
50   shape_node->dim(0) = shape.size();
51   shape_node->size<loco::DataType::S32>(shape.size());
52   for (uint32_t i = 0; i < shape.size(); ++i)
53   {
54     shape_node->at<loco::DataType::S32>(i) = shape[i];
55   }
56   return shape_node;
57 }
58
59 CircleNode *CircleReshapeGraphBuilder::build_node(const circle::OperatorT &op,
60                                                   const std::vector<CircleNode *> &inputs,
61                                                   loco::Graph *graph) const
62 {
63   // If the second input is not provided, generate it based on the value of the attribute.
64   // TODO Presence of the second input is the current requirement of the IR.
65   auto *shape_node = (inputs.size() == 2) ? inputs.at(1) : nullptr;
66   if (shape_node == nullptr)
67   {
68     const auto *options = op.builtin_options.AsReshapeOptions();
69     if (options != nullptr)
70       shape_node = create_shape_node(options->new_shape, graph);
71     else
72     {
73       shape_node = graph->nodes()->create<CircleOutputDummy>();
74       shape_node->dtype(loco::DataType::S32);
75       shape_node->rank(0);
76     }
77   }
78
79   auto *node = graph->nodes()->create<CircleReshape>();
80   node->tensor(inputs.at(0));
81   node->shape(shape_node);
82
83   const auto *options = op.builtin_options.AsReshapeOptions();
84   if (options)
85     setup_shape_attribute(options->new_shape, node);
86
87   return node;
88 }
89
90 } // namespace luci