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