From 83bfd00c576a064551333361c28f032e46059b5c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Dmitry=20Mozolev/SRR-AI=20Tools=20Lab/=2E/=EC=82=BC?= =?utf8?q?=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Wed, 16 May 2018 11:23:36 +0300 Subject: [PATCH] Add NN importer interface and memory mapper (#168) * Add NN importer interface and memory mapper This commit adds common code needed by NN model importers which is - abstract interface for NN model importers targeting CPU/GPU (possibly also will be ok for DSP/NPU) - code for memory mapped access of NN model files to avoid unnecessary data copying (trained NN models can be large) Signed-off-by: Dmitry Mozolev * Add NN importer interface and memory mapper Change namespace names Signed-off-by: Dmitry Mozolev --- contrib/nnc/CMakeLists.txt | 2 + contrib/nnc/libs/CMakeLists.txt | 1 + contrib/nnc/libs/frontend/CMakeLists.txt | 12 ++++++ .../nnc/libs/frontend/common/model_allocation.cpp | 49 ++++++++++++++++++++++ .../nnc/libs/frontend/include/model_allocation.h | 45 ++++++++++++++++++++ contrib/nnc/libs/frontend/include/nn_importer.h | 28 +++++++++++++ 6 files changed, 137 insertions(+) create mode 100644 contrib/nnc/libs/CMakeLists.txt create mode 100644 contrib/nnc/libs/frontend/CMakeLists.txt create mode 100644 contrib/nnc/libs/frontend/common/model_allocation.cpp create mode 100644 contrib/nnc/libs/frontend/include/model_allocation.h create mode 100644 contrib/nnc/libs/frontend/include/nn_importer.h diff --git a/contrib/nnc/CMakeLists.txt b/contrib/nnc/CMakeLists.txt index ddf14b4..8e79103 100644 --- a/contrib/nnc/CMakeLists.txt +++ b/contrib/nnc/CMakeLists.txt @@ -5,3 +5,5 @@ file(GLOB_RECURSE CL_SRC src/*.cpp include/*.h) add_executable(nnc ${CL_SRC}) target_link_libraries(nnc PRIVATE nncc_core) target_include_directories(nnc PUBLIC include) + +add_subdirectory(libs) diff --git a/contrib/nnc/libs/CMakeLists.txt b/contrib/nnc/libs/CMakeLists.txt new file mode 100644 index 0000000..4dcc379 --- /dev/null +++ b/contrib/nnc/libs/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(frontend) diff --git a/contrib/nnc/libs/frontend/CMakeLists.txt b/contrib/nnc/libs/frontend/CMakeLists.txt new file mode 100644 index 0000000..672e864 --- /dev/null +++ b/contrib/nnc/libs/frontend/CMakeLists.txt @@ -0,0 +1,12 @@ +########################################## +# Common for every importer code library # +########################################## + +file(GLOB common_sources common/*) +file(GLOB common_headers include/*) + +set(nn_import_common nn_import_common) +add_library(${nn_import_common} STATIC ${common_sources} ${common_headers}) + +target_include_directories(${nn_import_common} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) +set_target_properties(${nn_import_common} PROPERTIES POSITION_INDEPENDENT_CODE ON) diff --git a/contrib/nnc/libs/frontend/common/model_allocation.cpp b/contrib/nnc/libs/frontend/common/model_allocation.cpp new file mode 100644 index 0000000..01bd6b5 --- /dev/null +++ b/contrib/nnc/libs/frontend/common/model_allocation.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#include "model_allocation.h" + +using namespace nncc::contrib::frontend::common; + +ModelAllocation::ModelAllocation(std::string filename) +{ + using stat = struct stat; + fd = open(filename.c_str(), O_RDONLY); + + if (fd == -1) + { + return; + } + + stat st{}; + fstat(fd, &st); + + numBytes = st.st_size; + + dataPnt = mmap(nullptr, numBytes, PROT_READ, MAP_SHARED, fd, 0); + + if (dataPnt != MAP_FAILED) + { + mmapState = MAPPED; + } +} + +ModelAllocation::~ModelAllocation() +{ + if (mmapState == MAPPED) + { + munmap(dataPnt, numBytes); + mmapState = UNMAPPED; + } + + if (fd != -1) + { + close(fd); + } +} + +const void *ModelAllocation::getDataPnt() { return mmapState == MAPPED ? dataPnt : nullptr; } + +size_t ModelAllocation::getNumBytes() { return mmapState == MAPPED ? numBytes : 0; } diff --git a/contrib/nnc/libs/frontend/include/model_allocation.h b/contrib/nnc/libs/frontend/include/model_allocation.h new file mode 100644 index 0000000..8c0e5bd --- /dev/null +++ b/contrib/nnc/libs/frontend/include/model_allocation.h @@ -0,0 +1,45 @@ +#ifndef FRONTEND_COMMON_MODEL_ALLOCATION_H_ +#define FRONTEND_COMMON_MODEL_ALLOCATION_H_ + +#include +#include + +namespace nncc +{ +namespace contrib +{ +namespace frontend +{ +namespace common +{ + +// Class that can be used to memory map a file with NN model +class ModelAllocation +{ +public: + explicit ModelAllocation(std::string filename); + virtual ~ModelAllocation(); + + const void *getDataPnt(); + size_t getNumBytes(); + +private: + enum MmapState + { + MAPPED, + UNMAPPED + }; + + MmapState mmapState = UNMAPPED; + void *dataPnt = nullptr; + size_t numBytes = 0; + + int fd = -1; +}; + +} // namespace common +} // namespace frontend +} // namespace contrib +} // namespace nncc + +#endif // FRONTEND_COMMON_MODEL_ALLOCATION_H_ diff --git a/contrib/nnc/libs/frontend/include/nn_importer.h b/contrib/nnc/libs/frontend/include/nn_importer.h new file mode 100644 index 0000000..1855358 --- /dev/null +++ b/contrib/nnc/libs/frontend/include/nn_importer.h @@ -0,0 +1,28 @@ +#ifndef FRONTEND_COMMON_INCLUDE_NN_IMPORTER_ +#define FRONTEND_COMMON_INCLUDE_NN_IMPORTER_ + +namespace nncc +{ +namespace contrib +{ +namespace frontend +{ +namespace common +{ + +class NNImporter +{ +public: + virtual ~NNImporter() = default; + + virtual bool import() = 0; + virtual void *createIR() = 0; + virtual void dump() = 0; +}; + +} // namespace common +} // namespace frontend +} // namespace contrib +} // namespace nncc + +#endif // FRONTEND_COMMON_INCLUDE_NN_IMPORTER_ -- 2.7.4