From 6ff8547402ffbf5b53a66d1f14907b104a9a1c4d Mon Sep 17 00:00:00 2001 From: Jiho Chu Date: Mon, 17 Oct 2022 11:18:17 +0900 Subject: [PATCH] [Tensor] Add constructor for user swap path Swap file path could be changed by model property. Signed-off-by: Jiho Chu --- nntrainer/graph/network_graph.h | 6 ++++-- nntrainer/tensor/cache_pool.cpp | 20 ++++++++++++++------ nntrainer/tensor/cache_pool.h | 20 +++++++++++++++----- nntrainer/tensor/manager.h | 6 +++--- nntrainer/tensor/swap_device.cpp | 9 +++------ nntrainer/tensor/swap_device.h | 16 ++++++++++++---- nntrainer/tensor/tensor_pool.cpp | 6 +++--- nntrainer/tensor/tensor_pool.h | 7 ++++--- 8 files changed, 58 insertions(+), 32 deletions(-) diff --git a/nntrainer/graph/network_graph.h b/nntrainer/graph/network_graph.h index 1272c1f..42c5972 100644 --- a/nntrainer/graph/network_graph.h +++ b/nntrainer/graph/network_graph.h @@ -52,9 +52,11 @@ public: /** * @brief Constructor of NeuralNetwork Graph Class + * @param[in] enable_swap enable memory swap for tensor + * @param[in] swap_path memory swap file path when the swap is enabled */ - NetworkGraph(bool enable_swap) : - tensor_manager(std::make_shared(enable_swap)), + NetworkGraph(bool enable_swap, const std::string &swap_path = "") : + tensor_manager(std::make_shared(enable_swap, swap_path)), graph(), compiled(false), batch_size(0), diff --git a/nntrainer/tensor/cache_pool.cpp b/nntrainer/tensor/cache_pool.cpp index 74165b3..0757da7 100644 --- a/nntrainer/tensor/cache_pool.cpp +++ b/nntrainer/tensor/cache_pool.cpp @@ -45,7 +45,14 @@ void CacheElem::swapOut() { } CachePool::CachePool(const std::string &name) : - swap_device(std::make_shared(name + std::to_string(getpid()))) { + swap_device(std::make_shared(name + std::to_string(getpid()))) {} + +CachePool::CachePool(const std::string &path, const std::string &name) { + if (path.empty()) + swap_device = std::make_shared(name + std::to_string(getpid())); + else + swap_device = + std::make_shared(path, name + std::to_string(getpid())); } CachePool::~CachePool() { deallocate(); } @@ -93,11 +100,11 @@ std::shared_ptr> CachePool::getMemory(unsigned int id) { int offset = getMemoryOffset().at(id - 1); size_t len = getMemorySize().at(id - 1); auto exe_order = getMemoryExecOrder().at(id - 1); - auto mem_data = std::make_shared>(id, - std::bind(&CachePool::validate, this, std::placeholders::_1), + auto mem_data = std::make_shared>( + id, std::bind(&CachePool::validate, this, std::placeholders::_1), std::bind(&CachePool::invalidate, this, std::placeholders::_1)); - auto elem = - std::make_shared(swap_device, id, offset, len, mem_data, exe_order); + auto elem = std::make_shared(swap_device, id, offset, len, + mem_data, exe_order); elems[id] = elem; @@ -106,7 +113,8 @@ std::shared_ptr> CachePool::getMemory(unsigned int id) { ords.append(std::to_string(o)); ords.append(" "); } - ml_logd("[%d] exe_order(%s), offset: 0x%x, len: %zu", id, ords.c_str(), offset, len); + ml_logd("[%d] exe_order(%s), offset: 0x%x, len: %zu", id, ords.c_str(), + offset, len); return mem_data; } diff --git a/nntrainer/tensor/cache_pool.h b/nntrainer/tensor/cache_pool.h index 709748c..7f0ca86 100644 --- a/nntrainer/tensor/cache_pool.h +++ b/nntrainer/tensor/cache_pool.h @@ -14,10 +14,10 @@ #ifndef __CACHE_POOL_H__ #define __CACHE_POOL_H__ +#include #include + #include -#include -#include #include namespace nntrainer { @@ -83,18 +83,22 @@ public: */ size_t getLength() const { return length; } - + /** + * @brief get id of cache element + * + * @return cache element id + */ unsigned int getId() const { return id; } private: - std::mutex device_mutex; /**< protect device */ + std::mutex device_mutex; /**< protect device */ std::shared_ptr device; /**< swap device */ bool active; /**< element is loaded */ unsigned int id; /**< memory id */ int offset; /**< element offset from swap device */ size_t length; /**< element size */ std::shared_ptr> mem_data; /**< allocated memory data */ - std::vector exe_order; /**< execution order */ + std::vector exe_order; /**< execution order */ }; /** @@ -110,6 +114,12 @@ public: explicit CachePool(const std::string &name); /** + * @brief CachePool constructor with cache path + * + */ + explicit CachePool(const std::string &path, const std::string &name); + + /** * @brief MemoryPool destructor * */ diff --git a/nntrainer/tensor/manager.h b/nntrainer/tensor/manager.h index 2617d83..5e7b4f0 100644 --- a/nntrainer/tensor/manager.h +++ b/nntrainer/tensor/manager.h @@ -135,9 +135,9 @@ public: /** * @brief Constructor of Manager */ - Manager(bool enable_swap) : - weight_pool(enable_swap, "weight_pool"), - tensor_pool(enable_swap, "tensor_pool"), + Manager(bool enable_swap, const std::string &swap_path = "") : + weight_pool(enable_swap, swap_path, "weight_pool"), + tensor_pool(enable_swap, swap_path, "tensor_pool"), enable_optimizations(true) {} /** diff --git a/nntrainer/tensor/swap_device.cpp b/nntrainer/tensor/swap_device.cpp index b1b8cc2..3a2a6d2 100644 --- a/nntrainer/tensor/swap_device.cpp +++ b/nntrainer/tensor/swap_device.cpp @@ -45,8 +45,7 @@ void SwapDevice::start(size_t size) { } void *SwapDevice::getBuffer(int offset, size_t size) { - NNTR_THROW_IF(fd <= 0, std::runtime_error) - << "SwapDevice is not started"; + NNTR_THROW_IF(fd <= 0, std::runtime_error) << "SwapDevice is not started"; #ifdef USE_MMAP // page aligned @@ -69,8 +68,7 @@ void *SwapDevice::getBuffer(int offset, size_t size) { void *ptr; ptr = calloc(1, size); - NNTR_THROW_IF(ptr == NULL, std::runtime_error) - << "memory alloc failed"; + NNTR_THROW_IF(ptr == NULL, std::runtime_error) << "memory alloc failed"; ret = lseek(fd, offset, SEEK_SET); NNTR_THROW_IF(ret < 0, std::runtime_error) << "seek file: " << dev_path; @@ -88,8 +86,7 @@ void *SwapDevice::getBuffer(int offset, size_t size) { void SwapDevice::putBuffer(void *ptr) { int ret; - NNTR_THROW_IF(fd <= 0, std::runtime_error) - << "SwapDevice is not started"; + NNTR_THROW_IF(fd <= 0, std::runtime_error) << "SwapDevice is not started"; #ifdef USE_MMAP NNTR_THROW_IF(mapped.find(ptr) == mapped.end(), std::runtime_error) << "Couldn't find buffer"; diff --git a/nntrainer/tensor/swap_device.h b/nntrainer/tensor/swap_device.h index fa42e51..5cecd9f 100644 --- a/nntrainer/tensor/swap_device.h +++ b/nntrainer/tensor/swap_device.h @@ -25,7 +25,6 @@ #include #include - /* Uncomment this to use mmap for swap data */ //#define USE_MMAP @@ -41,14 +40,23 @@ public: * @brief swap device default path * */ - const std::string swap_device_path = "/tmp/"; + const std::string swap_device_default_path = "."; + + /** + * @brief SwapDevice default constructor + * + */ + explicit SwapDevice(const std::string &name) : + dev_path(swap_device_default_path + name), + fd(-1) {} /** * @brief SwapDevice default constructor * */ - explicit SwapDevice(const std::string &name) - : dev_path(swap_device_path + name), fd(-1) {} + explicit SwapDevice(const std::string &path, const std::string &name) : + dev_path(path + "/" + name), + fd(-1) {} /** * @brief SwapDevice destructor diff --git a/nntrainer/tensor/tensor_pool.cpp b/nntrainer/tensor/tensor_pool.cpp index bb76a13..70ce920 100644 --- a/nntrainer/tensor/tensor_pool.cpp +++ b/nntrainer/tensor/tensor_pool.cpp @@ -154,9 +154,9 @@ void TensorPool::finalize(const MemoryPlanner &planner, * 3. requestMemory for all the tensors and set their tokens * @note +1 is to make the validity_end exlusive in the interval range */ - details->token = mem_pool->requestMemory(spec.tensor->bytes(), - validity_start, validity_end + 1, - details->exec_order); + details->token = + mem_pool->requestMemory(spec.tensor->bytes(), validity_start, + validity_end + 1, details->exec_order); #ifdef DEBUG if (details->token == 0) throw std::runtime_error("Received invalid token from memory pool"); diff --git a/nntrainer/tensor/tensor_pool.h b/nntrainer/tensor/tensor_pool.h index d21aa3e..a4264a6 100644 --- a/nntrainer/tensor/tensor_pool.h +++ b/nntrainer/tensor/tensor_pool.h @@ -46,9 +46,10 @@ public: /** * @brief Constructor of TensorPool */ - TensorPool(bool enable_swap, const std::string &name = std::string()) { + TensorPool(bool enable_swap, const std::string &swap_path = "", + const std::string &swap_name = "") { if (enable_swap) - mem_pool = std::make_unique(name); + mem_pool = std::make_unique(swap_path, swap_name); else mem_pool = std::make_unique(); } @@ -351,7 +352,7 @@ private: */ std::vector pool; /**< list of requested tensors */ std::unordered_map - name_map; /**< indexing of requested tensors */ + name_map; /**< indexing of requested tensors */ std::unique_ptr mem_pool; /**< memory pool for the tensors */ /** -- 2.7.4