From: 박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 Date: Thu, 7 Nov 2019 07:33:33 +0000 (+0900) Subject: [tfl-verify] enable tflite file verify (#8829) X-Git-Tag: submit/tizen/20191205.083104~352 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ae0fc6a513e62c79082976c0aac05380ad2f3861;p=platform%2Fcore%2Fml%2Fnnfw.git [tfl-verify] enable tflite file verify (#8829) * [tfl-verify] enable tflite file verify This will enable tflite file verify of tfl-verify tool Signed-off-by: SaeHie Park * fix comment --- diff --git a/compiler/tfl-verify/CMakeLists.txt b/compiler/tfl-verify/CMakeLists.txt index f9fcc5c..a368b29 100644 --- a/compiler/tfl-verify/CMakeLists.txt +++ b/compiler/tfl-verify/CMakeLists.txt @@ -1,7 +1,12 @@ +if(NOT TARGET mio_tflite) + return() +endif(NOT TARGET mio_tflite) + file(GLOB_RECURSE SOURCES "src/*.cpp") add_executable(tfl-verify ${SOURCES}) target_include_directories(tfl-verify PRIVATE src) +target_link_libraries(tfl-verify mio_tflite) target_link_libraries(tfl-verify safemain) target_link_libraries(tfl-verify cwrap) target_link_libraries(tfl-verify stdex) diff --git a/compiler/tfl-verify/requires.cmake b/compiler/tfl-verify/requires.cmake index bd5d02a..e479a83 100644 --- a/compiler/tfl-verify/requires.cmake +++ b/compiler/tfl-verify/requires.cmake @@ -1,3 +1,4 @@ +require("mio-tflite") require("safemain") require("cwrap") require("stdex") diff --git a/compiler/tfl-verify/src/Driver.cpp b/compiler/tfl-verify/src/Driver.cpp index d3562eb..367c731 100644 --- a/compiler/tfl-verify/src/Driver.cpp +++ b/compiler/tfl-verify/src/Driver.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "VerifyFlatBuffers.h" + #include #include @@ -28,12 +30,13 @@ int entry(int argc, char **argv) std::cerr << "USAGE: " << argv[0] << " [tflite]" << std::endl; return 255; } + auto verifier = stdex::make_unique(); std::string model_file = argv[argc - 1]; std::cout << "[ RUN ] Check " << model_file << std::endl; - auto result = -1; + auto result = verifier->run(model_file); if (result == 0) { diff --git a/compiler/tfl-verify/src/Model.cpp b/compiler/tfl-verify/src/Model.cpp new file mode 100644 index 0000000..efac121 --- /dev/null +++ b/compiler/tfl-verify/src/Model.cpp @@ -0,0 +1,90 @@ +/* + * 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 "Model.h" + +#include + +#include +#include +#include +#include + +namespace +{ + +class MemoryMappedModel final : public ModelData +{ +public: + /** + * @require fd and data SHOULD be valid + */ + explicit MemoryMappedModel(int fd, void *data, size_t size) : _fd{fd}, _data{data}, _size{size} + { + // DO NOTHING + } + +public: + ~MemoryMappedModel() + { + munmap(_data, _size); + close(_fd); + } + +public: + MemoryMappedModel(const MemoryMappedModel &) = delete; + MemoryMappedModel(MemoryMappedModel &&) = delete; + +public: + const void *data(void) const override { return _data; }; + const size_t size(void) const override { return _size; }; + +private: + int _fd = -1; + void *_data = nullptr; + size_t _size = 0; +}; + +} // namespace + +std::unique_ptr load_modeldata(const std::string &path) +{ + cwrap::Fildes fd(open(path.c_str(), O_RDONLY)); + + if (fd.get() == -1) + { + // Return nullptr on open failure + return nullptr; + } + + struct stat st; + if (fstat(fd.get(), &st) == -1) + { + // Return nullptr on fstat failure + return nullptr; + } + + auto size = st.st_size; + auto data = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd.get(), 0); + + if (data == MAP_FAILED) + { + // Return nullptr on mmap failure + return nullptr; + } + + return std::unique_ptr{new MemoryMappedModel(fd.release(), data, size)}; +} diff --git a/compiler/tfl-verify/src/Model.h b/compiler/tfl-verify/src/Model.h new file mode 100644 index 0000000..44f40e2 --- /dev/null +++ b/compiler/tfl-verify/src/Model.h @@ -0,0 +1,38 @@ +/* + * 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 __MODEL_H__ +#define __MODEL_H__ + +#include +#include + +struct ModelData +{ + virtual ~ModelData() = default; + + virtual const void *data(void) const = 0; + virtual const size_t size(void) const = 0; +}; + +/** + * @brief Load TF Lite model (as a raw data) from a given path + * + * @note May return a nullptr + */ +std::unique_ptr load_modeldata(const std::string &path); + +#endif // __MODEL_H__ diff --git a/compiler/tfl-verify/src/VerifyFlatBuffers.cpp b/compiler/tfl-verify/src/VerifyFlatBuffers.cpp new file mode 100644 index 0000000..253fcb2 --- /dev/null +++ b/compiler/tfl-verify/src/VerifyFlatBuffers.cpp @@ -0,0 +1,36 @@ +/* + * 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 "VerifyFlatBuffers.h" + +#include "Model.h" + +#include + +int VerifyFlatbuffers::run(const std::string &model_file) +{ + auto modeldata = load_modeldata(model_file); + + const uint8_t *data = reinterpret_cast(modeldata->data()); + flatbuffers::Verifier verifier{data, modeldata->size()}; + + if (!tflite::VerifyModelBuffer(verifier)) + { + return -1; + } + + return 0; +} diff --git a/compiler/tfl-verify/src/VerifyFlatBuffers.h b/compiler/tfl-verify/src/VerifyFlatBuffers.h new file mode 100644 index 0000000..c301b5b --- /dev/null +++ b/compiler/tfl-verify/src/VerifyFlatBuffers.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 __VERIFY_FLATBUFFERS_H__ +#define __VERIFY_FLATBUFFERS_H__ + +#include +#include + +class VerifyFlatbuffers +{ +public: + VerifyFlatbuffers() = default; + +public: + int run(const std::string &model_file); +}; + +#endif // __VERIFY_FLATBUFFERS_H__