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 #include <executorch/backends/qualcomm/runtime/Logging.h>
9 #include <executorch/backends/qualcomm/runtime/backends/QnnBackendCache.h>
10 #include <executorch/backends/qualcomm/runtime/backends/htpbackend/HtpGraphCustomConfig.h>
11 namespace executorch {
12 namespace backends {
13 namespace qnn {
14 std::vector<QnnGraph_CustomConfig_t>
CreateGraphCustomConfigCommon(const SocInfo * qcom_target_soc_info,float opt_level)15 HtpGraphCustomConfig::CreateGraphCustomConfigCommon(
16     const SocInfo* qcom_target_soc_info,
17     float opt_level) {
18   std::vector<QnnGraph_CustomConfig_t> ret;
19   QnnHtpGraph_CustomConfig_t* p_custom_config = nullptr;
20 
21   if (!htp_options_->use_conv_hmx()) {
22     p_custom_config = AllocGraphCustomConfig();
23     p_custom_config->option =
24         QNN_HTP_GRAPH_CONFIG_OPTION_SHORT_DEPTH_CONV_ON_HMX_OFF;
25     p_custom_config->shortDepthConvOnHmxOff = true;
26     ret.push_back(static_cast<QnnGraph_CustomConfig_t>(p_custom_config));
27   }
28 
29   if (!htp_options_->use_fold_relu()) {
30     p_custom_config = AllocGraphCustomConfig();
31     p_custom_config->option =
32         QNN_HTP_GRAPH_CONFIG_OPTION_FOLD_RELU_ACTIVATION_INTO_CONV_OFF;
33     p_custom_config->foldReluActivationIntoConvOff = true;
34     ret.push_back(static_cast<QnnGraph_CustomConfig_t>(p_custom_config));
35   }
36 
37   switch (htp_options_->precision()) {
38     case QnnExecuTorchHtpPrecision::kHtpFp16:
39       p_custom_config = AllocGraphCustomConfig();
40       p_custom_config->option = QNN_HTP_GRAPH_CONFIG_OPTION_PRECISION;
41       p_custom_config->precision = QNN_PRECISION_FLOAT16;
42       ret.push_back(static_cast<QnnGraph_CustomConfig_t>(p_custom_config));
43       break;
44     case QnnExecuTorchHtpPrecision::kHtpQuantized:
45     default:
46       break;
47   }
48 
49   QNN_EXECUTORCH_LOG_INFO(
50       "Running level=%d optimization.", static_cast<int>(opt_level));
51 
52   p_custom_config = AllocGraphCustomConfig();
53   p_custom_config->option = QNN_HTP_GRAPH_CONFIG_OPTION_OPTIMIZATION;
54   p_custom_config->optimizationOption.type =
55       QNN_HTP_GRAPH_OPTIMIZATION_TYPE_FINALIZE_OPTIMIZATION_FLAG;
56   p_custom_config->optimizationOption.floatValue = opt_level;
57   ret.push_back(static_cast<QnnGraph_CustomConfig_t>(p_custom_config));
58 
59   p_custom_config = AllocGraphCustomConfig();
60   p_custom_config->option = QNN_HTP_GRAPH_CONFIG_OPTION_VTCM_SIZE;
61   p_custom_config->vtcmSizeInMB =
62       qcom_target_soc_info->htp_info()->vtcm_size_in_mb();
63   ret.push_back(static_cast<QnnGraph_CustomConfig_t>(p_custom_config));
64 
65   p_custom_config = AllocGraphCustomConfig();
66   p_custom_config->option = QNN_HTP_GRAPH_CONFIG_OPTION_OPTIMIZATION;
67   p_custom_config->optimizationOption.type =
68       QNN_HTP_GRAPH_OPTIMIZATION_TYPE_ENABLE_DLBC;
69   p_custom_config->optimizationOption.floatValue =
70       htp_options_->use_dlbc() ? 1.0 : 0.0;
71   ret.push_back(static_cast<QnnGraph_CustomConfig_t>(p_custom_config));
72 
73   return ret;
74 }
75 } // namespace qnn
76 } // namespace backends
77 } // namespace executorch
78