Skip to content

Commit 21ad004

Browse files
authored
Add QNN UTs for QNN Pad Op with FP16 data on HTP backend (microsoft#21142)
### Description 1. Add QNN UTs for QNN Pad Op with FP16 data on HTP backend 2. Improve Pad op builder to handle invalid optional input 3. Add UT for ReduceSum for FP16 precision with 5D for issue reproduce
1 parent 587e92c commit 21ad004

File tree

3 files changed

+198
-100
lines changed

3 files changed

+198
-100
lines changed

onnxruntime/core/providers/qnn/builder/opbuilder/pad_op_builder.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Status PadOpBuilder::ProcessInputs(QnnModelWrapper& qnn_model_wrapper,
5151
auto& pads_input_name = inputs[1].node_arg.Name();
5252
ORT_RETURN_IF_NOT(qnn_model_wrapper.IsInitializerInput(pads_input_name),
5353
"Qnn doesn't support dynamic pad input");
54-
if (node_unit.Inputs().size() > 2) {
54+
if (inputs.size() > 2 && inputs[2].node_arg.Exists()) {
5555
auto& constant_value_input_name = inputs[2].node_arg.Name();
5656
ORT_RETURN_IF_NOT(qnn_model_wrapper.IsInitializerInput(constant_value_input_name),
5757
"Qnn doesn't support dynamic constant_value input");
@@ -227,13 +227,13 @@ Status PadOpBuilder::ProcessAttributesAndOutputs(QnnModelWrapper& qnn_model_wrap
227227
param_tensor_names.push_back(mode_param.GetParamTensorName());
228228
qnn_model_wrapper.AddParamWrapper(std::move(mode_param));
229229

230-
QnnParamWrapper multiples_param(node_unit.Index(), node_unit.Name(), QNN_OP_PAD_PARAM_PAD_AMOUNT,
231-
std::move(pad_amount_dim), std::move(pad_amount));
232-
param_tensor_names.push_back(multiples_param.GetParamTensorName());
233-
qnn_model_wrapper.AddParamWrapper(std::move(multiples_param));
230+
QnnParamWrapper pad_amount_param(node_unit.Index(), node_unit.Name(), QNN_OP_PAD_PARAM_PAD_AMOUNT,
231+
std::move(pad_amount_dim), std::move(pad_amount));
232+
param_tensor_names.push_back(pad_amount_param.GetParamTensorName());
233+
qnn_model_wrapper.AddParamWrapper(std::move(pad_amount_param));
234234

235235
// Process optional input constant_value
236-
if (node_unit.Inputs().size() > 2) {
236+
if (inputs.size() > 2 && inputs[2].node_arg.Exists()) {
237237
ORT_RETURN_IF_ERROR(ProcessConstantValue(qnn_model_wrapper, param_tensor_names, node_unit, inputs[2]));
238238
} // constant_value
239239

onnxruntime/test/providers/qnn/pad_op_test.cpp

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,33 @@ static void RunPadOpTest(const TestInputDef<float>& data_def,
9898
const std::vector<ONNX_NAMESPACE::AttributeProto>& attrs,
9999
ExpectedEPNodeAssignment expected_ep_assignment,
100100
bool has_constant_value = true,
101-
int opset = 18) {
101+
int opset = 18,
102+
bool use_htp = false,
103+
bool enable_fp16_precision = false,
104+
float f32_abs_err = 1e-5f) {
102105
ProviderOptions provider_options;
106+
if (use_htp) {
103107
#if defined(_WIN32)
104-
provider_options["backend_path"] = "QnnCpu.dll";
108+
provider_options["backend_path"] = "QnnHtp.dll";
105109
#else
106-
provider_options["backend_path"] = "libQnnCpu.so";
110+
provider_options["backend_path"] = "libQnnHtp.so";
107111
#endif
112+
} else {
113+
#if defined(_WIN32)
114+
provider_options["backend_path"] = "QnnCpu.dll";
115+
#else
116+
provider_options["backend_path"] = "libQnnCpu.so";
117+
#endif
118+
}
119+
120+
if (enable_fp16_precision) {
121+
provider_options["enable_htp_fp16_precision"] = "1";
122+
}
108123

109124
RunQnnModelTest(BuildPadTestCase(data_def, pads_def, constant_value_def, attrs, has_constant_value),
110125
provider_options,
111126
opset,
112-
expected_ep_assignment);
127+
expected_ep_assignment, f32_abs_err);
113128
}
114129

115130
// Runs a QDQ Pad model on the QNN HTP backend. Checks the graph node assignment, and that inference
@@ -229,6 +244,60 @@ TEST_F(QnnCPUBackendTests, Pad6d) {
229244
#if defined(__aarch64__) || defined(_M_ARM64) || defined(__linux__)
230245
//
231246
// HTP tests:
247+
TEST_F(QnnHTPBackendTests, PadNoConstantValue_fp16_test) {
248+
bool has_constant_value_input = false;
249+
bool use_htp = true;
250+
bool enable_fp16_precision = true;
251+
RunPadOpTest(TestInputDef<float>({3, 2}, false, {1.0f, 1.2f, 2.3f, 3.4f, 4.5f, 5.6f}),
252+
TestInputDef<int64_t>({4}, true, {0, 2, 0, 0}),
253+
TestInputDef<float>({1}, true, {0.0f}),
254+
{utils::MakeAttribute("mode", "constant")},
255+
ExpectedEPNodeAssignment::All,
256+
has_constant_value_input,
257+
18, // opset
258+
use_htp,
259+
enable_fp16_precision,
260+
2e-3f);
261+
}
262+
263+
TEST_F(QnnHTPBackendTests, PadReflectMode_fp16) {
264+
bool has_constant_value_input = false;
265+
bool use_htp = true;
266+
bool enable_fp16_precision = true;
267+
RunPadOpTest(TestInputDef<float>({3, 2}, false, {1.0f, 1.2f, 2.3f, 3.4f, 4.5f, 5.6f}),
268+
TestInputDef<int64_t>({4}, true, {0, 1, 0, 0}),
269+
TestInputDef<float>({1}, true, {0.0f}),
270+
{utils::MakeAttribute("mode", "reflect")},
271+
ExpectedEPNodeAssignment::All,
272+
has_constant_value_input,
273+
18, // opset
274+
use_htp,
275+
enable_fp16_precision,
276+
2e-3f);
277+
}
278+
279+
// HTP\HTP\src\hexagon\prepare\graph_prepare.cc:203:ERROR:could not create op: q::flat_from_vtcm
280+
// HTP\HTP\src\hexagon\prepare\graph_prepare.cc:1238:ERROR:Op 0x104100000011 preparation failed with err:-1
281+
// Completed stage: Graph Transformations and Optimizations (13372 us)
282+
// QnnDsp <E> "node" generated: could not create op
283+
// QnnDsp <E> RouterWindows graph prepare failed 12
284+
// QnnDsp <E> Failed to finalize graph (id: 1) with err 1002
285+
TEST_F(QnnHTPBackendTests, DISABLED_PadReflectMode_FP16_big_data) {
286+
bool has_constant_value_input = false;
287+
bool use_htp = true;
288+
bool enable_fp16_precision = true;
289+
RunPadOpTest(TestInputDef<float>({1, 4, 512, 512}, false, GetFloatDataInRange(1.0f, 10.0f, 4 * 512 * 512)),
290+
TestInputDef<int64_t>({8}, true, {0, 0, 3, 3, 0, 0, 3, 3}),
291+
TestInputDef<float>({1}, true, {0.0f}),
292+
{utils::MakeAttribute("mode", "reflect")},
293+
ExpectedEPNodeAssignment::All,
294+
has_constant_value_input,
295+
18, // opset
296+
use_htp,
297+
enable_fp16_precision,
298+
2e-3f);
299+
}
300+
232301
//
233302
// QDQ Pad
234303
TEST_F(QnnHTPBackendTests, PadNoConstantValue) {

0 commit comments

Comments
 (0)