Skip to content

Commit 4457cf6

Browse files
neuropilot-captainneuropilot-captainMediaTek-aicccclai
authored
Neuron buffer allocator decouple from ExecuTorch framework (#8760)
### Summary 1. Decouple out Neuron buffer allocator as a standalone shared library 2. Refactor and update the build scripts 3. Pending the release of the new buffer allocator lib Fixes #6074 Fixes #7549 Fixes #10389 cc @cccclai @cbilgin --------- Co-authored-by: neuropilot-captain <[email protected]> Co-authored-by: Poyuan Jeng <[email protected]> Co-authored-by: cccclai <[email protected]>
1 parent 762333d commit 4457cf6

File tree

7 files changed

+131
-43
lines changed

7 files changed

+131
-43
lines changed

backends/mediatek/CMakeLists.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010

1111
# Let include directory as "executorch/..."
1212
set(_common_include_directories ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
13-
set(NEURON_BUFFER_ALLOCATOR_LIB
14-
""
15-
CACHE PATH "Path to Neuron Buffer Allocator library"
16-
)
17-
message(
18-
STATUS "Looking for neuron_buffer_allocator in ${NEURON_BUFFER_ALLOCATOR_LIB}"
19-
)
13+
14+
if(EXECUTORCH_BUILD_NEURON_BUFFER_ALLOCATOR)
15+
message(STATUS "Neuron buffer allocator Build is enabled")
16+
add_subdirectory(runtime/proprietary)
17+
endif()
2018

2119
include_directories(BEFORE ${_common_include_directories})
2220

@@ -31,7 +29,6 @@ target_link_libraries(neuron_backend
3129
executorch_core
3230
android
3331
log
34-
${NEURON_BUFFER_ALLOCATOR_LIB}
3532
)
3633
target_sources(
3734
neuron_backend
@@ -43,6 +40,7 @@ target_sources(
4340
${CMAKE_CURRENT_LIST_DIR}/runtime/include/api/NeuronAdapterShim.h
4441
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/runtime/NeuronBackend.cpp
4542
${CMAKE_CURRENT_LIST_DIR}/runtime/NeuronExecutor.cpp
43+
${CMAKE_CURRENT_LIST_DIR}/runtime/NeuronBufferAllocator.cpp
4644
)
4745
target_link_options_shared_lib(neuron_backend)
4846

backends/mediatek/runtime/NeuronBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ Error NeuronExecuTorchDelegate::execute(
111111
return Error::InvalidState;
112112
};
113113

114-
auto allocator = dynamic_cast<torch::executor::neuron::BufferAllocator*>(
115-
context.get_temp_allocator());
114+
auto allocator =
115+
dynamic_cast<neuron::BufferAllocator*>(context.get_temp_allocator());
116116
size_t inputCount = mInputSizes.size(), outputCount = mOutputSizes.size();
117117

118118
for (int i = 0; i < inputCount; i++) {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2025 MediaTek Inc.
3+
*
4+
* Licensed under the BSD License (the "License"); you may not use this file
5+
* except in compliance with the License. See the license file in the root
6+
* directory of this source tree for more details.
7+
*/
8+
9+
#include "include/NeuronBufferAllocator.h"
10+
11+
#include <algorithm>
12+
#include <cstddef>
13+
#include <mutex>
14+
15+
namespace executorch {
16+
namespace backends {
17+
namespace neuron {
18+
19+
bool loadLibrary() {
20+
handle = dlopen("libneuron_buffer_allocator.so", RTLD_LAZY);
21+
if (!handle) {
22+
std::cerr << "Failed to load library: " << dlerror() << std::endl;
23+
return false;
24+
}
25+
26+
create_func = (CreateFunc)dlsym(handle, "neuron_buffer_allocator_create");
27+
allocate_func = (AllocateFunc)dlsym(handle, "neuron_buffer_allocate");
28+
remove_func = (RemoveFunc)dlsym(handle, "neuron_buffer_remove");
29+
find_func = (FindFunc)dlsym(handle, "neuron_buffer_find");
30+
clear_func = (ClearFunc)dlsym(handle, "neuron_buffer_clear");
31+
32+
if (!create_func || !allocate_func || !remove_func || !find_func ||
33+
!clear_func) {
34+
std::cerr << "Failed to retrieve symbols: " << dlerror() << std::endl;
35+
dlclose(handle);
36+
handle = nullptr;
37+
return false;
38+
}
39+
40+
return true;
41+
}
42+
43+
void unloadLibrary() {
44+
if (handle) {
45+
dlclose(handle);
46+
handle = nullptr;
47+
}
48+
}
49+
50+
BufferAllocator& BufferAllocator::GetInstance() {
51+
static BufferAllocator instance;
52+
53+
if (handle == nullptr) {
54+
loadLibrary();
55+
if (allocatorHandle == nullptr) {
56+
allocatorHandle = create_func();
57+
}
58+
}
59+
60+
return instance;
61+
};
62+
63+
void* BufferAllocator::Allocate(size_t size) {
64+
std::scoped_lock Guard(mMutex);
65+
return allocate_func(allocatorHandle, size);
66+
}
67+
68+
bool BufferAllocator::RemoveBuffer(void* address) {
69+
std::scoped_lock Guard(mMutex);
70+
return remove_func(allocatorHandle, address);
71+
}
72+
73+
const MemoryUnit* BufferAllocator::Find(void* address) {
74+
std::scoped_lock Guard(mMutex);
75+
return static_cast<const MemoryUnit*>(find_func(allocatorHandle, address));
76+
}
77+
78+
void BufferAllocator::Clear() {
79+
std::scoped_lock Guard(mMutex);
80+
clear_func(allocatorHandle);
81+
}
82+
83+
} // namespace neuron
84+
} // namespace backends
85+
} // namespace executorch
86+
87+
namespace {
88+
static auto& singletonInstance = GET_NEURON_ALLOCATOR;
89+
} // namespace

backends/mediatek/runtime/include/NeuronBufferAllocator.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 MediaTek Inc.
2+
* Copyright (c) 2025 MediaTek Inc.
33
*
44
* Licensed under the BSD License (the "License"); you may not use this file
55
* except in compliance with the License. See the license file in the root
@@ -16,18 +16,33 @@
1616

1717
#include <executorch/runtime/core/memory_allocator.h>
1818

19+
#include <dlfcn.h>
20+
#include <iostream>
1921
#include <map>
2022
#include <memory>
2123
#include <mutex>
2224
#include <new>
2325

2426
#define GET_NEURON_ALLOCATOR \
25-
::torch::executor::neuron::BufferAllocator::GetInstance()
26-
27-
// TODO: Move this code to the executorch::backends::neuron namespace.
28-
// The torch:: namespace is deprecated for ExecuTorch code.
29-
namespace torch {
30-
namespace executor {
27+
::executorch::backends::neuron::BufferAllocator::GetInstance()
28+
29+
// Define the types for the function pointers
30+
typedef void* (*CreateFunc)();
31+
typedef void* (*AllocateFunc)(void*, size_t);
32+
typedef bool (*RemoveFunc)(void*, void*);
33+
typedef const void* (*FindFunc)(void*, void*);
34+
typedef void (*ClearFunc)(void*);
35+
36+
static CreateFunc create_func = nullptr;
37+
static AllocateFunc allocate_func = nullptr;
38+
static RemoveFunc remove_func = nullptr;
39+
static FindFunc find_func = nullptr;
40+
static ClearFunc clear_func = nullptr;
41+
static void* handle = nullptr;
42+
static void* allocatorHandle = nullptr;
43+
44+
namespace executorch {
45+
namespace backends {
3146
namespace neuron {
3247

3348
struct BufferDeleter {
@@ -135,11 +150,9 @@ class BufferAllocator : public executorch::runtime::MemoryAllocator {
135150
}
136151

137152
private:
138-
std::map<void*, std::unique_ptr<MemoryUnit>> mPool;
139-
140153
std::mutex mMutex;
141154
};
142155

143156
} // namespace neuron
144-
} // namespace executor
145-
} // namespace torch
157+
} // namespace backends
158+
} // namespace executorch

backends/mediatek/scripts/mtk_build.sh

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,24 @@ if [ -z "$ANDROID_NDK" ]; then
1818
exit 1
1919
fi
2020

21-
# Check if the NEURON_BUFFER_ALLOCATOR_LIB environment variable is set
22-
if [ -z "$NEURON_BUFFER_ALLOCATOR_LIB" ]; then
23-
echo "Error: NEURON_BUFFER_ALLOCATOR_LIB environment variable is not set." >&2
24-
exit 1
25-
fi
26-
2721
# Create and enter the build directory
22+
# Set build directory
23+
build_dir="cmake-android-out"
2824
cd "$SOURCE_DIR"
29-
rm -rf cmake-android-out && mkdir cmake-android-out && cd cmake-android-out
25+
rm -rf "${build_dir}"
3026

3127
# Configure the project with CMake
3228
# Note: Add any additional configuration options you need here
33-
cmake -DBUCK2="$BUCK_PATH" \
29+
cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \
3430
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
3531
-DANDROID_ABI=arm64-v8a \
32+
-DANDROID_NATIVE_API_LEVEL=26 \
3633
-DANDROID_PLATFORM=android-26 \
3734
-DEXECUTORCH_BUILD_NEURON=ON \
38-
-DNEURON_BUFFER_ALLOCATOR_LIB="$NEURON_BUFFER_ALLOCATOR_LIB" \
39-
..
35+
-B"${build_dir}"
4036

4137
# Build the project
42-
cd ..
43-
cmake --build cmake-android-out -j4
38+
cmake --build "${build_dir}" --target install --config Release -j5
4439

4540
# Switch back to the original directory
4641
cd - > /dev/null

examples/mediatek/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ if(${ANDROID})
167167
target_link_libraries(
168168
mtk_llama_executor_runner
169169
${_executor_runner_libs}
170-
${NEURON_BUFFER_ALLOCATOR_LIB}
171170
neuron_backend
172171
gflags
173172
mtk_llama_executor_lib

examples/mediatek/mtk_build_examples.sh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ if [ -z "$ANDROID_NDK" ]; then
1919
exit 1
2020
fi
2121

22-
# Check if the NEURON_BUFFER_ALLOCATOR_LIB environment variable is set
23-
if [ -z "$NEURON_BUFFER_ALLOCATOR_LIB" ]; then
24-
echo "Error: NEURON_BUFFER_ALLOCATOR_LIB environment variable is not set." >&2
25-
exit 1
26-
fi
27-
2822
main() {
2923
# Set build directory
3024
local build_dir="cmake-android-out"
@@ -39,15 +33,14 @@ main() {
3933
-DBUCK2="$BUCK_PATH" \
4034
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
4135
-DANDROID_ABI=arm64-v8a \
36+
-DANDROID_NATIVE_API_LEVEL=26 \
4237
-DANDROID_PLATFORM=android-26 \
43-
-DANDROID_NATIVE_API_LEVEL=23 \
4438
-DEXECUTORCH_BUILD_NEURON=ON \
45-
-DNEURON_BUFFER_ALLOCATOR_LIB="$NEURON_BUFFER_ALLOCATOR_LIB" \
4639
-B"${build_dir}"
4740

4841

4942
# Build the project
50-
cmake --build cmake-android-out --target install --config Release -j5
43+
cmake --build "${build_dir}" --target install --config Release -j5
5144

5245
## Build example
5346
local example_dir=examples/mediatek
@@ -59,6 +52,7 @@ main() {
5952
cmake -DCMAKE_PREFIX_PATH="${cmake_prefix_path}" \
6053
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
6154
-DANDROID_ABI=arm64-v8a \
55+
-DANDROID_NATIVE_API_LEVEL=26 \
6256
-DANDROID_PLATFORM=android-26 \
6357
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \
6458
-DNEURON_BUFFER_ALLOCATOR_LIB="$NEURON_BUFFER_ALLOCATOR_LIB" \

0 commit comments

Comments
 (0)