xref: /aosp_15_r20/external/executorch/backends/qualcomm/runtime/backends/QnnBackendCache.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/backends/qualcomm/runtime/QnnExecuTorch.h>
11 #include <executorch/backends/qualcomm/runtime/backends/QnnSysImplementation.h>
12 
13 #include <string>
14 #include <unordered_map>
15 #include <vector>
16 
17 namespace executorch {
18 namespace backends {
19 namespace qnn {
20 class QnnBackendCache {
21  public:
22   enum CacheState {
23     INVALID = 0,
24     SERIALIZE = 1,
25     DESERIALIZE = 2,
26     ONLINE_PREPARE = 3,
27   };
QnnBackendCache(const QnnExecuTorchContextBinary & qnn_context_blob,const std::string & aot_graph_name)28   explicit QnnBackendCache(
29       const QnnExecuTorchContextBinary& qnn_context_blob,
30       const std::string& aot_graph_name)
31       : qnn_context_blob_(qnn_context_blob), aot_graph_name_(aot_graph_name) {}
32   virtual ~QnnBackendCache();
33   QnnBackendCache(const QnnBackendCache&) = delete;
34   QnnBackendCache(QnnBackendCache&&) = delete;
35   QnnBackendCache& operator=(const QnnBackendCache&) = delete;
36   QnnBackendCache& operator=(QnnBackendCache&&) = delete;
37 
38   std::vector<Qnn_Tensor_t> GetGraphInputs(const std::string& graph_name);
39 
40   std::vector<Qnn_Tensor_t> GetGraphOutputs(const std::string& graph_name);
41 
GetQnnContextBlob()42   const QnnExecuTorchContextBinary& GetQnnContextBlob() {
43     return qnn_context_blob_;
44   };
45 
GetCacheState()46   QnnBackendCache::CacheState GetCacheState() {
47     return state_;
48   };
49 
InvalidateCache()50   void InvalidateCache() {
51     state_ = INVALID;
52   }
53 
GetGraphNames()54   std::vector<std::string> GetGraphNames() {
55     return graph_names_;
56   }
57 
58   executorch::runtime::Error Configure();
59 
60  protected:
RetrieveBackendBinaryInfo(__ET_UNUSED const QnnSystemContext_BinaryInfo_t * binaryinfo)61   virtual executorch::runtime::Error RetrieveBackendBinaryInfo(
62       __ET_UNUSED const QnnSystemContext_BinaryInfo_t* binaryinfo) {
63     return executorch::runtime::Error::Ok;
64   }
65 
66   Error Configure();
67 
68  protected:
RetrieveBackendBinaryInfo(__ET_UNUSED const QnnSystemContext_BinaryInfo_t * binaryinfo)69   virtual Error RetrieveBackendBinaryInfo(
70       __ET_UNUSED const QnnSystemContext_BinaryInfo_t* binaryinfo) {
71     return Error::Ok;
72   }
73 
74  private:
75   executorch::runtime::Error GetQnnGraphInfoFromBinary(
76       void* buffer,
77       uint32_t nbytes);
78 
79   template <typename INFO>
80   void RetrieveGraphInfo(const INFO& info);
81 
82   CacheState state_{INVALID};
83 
84   QnnExecuTorchContextBinary qnn_context_blob_;
85   QnnSystemContext_Handle_t sys_context_handle_{nullptr};
86   QnnSystemImplementation qnn_sys_impl_{"libQnnSystem.so"};
87   std::vector<std::string> graph_names_;
88   std::string aot_graph_name_;
89   std::unordered_map<std::string, std::vector<Qnn_Tensor_t>>
90       input_tensor_structs_;
91   std::unordered_map<std::string, std::vector<Qnn_Tensor_t>>
92       output_tensor_structs_;
93 };
94 } // namespace qnn
95 } // namespace backends
96 } // namespace executorch
97