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/CircleSplitV.h"
19 #include <luci/IR/Nodes/CircleSplitV.h>
20 #include <luci/IR/Nodes/CircleSplitVOut.h>
23 #include <oops/UserExn.h>
28 bool CircleSplitVGraphBuilder::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.AsSplitVOptions();
34 if (inputs.size() != 3)
37 if (static_cast<int32_t>(outputs.size()) != options->num_splits)
46 * @brief SplitV Node builder
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
52 * --- CircleSplitV --- FullyConnected ---
53 * \- FullyConnected ---
55 * will be created like this
57 * --- CircleSplitV --- CircleSplitVOut --- FullyConnected ---
58 * \- CircleSplitVOut --- FullyConnected ---
61 void CircleSplitVGraphBuilder::build(const circle::OperatorT &op,
62 GraphBuilderContext *context) const
64 assert(context != nullptr);
66 auto graph = context->graph();
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);
75 std::vector<CircleNode *> input_nodes;
76 for (const int32_t input_tensor_index : inputs)
78 input_nodes.push_back(context->nodefinder()->node(input_tensor_index));
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]);
87 const auto *options = op.builtin_options.AsSplitVOptions();
88 node->num_split(options->num_splits);
90 assert(outputs.size() > 0);
91 assert(int32_t(outputs.size()) == options->num_splits);
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);
98 // NOTE We don't set quantization for Split itself but to virtual outputs
101 // Create virtual outputs of Split
102 for (int32_t n = 0; n < options->num_splits; ++n)
104 const circle::TensorT &output_tensor = *tensors[outputs[n]];
106 auto *nodeout = graph->nodes()->create<CircleSplitVOut>();
107 copy_tensor_attributes(output_tensor, nodeout);
109 if (tensors_ptr->Get(outputs[n])->shape() == nullptr)
110 nodeout->shape_status(ShapeStatus::NOSHAPE);
112 nodeout->shape_status(ShapeStatus::VALID);
114 nodeout->input(node);
117 context->nodefinder()->enroll(outputs[n], nodeout);