Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / compiler / circlechef / circle / src / CircleImport.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 __CIRCLE_IMPORT_H__
18 #define __CIRCLE_IMPORT_H__
19
20 #include <mio/circle/schema_generated.h>
21
22 #include <souschef/TensorFiller.h>
23
24 #include <circlechef.pb.h>
25
26 #include <map>
27 #include <vector>
28
29 namespace circlechef
30 {
31
32 using CircleSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<circle::SubGraph>>;
33 using CircleTensors_t = flatbuffers::Vector<flatbuffers::Offset<circle::Tensor>>;
34 using CircleBuffers_t = flatbuffers::Vector<flatbuffers::Offset<circle::Buffer>>;
35 using CircleOperators_t = flatbuffers::Vector<flatbuffers::Offset<circle::Operator>>;
36
37 const char *tensor_type(const circle::Tensor *tensor);
38 const char *tensor_name(const circle::Tensor *tensor);
39 bool is_valid(const circle::OperatorCode *opcode);
40 bool is_custom(const circle::OperatorCode *opcode);
41
42 /**
43  * @brief Loads TF lite file and provides helpers to access attributes
44  */
45 class CircleImport : public souschef::TensorFiller
46 {
47 public:
48   CircleImport(const circle::Model *model);
49
50   CircleImport() = delete;
51
52 public:
53   bool select_sub_graph(uint32_t subgraph);
54
55 public:
56   const CircleBuffers_t *buffers() { return _buffers; }
57   const CircleTensors_t *tensors() { return _tensors; }
58   const CircleOperators_t *operators() { return _operators; }
59   const std::vector<int32_t> &inputs() const { return _inputs; }
60   const std::vector<int32_t> &outputs() const { return _outputs; }
61
62   uint32_t num_subgraph() const { return _subgraphs->Length(); }
63
64   circle::BuiltinOperator builtin_code(const circle::Operator *op) const;
65   std::string opcode_name(const circle::Operator *op) const;
66   size_t buffer_info(const circle::Tensor *tensor, const uint8_t **buff_data);
67
68 private:
69   const CircleSubGraphs_t *_subgraphs{nullptr};
70   const CircleBuffers_t *_buffers{nullptr};
71   const CircleTensors_t *_tensors{nullptr};
72   const CircleOperators_t *_operators{nullptr};
73
74   std::vector<const circle::OperatorCode *> _op_codes{};
75   std::vector<int32_t> _inputs{};
76   std::vector<int32_t> _outputs{};
77 };
78
79 } // namespace circlechef
80
81 #endif // __CIRCLE_IMPORT_H__