Skip to content

Add QNN EP HTP shared memory allocator#23136

Merged
edgchen1 merged 61 commits intomainfrom
edgchen1/qnn_ep_rpcmem
Jan 14, 2025
Merged

Add QNN EP HTP shared memory allocator#23136
edgchen1 merged 61 commits intomainfrom
edgchen1/qnn_ep_rpcmem

Conversation

@edgchen1
Copy link
Contributor

@edgchen1 edgchen1 commented Dec 18, 2024

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 to 1. QNNExecutionProvider::CreatePreferredAllocators() will then return an instance of HtpSharedMemoryAllocator.

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:

  • HTP shared memory usage is only supported for graph inputs and outputs. Intermediate values are not supported.
  • An allocation is assigned to a single shared memory buffer. The allocator is not smart enough to have multiple allocations share a single shared memory buffer.

Motivation and Context

Improve performance by using HTP shared memory to avoid overhead from copying data between CPU and NPU.

edgchen1 and others added 30 commits November 5, 2024 15:12
… declarations and definitions for IAllocator::TensorAlloc().
@yuslepukhin
Copy link
Member

Can this be used for Lora support when the model is modified to have optional inputs, and the data can be fed to override default initializers?

Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕐

@edgchen1
Copy link
Contributor Author

Can this be used for Lora support when the model is modified to have optional inputs, and the data can be fed to override default initializers?

I'm not too familiar with the scenario. If that can be done using OrtValues, an input OrtValue can use this new allocator.

@edgchen1 edgchen1 requested a review from yuslepukhin January 13, 2025 23:03
Copy link
Member

@yuslepukhin yuslepukhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@edgchen1
Copy link
Contributor Author

/azp run Windows GPU WebGPU CI Pipeline

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@edgchen1 edgchen1 merged commit 04030f6 into main Jan 14, 2025
98 checks passed
@edgchen1 edgchen1 deleted the edgchen1/qnn_ep_rpcmem branch January 14, 2025 19:09
guschmue pushed a commit that referenced this pull request Mar 6, 2025
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` to `1`.
`QNNExecutionProvider::CreatePreferredAllocators()` will then return an
instance of `HtpSharedMemoryAllocator`.

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:
- HTP shared memory usage is only supported for graph inputs and
outputs. Intermediate values are not supported.
- An allocation is assigned to a single shared memory buffer. The
allocator is not smart enough to have multiple allocations share a
single shared memory buffer.

Co-authored-by: Baiju Meswani <bmeswani@microsoft.com>
ashrit-ms pushed a commit that referenced this pull request Mar 17, 2025
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` to `1`.
`QNNExecutionProvider::CreatePreferredAllocators()` will then return an
instance of `HtpSharedMemoryAllocator`.

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:
- HTP shared memory usage is only supported for graph inputs and
outputs. Intermediate values are not supported.
- An allocation is assigned to a single shared memory buffer. The
allocator is not smart enough to have multiple allocations share a
single shared memory buffer.

Co-authored-by: Baiju Meswani <bmeswani@microsoft.com>
HectorSVC pushed a commit that referenced this pull request Apr 26, 2025
…24196)

### Description
During inference, using the QNN EP option to set enable_htp_shared_memory_allocator gives a hint that we use RPC allocated buffers to avoid buffer copy between CPU and NPU.

With the current PR, we add hints in the compilation phase that if RPC memory is going to be used, any additional  allocations done on the CPU can be avoided.

### Motivation and Context
This should help reduce the peak CPU memory consumption while running AI work loads using shared memory.

Related PR: #23136

Co-authored-by: Ashish Garg (AISW) <ashigarg@qti.qualcomm.com>
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request May 12, 2025
…icrosoft#24196)

### Description
During inference, using the QNN EP option to set enable_htp_shared_memory_allocator gives a hint that we use RPC allocated buffers to avoid buffer copy between CPU and NPU.

With the current PR, we add hints in the compilation phase that if RPC memory is going to be used, any additional  allocations done on the CPU can be avoided.

### Motivation and Context
This should help reduce the peak CPU memory consumption while running AI work loads using shared memory.

Related PR: microsoft#23136

Co-authored-by: Ashish Garg (AISW) <ashigarg@qti.qualcomm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ep:QNN issues related to QNN exeution provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants