[tfl-inspect] Introduce Reader (#8461)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Sun, 27 Oct 2019 21:26:44 +0000 (06:26 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Sun, 27 Oct 2019 21:26:44 +0000 (06:26 +0900)
* [tfl-inspect] Introduce Reader

This will introduce tflite file Reader class and required methods

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
* use tflinspect namespace

* move Model inside src

compiler/tfl-inspect/CMakeLists.txt
compiler/tfl-inspect/requires.cmake
compiler/tfl-inspect/src/Load.cpp [new file with mode: 0644]
compiler/tfl-inspect/src/Model.h [new file with mode: 0644]
compiler/tfl-inspect/src/Reader.cpp [new file with mode: 0644]
compiler/tfl-inspect/src/Reader.h [new file with mode: 0644]

index b4f38e1..c2c7dfe 100644 (file)
@@ -1,4 +1,13 @@
+if(NOT TARGET mio_tflite)
+  return()
+endif(NOT TARGET mio_tflite)
+
 set(DRIVER "driver/Driver.cpp")
 
+file(GLOB_RECURSE SOURCES "src/*.cpp")
+
 add_executable(tfl-inspect ${DRIVER} ${SOURCES})
+target_include_directories(tfl-inspect PRIVATE src)
+target_link_libraries(tfl-inspect mio_tflite)
 target_link_libraries(tfl-inspect safemain)
+target_link_libraries(tfl-inspect stdex)
index 3d1ca09..2aa101e 100644 (file)
@@ -1 +1,3 @@
+require("mio-tflite")
 require("safemain")
+require("stdex")
diff --git a/compiler/tfl-inspect/src/Load.cpp b/compiler/tfl-inspect/src/Load.cpp
new file mode 100644 (file)
index 0000000..1f8c074
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * 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 <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+
+namespace
+{
+
+class MemoryMappedModel final : public tflinspect::Model
+{
+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 ::tflite::Model *model(void) const override { return ::tflite::GetModel(_data); }
+
+private:
+  int _fd = -1;
+  void *_data = nullptr;
+  size_t _size = 0;
+};
+
+class FileDescriptor final
+{
+public:
+  FileDescriptor(int value) : _value{value}
+  {
+    // DO NOTHING
+  }
+
+public:
+  // NOTE Copy is not allowed
+  FileDescriptor(const FileDescriptor &) = delete;
+
+public:
+  // NOTE Move is allowed
+  FileDescriptor(FileDescriptor &&fd) { _value = fd.release(); }
+
+public:
+  ~FileDescriptor()
+  {
+    if (_value != -1)
+    {
+      // Close on descturction
+      close(_value);
+    }
+  }
+
+public:
+  int value(void) const { return _value; }
+
+public:
+  int release(void)
+  {
+    auto res = _value;
+    _value = -1;
+    return res;
+  }
+
+private:
+  int _value = -1;
+};
+
+} // namespace
+
+namespace tflinspect
+{
+
+std::unique_ptr<Model> load_tflite(const std::string &path)
+{
+  FileDescriptor fd = open(path.c_str(), O_RDONLY);
+
+  if (fd.value() == -1)
+  {
+    // Return nullptr on open failure
+    return nullptr;
+  }
+
+  struct stat st;
+  if (fstat(fd.value(), &st) == -1)
+  {
+    // Return nullptr on fstat failure
+    return nullptr;
+  }
+
+  auto size = st.st_size;
+  auto data = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd.value(), 0);
+
+  if (data == MAP_FAILED)
+  {
+    // Return nullptr on mmap failure
+    return nullptr;
+  }
+
+  return std::unique_ptr<tflinspect::Model>{new MemoryMappedModel(fd.release(), data, size)};
+}
+
+} // namespace tflinspect
diff --git a/compiler/tfl-inspect/src/Model.h b/compiler/tfl-inspect/src/Model.h
new file mode 100644 (file)
index 0000000..a69fb8b
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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 <mio/tflite/schema_generated.h>
+
+#include <memory>
+
+namespace tflinspect
+{
+
+struct Model
+{
+  virtual ~Model() = default;
+
+  virtual const ::tflite::Model *model(void) const = 0;
+};
+
+/**
+ * @brief Load TensorFlow Lite model (as a raw Model) from a given path
+ *
+ * @note May return a nullptr
+ */
+std::unique_ptr<Model> load_tflite(const std::string &path);
+
+} // namespace tflinspeat
+
+#endif // __MODEL_H__
diff --git a/compiler/tfl-inspect/src/Reader.cpp b/compiler/tfl-inspect/src/Reader.cpp
new file mode 100644 (file)
index 0000000..a22d359
--- /dev/null
@@ -0,0 +1,160 @@
+/*
+ * 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 "Reader.h"
+
+#include <sstream>
+#include <string>
+
+namespace tflinspect
+{
+
+bool is_valid(const tflite::OperatorCode *opcode)
+{
+  tflite::BuiltinOperator code = opcode->builtin_code();
+  return (tflite::BuiltinOperator_MIN <= code && code <= tflite::BuiltinOperator_MAX);
+}
+
+bool is_custom(const tflite::OperatorCode *opcode)
+{
+  tflite::BuiltinOperator code = opcode->builtin_code();
+  return (code == tflite::BuiltinOperator_CUSTOM);
+}
+
+std::string opcode_name(const tflite::OperatorCode *opcode)
+{
+  assert(opcode);
+
+  if (!is_valid(opcode))
+  {
+    std::ostringstream oss;
+    oss << "(invalid)";
+    return oss.str();
+  }
+
+  if (is_custom(opcode))
+  {
+    if (!opcode->custom_code())
+      return "(invalid custom)";
+
+    return opcode->custom_code()->c_str();
+  }
+
+  tflite::BuiltinOperator code = opcode->builtin_code();
+  return tflite::EnumNameBuiltinOperator(code);
+}
+
+const char *tensor_type(const tflite::Tensor *tensor)
+{
+  return tflite::EnumNameTensorType(tensor->type());
+}
+
+const char *tensor_name(const tflite::Tensor *tensor)
+{
+  static const char *kEmptyTensorName = "(noname)";
+
+  auto name = tensor->name();
+  if (name)
+    return name->c_str();
+
+  return kEmptyTensorName;
+}
+
+Reader::Reader(const tflite::Model *model)
+{
+  _subgraphs = model->subgraphs();
+  _buffers = model->buffers();
+
+  auto opcodes = model->operator_codes();
+  for (const ::tflite::OperatorCode *opcode : *opcodes)
+  {
+    _op_codes.push_back(opcode);
+  }
+}
+
+size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
+{
+  *buff_data = nullptr;
+
+  if (buf_idx == 0)
+    return 0;
+
+  if (auto *buffer = (*_buffers)[buf_idx])
+  {
+    if (auto *array = buffer->data())
+    {
+      if (size_t size = array->size())
+      {
+        *buff_data = reinterpret_cast<const uint8_t *>(array->data());
+        return size;
+      }
+    }
+  }
+
+  return 0;
+}
+
+tflite::BuiltinOperator Reader::builtin_code(const tflite::Operator *op) const
+{
+  uint32_t index = op->opcode_index();
+  assert(index < _op_codes.size());
+  const tflite::OperatorCode *opcode = _op_codes.at(index);
+
+  return opcode->builtin_code();
+}
+
+std::string Reader::opcode_name(const tflite::Operator *op) const
+{
+  uint32_t index = op->opcode_index();
+  assert(index < _op_codes.size());
+  const tflite::OperatorCode *opcode = _op_codes.at(index);
+
+  if (!is_valid(opcode))
+  {
+    std::ostringstream oss;
+    oss << "(invalid: " << index << ")";
+    return oss.str();
+  }
+
+  return tflinspect::opcode_name(opcode);
+}
+
+bool Reader::select_subgraph(uint32_t sgindex)
+{
+  _tensors = nullptr;
+  _operators = nullptr;
+
+  _inputs.clear();
+  _outputs.clear();
+
+  if (_subgraphs->Length() <= sgindex)
+  {
+    assert(false);
+    return false;
+  }
+
+  const tflite::SubGraph *subgraph = (*_subgraphs)[sgindex];
+
+  _tensors = subgraph->tensors();
+  _operators = subgraph->operators();
+
+  _inputs = as_index_vector(subgraph->inputs());
+  _outputs = as_index_vector(subgraph->outputs());
+
+  return true;
+}
+
+} // namespace tflinspect
diff --git a/compiler/tfl-inspect/src/Reader.h b/compiler/tfl-inspect/src/Reader.h
new file mode 100644 (file)
index 0000000..e9e182a
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * 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 __READER_H__
+#define __READER_H__
+
+#include <mio/tflite/schema_generated.h>
+
+#include <map>
+#include <string>
+#include <vector>
+
+namespace tflinspect
+{
+
+template <typename T> std::vector<T> as_index_vector(const flatbuffers::Vector<T> *flat_array)
+{
+  std::vector<T> ret(flat_array->Length());
+  for (uint32_t i = 0; i < flat_array->Length(); i++)
+  {
+    ret[i] = flat_array->Get(i);
+  }
+  return ret;
+}
+
+bool is_valid(const tflite::OperatorCode *opcode);
+bool is_custom(const tflite::OperatorCode *opcode);
+std::string opcode_name(const tflite::OperatorCode *opcode);
+const char *tensor_type(const tflite::Tensor *tensor);
+const char *tensor_name(const tflite::Tensor *tensor);
+
+/**
+ * @brief Loads TF lite file and provides helpers to access attributes
+ */
+class Reader
+{
+private:
+  using TFliteSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<tflite::SubGraph>>;
+  using TFliteBuffers_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Buffer>>;
+  using TFliteTensors_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Tensor>>;
+  using TFliteOperators_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Operator>>;
+
+public:
+  Reader(const tflite::Model *model);
+
+  Reader() = delete;
+
+public:
+  const std::vector<const tflite::OperatorCode *> &opcodes() { return _op_codes; }
+  const TFliteBuffers_t *buffers() { return _buffers; }
+  const TFliteTensors_t *tensors() { return _tensors; }
+  const TFliteOperators_t *operators() { return _operators; }
+  const std::vector<int32_t> &inputs() const { return _inputs; }
+  const std::vector<int32_t> &outputs() const { return _outputs; }
+
+  uint32_t num_subgraph() const { return _subgraphs->Length(); }
+
+  size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data);
+  tflite::BuiltinOperator builtin_code(const tflite::Operator *op) const;
+  std::string opcode_name(const tflite::Operator *op) const;
+
+public:
+  bool select_subgraph(uint32_t subgraph);
+
+private:
+  const TFliteSubGraphs_t *_subgraphs{nullptr};
+  const TFliteBuffers_t *_buffers{nullptr};
+  const TFliteTensors_t *_tensors{nullptr};
+  const TFliteOperators_t *_operators{nullptr};
+
+  std::vector<const tflite::OperatorCode *> _op_codes;
+  std::vector<int32_t> _inputs;
+  std::vector<int32_t> _outputs;
+};
+
+} // namespace tflinspect
+
+#endif // __READER_H__