forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedBuffer.h
More file actions
103 lines (84 loc) · 2.96 KB
/
SharedBuffer.h
File metadata and controls
103 lines (84 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) Qualcomm Innovation Center, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <QnnTypes.h>
#include <executorch/backends/qualcomm/runtime/QnnExecuTorch.h>
#include <executorch/runtime/core/error.h>
#include <atomic>
#include <cstdint>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <unordered_set>
using RpcMemAllocFn_t = void* (*)(int, uint32_t, int);
using RpcMemFreeFn_t = void (*)(void*);
using RpcMemToFdFn_t = int (*)(void*);
// TODO Finad a better file to place CustomMemTensorInfo
bool operator==(const CustomMemTensorInfo& lhs, const CustomMemTensorInfo& rhs);
template <>
struct std::hash<CustomMemTensorInfo> {
std::size_t operator()(const CustomMemTensorInfo& info) const noexcept;
};
namespace torch {
namespace executor {
namespace qnn {
class SharedBuffer final {
public:
SharedBuffer(const SharedBuffer&) = delete;
SharedBuffer& operator=(const SharedBuffer&) = delete;
SharedBuffer(SharedBuffer&&) = delete;
SharedBuffer& operator=(SharedBuffer&&) = delete;
~SharedBuffer();
static SharedBuffer& GetSharedBufferManager();
void* AllocMem(size_t bytes, size_t alignment);
// map a buffer allocated via RPCMem to a file descriptor so it can be
// registered with a backend via QnnMem_register()
int32_t MemToFd(void* buf);
void FreeMem(void* buf);
bool IsAllocated(void* buf);
bool GetInitialize() {
return initialize_;
}
void SetInitialize(bool initialize) {
initialize_ = initialize;
}
// memory handle is registered during execution
void AddCusomMemTensorAddr(void* tensor_addr, void* custom_mem);
// memory handle can be registered before execution
void AddCusomMemTensorInfo(const CustomMemTensorInfo& info);
size_t GetAllocatedSize(void* buf);
void* GetCustomMemBase(void* buf);
void* GetUnAlignedAddr(void* buf);
const std::unordered_set<CustomMemTensorInfo>& GetCustomMemTensorInfoSet() {
return custom_mem_tensor_info_set_;
};
private:
SharedBuffer() = default;
// dlopen RPCMem library and dlysm required functions
Error Load();
Error UnLoad();
// Pointer to the dlopen'd libcdsprpc.so shared library which contains
// rpcmem_alloc, rpcmem_free, rpcmem_to_fd APIs
[[maybe_unused]] void* lib_cdsp_rpc_;
// Function pointer to rpcmem_alloc
RpcMemAllocFn_t rpc_mem_alloc_;
// Function pointer to rpcmem_free
RpcMemFreeFn_t rpc_mem_free_;
// Function pointer to rpcmem_to_fd
RpcMemToFdFn_t rpc_mem_to_fd_;
std::unordered_map<void*, void*> restore_map_;
std::unordered_map<void*, size_t> allocated_size_map_;
// Maps for the custom memory
std::unordered_map<void*, void*> tensor_addr_to_custom_mem_;
std::unordered_set<CustomMemTensorInfo> custom_mem_tensor_info_set_;
std::atomic_bool initialize_{false};
static std::mutex init_mutex_;
};
} // namespace qnn
} // namespace executor
} // namespace torch