From a38bbbac7e352cdba07c9e6528da4585088efc23 Mon Sep 17 00:00:00 2001 From: JiHwan Yeo Date: Thu, 13 Aug 2020 17:13:09 +0900 Subject: [PATCH] [onert] Use CachedData instead of MMapedData (1.8.0) (#3817) - MMapedData reduces load/prepare time, but increases inference time - Use CachedData for weight tensor for inference performance - Includes Revert "[onert] Remove unused function for munmap (#3710)" ONE-DCO-1.0-Signed-off-by: JiHwan Yeo --- .../base_loader/include/base_loader.h | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/runtime/onert/frontend/base_loader/include/base_loader.h b/runtime/onert/frontend/base_loader/include/base_loader.h index 295cb45d9f9..0f6a2a5d027 100644 --- a/runtime/onert/frontend/base_loader/include/base_loader.h +++ b/runtime/onert/frontend/base_loader/include/base_loader.h @@ -93,6 +93,7 @@ template class BaseLoader ir::Activation convertActivation(ActivationFunctionType type); ir::DataType tensorTypeToDataType(TensorType type); ir::OperandIndex tensorIdxToOperandIdx(int32_t tensorIdx); + void deallocateMmappedArea(uint8_t *ptr, size_t size); // Create operands form tflite::Tensor ir::OperandIndex loadOperand(const Tensor *tensor, ir::Graph &subg); @@ -225,7 +226,6 @@ void BaseLoader::BaseLoader::loadFromFile(const ch _verifier = std::make_unique(reinterpret_cast(_base), size); loadModel(); - munmap(_base, size); close(_fd); } @@ -292,6 +292,31 @@ BaseLoader::BaseLoader::tensorIdxToOperandIdx(int3 return isOptionalInputTensor(tensorIdx) ? ir::OperandIndex() : _tensor_to_operand[tensorIdx]; } +template +void BaseLoader::BaseLoader::deallocateMmappedArea(uint8_t *ptr, + size_t size) +{ + // Calculate offset from base address of mapped region + ptrdiff_t unaligned_offset_start = ptr - _base; + ptrdiff_t unaligned_offset_end = unaligned_offset_start + size; + + // Calculated aligned offset from base address of mapped region + // munmap accepts memory address which is a multiple of the pagesize + ptrdiff_t aligned_offset_start = + ((unaligned_offset_start + (_pagesize - 1)) / _pagesize) * _pagesize; + ptrdiff_t aligned_offset_end = (unaligned_offset_end / _pagesize) * _pagesize; + + ptrdiff_t area_size = aligned_offset_end - aligned_offset_start; + if (area_size > 0) + { + // Unmap mapped region for CachedData + if (munmap(_base + aligned_offset_start, area_size) == -1) + { + VERBOSE(BASE_LOADER) << "munmap failed" << std::endl; + } + } +} + /* Copied from tensorflow lite. Need to append copyright */ template bool Copy(const T *data_ptr, std::vector &arr) { @@ -435,17 +460,8 @@ ir::OperandIndex BaseLoader::loadOperand(const Ten } else // Model is loaded(mmap'd) from a file { - size_t data_size = data->size(); - ptrdiff_t unaligned_offset_start = data->data() - _base; - ptrdiff_t offset_end = unaligned_offset_start + data_size; - - // Calculated aligned offset from base address of mapped region - // munmap accepts memory address which is a multiple of the pagesize - ptrdiff_t aligned_offset_start = (unaligned_offset_start / _pagesize) * _pagesize; - size_t mmap_size = offset_end - aligned_offset_start; - - data_obj = std::make_unique(_fd, aligned_offset_start, mmap_size, - unaligned_offset_start, data_size); + data_obj = std::make_unique(data->data(), data->size()); + deallocateMmappedArea(const_cast(data->data()), data->size()); } subg.setOperandValue(operand_index, std::move(data_obj)); }