diff --git a/backends/mediatek/runtime/NeuronBufferAllocator.cpp b/backends/mediatek/runtime/NeuronBufferAllocator.cpp index c4e4d2a2f36..edac0c081dd 100644 --- a/backends/mediatek/runtime/NeuronBufferAllocator.cpp +++ b/backends/mediatek/runtime/NeuronBufferAllocator.cpp @@ -60,7 +60,7 @@ BufferAllocator& BufferAllocator::GetInstance() { return instance; }; -void* BufferAllocator::Allocate(size_t size) { +void* BufferAllocator::Allocate(size_t size, bool is_cached) { std::scoped_lock Guard(mMutex); return allocate_func(allocatorHandle, size); } diff --git a/backends/mediatek/runtime/include/NeuronBackend.h b/backends/mediatek/runtime/include/NeuronBackend.h index 1d2e8563ab3..9c461a03e96 100644 --- a/backends/mediatek/runtime/include/NeuronBackend.h +++ b/backends/mediatek/runtime/include/NeuronBackend.h @@ -38,7 +38,7 @@ class NeuronSharedWeights { explicit NeuronSharedWeights(const FreeableBuffer& shared_weights_buffer) { auto& buffer_allocator = GET_NEURON_ALLOCATOR; nbytes_ = shared_weights_buffer.size(); - data_ = buffer_allocator.Allocate(nbytes_); + data_ = buffer_allocator.Allocate(nbytes_, /* cacheable */ false); ET_CHECK_MSG( data_ != nullptr, "Error: Failed to allocate memory for shared weights of size %zu", diff --git a/backends/mediatek/runtime/include/NeuronBufferAllocator.h b/backends/mediatek/runtime/include/NeuronBufferAllocator.h index 47cda50dcd6..684255f6bcf 100644 --- a/backends/mediatek/runtime/include/NeuronBufferAllocator.h +++ b/backends/mediatek/runtime/include/NeuronBufferAllocator.h @@ -56,8 +56,9 @@ struct BufferDeleter { class MemoryUnit { public: - static std::unique_ptr Create(size_t size) { - auto obj = std::unique_ptr(new (std::nothrow) MemoryUnit(size)); + static std::unique_ptr Create(size_t size, bool is_cached = true) { + auto obj = std::unique_ptr( + new (std::nothrow) MemoryUnit(size, is_cached)); return (obj && (obj->Allocate() == NEURON_NO_ERROR)) ? std::move(obj) : nullptr; } @@ -80,7 +81,12 @@ class MemoryUnit { } private: - explicit MemoryUnit(size_t size) : mSize(size) {} + explicit MemoryUnit(size_t size, bool is_cached) : mSize(size) { + if (!is_cached) { + mAhwbType = AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | + AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY; + } + } int Allocate() { AHardwareBuffer_Desc iDesc{ @@ -126,7 +132,7 @@ class BufferAllocator : public executorch::runtime::MemoryAllocator { public: static BufferAllocator& GetInstance(); - void* Allocate(size_t size); + void* Allocate(size_t size, bool is_cached = true); void* allocate(size_t size, size_t alignment = kDefaultAlignment) override { return Allocate(size); diff --git a/backends/mediatek/scripts/mtk_build.sh b/backends/mediatek/scripts/mtk_build.sh index d42e5f7e10a..b0592970a10 100755 --- a/backends/mediatek/scripts/mtk_build.sh +++ b/backends/mediatek/scripts/mtk_build.sh @@ -31,6 +31,7 @@ cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \ -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ -DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \ -DEXECUTORCH_BUILD_EXTENSION_NAMED_DATA_MAP=ON \ + -DEXECUTORCH_BUILD_NEURON_BUFFER_ALLOCATOR=OFF \ -DEXECUTORCH_BUILD_NEURON=ON \ -B"${build_dir}"