2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "luci/Import/Nodes/CircleSplit.h"
19 #include <luci/IR/Nodes/CircleSplit.h>
20 #include <luci/IR/Nodes/CircleSplitOut.h>
23 #include <oops/UserExn.h>
28 bool CircleSplitGraphBuilder::validate(const ValidateArgs &args) const
30 const auto &inputs = args.op.inputs;
31 const auto &outputs = args.op.outputs;
32 const auto *options = args.op.builtin_options.AsSplitOptions();
34 if (inputs.size() != 2)
37 if (static_cast<int32_t>(outputs.size()) != options->num_splits)
46 * @brief Split Node builder
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
52 * --- CircleSplit --- FullyConnected ---
53 * \- FullyConnected ---
55 * will be created like this
57 * --- CircleSplit --- CircleSplitOut --- FullyConnected ---
58 * \- CircleSplitOut --- FullyConnected ---
61 void CircleSplitGraphBuilder::build(const circle::OperatorT &op, GraphBuilderContext *context) const
63 assert(context != nullptr);
65 auto graph = context->graph();
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);
74 std::vector<CircleNode *> input_nodes;
75 for (const int32_t input_tensor_index : inputs)
77 input_nodes.push_back(context->nodefinder()->node(input_tensor_index));
81 auto node = graph->nodes()->create<CircleSplit>();
82 node->split_dim(input_nodes[0]);
83 node->input(input_nodes[1]);
85 const auto *options = op.builtin_options.AsSplitOptions();
86 node->num_split(options->num_splits);
88 assert(outputs.size() > 0);
89 assert(int32_t(outputs.size()) == options->num_splits);
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);
96 // NOTE We don't set quantization for Split itself but to virtual outputs
99 // Create virtual outputs of Split
100 for (int32_t n = 0; n < options->num_splits; ++n)
102 const circle::TensorT &output_tensor = *tensors[outputs[n]];
104 auto *nodeout = graph->nodes()->create<CircleSplitOut>();
105 copy_tensor_attributes(output_tensor, nodeout);
107 if (tensors_ptr->Get(outputs[n])->shape() == nullptr)
108 nodeout->shape_status(ShapeStatus::NOSHAPE);
110 nodeout->shape_status(ShapeStatus::VALID);
112 nodeout->input(node);
115 context->nodefinder()->enroll(outputs[n], nodeout);