xref: /aosp_15_r20/external/executorch/backends/qualcomm/runtime/backends/htpbackend/HtpDevice.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/backends/QnnDeviceCommon.h>
11 #include <executorch/backends/qualcomm/runtime/backends/htpbackend/HtpDeviceCustomConfig.h>
12 #include <executorch/backends/qualcomm/runtime/backends/htpbackend/HtpDevicePlatformInfoConfig.h>
13 #include <memory>
14 
15 #include "HTP/QnnHtpDevice.h"
16 
17 #define QNN_HTP_DEPRECATED_HTP_ARCH_VERSION_MAJOR 5
18 #define QNN_HTP_DEPRECATED_HTP_ARCH_VERSION_MINOR 14
19 
20 namespace executorch {
21 namespace backends {
22 namespace qnn {
23 class HtpDevice : public QnnDevice {
24  public:
HtpDevice(const QnnImplementation & implementation,QnnLogger * logger,const SocInfo * soc_info,const QnnExecuTorchHtpBackendOptions * htp_options)25   HtpDevice(
26       const QnnImplementation& implementation,
27       QnnLogger* logger,
28       const SocInfo* soc_info,
29       const QnnExecuTorchHtpBackendOptions* htp_options)
30       : QnnDevice(implementation, logger),
31         qcom_target_soc_info_(soc_info),
32         htp_options_(htp_options) {
33     htp_device_platform_info_config_ =
34         std::make_unique<HtpDevicePlatformInfoConfig>(htp_options);
35     htp_device_custom_config_ =
36         std::make_unique<HtpDeviceCustomConfig>(htp_options);
37   }
38   ~HtpDevice();
39 
40   // Defines Qnn performance mode vote types for htpbackend
41   enum PerformanceModeVoteType {
42     kNoVote = 0,
43     kUpVote = 1,
44     kDownVote = 2,
45   };
46 
47  protected:
48   executorch::runtime::Error MakeConfig(
49       std::vector<const QnnDevice_Config_t*>& config) override;
50 
51   executorch::runtime::Error AfterCreateDevice() override;
52 
53  private:
54   void PerformanceVote();
55   void ReleasePerformanceVote();
56 
IsPerfModeEnabled()57   inline bool IsPerfModeEnabled() {
58     return htp_options_->performance_mode() !=
59         QnnExecuTorchHtpPerformanceMode::kHtpDefault;
60   }
61 
62   template <typename T>
ObtainNullTermPtrVector(const std::vector<T> & vec)63   std::vector<std::add_pointer_t<std::add_const_t<T>>> ObtainNullTermPtrVector(
64       const std::vector<T>& vec) {
65     std::vector<std::add_pointer_t<std::add_const_t<T>>> ret;
66     for (auto& elem : vec) {
67       ret.push_back(&elem);
68     }
69     ret.push_back(nullptr);
70     return ret;
71   }
72 
73   std::unique_ptr<HtpDevicePlatformInfoConfig> htp_device_platform_info_config_;
74   std::unique_ptr<HtpDeviceCustomConfig> htp_device_custom_config_;
75 
76   std::vector<QnnDevice_Config_t> device_config_;
77 
78   std::uint32_t powerconfig_client_id_{0};
79   QnnHtpDevice_PerfInfrastructure_t owned_htp_perf_infra_ =
80       QNN_HTP_DEVICE_PERF_INFRASTRUCTURE_INIT;
81   QnnHtpDevice_PerfInfrastructure_t* htp_perf_infra_{nullptr};
82   std::vector<QnnHtpPerfInfrastructure_PowerConfig_t> perf_power_configs_;
83   std::vector<QnnHtpPerfInfrastructure_PowerConfig_t> down_vote_power_configs_;
84   std::vector<QnnHtpPerfInfrastructure_PowerConfig_t> rpc_power_configs_;
85   std::vector<const QnnHtpPerfInfrastructure_PowerConfig_t*>
86       rpc_power_configs_ptr_;
87   std::vector<const QnnHtpPerfInfrastructure_PowerConfig_t*>
88       perf_power_configs_ptr_;
89   std::vector<const QnnHtpPerfInfrastructure_PowerConfig_t*>
90       down_vote_power_configs_ptr_;
91 
92   const SocInfo* qcom_target_soc_info_;
93   const QnnExecuTorchHtpBackendOptions* htp_options_;
94 };
95 } // namespace qnn
96 } // namespace backends
97 } // namespace executorch
98