Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / mio-circle05 / include / mio_circle / Reader.h
1 /*
2  * Copyright (c) 2023 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 __MIO_CIRCLE05_READER_H__
18 #define __MIO_CIRCLE05_READER_H__
19
20 #include <mio/circle/schema_generated.h>
21
22 #include <map>
23 #include <string>
24 #include <vector>
25
26 // NOTE Reader class originated from circledump and for circle-tensordump
27 //      where this class has more work to be done for stability
28 //      as the tools are for developers not customores.
29
30 namespace mio
31 {
32 namespace circle
33 {
34
35 /**
36  * @brief Loads Circle file and provides helpers to access attributes
37  */
38 class Reader
39 {
40 private:
41   using CircleSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<::circle::SubGraph>>;
42   using CircleBuffers_t = flatbuffers::Vector<flatbuffers::Offset<::circle::Buffer>>;
43   using CircleTensors_t = flatbuffers::Vector<flatbuffers::Offset<::circle::Tensor>>;
44   using CircleOperators_t = flatbuffers::Vector<flatbuffers::Offset<::circle::Operator>>;
45   using CircleMetadata_t = flatbuffers::Vector<flatbuffers::Offset<::circle::Metadata>>;
46   using CircleSignatureDef_t = flatbuffers::Vector<flatbuffers::Offset<::circle::SignatureDef>>;
47
48 public:
49   Reader(const ::circle::Model *model);
50
51   Reader() = delete;
52
53 public:
54   uint32_t version() const { return _version; }
55
56   const std::vector<const ::circle::OperatorCode *> &opcodes() { return _op_codes; }
57   const CircleBuffers_t *buffers() { return _buffers; }
58   const CircleTensors_t *tensors() { return _tensors; }
59   const CircleOperators_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   const ::circle::DataFormat &data_format() const { return _data_format; }
63   const CircleMetadata_t *metadata() const { return _metadata; }
64   const CircleSignatureDef_t *signature_defs() const { return _signature_defs; }
65
66   uint32_t num_subgraph() const { return _subgraphs->Length(); }
67
68   size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data);
69   ::circle::BuiltinOperator builtin_code(const ::circle::Operator *op) const;
70   std::string opcode_name(const ::circle::Operator *op) const;
71   std::vector<int32_t> outputs(const ::circle::Operator *op) const;
72   std::string tensor_name(const ::circle::Tensor *tensor) const;
73   std::string tensor_dtype(const ::circle::Tensor *tensor) const;
74
75 public:
76   bool select_subgraph(uint32_t subgraph);
77   const std::string &subgraph_name(void) const { return _subgraph_name; }
78   uint32_t subgraph_index(void) const { return _subgraph_index; }
79
80 private:
81   uint32_t _version;
82
83   const CircleSubGraphs_t *_subgraphs{nullptr};
84   const CircleBuffers_t *_buffers{nullptr};
85   const CircleTensors_t *_tensors{nullptr};
86   const CircleOperators_t *_operators{nullptr};
87   const CircleMetadata_t *_metadata{nullptr};
88   const CircleSignatureDef_t *_signature_defs{nullptr};
89
90   uint32_t _subgraph_index = 0;
91   std::string _subgraph_name;
92   std::vector<const ::circle::OperatorCode *> _op_codes;
93   std::vector<int32_t> _inputs;
94   std::vector<int32_t> _outputs;
95   ::circle::DataFormat _data_format = ::circle::DataFormat::DataFormat_CHANNELS_FIRST;
96 };
97
98 } // namespace circle
99 } // namespace mio
100
101 #endif // __MIO_CIRCLE05_READER_H__