Skip to content

Commit 1aa5902

Browse files
adrianlizarragaashrit-ms
authored andcommitted
[QNN EP] workaround for QNN validation bug for Tanh with uint16 quantized output (#23432)
### Description - Skip QNN validation for Tanh with uint16 quantized output (workaround for QNN validation bug). - Re-enables unit test for Tanh with uint16 quantized output. The [QNN documentation](https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-50/HtpOpDefSupplement.html#tanh) states that the output scale and offset for `ufixed_point_16` should be (1/32768) and -32768, respectively. However, the QNN validator incorrectly rejects these values. So, we skip validation for this configuration of Tanh. Building an actual QNN graph with the correct scale/offset still works. ### Motivation and Context This QNN validation bug appeared in QNN SDK 2.28.0 and is still present in QNN SDK 2.30.0. A previous PR disabled the corresponding unit test: https://github.com/microsoft/onnxruntime/pull/22724/files#diff-57f590c6c548b073ba8cd8af6cf198799906f7059ea46b31cd33972ea9b01983R232
1 parent 7f5582a commit 1aa5902

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,22 @@ Status SimpleOpBuilder::ProcessAttributesAndOutputs(QnnModelWrapper& qnn_model_w
262262
if (node_unit.Domain() != kMSInternalNHWCDomain && (op_type == "DepthToSpace" || op_type == "SpaceToDepth" || op_type == "GridSample")) {
263263
return Status::OK();
264264
}
265+
266+
#if QNN_API_VERSION_MAJOR >= 2 && QNN_API_VERSION_MINOR >= 21 && QNN_API_VERSION_MINOR <= 23
267+
// Skip QNN validation for Tanh with uint16 (quantized) output.
268+
// This gets around a Tanh QNN validation bug in QNN SDK 2.28.0 - 2.30.0.
269+
// The QNN documentation states that the output scale and offset for ufixed_point_16 should be
270+
// (1/32768) and -32768, respectively. However, the QNN validator incorrectly rejects these values.
271+
if (op_type == "Tanh") {
272+
TensorInfo output_info = {};
273+
ORT_RETURN_IF_ERROR(qnn_model_wrapper.GetTensorInfo(node_unit.Outputs()[0], output_info));
274+
if (output_info.qnn_data_type == QNN_DATATYPE_UFIXED_POINT_16) {
275+
LOGS(logger, INFO) << "Skipping QNN validation for Tanh node '"
276+
<< node_unit.Name() << "' with quantized unit16 output.";
277+
return Status::OK();
278+
}
279+
}
280+
#endif
265281
}
266282

267283
std::vector<std::string> param_tensor_names;

onnxruntime/core/providers/qnn/qnn_allocator.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ size_t DivRoundUp(size_t a, size_t b) { // TODO is there already a helper funct
6060
}
6161

6262
bool IsAligned(const void* address, size_t alignment) {
63-
assert((alignment & alignment - 1) == 0); // alignment must be a power of two
63+
assert((alignment & (alignment - 1)) == 0); // alignment must be a power of two
6464
return (reinterpret_cast<uintptr_t>(address) & (alignment - 1)) == 0;
6565
}
6666

onnxruntime/test/providers/qnn/simple_op_htp_test.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,19 @@ TEST_F(QnnHTPBackendTests, UnaryOp_Tanh) {
230230
}
231231

232232
// disabled for QNN 2.28.0.241029 backendValidateOpConfig failed
233-
// still fails on QNN 2.28.2.
233+
// still fails on QNN 2.28.2 and QNN 2.30.0
234234
// QnnDsp <E> [4294967295] has incorrect Value -32768, expected equal to 0.
235235
// QnnDsp <V> validateNativeOps node_token_6:qti.aisw:Tanh htp op validator failed 3110
236236
// QnnDsp <V> registered validator failed => 3110
237237
// QnnDsp <E> QnnBackend_validateOpConfig failed 3110
238238
// QnnDsp <V> Wake up free backend (id: 1)'s thread(s)
239239
// QnnDsp <E> Failed to validate op node_token_6 with error 0xc26
240240
// Tests accuracy of 16-bit QDQ Tanh.
241-
TEST_F(QnnHTPBackendTests, DISABLED_UnaryOp_Tanh_U16) {
241+
//
242+
// We now skip QNN validation as a workaround for QNN SDK 2.28.0 to 2.30.0
243+
TEST_F(QnnHTPBackendTests, UnaryOp_Tanh_U16) {
242244
RunQDQOpTest<uint16_t>("Tanh",
243-
{TestInputDef<float>({1, 2, 3}, false, GetFloatDataInRange(-10.0f, 10.0f, 6))},
245+
{TestInputDef<float>({1, 2, 64}, false, GetFloatDataInRange(-10.0f, 10.0f, 128))},
244246
{},
245247
13,
246248
ExpectedEPNodeAssignment::All,

0 commit comments

Comments
 (0)