From 6e05e0171a4a2dc559cb5ed42af33ada88a2d97b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=ED=98=84=EC=8B=9D/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 12 Aug 2019 19:59:29 +0900 Subject: [PATCH] [tf2tflite] Loading customop.conf file into ModelSignature (#6406) * [tf2tflite] Loading customop.conf file into ModelSignature Adds code for loading customop.conf into ModelSignature Signed-off-by: Hyun Sik Yoon * 1) class to function 2) remove loco from cmake file 3) remove some headers * fix wrong lambda comment // use argv[5] directly --- compiler/tf2tflite/CMakeLists.txt | 2 + compiler/tf2tflite/requires.cmake | 3 + compiler/tf2tflite/src/CustomopConfLoader.cpp | 137 ++++++++++++++++++++++++++ compiler/tf2tflite/src/CustomopConfLoader.h | 32 ++++++ compiler/tf2tflite/src/Driver.cpp | 5 +- 5 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 compiler/tf2tflite/src/CustomopConfLoader.cpp create mode 100644 compiler/tf2tflite/src/CustomopConfLoader.h diff --git a/compiler/tf2tflite/CMakeLists.txt b/compiler/tf2tflite/CMakeLists.txt index 1658d4a..872c82a 100644 --- a/compiler/tf2tflite/CMakeLists.txt +++ b/compiler/tf2tflite/CMakeLists.txt @@ -39,6 +39,8 @@ target_link_libraries(tf2tflite PRIVATE exo_tflite) target_link_libraries(tf2tflite PRIVATE locop) target_link_libraries(tf2tflite PRIVATE hermes_std) target_link_libraries(tf2tflite PRIVATE stdex) +target_link_libraries(tf2tflite PRIVATE angkor cwrap) +target_link_libraries(tf2tflite PRIVATE tf2tflite_customop_info_proto) nncc_find_resource(TensorFlowTests) diff --git a/compiler/tf2tflite/requires.cmake b/compiler/tf2tflite/requires.cmake index ff9a2cb..ce98653 100644 --- a/compiler/tf2tflite/requires.cmake +++ b/compiler/tf2tflite/requires.cmake @@ -7,3 +7,6 @@ require("locop") require("tfkit") require("nnkit") require("i5diff") +require("loco") +require("cwrap") +require("angkor") diff --git a/compiler/tf2tflite/src/CustomopConfLoader.cpp b/compiler/tf2tflite/src/CustomopConfLoader.cpp new file mode 100644 index 0000000..726b066 --- /dev/null +++ b/compiler/tf2tflite/src/CustomopConfLoader.cpp @@ -0,0 +1,137 @@ +/* + * 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. + */ + +#include "CustomopConfLoader.h" + +#include +#include +#include + +#include + +#include +#include + +#include + +namespace +{ +bool load_text(const cwrap::Fildes &fildes, tf2tflite::CustomOpInfoDef &def) +{ + google::protobuf::io::FileInputStream fis(fildes.get()); + + return google::protobuf::TextFormat::Parse(&fis, &def); +} + +angkor::TensorShape convert_shape(const tf2tflite::ShapeProto &shape) +{ + angkor::TensorShape to_shape; + + int64_t rank64 = shape.dim_size(); + assert(rank64 < std::numeric_limits::max()); + + int32_t rank = static_cast(rank64); + to_shape.resize(rank); + + for (int32_t d = 0; d < rank; d++) + { + int64_t dim_value = shape.dim(d).size(); + assert(dim_value >= 0ULL); + assert(dim_value < std::numeric_limits::max()); + + uint32_t dim_value32 = static_cast(dim_value); + to_shape.dim(d) = dim_value32; + } + + return to_shape; +} + +loco::DataType convert_dtype(const tf2tflite::DataType &dtype) +{ + if (dtype == tf2tflite::DT_FLOAT) + return loco::DataType::FLOAT32; + else if (dtype == tf2tflite::DT_INT32) + return loco::DataType::S32; + else + throw std::runtime_error("Not yet supported datatype. Cannot convert."); +} + +// Note : the following functions look similar with plier::tf::Convert.h. +// However, the schema is different.(not "tensorflow::..." but "tf2tflite::...") +// So, plier::tf cannot be used. +loco::DataType get_dtype_attr(const tf2tflite::CustomOpDef &custom_op) +{ + std::string type_attr_name("dtype"); + + assert(custom_op.attr().count(type_attr_name) > 0); + const auto &attr = custom_op.attr().at(type_attr_name); + assert(attr.value_case() == tf2tflite::AttrValue::kType); + auto dtype_def = attr.type(); + + return convert_dtype(dtype_def); +} + +angkor::TensorShape get_shape_attr(const tf2tflite::CustomOpDef &custom_op) +{ + std::string shape_attr_name("output_shape"); + + assert(custom_op.attr().count(shape_attr_name) > 0); + const auto &attr = custom_op.attr().at(shape_attr_name); + assert(attr.value_case() == tf2tflite::AttrValue::kShape); + auto shape_def = attr.shape(); + + return convert_shape(shape_def); +} + +void add_customop(tf2tflite::CustomOpInfoDef &def, moco::tf::ModelSignature &sig) +{ + for (const auto &custom_op : def.custom_op()) + { + sig.add_customop(custom_op.op()); + + auto name = custom_op.name(); + + // setting dtype and shape to ModelSignature + sig.dtype(name, get_dtype_attr(custom_op)); + sig.shape(name, get_shape_attr(custom_op)); + } +} + +} // namespace + +namespace tf2tflite +{ + +void load_customop_conf(const std::string &path, moco::tf::ModelSignature &sig) +{ + CustomOpInfoDef def; + + cwrap::Fildes fildes{open(path.c_str(), O_RDONLY)}; + + if (fildes.get() < 0) + { + throw std::runtime_error{"Error: " + path + " not found"}; + } + + if (!load_text(fildes, def)) + { + throw std::runtime_error{"Error: Failed to parse prototxt " + path}; + } + + add_customop(def, sig); +} + +} // namespace tf2tflite diff --git a/compiler/tf2tflite/src/CustomopConfLoader.h b/compiler/tf2tflite/src/CustomopConfLoader.h new file mode 100644 index 0000000..da3ab3d --- /dev/null +++ b/compiler/tf2tflite/src/CustomopConfLoader.h @@ -0,0 +1,32 @@ +/* + * 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 __CUSTOMOP_CONF_LOADER_H__ +#define __CUSTOMOP_CONF_LOADER_H__ + +#include + +#include + +namespace tf2tflite +{ + +/// @brief Loads customop.conf into ModelSignature +void load_customop_conf(const std::string &path, moco::tf::ModelSignature &sig); + +} // namespace tf2tflite + +#endif // __CUSTOMOP_CONF_LOADER_H__ diff --git a/compiler/tf2tflite/src/Driver.cpp b/compiler/tf2tflite/src/Driver.cpp index 8174b44..26d01d8 100644 --- a/compiler/tf2tflite/src/Driver.cpp +++ b/compiler/tf2tflite/src/Driver.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "CustomopConfLoader.h" + #include #include @@ -128,8 +130,7 @@ int main(int argc, char **argv) { if (std::string{argv[4]} == "--customop") { - // TODO fill info from customop.conf - throw std::runtime_error("Not yet implemented"); + tf2tflite::load_customop_conf(argv[5], sig); } else { -- 2.7.4