Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / tflite2circle / include / CircleModel.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_MODEL_H__
18 #define __CIRCLE_MODEL_H__
19
20 #include <mio/tflite/schema_generated.h>
21 #include <mio/circle/schema_generated.h>
22
23 #include <iostream>
24 #include <string>
25 #include <vector>
26
27 #include "TFLModel.h"
28
29 namespace tflite2circle
30 {
31
32 using FlatBufBuilder = std::unique_ptr<flatbuffers::FlatBufferBuilder>;
33
34 struct OperatorCodeLink
35 {
36   using TFL = flatbuffers::Offset<::tflite::OperatorCode>;
37   using CIR = flatbuffers::Offset<::circle::OperatorCode>;
38 };
39
40 struct SubGraphLink
41 {
42   using TFL = flatbuffers::Offset<::tflite::SubGraph>;
43   using CIR = flatbuffers::Offset<::circle::SubGraph>;
44 };
45
46 struct BufferLink
47 {
48   using TFL = flatbuffers::Offset<::tflite::Buffer>;
49   using CIR = flatbuffers::Offset<::circle::Buffer>;
50 };
51
52 struct MetaDataBufferLink
53 {
54   using TFL = int32_t;
55   using CIR = int32_t;
56 };
57
58 template <typename T> class Offset
59 {
60 private:
61   using TFLFlatBufVec = flatbuffers::Vector<typename T::TFL>;
62   using CIRFlatBufVecOffset = flatbuffers::Offset<flatbuffers::Vector<typename T::CIR>>;
63
64 public:
65   Offset(void) = delete;
66   Offset(FlatBufBuilder &fb, const TFLFlatBufVec *tflite_flatbuffer_vec);
67
68 public:
69   CIRFlatBufVecOffset offset(void) const { return _circle_flatbuffer_vec_offset; }
70
71 private:
72   CIRFlatBufVecOffset _circle_flatbuffer_vec_offset;
73 };
74
75 class CircleModel
76 {
77 private:
78   using Description = flatbuffers::Offset<flatbuffers::String>;
79
80 public:
81   CircleModel(void) = delete;
82   CircleModel(FlatBufBuilder &fb, TFLModel &tfl_model);
83
84 public:
85   void model_build(void) const;
86   const char *base(void) const;
87   size_t size(void) const;
88
89 private:
90   uint32_t _version;
91   Description _description;
92   FlatBufBuilder &_fb;
93   std::unique_ptr<Offset<OperatorCodeLink>> _operator_codes_offset;
94   std::unique_ptr<Offset<SubGraphLink>> _subGraphs_offset;
95   std::unique_ptr<Offset<BufferLink>> _buffers_offset;
96   std::unique_ptr<Offset<MetaDataBufferLink>> _metadata_buffer_offset;
97 };
98
99 } // namespace tflite2circle
100
101 #endif // __CIRCLE_MODEL_H__