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