Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / luci / import / src / Nodes / CircleSplit.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/CircleSplit.h"
18
19 #include <luci/IR/Nodes/CircleSplit.h>
20 #include <luci/IR/Nodes/CircleSplitOut.h>
21
22 #include <loco.h>
23 #include <oops/UserExn.h>
24
25 namespace luci
26 {
27
28 bool CircleSplitGraphBuilder::validate(const ValidateArgs &args) const
29 {
30   const auto &inputs = args.op.inputs;
31   const auto &outputs = args.op.outputs;
32   const auto *options = args.op.builtin_options.AsSplitOptions();
33
34   if (inputs.size() != 2)
35     return false;
36
37   if (static_cast<int32_t>(outputs.size()) != options->num_splits)
38     return false;
39
40   // TODO check types
41
42   return true;
43 }
44
45 /**
46  * @brief  Split Node builder
47  *
48  * @note   Current loco does not provide multiple outputs
49  *         We will create multiple CircleSplitOut nodes to emulate this
50  *         For two outputs that may look like this
51  *
52  *         --- CircleSplit --- FullyConnected ---
53  *                          \- FullyConnected ---
54  *
55  *         will be created like this
56  *
57  *         --- CircleSplit --- CircleSplitOut --- FullyConnected ---
58  *                          \- CircleSplitOut --- FullyConnected ---
59  */
60
61 void CircleSplitGraphBuilder::build(const circle::OperatorT &op, GraphBuilderContext *context) const
62 {
63   assert(context != nullptr);
64
65   auto graph = context->graph();
66
67   const std::vector<int32_t> &inputs = op.inputs;
68   const std::vector<int32_t> &outputs = op.outputs;
69   const auto &tensors = context->reader()->tensors();
70   const auto &opcodes = context->reader()->opcodes();
71   auto tensors_ptr = context->reader()->tensors_ptr();
72   assert(tensors_ptr != nullptr);
73
74   std::vector<CircleNode *> input_nodes;
75   for (const int32_t input_tensor_index : inputs)
76   {
77     input_nodes.push_back(context->nodefinder()->node(input_tensor_index));
78   }
79
80   // Create CircleSplit
81   auto node = graph->nodes()->create<CircleSplit>();
82   node->split_dim(input_nodes[0]);
83   node->input(input_nodes[1]);
84
85   const auto *options = op.builtin_options.AsSplitOptions();
86   node->num_split(options->num_splits);
87
88   assert(outputs.size() > 0);
89   assert(int32_t(outputs.size()) == options->num_splits);
90   {
91     // Let's use name of output 0 as Split name
92     const circle::TensorT &output_tensor = *tensors[outputs[0]];
93     node->name(tensor_name(output_tensor));
94     node->op_version(opcodes[op.opcode_index].get()->version);
95
96     // NOTE We don't set quantization for Split itself but to virtual outputs
97   }
98
99   // Create virtual outputs of Split
100   for (int32_t n = 0; n < options->num_splits; ++n)
101   {
102     const circle::TensorT &output_tensor = *tensors[outputs[n]];
103
104     auto *nodeout = graph->nodes()->create<CircleSplitOut>();
105     copy_tensor_attributes(output_tensor, nodeout);
106     // mark shape_status
107     if (tensors_ptr->Get(outputs[n])->shape() == nullptr)
108       nodeout->shape_status(ShapeStatus::NOSHAPE);
109     else
110       nodeout->shape_status(ShapeStatus::VALID);
111
112     nodeout->input(node);
113     nodeout->index(n);
114
115     context->nodefinder()->enroll(outputs[n], nodeout);
116   }
117 }
118
119 } // namespace luci