From e2ef8438a24f7c56a0744eb579a6e293ee2fbf8e Mon Sep 17 00:00:00 2001 From: Chunseok Lee Date: Thu, 23 Apr 2020 14:45:49 +0900 Subject: Imported Upstream version 1.4.0 --- compiler/exo/src/TFLite/TFLExporterUtils.h | 118 +++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 compiler/exo/src/TFLite/TFLExporterUtils.h (limited to 'compiler/exo/src/TFLite/TFLExporterUtils.h') diff --git a/compiler/exo/src/TFLite/TFLExporterUtils.h b/compiler/exo/src/TFLite/TFLExporterUtils.h new file mode 100644 index 000000000..dbd7a52fb --- /dev/null +++ b/compiler/exo/src/TFLite/TFLExporterUtils.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __TFL_EXPORTER_UTILS_H__ +#define __TFL_EXPORTER_UTILS_H__ + +#include "ExporterUtils.h" + +#include "schema_generated.h" + +#include "Dialect/IR/TFLNodes.h" + +#include + +#include + +namespace exo +{ +namespace tflite_detail +{ + +struct OpCode +{ + tflite::BuiltinOperator opcode; + + bool operator==(const OpCode &rhs) const { return opcode == rhs.opcode; } +}; + +} // namespace tflite_detail +} // namespace exo + +namespace exo +{ + +tflite::ActivationFunctionType to_tflite_actfunc(locoex::FusedActFunc func); + +} // namespace exo + +namespace std +{ + +template <> struct hash +{ + size_t operator()(const exo::tflite_detail::OpCode &x) const { return hash()(x.opcode); } +}; + +} // namespace std + +namespace exo +{ +namespace tflite_detail +{ + +/** + * @breif Record the information of T/F Lite SubGraph and its mapping to loco + */ +struct SubGraphContext +{ + /// @brief SubGraph input tensor id + std::vector _inputs; + /// @brief SubGraph output tensor id + std::vector _outputs; +}; + +// Prerequisites for tflite::Model object creation +struct SerializedModelData final : public SubGraphContext +{ + SerializedModelData() = default; + SerializedModelData(const SerializedModelData &) = delete; + + std::unordered_map _operator_codes; + std::unordered_map _custom_operator_codes; + std::vector> _operators; + std::vector> _tensors; + std::vector> _buffers; + + // Graph input and output names + std::unordered_map _pull_to_name; + std::unordered_map _push_to_name; + + /** + * @brief if opcode is not registered in table of opcodes add it + * @param builtin_code + * @return idx of opcode in table of opcodes (see schema) + */ + uint32_t registerBuiltinOpcode(tflite::BuiltinOperator builtin_code); + uint32_t registerCustomOpcode(const std::string &custom_op); +}; + +tflite::Padding getOpPadding(const loco::Padding2D *pad, const loco::Stride<2> *stride, + const ShapeDescription &ifm, const ShapeDescription &ofm); +tflite::Padding getOpPadding(const locoex::Padding pad); + +/// @brief Register graph input and output names to SerializedModelData +void registerGraphIOName(loco::Graph *graph, SerializedModelData &gd); + +using TFLTensorIndex = int32_t; + +void set_tensor_index(loco::Node *node, const TFLTensorIndex &tensor_id); +TFLTensorIndex get_tensor_index(loco::Node *node); + +} // namespace tflite_detail +} // namespace exo + +#endif // __TFL_EXPORTER_UTILS_H__ -- cgit v1.2.3