Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / luci / import / src / GraphBuilder.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/GraphBuilder.h"
18
19 #include <luci/Log.h>
20
21 namespace luci
22 {
23
24 void GraphBuilder::build(const circle::OperatorT &op, GraphBuilderContext *context) const
25 {
26   LOGGER(l);
27
28   assert(context != nullptr);
29
30   const std::vector<int32_t> &inputs = op.inputs;
31   const std::vector<int32_t> &outputs = op.outputs;
32   const auto &tensors = context->reader()->tensors();
33   const auto &opcodes = context->reader()->opcodes();
34   auto tensors_ptr = context->reader()->tensors_ptr();
35   assert(tensors_ptr != nullptr);
36
37   std::vector<CircleNode *> input_nodes;
38   for (const int32_t input_tensor_index : inputs)
39   {
40     if (input_tensor_index >= 0)
41     {
42       auto input = context->nodefinder()->node(input_tensor_index);
43       if (input == nullptr)
44         INFO(l) << "[luci] Warning: input node is null " << input_tensor_index << std::endl;
45       input_nodes.push_back(input);
46     }
47     else
48     {
49       // If there is no tensor, insert CircleOutputExclude.
50       input_nodes.push_back(context->graph()->nodes()->create<luci::CircleOutputExclude>());
51     }
52   }
53
54   CircleNode *node = build_node(op, input_nodes, context->graph());
55
56   // Set up node parameters.
57   assert(outputs.size() == 1);
58   {
59     const circle::TensorT &output_tensor = *tensors[outputs[0]];
60     copy_tensor_attributes(output_tensor, node);
61     // mark shape_status
62     if (tensors_ptr->Get(outputs[0])->shape() == nullptr)
63       node->shape_status(ShapeStatus::NOSHAPE);
64     else
65       node->shape_status(ShapeStatus::VALID);
66
67     // mark operator version
68     node->op_version(opcodes[op.opcode_index].get()->version);
69   }
70
71   // Register node's only output.
72   assert(outputs.size() == 1);
73   {
74     context->nodefinder()->enroll(outputs[0], node);
75   }
76 }
77
78 } // namespace luci