-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add QNN EP HTP shared memory allocator #23136
base: main
Are you sure you want to change the base?
Conversation
… declarations and definitions for IAllocator::TensorAlloc().
…ion clean up callback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can commit the suggested changes from lintrunner.
@@ -63,6 +65,12 @@ size_t GetElementSizeByType(ONNXTensorElementDataType elem_type) { | |||
return pos->second; | |||
} | |||
|
|||
size_t GetQnnTensorDataSize(gsl::span<const uint32_t> shape, Qnn_DataType_t element_type) { | |||
ORT_ENFORCE(!shape.empty(), "Empty shape not allowed."); // TODO can we just treat empty shape as a scalar? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this check is copied from the original implementation here:
ORT_RETURN_IF(dims.empty(), "Tensor dimensions is nullptr"); |
I'm not sure if it's needed
// - QNN context handle is still valid. This should be true as long as QNN contexts are not freed from | ||
// anywhere other than the destructor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be true as long as QNN contexts are not freed from anywhere other than the destructor.
it seems kind of brittle to depend on this.
@@ -1098,6 +1099,38 @@ TEST_F(QnnHTPBackendTests, EPOffloadsGraphIOQuantDequant) { | |||
} | |||
} | |||
|
|||
TEST_F(QnnHTPBackendTests, UseHtpSharedMemoryAllocatorForInputs) { | |||
#if !defined(__ANDROID__) && !defined(_WIN32) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QC device for windows is Arm64 based, so you can check defined(aarch64) defined(_M_ARM64)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code is within an ifdef that checks for those macros:
#if defined(__aarch64__) || defined(_M_ARM64) || defined(__linux__) |
@@ -1098,6 +1099,38 @@ TEST_F(QnnHTPBackendTests, EPOffloadsGraphIOQuantDequant) { | |||
} | |||
} | |||
|
|||
TEST_F(QnnHTPBackendTests, UseHtpSharedMemoryAllocatorForInputs) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also have some codes to demonstrate how this feature get used from user code.
Here are some IObinding examples for other EPs:
#if defined(USE_CUDA) || defined(USE_TENSORRT) |
|
||
struct AllocationRecord { | ||
SharedMemoryInfo shared_memory_info; | ||
InlinedVector<AllocationCleanUpFn, 1> clean_up_fns; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we expect more than one cleanup func?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not unexpected. e.g., if the same shared memory is used from more than one QNN context, there will be a separate cleanup function per QNN context.
marker.fill('\0'); | ||
allocator_ptr = nullptr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we limit doing the fill
to a debug build? not sure how many allocations QNN makes and whether there's any meaningful perf cost.
|
||
namespace { | ||
|
||
struct AllocationHeader { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be great to add a comment describing the overall setup and how it uses this header.
const size_t allocation_offset = AllocationOffsetFromStartOfHeader(); | ||
const size_t shared_memory_block_size_in_bytes = allocation_offset + requested_size; | ||
|
||
// rpcmem_alloc() has an int size parameter. make sure we don't overflow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use SafeInt?
htp_arch, | ||
soc_model, | ||
enable_htp_weight_sharing); | ||
static const std::string QNN_HTP_SHARED_MEMORY_ALLOCATOR_ENABLED = "enable_htp_shared_memory_allocator"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be more user visible?
SharedContext(const SharedContext&) = delete; | ||
SharedContext& operator=(const SharedContext&) = delete; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: moved SharedContext class from qnn_execution_provider.h to its own file.
Description
Adds QNN EP HTP shared memory allocator.
The HTP shared memory allocator (
HtpSharedMemoryAllocator
) calls the rpcmem shared library (libcdsprpc.so/dll) to allocate and free memory that can be shared between HTP and CPU.The allocator can be enabled by setting QNN EP option
enable_htp_shared_memory_allocator
to1
.QNNExecutionProvider::CreatePreferredAllocators()
will then return an instance ofHtpSharedMemoryAllocator
.For each QNN context, we also need to register and unregister memory handles in order to use the HTP shared memory. This memory handle management is added to
QnnBackendManager
, which also manages the QNN context handles.For more information about using HTP shared memory with QNN, see: https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-50/htp_shared_buffer_tutorial.html#shared-buffer-tutorial
Limitations:
Motivation and Context
Improve performance by using HTP shared memory to avoid overhead from copying data between CPU and NPU.