7af2fa59b2b3b5ba3def5d0fb0e8784ed65e58fc
[platform/core/ml/nnfw.git] / compiler / tfldump / src / Read.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 __TFLREAD_READ_H__
18 #define __TFLREAD_READ_H__
19
20 #include <mio/tflite/schema_generated.h>
21
22 #include <map>
23 #include <string>
24 #include <vector>
25
26 namespace tflread
27 {
28
29 template <typename T> std::vector<T> as_index_vector(const flatbuffers::Vector<T> *flat_array)
30 {
31   std::vector<T> ret(flat_array->Length());
32   for (uint32_t i = 0; i < flat_array->Length(); i++)
33   {
34     ret[i] = flat_array->Get(i);
35   }
36   return ret;
37 }
38
39 bool is_valid(const tflite::OperatorCode *opcode);
40 bool is_custom(const tflite::OperatorCode *opcode);
41 std::string opcode_name(const tflite::OperatorCode *opcode);
42 const char *tensor_type(const tflite::Tensor *tensor);
43 const char *tensor_name(const tflite::Tensor *tensor);
44
45 /**
46  * @brief Loads TF lite file and provides helpers to access attributes
47  */
48 class Reader
49 {
50 private:
51   using TFliteSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<tflite::SubGraph>>;
52   using TFliteBuffers_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Buffer>>;
53   using TFliteTensors_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Tensor>>;
54   using TFliteOperators_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Operator>>;
55
56 public:
57   Reader(const tflite::Model *model);
58
59   Reader() = delete;
60
61 public:
62   uint32_t version() const { return _version; }
63
64   const std::vector<const tflite::OperatorCode *> &opcodes() { return _op_codes; }
65   const TFliteBuffers_t *buffers() { return _buffers; }
66   const TFliteTensors_t *tensors() { return _tensors; }
67   const TFliteOperators_t *operators() { return _operators; }
68   const std::vector<int32_t> &inputs() const { return _inputs; }
69   const std::vector<int32_t> &outputs() const { return _outputs; }
70
71   uint32_t num_subgraph() const { return _subgraphs->Length(); }
72
73   size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data);
74   tflite::BuiltinOperator builtin_code(const tflite::Operator *op) const;
75   std::string opcode_name(const tflite::Operator *op) const;
76
77 public:
78   bool select_subgraph(uint32_t subgraph);
79   const std::string &subgraph_name(void) const { return _subgraph_name; }
80   uint32_t subgraph_index(void) const { return _subgraph_index; }
81
82 private:
83   uint32_t _version;
84
85   const TFliteSubGraphs_t *_subgraphs{nullptr};
86   const TFliteBuffers_t *_buffers{nullptr};
87   const TFliteTensors_t *_tensors{nullptr};
88   const TFliteOperators_t *_operators{nullptr};
89
90   uint32_t _subgraph_index;
91   std::string _subgraph_name;
92   std::vector<const tflite::OperatorCode *> _op_codes;
93   std::vector<int32_t> _inputs;
94   std::vector<int32_t> _outputs;
95 };
96
97 } // namespace tflread
98
99 #endif // __TFLREAD_READ_H__