From 89f875b5f18f4b395fb8c4cf10dd8db33b4b2b9a Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=EC=A7=80=EC=98=81/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Wed, 8 May 2019 12:50:30 +0900 Subject: [PATCH] [nnkit] Add Allocator for onnx backend (#3372) * [nnkit] Add Allocator for onnx backend This class is for management onnx tensor memory. Signed-off-by: Jiyoung Yun * Add comments --- .../onnx/include/nnkit/support/onnx/Allocator.h | 56 ++++++++++++++ contrib/nnkit/libs/support/onnx/src/Allocator.cpp | 88 ++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 contrib/nnkit/libs/support/onnx/include/nnkit/support/onnx/Allocator.h create mode 100644 contrib/nnkit/libs/support/onnx/src/Allocator.cpp diff --git a/contrib/nnkit/libs/support/onnx/include/nnkit/support/onnx/Allocator.h b/contrib/nnkit/libs/support/onnx/include/nnkit/support/onnx/Allocator.h new file mode 100644 index 0000000..bf4d735 --- /dev/null +++ b/contrib/nnkit/libs/support/onnx/include/nnkit/support/onnx/Allocator.h @@ -0,0 +1,56 @@ +/* + * 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 __NNKIT_SUPPORT_ONNX_ALLOCATOR_H__ +#define __NNKIT_SUPPORT_ONNX_ALLOCATOR_H__ + +#include + +#include + +namespace nnkit +{ +namespace support +{ +namespace onnx +{ + +class Allocator final : public OrtAllocator +{ +public: + Allocator(void); + ~Allocator(void); + + void *Alloc(size_t size); + void Free(void *p); + const OrtAllocatorInfo *Info(void) const; + + void LeakCheck(void); + + // Disallow copying + Allocator(const Allocator &) = delete; + Allocator &operator=(const Allocator &) = delete; + +private: + std::atomic _memory_inuse{0}; + OrtAllocatorInfo *_cpu_allocator_info; +}; + +} // namespace onnx +} // namespace support +} // namespace nnkit + +#endif // __NNKIT_SUPPORT_ONNX_ALLOCATOR_H__ diff --git a/contrib/nnkit/libs/support/onnx/src/Allocator.cpp b/contrib/nnkit/libs/support/onnx/src/Allocator.cpp new file mode 100644 index 0000000..331f4ec --- /dev/null +++ b/contrib/nnkit/libs/support/onnx/src/Allocator.cpp @@ -0,0 +1,88 @@ +/* + * 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 "nnkit/support/onnx/Allocator.h" +#include "nnkit/support/onnx/Status.h" + +#include + +namespace nnkit +{ +namespace support +{ +namespace onnx +{ + +Allocator::Allocator(void) +{ + OrtAllocator::version = ORT_API_VERSION; + OrtAllocator::Alloc = [](OrtAllocator *this_, size_t size) { + return static_cast(this_)->Alloc(size); + }; + OrtAllocator::Free = [](OrtAllocator *this_, void *p) { + static_cast(this_)->Free(p); + }; + OrtAllocator::Info = [](const OrtAllocator *this_) { + return static_cast(this_)->Info(); + }; + + Status status; + status = OrtCreateCpuAllocatorInfo(OrtDeviceAllocator, OrtMemTypeDefault, &_cpu_allocator_info); + status.throwOnError(); +} + +Allocator::~Allocator(void) { OrtReleaseAllocatorInfo(_cpu_allocator_info); } + +void *Allocator::Alloc(size_t size) +{ + // NOTE The extra_len is added to check resource leak. + // + // This Alloc function will allocate the given size with extra_len. + // The first extra_len will save the allocated memory size and + // the user will use address from the allocated memory plus extra_len. + // The size value that saved in extra_len is used to Free function + // to check resource leak. The size value uses in _memory_inuse. + constexpr size_t extra_len = sizeof(size_t); + _memory_inuse.fetch_add(size += extra_len); + void *p = ::malloc(size); + *(size_t *)p = size; + return (char *)p + extra_len; +} + +void Allocator::Free(void *p) +{ + constexpr size_t extra_len = sizeof(size_t); + if (!p) + return; + p = (char *)p - extra_len; + size_t len = *(size_t *)p; + _memory_inuse.fetch_sub(len); + return ::free(p); +} + +const OrtAllocatorInfo *Allocator::Info(void) const { return _cpu_allocator_info; } + +void Allocator::LeakCheck(void) +{ + if (_memory_inuse.load()) + { + throw std::runtime_error{"memory leak!!!"}; + } +} + +} // namespace onnx +} // namespace support +} // namespace nnkit -- 2.7.4