2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "circle_loader.h"
18 #include "base_loader.h"
19 #include "circle_schema_generated.h"
23 namespace circle_loader
29 ir::Layout convertDataFormat(circle::DataFormat data_format)
33 case circle::DataFormat::DataFormat_CHANNELS_FIRST:
34 return ir::Layout::NCHW;
35 case circle::DataFormat::DataFormat_CHANNELS_LAST:
36 return ir::Layout::NHWC;
38 throw std::runtime_error("Unsupported DataFormat");
44 using Verifier = flatbuffers::Verifier;
45 using ActivationFunctionType = circle::ActivationFunctionType;
46 using Buffer = circle::Buffer;
47 using BuiltinOperator = circle::BuiltinOperator;
48 using CustomOptionsFormat = circle::CustomOptionsFormat;
49 using Model = circle::Model;
50 using Operator = circle::Operator;
51 using Padding = circle::Padding;
52 using Pool2DOptions = circle::Pool2DOptions;
53 using Tensor = circle::Tensor;
54 using TensorType = circle::TensorType;
55 using SubGraph = circle::SubGraph;
57 static const char *EnumNameBuiltinOperator(BuiltinOperator e)
59 return circle::EnumNameBuiltinOperator(e);
61 static const char *EnumNameActivationFunctionType(ActivationFunctionType e)
63 return circle::EnumNameActivationFunctionType(e);
65 static const char *EnumNameTensorType(TensorType e) { return circle::EnumNameTensorType(e); }
66 static const Model *GetModel(const void *buf) { return circle::GetModel(buf); }
67 static bool VerifyModelBuffer(Verifier &verifier) { return circle::VerifyModelBuffer(verifier); }
70 class CircleLoader final : public base_loader::BaseLoader<LoaderDomain, CircleLoader>
73 using BaseLoader::BaseLoader;
75 bool allowOptionalInputTensor(BuiltinOperator op) override
79 case BuiltinOperator::BuiltinOperator_FULLY_CONNECTED:
80 case BuiltinOperator::BuiltinOperator_BCQ_FULLY_CONNECTED:
87 std::unique_ptr<ir::Graph> loadSubgraph(const circle::SubGraph *circle_subg)
89 auto subg = std::make_unique<ir::Graph>();
91 _tensor_to_operand.resize(circle_subg->tensors()->size());
92 for (flatbuffers::uoffset_t i = 0; i < circle_subg->tensors()->size(); ++i)
94 _tensor_to_operand[i] = loadOperand(circle_subg->tensors()->Get(i), *subg);
97 for (const std::int32_t input_ind : *circle_subg->inputs())
99 subg->addInput(tensorIdxToOperandIdx(input_ind));
102 for (const std::int32_t output_ind : *circle_subg->outputs())
104 subg->addOutput(tensorIdxToOperandIdx(output_ind));
107 for (const auto *op : *circle_subg->operators())
109 CircleLoader::loadOperation(op, *subg);
112 subg->setLayout(convertDataFormat(circle_subg->data_format()));
114 subg->finishBuilding();
119 void loadOperation(const circle::Operator *op, ir::Graph &subg)
121 const auto builtin_op = _model->operator_codes()->Get(op->opcode_index())->builtin_code();
125 case circle::BuiltinOperator::BuiltinOperator_INSTANCE_NORM:
126 loadInstanceNorm(op, subg);
128 case circle::BuiltinOperator::BuiltinOperator_BCQ_FULLY_CONNECTED:
129 loadBCQFullyConnected(op, subg);
131 case circle::BuiltinOperator::BuiltinOperator_BCQ_GATHER:
132 loadBCQGather(op, subg);
135 BaseLoader::loadOperation(op, subg);
143 std::unique_ptr<ir::Subgraphs> loadModel(const char *filename)
145 auto subgraphs = std::make_unique<ir::Subgraphs>();
146 CircleLoader loader(subgraphs);
147 loader.loadFromFile(filename);
151 } // namespace circle_loader