Refine auto_pad based pad computation in ConvTranspose (#6305)

This commit is contained in:
Hariharan Seshadri 2021-01-20 08:31:49 +05:30 committed by GitHub
parent ac36596fb8
commit d7bdd96425
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 90 additions and 6 deletions

View file

@ -222,7 +222,7 @@
"component": {
"type": "git",
"git": {
"commitHash": "bc7a70b46f4ae88383a4ca49c477fd859715620c",
"commitHash": "174de7d086a768cba29374a56a7461eff87cfdb3",
"repositoryUrl": "https://github.com/onnx/onnx"
},
"comments": "git submodule at cmake/external/onnx"

2
cmake/external/onnx vendored

@ -1 +1 @@
Subproject commit bc7a70b46f4ae88383a4ca49c477fd859715620c
Subproject commit 174de7d086a768cba29374a56a7461eff87cfdb3

View file

@ -88,6 +88,9 @@ inline Status ComputePad(const int64_t in_dim,
return Status(common::ONNXRUNTIME, common::INVALID_ARGUMENT,
"Dilation not supported for AutoPadType::SAME_UPPER or AutoPadType::SAME_LOWER.");
// The ONNX spec says if `auto_pad` attribute is set, pad until the `legacy_target_size`
// is `ceil (in_dim / stride)`. The following line of code is essentially just that and
// is retained as is
int64_t legacy_target_size = (in_dim + stride - 1) / stride;
int64_t pad_needed = (legacy_target_size - 1) * stride + kernel - in_dim;
// make sure padding is symmetric

View file

@ -210,9 +210,10 @@ struct ConvTransposeAttributes : public ConvAttributes {
// Compute padding if the auto_pad attribute is SAME_UPPER/SAME_LOWER
if (pad_type == AutoPadType::SAME_UPPER || pad_type == AutoPadType::SAME_LOWER) {
// total pad
// The ONNX spec says if `auto_pad` attribute is set, pad until the `out_size`
// is `in_size * stride`
auto total_pad = ComputeTotalPad(in_size, stride, adj,
kernel, dilation, in_size);
kernel, dilation, /*out_size = */ in_size * stride);
DistributePadding(pad_type, total_pad, *pad_head, *pad_tail);
}

View file

@ -678,5 +678,40 @@ TEST(ConvTest, Conv1D_asymmetric_padding) {
TestConvOp(attrs, {X, W, B}, {X_shape, W_shape, B_shape}, expected_vals, Y_shape, true);
}
TEST(ConvTest, Conv_AutoPad_with_non_default_strides) {
ConvOpAndTestAttributes attrs = {
"SAME_LOWER", // auto_pad
vector<int64_t>{1, 1}, // dilations
1, // group
vector<int64_t>{3, 3}, // kernel_shape
vector<int64_t>{}, // pads
vector<int64_t>{2, 2}, // strides
{} // excluded EPs
};
vector<float> X = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f,
5.0f, 6.0f, 7.0f, 8.0f, 9.0f,
10.0f, 11.0f, 12.0f, 13.0f, 14.0f,
15.0f, 16.0f, 17.0f, 18.0f,
19.0f, 20.0f, 21.0, 22.0f, 23.0f, 24.0f};
vector<int64_t> X_shape = {1, 1, 5, 5};
vector<float> W = {1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f};
vector<int64_t> W_shape = {1, 1, 3, 3};
auto expected_vals = {12.0f, 27.0f, 24.0f,
63.0f, 108.0f, 81.0f,
72.0f, 117.0f, 84.0f};
vector<int64_t> Y_shape = {1, 1, 3, 3};
// Test with weight as initializer
TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape);
// Test with weight as initializer
TestConvOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape, true);
}
} // namespace test
} // namespace onnxruntime

View file

@ -803,5 +803,50 @@ TEST(ConvTransposeTest, ConvTranspose_1D_AutoPad_SameLower) {
OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider});
}
TEST(ConvTransposeTest, ConvTranspose_AutoPad_with_non_default_strides) {
ConvTransposeOpAttributes attrs = {
vector<int64_t>{3, 3}, // kernel_shape
{}, // output_padding
{}, // output_shape
{}, // pads
vector<int64_t>{2, 2}, // strides
vector<int64_t>{1, 1}, // dilations
1, // group
"SAME_LOWER" // auto_pad
};
vector<float> X = {0.0f, 1.0f, 2.0f,
3.0f, 4.0f, 5.0f,
6.0f, 7.0f, 8.0f};
vector<int64_t> X_shape = {1, 1, 3, 3};
vector<float> W = {1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f};
vector<int64_t> W_shape = {1, 2, 3, 3};
auto expected_vals = {0.0f, 0.0f, 1.0f, 1.0f, 3.0f, 2.0f,
0.0f, 0.0f, 1.0f, 1.0f, 3.0f, 2.0f,
3.0f, 3.0f, 8.0f, 5.0f, 12.0f, 7.0f,
3.0f, 3.0f, 7.0f, 4.0f, 9.0f, 5.0f,
9.0f, 9.0f, 20.0f, 11.0f, 24.0f, 13.0f,
6.0f, 6.0f, 13.0f, 7.0f, 15.0f, 8.0f,
0.0f, 0.0f, 1.0f, 1.0f, 3.0f, 2.0f,
0.0f, 0.0f, 1.0f, 1.0f, 3.0f, 2.0f,
3.0f, 3.0f, 8.0f, 5.0f, 12.0f, 7.0f,
3.0f, 3.0f, 7.0f, 4.0f, 9.0f, 5.0f,
9.0f, 9.0f, 20.0f, 11.0f, 24.0f, 13.0f,
6.0f, 6.0f, 13.0f, 7.0f, 15.0f, 8.0f};
vector<int64_t> Y_shape = {1, 2, 6, 6};
TestConvTransposeOp(attrs, {X, W}, {X_shape, W_shape}, expected_vals, Y_shape,
OpTester::ExpectResult::kExpectSuccess, "", {kTensorrtExecutionProvider});
}
} // namespace test
} // namespace onnxruntime

View file

@ -3,7 +3,7 @@ mypy
pytest
setuptools>=41.4.0
wheel
git+http://github.com/onnx/onnx.git@bc7a70b46f4ae88383a4ca49c477fd859715620c#egg=onnx
git+http://github.com/onnx/onnx.git@174de7d086a768cba29374a56a7461eff87cfdb3#egg=onnx
protobuf
sympy==1.1.1
flake8

View file

@ -4,7 +4,7 @@ mypy
pytest
setuptools>=41.4.0
wheel
git+http://github.com/onnx/onnx.git@bc7a70b46f4ae88383a4ca49c477fd859715620c#egg=onnx
git+http://github.com/onnx/onnx.git@174de7d086a768cba29374a56a7461eff87cfdb3#egg=onnx
argparse
sympy==1.1.1
flake8