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>
26 #include <luci/IR/CircleShapeSignature.h>
27 #include <luci/IR/SparsityParam.h>
39 bool is_valid(const circle::OperatorCodeT &opcode);
40 bool is_custom(const circle::OperatorCodeT &opcode);
41 std::string opcode_name(const circle::OperatorCodeT &opcode);
42 const char *tensor_name(const circle::TensorT &tensor);
43 const circle::QuantizationParametersT *tensor_quantization(const circle::TensorT &tensor);
45 loco::DataType luci_datatype(circle::TensorType type);
46 FusedActFunc luci_actfunc(const circle::ActivationFunctionType type);
47 Padding luci_padding(const circle::Padding padding);
48 MirrorPadMode luci_mirrorpad_mode(const circle::MirrorPadMode mode);
49 luci::CircleFullyConnected::WeightsFormat
50 luci_weights_format(const circle::FullyConnectedOptionsWeightsFormat weights_format);
51 std::unique_ptr<CircleQuantParam>
52 luci_quantparam(const circle::QuantizationParametersT *quantization);
54 /// @brief Copy common tensor attributes such as name, type, etc. to node.
55 void copy_tensor_attributes(const circle::TensorT &tensor, CircleNode *node);
58 * @brief Loads Circle file and provides helpers to access attributes
63 using CircleBuffers_t = std::vector<std::unique_ptr<circle::BufferT>>;
64 using CircleTensors_t = std::vector<std::unique_ptr<circle::TensorT>>;
65 using CircleOperators_t = std::vector<std::unique_ptr<circle::OperatorT>>;
66 using CircleOperatorCodes_t = std::vector<std::unique_ptr<circle::OperatorCodeT>>;
68 using CircleSubGraphsPtr_t = flatbuffers::Vector<flatbuffers::Offset<circle::SubGraph>>;
69 using CircleTensorsPtr_t = flatbuffers::Vector<flatbuffers::Offset<circle::Tensor>>;
72 CircleReader() = default;
75 const CircleOperatorCodes_t &opcodes() const { return _model->operator_codes; }
76 const CircleBuffers_t &buffers() const { return _model->buffers; }
77 const CircleTensors_t &tensors() const { return _current_subgraph->tensors; }
78 const CircleOperators_t &operators() const { return _current_subgraph->operators; }
79 const std::vector<int32_t> &inputs() const { return _current_subgraph->inputs; }
80 const std::vector<int32_t> &outputs() const { return _current_subgraph->outputs; }
81 const std::string &name() const { return _current_subgraph->name; }
83 const CircleTensorsPtr_t *tensors_ptr() const { return _tensors_ptr; }
85 uint32_t num_subgraph() const { return _model->subgraphs.size(); }
87 circle::BuiltinOperator builtin_code(const circle::OperatorT &op) const;
88 std::string opcode_name(const circle::OperatorT &op) const;
91 bool parse(const circle::Model *model);
92 bool select_subgraph(uint32_t subgraph);
95 std::unique_ptr<const circle::ModelT> _model;
96 const circle::SubGraphT *_current_subgraph{nullptr};
98 const circle::Model *_model_ptr{nullptr};
99 const CircleTensorsPtr_t *_tensors_ptr{nullptr};
104 #endif // __LUCI_IMPORT_GRAPHREADER_H__