Skip to content

Commit

Permalink
Unit test for QDQ nodes w/ nonpositive scale around MaxPool
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollinswisc committed Jun 26, 2024
1 parent 2160e44 commit a36db30
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion onnxruntime/test/optimizer/qdq_transformer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ void QDQTransformerGemmTests(bool has_output_q, bool has_bias, bool beta_not_one
auto check_binary_op_graph = [&](InferenceSessionWrapper& session) {
auto op_to_count = CountOpsInGraph(session.GetGraph());
const QDQOpKeys qdq_keys = GetQDQOpKeys(use_contrib_qdq);
if ((!has_output_q || std::is_same_v<Input1Type, OutputType>)&&(!has_bias || (std::is_same_v<BiasType, int32_t> && !beta_not_one)) &&
if ((!has_output_q || std::is_same_v<Input1Type, OutputType>) && (!has_bias || (std::is_same_v<BiasType, int32_t> && !beta_not_one)) &&
(std::is_same_v<Input1Type, uint8_t> || std::is_same_v<Input2Type, int8_t>)) {
EXPECT_EQ(op_to_count["com.microsoft.QGemm"], 1);
EXPECT_EQ(op_to_count["Gemm"], 0);
Expand Down Expand Up @@ -979,6 +979,52 @@ TEST(QDQTransformerTests, ReshapeDropQDQ) {
RunReshapeDropQDQTestCase<uint16_t>({1, 3, 2, 2}, {1, 12}, false, 21); // Use int16 ONNX QDQ ops
}

// Runs a test case that checks if Q/DQ nodes are *not* dropped from DQ -> MaxPool -> Q if the quantization scale is
// negative.
template <typename QuantType>
static void RunMaxPoolNegativeScaleDropQDQTestCase() {
auto build_test_case = [](ModelTestBuilder& builder) {
constexpr QuantType qmin = std::numeric_limits<QuantType>::min();
constexpr QuantType qmax = std::numeric_limits<QuantType>::max();

const std::vector<int64_t> input_shape = {1, 17, 17, 3};
auto* input_arg = builder.MakeInput<QuantType>(input_shape, qmin, qmax);
auto* output_arg = builder.MakeOutput();

constexpr float scale = -0.003f;
QuantType zero_point = 1 + (qmax + qmin) / 2;

auto* input_arg_dq = builder.MakeIntermediate();
auto* maxpool_output = builder.MakeIntermediate();

builder.AddDequantizeLinearNode<QuantType>(input_arg, scale, zero_point, input_arg_dq);

Node& maxpool_node = builder.AddNode("MaxPool", {input_arg_dq}, {maxpool_output});
maxpool_node.AddAttribute("auto_pad", "VALID");
maxpool_node.AddAttribute("kernel_shape", std::vector<int64_t>({2, 2}));

builder.AddQuantizeLinearNode<QuantType>(maxpool_output, scale, zero_point, output_arg);
};

auto check_graph = [](InferenceSessionWrapper& session) {
auto op_to_count = CountOpsInGraph(session.GetGraph());
EXPECT_EQ(op_to_count["MaxPool"], 1);
EXPECT_EQ(op_to_count["QuantizeLinear"], 1);
EXPECT_EQ(op_to_count["DequantizeLinear"], 1);
};

constexpr int opset = 21;
TransformerTester(build_test_case, check_graph, TransformerLevel::Level1, TransformerLevel::Level2, opset);
}

// Checks that Q/DQ nodes are *not* dropped from DQ -> MaxPool -> Q for negative scale. Uses 8-bit and 16-bit Q/DQ ops.
TEST(QDQTransformerTests, MaxpoolDontDropQDQForNegativeScale) {
RunMaxPoolNegativeScaleDropQDQTestCase<int8_t>();
RunMaxPoolNegativeScaleDropQDQTestCase<uint8_t>();
RunMaxPoolNegativeScaleDropQDQTestCase<int16_t>();
RunMaxPoolNegativeScaleDropQDQTestCase<uint16_t>();
}

// Runs a test case that checks if Q/DQ nodes are dropped from DQ -> (Un)Squeeze -> Q.
template <typename QuantType>
static void RunSqueezeUnsqueezeDropQDQTestCase(const std::string& squeeze_type,
Expand Down

0 comments on commit a36db30

Please sign in to comment.