2 * Copyright (c) 2020 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 #ifndef __LUCI_IMPORT_GRAPHREADER_H__
18 #define __LUCI_IMPORT_GRAPHREADER_H__
20 #include <mio/circle/schema_generated.h>
22 #include <luci/IR/AttrFusedActFunc.h>
23 #include <luci/IR/AttrPadding.h>
24 #include <luci/IR/CircleNode.h>
25 #include <luci/IR/CircleQuantParam.h>
37 bool is_valid(const circle::OperatorCodeT &opcode);
38 bool is_custom(const circle::OperatorCodeT &opcode);
39 std::string opcode_name(const circle::OperatorCodeT &opcode);
40 const char *tensor_name(const circle::TensorT &tensor);
41 const circle::QuantizationParametersT *tensor_quantization(const circle::TensorT &tensor);
43 loco::DataType luci_datatype(circle::TensorType type);
44 FusedActFunc luci_actfunc(const circle::ActivationFunctionType type);
45 Padding luci_padding(const circle::Padding padding);
46 MirrorPadMode luci_mirrorpad_mode(const circle::MirrorPadMode mode);
47 std::unique_ptr<CircleQuantParam>
48 luci_quantparam(const circle::QuantizationParametersT *quantization);
50 /// @brief Copy common tensor attributes such as name, type, etc. to node.
51 void copy_tensor_attributes(const circle::TensorT &tensor, CircleNode *node);
54 * @brief Loads Circle file and provides helpers to access attributes
59 using CircleBuffers_t = std::vector<std::unique_ptr<circle::BufferT>>;
60 using CircleTensors_t = std::vector<std::unique_ptr<circle::TensorT>>;
61 using CircleOperators_t = std::vector<std::unique_ptr<circle::OperatorT>>;
62 using CircleOperatorCodes_t = std::vector<std::unique_ptr<circle::OperatorCodeT>>;
64 using CircleSubGraphsPtr_t = flatbuffers::Vector<flatbuffers::Offset<circle::SubGraph>>;
65 using CircleTensorsPtr_t = flatbuffers::Vector<flatbuffers::Offset<circle::Tensor>>;
68 CircleReader() = default;
71 const CircleOperatorCodes_t &opcodes() const { return _model->operator_codes; }
72 const CircleBuffers_t &buffers() const { return _model->buffers; }
73 const CircleTensors_t &tensors() const { return _current_subgraph->tensors; }
74 const CircleOperators_t &operators() const { return _current_subgraph->operators; }
75 const std::vector<int32_t> &inputs() const { return _current_subgraph->inputs; }
76 const std::vector<int32_t> &outputs() const { return _current_subgraph->outputs; }
77 const std::string &name() const { return _current_subgraph->name; }
79 const CircleTensorsPtr_t *tensors_ptr() const { return _tensors_ptr; }
81 uint32_t num_subgraph() const { return _model->subgraphs.size(); }
83 circle::BuiltinOperator builtin_code(const circle::OperatorT &op) const;
84 std::string opcode_name(const circle::OperatorT &op) const;
87 bool parse(const circle::Model *model);
88 bool select_subgraph(uint32_t subgraph);
91 std::unique_ptr<const circle::ModelT> _model;
92 const circle::SubGraphT *_current_subgraph{nullptr};
94 const circle::Model *_model_ptr{nullptr};
95 const CircleTensorsPtr_t *_tensors_ptr{nullptr};
100 #endif // __LUCI_IMPORT_GRAPHREADER_H__