[circle-inspect] Introduce Load Reader and Model (#8525)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 28 Oct 2019 07:38:40 +0000 (16:38 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 28 Oct 2019 07:38:40 +0000 (16:38 +0900)
* [circle-inspect] Introduce Load Reader and Model

This will introduce circle model Load and Reader using mio-circle

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

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

index 70f9407..f4a926b 100644 (file)
@@ -1,4 +1,12 @@
+if(NOT TARGET mio_circle)
+  return()
+endif(NOT TARGET mio_circle)
+
 set(DRIVER "driver/Driver.cpp")
 
-add_executable(circle-inspect ${DRIVER})
+file(GLOB_RECURSE SOURCES "src/*.cpp")
+
+add_executable(circle-inspect ${DRIVER} ${SOURCES})
+target_include_directories(circle-inspect PRIVATE src)
+target_link_libraries(circle-inspect mio_circle)
 target_link_libraries(circle-inspect safemain)
diff --git a/compiler/circle-inspect/src/Load.cpp b/compiler/circle-inspect/src/Load.cpp
new file mode 100644 (file)
index 0000000..4b729a5
--- /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 circleinspect::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 ::circle::Model *model(void) const override { return ::circle::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 circleinspect
+{
+
+std::unique_ptr<Model> load_circle(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<circleinspect::Model>{new MemoryMappedModel(fd.release(), data, size)};
+}
+
+} // namespace circleinspect
diff --git a/compiler/circle-inspect/src/Model.h b/compiler/circle-inspect/src/Model.h
new file mode 100644 (file)
index 0000000..8206ed3
--- /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/circle/schema_generated.h>
+
+#include <memory>
+
+namespace circleinspect
+{
+
+struct Model
+{
+  virtual ~Model() = default;
+
+  virtual const ::circle::Model *model(void) const = 0;
+};
+
+/**
+ * @brief Load Circle model (as a raw Model) from a given path
+ *
+ * @note May return a nullptr
+ */
+std::unique_ptr<Model> load_circle(const std::string &path);
+
+} // namespace circleinspect
+
+#endif // __MODEL_H__
diff --git a/compiler/circle-inspect/src/Reader.cpp b/compiler/circle-inspect/src/Reader.cpp
new file mode 100644 (file)
index 0000000..ed090b4
--- /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 circleinspect
+{
+
+bool is_valid(const circle::OperatorCode *opcode)
+{
+  circle::BuiltinOperator code = opcode->builtin_code();
+  return (circle::BuiltinOperator_MIN <= code && code <= circle::BuiltinOperator_MAX);
+}
+
+bool is_custom(const circle::OperatorCode *opcode)
+{
+  circle::BuiltinOperator code = opcode->builtin_code();
+  return (code == circle::BuiltinOperator_CUSTOM);
+}
+
+std::string opcode_name(const circle::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();
+  }
+
+  circle::BuiltinOperator code = opcode->builtin_code();
+  return circle::EnumNameBuiltinOperator(code);
+}
+
+const char *tensor_type(const circle::Tensor *tensor)
+{
+  return circle::EnumNameTensorType(tensor->type());
+}
+
+const char *tensor_name(const circle::Tensor *tensor)
+{
+  static const char *kEmptyTensorName = "(noname)";
+
+  auto name = tensor->name();
+  if (name)
+    return name->c_str();
+
+  return kEmptyTensorName;
+}
+
+Reader::Reader(const circle::Model *model)
+{
+  _subgraphs = model->subgraphs();
+  _buffers = model->buffers();
+
+  auto opcodes = model->operator_codes();
+  for (const ::circle::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;
+}
+
+circle::BuiltinOperator Reader::builtin_code(const circle::Operator *op) const
+{
+  uint32_t index = op->opcode_index();
+  assert(index < _op_codes.size());
+  const circle::OperatorCode *opcode = _op_codes.at(index);
+
+  return opcode->builtin_code();
+}
+
+std::string Reader::opcode_name(const circle::Operator *op) const
+{
+  uint32_t index = op->opcode_index();
+  assert(index < _op_codes.size());
+  const circle::OperatorCode *opcode = _op_codes.at(index);
+
+  if (!is_valid(opcode))
+  {
+    std::ostringstream oss;
+    oss << "(invalid: " << index << ")";
+    return oss.str();
+  }
+
+  return circleinspect::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 circle::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 circleinspect
diff --git a/compiler/circle-inspect/src/Reader.h b/compiler/circle-inspect/src/Reader.h
new file mode 100644 (file)
index 0000000..b5a99df
--- /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/circle/schema_generated.h>
+
+#include <map>
+#include <string>
+#include <vector>
+
+namespace circleinspect
+{
+
+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 circle::OperatorCode *opcode);
+bool is_custom(const circle::OperatorCode *opcode);
+std::string opcode_name(const circle::OperatorCode *opcode);
+const char *tensor_type(const circle::Tensor *tensor);
+const char *tensor_name(const circle::Tensor *tensor);
+
+/**
+ * @brief Loads Circle file and provides helpers to access attributes
+ */
+class Reader
+{
+private:
+  using CircleSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<circle::SubGraph>>;
+  using CircleBuffers_t = flatbuffers::Vector<flatbuffers::Offset<circle::Buffer>>;
+  using CircleTensors_t = flatbuffers::Vector<flatbuffers::Offset<circle::Tensor>>;
+  using CircleOperators_t = flatbuffers::Vector<flatbuffers::Offset<circle::Operator>>;
+
+public:
+  Reader(const circle::Model *model);
+
+  Reader() = delete;
+
+public:
+  const std::vector<const circle::OperatorCode *> &opcodes() { return _op_codes; }
+  const CircleBuffers_t *buffers() { return _buffers; }
+  const CircleTensors_t *tensors() { return _tensors; }
+  const CircleOperators_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);
+  circle::BuiltinOperator builtin_code(const circle::Operator *op) const;
+  std::string opcode_name(const circle::Operator *op) const;
+
+public:
+  bool select_subgraph(uint32_t subgraph);
+
+private:
+  const CircleSubGraphs_t *_subgraphs{nullptr};
+  const CircleBuffers_t *_buffers{nullptr};
+  const CircleTensors_t *_tensors{nullptr};
+  const CircleOperators_t *_operators{nullptr};
+
+  std::vector<const circle::OperatorCode *> _op_codes;
+  std::vector<int32_t> _inputs;
+  std::vector<int32_t> _outputs;
+};
+
+} // namespace circleinspect
+
+#endif // __READER_H__