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