Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / circle-tensordump / src / Reader.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_TENSORDUMP_READER_H__
18 #define __CIRCLE_TENSORDUMP_READER_H__
19
20 #include <mio/circle/schema_generated.h>
21
22 #include <map>
23 #include <string>
24 #include <vector>
25
26 namespace circletensordump
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 circle::OperatorCode *opcode);
40 bool is_custom(const circle::OperatorCode *opcode);
41 std::string opcode_name(const circle::OperatorCode *opcode);
42 const char *tensor_type(const circle::Tensor *tensor);
43 const char *tensor_name(const circle::Tensor *tensor);
44
45 /**
46  * @brief Loads Circle file and provides helpers to access attributes
47  */
48 class Reader
49 {
50 private:
51   using CircleSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<circle::SubGraph>>;
52   using CircleBuffers_t = flatbuffers::Vector<flatbuffers::Offset<circle::Buffer>>;
53   using CircleTensors_t = flatbuffers::Vector<flatbuffers::Offset<circle::Tensor>>;
54   using CircleOperators_t = flatbuffers::Vector<flatbuffers::Offset<circle::Operator>>;
55
56 public:
57   Reader(const circle::Model *model);
58
59   Reader() = delete;
60
61 public:
62   const std::vector<const circle::OperatorCode *> &opcodes() { return _op_codes; }
63   const CircleBuffers_t *buffers() { return _buffers; }
64   const CircleTensors_t *tensors() { return _tensors; }
65   const CircleOperators_t *operators() { return _operators; }
66   const std::vector<int32_t> &inputs() const { return _inputs; }
67   const std::vector<int32_t> &outputs() const { return _outputs; }
68
69   uint32_t num_subgraph() const { return _subgraphs->Length(); }
70
71   size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data);
72   circle::BuiltinOperator builtin_code(const circle::Operator *op) const;
73   std::string opcode_name(const circle::Operator *op) const;
74
75 public:
76   bool select_subgraph(uint32_t subgraph);
77
78 private:
79   const CircleSubGraphs_t *_subgraphs{nullptr};
80   const CircleBuffers_t *_buffers{nullptr};
81   const CircleTensors_t *_tensors{nullptr};
82   const CircleOperators_t *_operators{nullptr};
83
84   std::vector<const circle::OperatorCode *> _op_codes;
85   std::vector<int32_t> _inputs;
86   std::vector<int32_t> _outputs;
87 };
88
89 } // namespace circletensordump
90
91 #endif // __CIRCLE_TENSORDUMP_READER_H__