Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / luci / import / include / luci / Import / CircleReader.h
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 #ifndef __LUCI_IMPORT_GRAPHREADER_H__
18 #define __LUCI_IMPORT_GRAPHREADER_H__
19
20 #include <mio/circle/schema_generated.h>
21
22 #include <luci/IR/AttrFusedActFunc.h>
23 #include <luci/IR/AttrPadding.h>
24 #include <luci/IR/CircleNode.h>
25 #include <luci/IR/CircleQuantParam.h>
26
27 #include <loco.h>
28
29 #include <map>
30 #include <memory>
31 #include <string>
32 #include <vector>
33
34 namespace luci
35 {
36
37 bool is_valid(const circle::OperatorCodeT &opcode);
38 bool is_custom(const circle::OperatorCodeT &opcode);
39 std::string opcode_name(const circle::OperatorCodeT &opcode);
40 const char *tensor_name(const circle::TensorT &tensor);
41 const circle::QuantizationParametersT *tensor_quantization(const circle::TensorT &tensor);
42
43 loco::DataType luci_datatype(circle::TensorType type);
44 FusedActFunc luci_actfunc(const circle::ActivationFunctionType type);
45 Padding luci_padding(const circle::Padding padding);
46 MirrorPadMode luci_mirrorpad_mode(const circle::MirrorPadMode mode);
47 std::unique_ptr<CircleQuantParam>
48 luci_quantparam(const circle::QuantizationParametersT *quantization);
49
50 /// @brief Copy common tensor attributes such as name, type, etc. to node.
51 void copy_tensor_attributes(const circle::TensorT &tensor, CircleNode *node);
52
53 /**
54  * @brief Loads Circle file and provides helpers to access attributes
55  */
56 class CircleReader
57 {
58 private:
59   using CircleBuffers_t = std::vector<std::unique_ptr<circle::BufferT>>;
60   using CircleTensors_t = std::vector<std::unique_ptr<circle::TensorT>>;
61   using CircleOperators_t = std::vector<std::unique_ptr<circle::OperatorT>>;
62   using CircleOperatorCodes_t = std::vector<std::unique_ptr<circle::OperatorCodeT>>;
63
64   using CircleSubGraphsPtr_t = flatbuffers::Vector<flatbuffers::Offset<circle::SubGraph>>;
65   using CircleTensorsPtr_t = flatbuffers::Vector<flatbuffers::Offset<circle::Tensor>>;
66
67 public:
68   CircleReader() = default;
69
70 public:
71   const CircleOperatorCodes_t &opcodes() const { return _model->operator_codes; }
72   const CircleBuffers_t &buffers() const { return _model->buffers; }
73   const CircleTensors_t &tensors() const { return _current_subgraph->tensors; }
74   const CircleOperators_t &operators() const { return _current_subgraph->operators; }
75   const std::vector<int32_t> &inputs() const { return _current_subgraph->inputs; }
76   const std::vector<int32_t> &outputs() const { return _current_subgraph->outputs; }
77   const std::string &name() const { return _current_subgraph->name; }
78
79   const CircleTensorsPtr_t *tensors_ptr() const { return _tensors_ptr; }
80
81   uint32_t num_subgraph() const { return _model->subgraphs.size(); }
82
83   circle::BuiltinOperator builtin_code(const circle::OperatorT &op) const;
84   std::string opcode_name(const circle::OperatorT &op) const;
85
86 public:
87   bool parse(const circle::Model *model);
88   bool select_subgraph(uint32_t subgraph);
89
90 private:
91   std::unique_ptr<const circle::ModelT> _model;
92   const circle::SubGraphT *_current_subgraph{nullptr};
93
94   const circle::Model *_model_ptr{nullptr};
95   const CircleTensorsPtr_t *_tensors_ptr{nullptr};
96 };
97
98 } // namespace luci
99
100 #endif // __LUCI_IMPORT_GRAPHREADER_H__