|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#if !defined(ORT_MINIMAL_BUILD) |
| 5 | + |
| 6 | +#include <string> |
| 7 | +#include "core/graph/constants.h" |
| 8 | +#include "test/providers/qnn/qnn_test_utils.h" |
| 9 | + |
| 10 | +#include "gtest/gtest.h" |
| 11 | + |
| 12 | +namespace onnxruntime { |
| 13 | +namespace test { |
| 14 | + |
| 15 | +// Runs a model with a QuickGelu operator on the QNN CPU backend. Checks the graph node assignment |
| 16 | +// and that inference outputs for QNN EP and CPU EP match. |
| 17 | +template <typename DataType> |
| 18 | +static void RunQuickGeluTest(const TestInputDef<DataType>& input_def, |
| 19 | + float alpha, |
| 20 | + ExpectedEPNodeAssignment expected_ep_assignment, |
| 21 | + const std::string& backend_name = "cpu", |
| 22 | + float fp32_abs_err = 5e-3f) { |
| 23 | + ProviderOptions provider_options; |
| 24 | + provider_options["backend_type"] = backend_name; |
| 25 | + |
| 26 | + if (backend_name == "htp") { |
| 27 | + provider_options["enable_htp_fp16_precision"] = "1"; |
| 28 | + } |
| 29 | + |
| 30 | + auto model_builder = [input_def, alpha](ModelTestBuilder& builder) { |
| 31 | + NodeArg* input = MakeTestInput<DataType>(builder, input_def); |
| 32 | + auto* output = builder.MakeOutput(); |
| 33 | + |
| 34 | + Node& node = builder.AddNode("QuickGelu", {input}, {output}, kMSDomain); |
| 35 | + node.AddAttribute("alpha", alpha); |
| 36 | + }; |
| 37 | + |
| 38 | + RunQnnModelTest(model_builder, |
| 39 | + provider_options, |
| 40 | + 13, // opset version for contrib ops |
| 41 | + expected_ep_assignment, |
| 42 | + fp32_abs_err); |
| 43 | +} |
| 44 | + |
| 45 | +// Tests the accuracy of a QDQ QuickGelu model on QNN EP by comparing to CPU EP. |
| 46 | +template <typename QType> |
| 47 | +static void RunQDQQuickGeluTest(const TestInputDef<float>& input_def, |
| 48 | + float alpha, |
| 49 | + ExpectedEPNodeAssignment expected_ep_assignment, |
| 50 | + const std::string& backend_name = "htp", |
| 51 | + bool use_contrib_qdq = false) { |
| 52 | + ProviderOptions provider_options; |
| 53 | + provider_options["backend_type"] = backend_name; |
| 54 | + provider_options["offload_graph_io_quantization"] = "0"; |
| 55 | + |
| 56 | + GetTestModelFn model_builder_fn = [input_def, alpha](ModelTestBuilder& builder) { |
| 57 | + NodeArg* input = MakeTestInput<float>(builder, input_def); |
| 58 | + auto* output = builder.MakeOutput(); |
| 59 | + |
| 60 | + Node& node = builder.AddNode("QuickGelu", {input}, {output}, kMSDomain); |
| 61 | + node.AddAttribute("alpha", alpha); |
| 62 | + }; |
| 63 | + |
| 64 | + GetTestQDQModelFn<QType> qdq_model_builder_fn = [input_def, alpha, use_contrib_qdq](ModelTestBuilder& builder, std::vector<QuantParams<QType>>& output_qparams) { |
| 65 | + NodeArg* input = MakeTestInput<float>(builder, input_def); |
| 66 | + QuantParams<QType> input_qparams = GetTestInputQuantParams<QType>(input_def); |
| 67 | + NodeArg* input_after_qdq = AddQDQNodePair<QType>(builder, input, input_qparams.scale, |
| 68 | + input_qparams.zero_point, use_contrib_qdq); |
| 69 | + |
| 70 | + // QuickGelu -> op_output |
| 71 | + auto* op_output = builder.MakeIntermediate(); |
| 72 | + Node& node = builder.AddNode("QuickGelu", {input_after_qdq}, {op_output}, kMSDomain); |
| 73 | + node.AddAttribute("alpha", alpha); |
| 74 | + |
| 75 | + // op_output -> Q -> DQ -> output |
| 76 | + AddQDQNodePairWithOutputAsGraphOutput<QType>(builder, op_output, output_qparams[0].scale, |
| 77 | + output_qparams[0].zero_point, use_contrib_qdq); |
| 78 | + }; |
| 79 | + |
| 80 | + TestQDQModelAccuracy(model_builder_fn, |
| 81 | + qdq_model_builder_fn, |
| 82 | + provider_options, |
| 83 | + 13, // opset version for contrib ops |
| 84 | + expected_ep_assignment, |
| 85 | + QDQTolerance(5e-3f)); |
| 86 | +} |
| 87 | + |
| 88 | +// |
| 89 | +// CPU tests: |
| 90 | +// |
| 91 | + |
| 92 | +// Test QuickGelu with default alpha value (1.0) |
| 93 | +TEST_F(QnnCPUBackendTests, QuickGelu_Default_Alpha) { |
| 94 | + RunQuickGeluTest<float>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 95 | + 1.0f, // alpha |
| 96 | + ExpectedEPNodeAssignment::All); |
| 97 | +} |
| 98 | + |
| 99 | +// Test QuickGelu with custom alpha value |
| 100 | +TEST_F(QnnCPUBackendTests, QuickGelu_Custom_Alpha) { |
| 101 | + RunQuickGeluTest<float>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 102 | + 1.702f, // alpha |
| 103 | + ExpectedEPNodeAssignment::All); |
| 104 | +} |
| 105 | + |
| 106 | +// Test QuickGelu with negative alpha value |
| 107 | +TEST_F(QnnCPUBackendTests, QuickGelu_Negative_Alpha) { |
| 108 | + RunQuickGeluTest<float>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 109 | + -1.702f, // alpha |
| 110 | + ExpectedEPNodeAssignment::All); |
| 111 | +} |
| 112 | + |
| 113 | +#if defined(__aarch64__) || defined(_M_ARM64) || defined(__linux__) |
| 114 | +// |
| 115 | +// HTP tests: |
| 116 | +// |
| 117 | + |
| 118 | +TEST_F(QnnHTPBackendTests, QuickGelu_Default_Alpha) { |
| 119 | + RunQuickGeluTest<float>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 120 | + 1.0f, |
| 121 | + ExpectedEPNodeAssignment::All, |
| 122 | + "htp", |
| 123 | + 0.01f); |
| 124 | +} |
| 125 | + |
| 126 | +// Test QuickGelu with custom alpha value on HTP |
| 127 | +TEST_F(QnnHTPBackendTests, QuickGelu_Custom_Alpha) { |
| 128 | + RunQuickGeluTest<float>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 129 | + 1.702f, // alpha |
| 130 | + ExpectedEPNodeAssignment::All, |
| 131 | + "htp"); |
| 132 | +} |
| 133 | + |
| 134 | +// Test QuickGelu with negative alpha value on HTP |
| 135 | +TEST_F(QnnHTPBackendTests, QuickGelu_Negative_Alpha) { |
| 136 | + RunQuickGeluTest<float>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 137 | + -1.702f, // alpha |
| 138 | + ExpectedEPNodeAssignment::All, |
| 139 | + "htp"); |
| 140 | +} |
| 141 | + |
| 142 | +TEST_F(QnnHTPBackendTests, QuickGelu_Float16_Default_Alpha) { |
| 143 | + RunQuickGeluTest<MLFloat16>(ConvertToFP16InputDef(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48))), |
| 144 | + 1.0f, |
| 145 | + ExpectedEPNodeAssignment::All, |
| 146 | + "htp", |
| 147 | + 0.01f); |
| 148 | +} |
| 149 | + |
| 150 | +// Test QuickGelu with float16 inputs and custom alpha on HTP |
| 151 | +TEST_F(QnnHTPBackendTests, QuickGelu_Float16_Custom_Alpha) { |
| 152 | + RunQuickGeluTest<MLFloat16>(ConvertToFP16InputDef(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48))), |
| 153 | + 1.702f, // alpha |
| 154 | + ExpectedEPNodeAssignment::All, |
| 155 | + "htp"); |
| 156 | +} |
| 157 | + |
| 158 | +// Test QuickGelu with float16 inputs and negative alpha on HTP |
| 159 | +TEST_F(QnnHTPBackendTests, QuickGelu_Float16_Negative_Alpha) { |
| 160 | + RunQuickGeluTest<MLFloat16>(ConvertToFP16InputDef(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48))), |
| 161 | + -1.702f, // alpha |
| 162 | + ExpectedEPNodeAssignment::All, |
| 163 | + "htp"); |
| 164 | +} |
| 165 | + |
| 166 | +// Test 8-bit QDQ QuickGelu with default alpha value on HTP |
| 167 | +TEST_F(QnnHTPBackendTests, QuickGelu_QDQ_U8_Default_Alpha) { |
| 168 | + RunQDQQuickGeluTest<uint8_t>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 169 | + 1.0f, // alpha |
| 170 | + ExpectedEPNodeAssignment::All); |
| 171 | +} |
| 172 | + |
| 173 | +// Test 8-bit QDQ QuickGelu with custom alpha value on HTP |
| 174 | +TEST_F(QnnHTPBackendTests, QuickGelu_QDQ_U8_Custom_Alpha) { |
| 175 | + RunQDQQuickGeluTest<uint8_t>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 176 | + 1.702f, // alpha |
| 177 | + ExpectedEPNodeAssignment::All); |
| 178 | +} |
| 179 | + |
| 180 | +// Test 16-bit QDQ QuickGelu with default alpha value on HTP |
| 181 | +TEST_F(QnnHTPBackendTests, QuickGelu_QDQ_U16_Default_Alpha) { |
| 182 | + RunQDQQuickGeluTest<uint16_t>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 183 | + 1.0f, // alpha |
| 184 | + ExpectedEPNodeAssignment::All, |
| 185 | + "htp", |
| 186 | + true); // Use com.microsoft Q/DQ ops |
| 187 | +} |
| 188 | + |
| 189 | +// Test 16-bit QDQ QuickGelu with custom alpha value on HTP |
| 190 | +TEST_F(QnnHTPBackendTests, QuickGelu_QDQ_U16_Custom_Alpha) { |
| 191 | + RunQDQQuickGeluTest<uint16_t>(TestInputDef<float>({1, 3, 4, 4}, false, GetFloatDataInRange(-10.0f, 10.0f, 48)), |
| 192 | + 1.702f, // alpha |
| 193 | + ExpectedEPNodeAssignment::All, |
| 194 | + "htp", |
| 195 | + true); // Use com.microsoft Q/DQ ops |
| 196 | +} |
| 197 | + |
| 198 | +#endif // defined(__aarch64__) || defined(_M_ARM64) || defined(__linux__) |
| 199 | + |
| 200 | +} // namespace test |
| 201 | +} // namespace onnxruntime |
| 202 | +#endif // !defined(ORT_MINIMAL_BUILD) |
0 commit comments