Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / frontend / tflite / src / tflite_loader.cc
1 /*
2  * Copyright (c) 2019 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 #include "tflite_loader.h"
18 #include "base_loader.h"
19 #include "tflite_schema_generated.h"
20
21 namespace onert
22 {
23 namespace tflite_loader
24 {
25
26 namespace
27 {
28
29 struct LoaderDomain
30 {
31   using Verifier = flatbuffers::Verifier;
32   using ActivationFunctionType = onert_tflite::ActivationFunctionType;
33   using Buffer = onert_tflite::Buffer;
34   using BuiltinOperator = onert_tflite::BuiltinOperator;
35   using CustomOptionsFormat = onert_tflite::CustomOptionsFormat;
36   using Model = onert_tflite::Model;
37   using Operator = onert_tflite::Operator;
38   using Padding = onert_tflite::Padding;
39   using Pool2DOptions = onert_tflite::Pool2DOptions;
40   using Tensor = onert_tflite::Tensor;
41   using TensorType = onert_tflite::TensorType;
42   using SubGraph = onert_tflite::SubGraph;
43
44   static const char *EnumNameBuiltinOperator(BuiltinOperator e)
45   {
46     return onert_tflite::EnumNameBuiltinOperator(e);
47   }
48   static const char *EnumNameActivationFunctionType(ActivationFunctionType e)
49   {
50     return onert_tflite::EnumNameActivationFunctionType(e);
51   }
52   static const char *EnumNameTensorType(TensorType e)
53   {
54     return onert_tflite::EnumNameTensorType(e);
55   }
56   static const Model *GetModel(const void *buf) { return onert_tflite::GetModel(buf); }
57   static bool VerifyModelBuffer(Verifier &verifier)
58   {
59     return onert_tflite::VerifyModelBuffer(verifier);
60   }
61 };
62
63 class TFLiteLoader final : public base_loader::BaseLoader<LoaderDomain, TFLiteLoader>
64 {
65 public:
66   using BaseLoader::BaseLoader;
67
68   bool allowOptionalInputTensor(BuiltinOperator op) override
69   {
70     switch (op)
71     {
72       case BuiltinOperator::BuiltinOperator_FULLY_CONNECTED:
73         return true;
74       default:
75         return false;
76     }
77   }
78
79   std::unique_ptr<ir::Graph> loadSubgraph(const onert_tflite::SubGraph *tflite_subg)
80   {
81     auto subg = std::make_unique<ir::Graph>();
82     // Load tensors
83     _tensor_to_operand.resize(tflite_subg->tensors()->size());
84     for (flatbuffers::uoffset_t i = 0; i < tflite_subg->tensors()->size(); ++i)
85     {
86       _tensor_to_operand[i] = loadOperand(tflite_subg->tensors()->Get(i), *subg);
87     }
88     // Set inputs
89     for (const std::int32_t input_ind : *tflite_subg->inputs())
90     {
91       subg->addInput(tensorIdxToOperandIdx(input_ind));
92     }
93     // Set outputs
94     for (const std::int32_t output_ind : *tflite_subg->outputs())
95     {
96       subg->addOutput(tensorIdxToOperandIdx(output_ind));
97     }
98     // Create operations
99     for (const auto *op : *tflite_subg->operators())
100     {
101       loadOperation(op, *subg);
102     }
103
104     subg->finishBuilding();
105
106     return subg;
107   }
108 };
109
110 } // namespace
111
112 std::unique_ptr<ir::Subgraphs> loadModel(const char *filename)
113 {
114   auto subgraphs = std::make_unique<ir::Subgraphs>();
115   TFLiteLoader loader(subgraphs);
116   loader.loadFromFile(filename);
117   return subgraphs;
118 }
119
120 } // namespace tflite_loader
121 } // namespace onert