Skip to content

Commit

Permalink
Update DML EP to accept broadcasted tensor of size 1 to match CPU (#1…
Browse files Browse the repository at this point in the history
…9081)

### Description
With QDQ enabled for Dml EP we are seeing some models not optimize
constant nodes with incorrect tensor size of scale[1] and zeropoint[1]
that does not match the input size. CPU accepts this parameter type so
updating Dml EP to match CPU behavior.



### Motivation and Context
Want to match CPU EP behavior.

---------

Co-authored-by: Christian Larson <[email protected]>
Co-authored-by: Dwayne Robinson <[email protected]>
  • Loading branch information
3 people authored Jan 11, 2024
1 parent daa22f9 commit 8a0a972
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,11 @@ class DmlOperatorElementwiseQLinear : public DmlOperator
{
ML_CHECK_VALID_ARGUMENT(axis < outputShapeDimCount);
uint32_t broadcastAxisLength = outputShape[axis];
ML_CHECK_VALID_ARGUMENT(inputTensorShape[0] == broadcastAxisLength);
ML_CHECK_VALID_ARGUMENT(
(inputTensorShape[0] == broadcastAxisLength) ||
// Treat as broadcast dimension to match CPU behavior.
(inputTensorShape[0] == 1)
);
inputTensorShape.insert(inputTensorShape.begin(), axis, 1);
inputTensorShape.insert(inputTensorShape.end(), outputShapeDimCount - 1 - axis, 1);
}
Expand Down
10 changes: 10 additions & 0 deletions onnxruntime/test/contrib_ops/quantize_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ TEST(DequantizeLinearOpTest, DequantizeLinear_per_tensor_float_int32_cpu) {
test.Run();
}

TEST(DequantizeLinearOpTest, DequantizeLinearOpTest_BroadcastTensorOfOne) {
OpTester test("DequantizeLinear", 1, onnxruntime::kMSDomain);

test.AddInput<int32_t>("x", {4}, {-30, -3, 100, 127});
test.AddInput<float>("x_scale", {1}, {2.0f}, true);
test.AddInput<int32_t>("zero_point", {1}, {0}, true);
test.AddOutput<float>("y", {4}, {-60.f, -6.f, 200.f, 254.f});
test.Run();
}

#ifdef USE_CUDA
TEST(DequantizeLinearOpTest, DequantizeLinear_per_tensor_half_uint8) {
OpTester test("DequantizeLinear", 1, onnxruntime::kMSDomain);
Expand Down
10 changes: 10 additions & 0 deletions onnxruntime/test/providers/cpu/tensor/quantize_linear_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ TEST(DequantizeLinearOpTest, Int32) {
test.Run();
}

TEST(DequantizeLinearOpTest_BroadcastTensor, Int32) {
OpTester test("DequantizeLinear", 13);
test.AddInput<int32_t>("x", {4}, {-30, -3, 100, 127});
test.AddAttribute<int64_t>("axis", 0);
test.AddInput<float>("x_scale", {1}, {2.0f});
test.AddInput<int32_t>("x_zero_point", {1}, {0});
test.AddOutput<float>("y", {4}, {-60.f, -6.f, 200.f, 254.f});
test.Run();
}

// 2d inputs
TEST(DequantizeLinearOpTest, 2D) {
OpTester test("DequantizeLinear", 10);
Expand Down

0 comments on commit 8a0a972

Please sign in to comment.