diff --git a/onnxruntime/core/optimizer/qdq_transformer/qdq_util.cc b/onnxruntime/core/optimizer/qdq_transformer/qdq_util.cc index 221c06d7c8dcf..b1ab641a23256 100644 --- a/onnxruntime/core/optimizer/qdq_transformer/qdq_util.cc +++ b/onnxruntime/core/optimizer/qdq_transformer/qdq_util.cc @@ -54,9 +54,26 @@ bool IsQDQPairSupported( Initializer dq_zp(*dq_zp_tensor_proto, model_path); Initializer dq_scale(*dq_scale_tensor_proto, model_path); - return q_zp.data_type() == dq_zp.data_type() && - SpanEq(q_zp.DataAsByteSpan(), dq_zp.DataAsByteSpan()) && - *q_scale.data() == *dq_scale.data(); + if (q_zp.data_type() != dq_zp.data_type() || + q_scale.data_type() != q_scale.data_type() || + !SpanEq(q_zp.DataAsByteSpan(), dq_zp.DataAsByteSpan())) { + return false; + } + + switch (q_scale.data_type()) { + case ONNX_NAMESPACE::TensorProto::FLOAT: + return *q_scale.data() == *dq_scale.data(); + + case ONNX_NAMESPACE::TensorProto::FLOAT16: + return *q_scale.data() == *dq_scale.data(); + + case ONNX_NAMESPACE::TensorProto::BFLOAT16: + return *q_scale.data() == *dq_scale.data(); + + default: + assert(false); + return false; + } } bool IsDQSupported(const Node& dq_node, const GetConstantInitializerFn& get_const_initializer) { diff --git a/onnxruntime/test/optimizer/graph_transform_test.cc b/onnxruntime/test/optimizer/graph_transform_test.cc index ef6e2d531bc1a..5adcb3c150b8d 100755 --- a/onnxruntime/test/optimizer/graph_transform_test.cc +++ b/onnxruntime/test/optimizer/graph_transform_test.cc @@ -4602,38 +4602,43 @@ TEST_F(GraphTransformationTests, GeluApproximation_SessionOptionConfig) { } // Test DoubleQDQPairsRemover to remove unnecessary DQ->Q nodes in the middle -TEST_F(GraphTransformationTests, DoublQDQRemover_RemoveDupQDQ) { - constexpr const ORTCHAR_T* model_uri = MODEL_FOLDER "qdq_optimization/dup_qdq.onnx"; - std::shared_ptr p_model; - ASSERT_STATUS_OK(Model::Load(model_uri, p_model, nullptr, *logger_)); - Graph& graph = p_model->MainGraph(); +TEST_F(GraphTransformationTests, DoublQDQRemover_RemoveDupQDQ_Float16) { + auto RunTest = [this](const ORTCHAR_T* model_uri) { + std::shared_ptr p_model; + ASSERT_STATUS_OK(Model::Load(model_uri, p_model, nullptr, *logger_)); + Graph& graph = p_model->MainGraph(); - onnxruntime::GraphTransformerManager graph_transformation_mgr{5}; - ASSERT_STATUS_OK(graph_transformation_mgr.Register(std::make_unique(), TransformerLevel::Level1)); - ASSERT_STATUS_OK(graph_transformation_mgr.ApplyTransformers(graph, TransformerLevel::Level1, *logger_)); + onnxruntime::GraphTransformerManager graph_transformation_mgr{5}; + ASSERT_STATUS_OK(graph_transformation_mgr.Register(std::make_unique(), TransformerLevel::Level1)); + ASSERT_STATUS_OK(graph_transformation_mgr.ApplyTransformers(graph, TransformerLevel::Level1, *logger_)); - std::map op_to_count = CountOpsInGraph(graph); - EXPECT_EQ(op_to_count["QuantizeLinear"], 3); - EXPECT_EQ(op_to_count["DequantizeLinear"], 4); + std::map op_to_count = CountOpsInGraph(graph); + EXPECT_EQ(op_to_count["QuantizeLinear"], 3); + EXPECT_EQ(op_to_count["DequantizeLinear"], 4); - std::string dq_scale_name_before_reshape_node; - std::string zp_name_before_reshape_node; - std::string dq_scale_name_after_reshape_node; - std::string zp_name_after_reshape_node; - for (auto& node : graph.Nodes()) { - if (node.Name() == "dq_2") { - dq_scale_name_before_reshape_node = node.InputDefs()[QDQ::InputIndex::SCALE_ID]->Name(); - zp_name_before_reshape_node = node.InputDefs()[QDQ::InputIndex::ZERO_POINT_ID]->Name(); - } - if (node.Name() == "q_3") { - dq_scale_name_after_reshape_node = node.InputDefs()[QDQ::InputIndex::SCALE_ID]->Name(); - zp_name_after_reshape_node = node.InputDefs()[QDQ::InputIndex::ZERO_POINT_ID]->Name(); + std::string dq_scale_name_before_reshape_node; + std::string zp_name_before_reshape_node; + std::string dq_scale_name_after_reshape_node; + std::string zp_name_after_reshape_node; + for (auto& node : graph.Nodes()) { + if (node.Name() == "dq_2") { + dq_scale_name_before_reshape_node = node.InputDefs()[QDQ::InputIndex::SCALE_ID]->Name(); + zp_name_before_reshape_node = node.InputDefs()[QDQ::InputIndex::ZERO_POINT_ID]->Name(); + } + if (node.Name() == "q_3") { + dq_scale_name_after_reshape_node = node.InputDefs()[QDQ::InputIndex::SCALE_ID]->Name(); + zp_name_after_reshape_node = node.InputDefs()[QDQ::InputIndex::ZERO_POINT_ID]->Name(); + } } - } - EXPECT_EQ(dq_scale_name_before_reshape_node.empty(), false); - EXPECT_EQ(zp_name_before_reshape_node.empty(), false); - EXPECT_EQ(dq_scale_name_before_reshape_node, dq_scale_name_after_reshape_node); - EXPECT_EQ(zp_name_before_reshape_node, zp_name_after_reshape_node); + EXPECT_EQ(dq_scale_name_before_reshape_node.empty(), false); + EXPECT_EQ(zp_name_before_reshape_node.empty(), false); + EXPECT_EQ(dq_scale_name_before_reshape_node, dq_scale_name_after_reshape_node); + EXPECT_EQ(zp_name_before_reshape_node, zp_name_after_reshape_node); + }; + + RunTest(MODEL_FOLDER "qdq_optimization/dup_qdq.onnx"); + RunTest(MODEL_FOLDER "qdq_optimization/dup_qdq_float16.onnx"); + RunTest(MODEL_FOLDER "qdq_optimization/dup_qdq_bfloat16.onnx"); } // Test Gelu -> FastGelu diff --git a/onnxruntime/test/testdata/transform/qdq_optimization/dup_qdq_bfloat16.onnx b/onnxruntime/test/testdata/transform/qdq_optimization/dup_qdq_bfloat16.onnx new file mode 100644 index 0000000000000..a04b7781fca40 Binary files /dev/null and b/onnxruntime/test/testdata/transform/qdq_optimization/dup_qdq_bfloat16.onnx differ diff --git a/onnxruntime/test/testdata/transform/qdq_optimization/dup_qdq_float16.onnx b/onnxruntime/test/testdata/transform/qdq_optimization/dup_qdq_float16.onnx new file mode 100644 index 0000000000000..691da77969b1c Binary files /dev/null and b/onnxruntime/test/testdata/transform/qdq_optimization/dup_qdq_float16.onnx differ