[tfl-verify] enable tflite file verify (#8829)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Thu, 7 Nov 2019 07:33:33 +0000 (16:33 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 7 Nov 2019 07:33:33 +0000 (16:33 +0900)
* [tfl-verify] enable tflite file verify

This will enable tflite file verify of tfl-verify tool

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
* fix comment

compiler/tfl-verify/CMakeLists.txt
compiler/tfl-verify/requires.cmake
compiler/tfl-verify/src/Driver.cpp
compiler/tfl-verify/src/Model.cpp [new file with mode: 0644]
compiler/tfl-verify/src/Model.h [new file with mode: 0644]
compiler/tfl-verify/src/VerifyFlatBuffers.cpp [new file with mode: 0644]
compiler/tfl-verify/src/VerifyFlatBuffers.h [new file with mode: 0644]

index f9fcc5c..a368b29 100644 (file)
@@ -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)
index bd5d02a..e479a83 100644 (file)
@@ -1,3 +1,4 @@
+require("mio-tflite")
 require("safemain")
 require("cwrap")
 require("stdex")
index d3562eb..367c731 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include "VerifyFlatBuffers.h"
+
 #include <stdex/Memory.h>
 
 #include <iostream>
@@ -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<VerifyFlatbuffers>();
 
   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 (file)
index 0000000..efac121
--- /dev/null
@@ -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 <cwrap/Fildes.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+
+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<ModelData> 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<ModelData>{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 (file)
index 0000000..44f40e2
--- /dev/null
@@ -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 <memory>
+#include <string>
+
+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<ModelData> 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 (file)
index 0000000..253fcb2
--- /dev/null
@@ -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 <mio/tflite/schema_generated.h>
+
+int VerifyFlatbuffers::run(const std::string &model_file)
+{
+  auto modeldata = load_modeldata(model_file);
+
+  const uint8_t *data = reinterpret_cast<const uint8_t *>(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 (file)
index 0000000..c301b5b
--- /dev/null
@@ -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 <ostream>
+#include <string>
+
+class VerifyFlatbuffers
+{
+public:
+  VerifyFlatbuffers() = default;
+
+public:
+  int run(const std::string &model_file);
+};
+
+#endif // __VERIFY_FLATBUFFERS_H__