Imported Upstream version 1.18.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) : _fb{fb} {};
67
68 public:
69   // TODO use _fb
70   void build(FlatBufBuilder &fb, const TFLFlatBufVec *tflite_flatbuffer_vec);
71
72 public:
73   CIRFlatBufVecOffset offset(void) const { return _circle_flatbuffer_vec_offset; }
74
75 private:
76   FlatBufBuilder &_fb;
77   CIRFlatBufVecOffset _circle_flatbuffer_vec_offset;
78 };
79
80 class CircleModel
81 {
82 private:
83   using Description = flatbuffers::Offset<flatbuffers::String>;
84
85 public:
86   CircleModel(void) = delete;
87   CircleModel(FlatBufBuilder &fb, const tflite::Model *tfl_model);
88
89 public:
90   void model_build(void) const;
91   const char *base(void) const;
92   size_t size(void) const;
93
94 private:
95   uint32_t _version;
96   Description _description;
97   FlatBufBuilder &_fb;
98   std::unique_ptr<Offset<OperatorCodeLink>> _operator_codes_offset;
99   std::unique_ptr<Offset<SubGraphLink>> _subGraphs_offset;
100   std::unique_ptr<Offset<BufferLink>> _buffers_offset;
101   std::unique_ptr<Offset<MetaDataBufferLink>> _metadata_buffer_offset;
102 };
103
104 } // namespace tflite2circle
105
106 #endif // __CIRCLE_MODEL_H__