xref: /aosp_15_r20/external/executorch/backends/qualcomm/runtime/QnnExecuTorch.h (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Qualcomm Innovation Center, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 #pragma once
9 
10 #include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
11 #ifdef __cplusplus
12 #include <cstddef>
13 #include <cstdint>
14 #else
15 #include <stddef.h>
16 #include <stdint.h>
17 #endif
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif // __cplusplus
22 typedef struct {
23   /// qnn_context_binary_blob
24   void* buffer;
25   /// number of bytes of buffer
26   uint64_t nbytes;
27 } QnnExecuTorchContextBinary;
28 
29 // clang-format off
30 #define QNN_EXECUTORCH_CONTEXT_BINARY    \
31   {                                      \
32     nullptr,        /*buffer*/           \
33     0,              /*nbytes*/           \
34   }
35 // clang-format on
36 
37 /// Allocate memory in different way, check qnn document for more details.
38 enum QnnMemDescriptor { kIon, kCustom };
39 
40 struct CustomMemTensorInfo {
41   void* custom_mem;
42   void* tensor_addr;
43   size_t pos;
44   size_t tensor_bytes;
45   uint32_t* shape;
46   uint32_t rank;
47   executorch::aten::ScalarType dtype;
48 };
49 
50 /// Allocate specific tensors (usually graph inputs and outputs) on shared
51 /// memory. Users are responsible to allocate "enough" tensor bytes, and set
52 /// alignment as MemoryAllocator::kDefaultAlignment.
53 /// See runtime/core/memory_allocator.h. The function returns a valid pointer
54 /// if allocation is successful.
55 void* QnnExecuTorchAllocCustomMem(size_t bytes, size_t alignment);
56 
57 /// Add tensor to custom memory with custom type descriptor. Create memory
58 /// handle to tensor wrapper during execution
59 void QnnExecuTorchAddCustomMemTensorAddr(void* tensor_addr, void* custom_mem);
60 
61 /// Add custom mem tensor info. Help to bring forward the memHandle creating
62 /// time from execution to initialization.
63 void QnnExecuTorchAddCustomMemTensorInfo(const CustomMemTensorInfo& info);
64 
65 /// Free the allocated shared memory.
66 void QnnExecuTorchFreeCustomMem(void* buffer_ptr);
67 
68 #ifdef __cplusplus
69 }
70 #endif // __cplusplus
71