xref: /aosp_15_r20/external/executorch/backends/qualcomm/runtime/backends/QnnFunctionInterface.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 "QnnInterface.h"
11 #include "Saver/QnnSaver.h"
12 
13 #include <utility>
14 
15 #define DEFINE_SHIM_FUNCTION_INTERFACE(F, pointer_name)           \
16   template <typename... Args>                                     \
17   inline auto qnn_##F(Args... args) const {                       \
18     return (qnn_interface_->QNN_INTERFACE_VER_NAME.pointer_name)( \
19         std::forward<Args>(args)...);                             \
20   }
21 namespace executorch {
22 namespace backends {
23 namespace qnn {
24 using QnnInterfaceGetProvidersFn = decltype(QnnInterface_getProviders);
25 using QnnSaverInitializeFn = decltype(QnnSaver_initialize);
26 
27 class QnnInterface {
28   friend class QnnImplementation;
29 
30  public:
31   QnnInterface() = default;
32 
33   // --------- QnnBackend ---------
34   DEFINE_SHIM_FUNCTION_INTERFACE(backend_create, backendCreate);
35   DEFINE_SHIM_FUNCTION_INTERFACE(backend_free, backendFree);
36   DEFINE_SHIM_FUNCTION_INTERFACE(
37       backend_register_op_package,
38       backendRegisterOpPackage);
39   DEFINE_SHIM_FUNCTION_INTERFACE(
40       backend_validate_op_config,
41       backendValidateOpConfig);
42   DEFINE_SHIM_FUNCTION_INTERFACE(backend_get_api_version, backendGetApiVersion);
43   // --------- QnnDevice ---------
44   DEFINE_SHIM_FUNCTION_INTERFACE(device_create, deviceCreate);
45   DEFINE_SHIM_FUNCTION_INTERFACE(device_free, deviceFree);
46   DEFINE_SHIM_FUNCTION_INTERFACE(
47       device_get_infrastructure,
48       deviceGetInfrastructure);
49   DEFINE_SHIM_FUNCTION_INTERFACE(
50       device_get_platform_info,
51       deviceGetPlatformInfo);
52   // DEFINE_SHIM_FUNCTION_INTERFACE(device_get_info, deviceGetInfo);
53   // --------- QnnContext ---------
54   DEFINE_SHIM_FUNCTION_INTERFACE(context_create, contextCreate);
55   DEFINE_SHIM_FUNCTION_INTERFACE(context_get_binary_size, contextGetBinarySize);
56   DEFINE_SHIM_FUNCTION_INTERFACE(context_get_binary, contextGetBinary);
57   DEFINE_SHIM_FUNCTION_INTERFACE(
58       context_create_from_binary,
59       contextCreateFromBinary);
60   DEFINE_SHIM_FUNCTION_INTERFACE(context_free, contextFree);
61   // --------- QnnGraph ---------
62   DEFINE_SHIM_FUNCTION_INTERFACE(graph_create, graphCreate);
63   DEFINE_SHIM_FUNCTION_INTERFACE(graph_add_node, graphAddNode);
64   DEFINE_SHIM_FUNCTION_INTERFACE(graph_finalize, graphFinalize);
65   DEFINE_SHIM_FUNCTION_INTERFACE(graph_execute, graphExecute);
66   DEFINE_SHIM_FUNCTION_INTERFACE(graph_retrieve, graphRetrieve);
67   // --------- QnnLog ---------
68   DEFINE_SHIM_FUNCTION_INTERFACE(log_create, logCreate);
69   DEFINE_SHIM_FUNCTION_INTERFACE(log_free, logFree);
70   DEFINE_SHIM_FUNCTION_INTERFACE(log_set_log_level, logSetLogLevel);
71   // --------- QnnProfile ---------
72   DEFINE_SHIM_FUNCTION_INTERFACE(profile_create, profileCreate);
73   DEFINE_SHIM_FUNCTION_INTERFACE(profile_set_config, profileSetConfig);
74   DEFINE_SHIM_FUNCTION_INTERFACE(profile_get_events, profileGetEvents);
75   DEFINE_SHIM_FUNCTION_INTERFACE(profile_get_sub_events, profileGetSubEvents);
76   DEFINE_SHIM_FUNCTION_INTERFACE(profile_get_event_data, profileGetEventData);
77   DEFINE_SHIM_FUNCTION_INTERFACE(profile_free, profileFree);
78   // --------- QnnMem ---------
79   DEFINE_SHIM_FUNCTION_INTERFACE(mem_register, memRegister);
80   DEFINE_SHIM_FUNCTION_INTERFACE(mem_de_register, memDeRegister);
81   // --------- QnnProperty --------
82   DEFINE_SHIM_FUNCTION_INTERFACE(
83       property_has_capability,
84       propertyHasCapability);
85   // --------- QnnTensor ---------
86   DEFINE_SHIM_FUNCTION_INTERFACE(
87       tensor_create_context_tensor,
88       tensorCreateContextTensor);
89   DEFINE_SHIM_FUNCTION_INTERFACE(
90       tensor_create_graph_tensor,
91       tensorCreateGraphTensor);
92 
SetQnnInterface(const QnnInterface_t * qnn_interface)93   void SetQnnInterface(const QnnInterface_t* qnn_interface) {
94     qnn_interface_ = qnn_interface;
95   }
96 
GetBackendId()97   uint32_t GetBackendId() const {
98     return qnn_interface_->backendId;
99   }
100 
IsLoaded()101   bool IsLoaded() const {
102     return qnn_interface_ != nullptr;
103   }
104 
105  private:
106   // --------- QnnInterface ---------
107   const QnnInterface_t* qnn_interface_{nullptr};
108 };
109 } // namespace qnn
110 } // namespace backends
111 } // namespace executorch
112