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