Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / luci / import / src / Nodes / CircleUnique.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/CircleUnique.h"
18
19 #include <luci/IR/Nodes/CircleUnique.h>
20 #include <luci/IR/Nodes/CircleUniqueOut.h>
21
22 #include <loco.h>
23
24 namespace luci
25 {
26
27 bool CircleUniqueGraphBuilder::validate(const ValidateArgs &args) const
28 {
29   if (args.op.inputs.size() != 1)
30     return false;
31
32   if (args.op.outputs.size() != 2)
33     return false;
34
35   return true;
36 }
37
38 void CircleUniqueGraphBuilder::build(const circle::OperatorT &op,
39                                      GraphBuilderContext *context) const
40 {
41   assert(context != nullptr);
42
43   auto graph = context->graph();
44
45   const std::vector<int32_t> &inputs = op.inputs;
46   const std::vector<int32_t> &outputs = op.outputs;
47   const auto &tensors = context->reader()->tensors();
48   auto tensors_ptr = context->reader()->tensors_ptr();
49   assert(tensors_ptr != nullptr);
50
51   std::vector<CircleNode *> input_nodes;
52   for (const int32_t input_tensor_index : inputs)
53   {
54     input_nodes.push_back(context->nodefinder()->node(input_tensor_index));
55   }
56
57   // Create CircleUnique
58   auto node = graph->nodes()->create<CircleUnique>();
59   node->input(input_nodes[0]);
60
61   const auto *options = op.builtin_options.AsUniqueOptions();
62   node->output_type(luci_datatype(options->idx_out_type));
63
64   assert(int32_t(outputs.size()) == 2);
65   // Let's use name of output 0 as Unique name
66   const circle::TensorT &output_tensor = *tensors[outputs[0]];
67   node->name(tensor_name(output_tensor));
68
69   // Create virtual outputs of Unique
70   for (int32_t n = 0; n < 2; ++n)
71   {
72     const circle::TensorT &output_tensor = *tensors[outputs[n]];
73
74     auto *nodeout = graph->nodes()->create<CircleUniqueOut>();
75     copy_tensor_attributes(output_tensor, nodeout);
76     // mark shape_status
77     if (tensors_ptr->Get(outputs[n])->shape() == nullptr)
78       nodeout->shape_status(ShapeStatus::NOSHAPE);
79     else
80       nodeout->shape_status(ShapeStatus::VALID);
81
82     nodeout->input(node);
83     nodeout->index(n);
84
85     context->nodefinder()->enroll(outputs[n], nodeout);
86   }
87 }
88
89 } // namespace luci