[WebNN EP] Fix bug of padding in Op ConvTranspose (#18577)

Get the dimensions of H and W according to the layout.
This commit is contained in:
zesongw 2023-12-01 04:59:36 +08:00 committed by GitHub
parent b1e749e3be
commit 4025bd8ebd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -251,8 +251,18 @@ Status ConvOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const N
std::vector<int64_t> input_shape;
ORT_RETURN_IF_NOT(GetShape(*input_defs[0], input_shape, logger), "Cannot get shape");
for (size_t i = 0; i < 2; i++) {
total_padding[i] = strides[i] * (narrow<size_t>(input_shape[i + 1]) - 1) +
output_padding[i] + ((kernel_shape[i] - 1) * dilations[i] + 1) - output_shape[i];
// Get the dimensions of H and W.
// For NHWC layout, the dimensions of H and W correspond to index 1 and 2.
// For NCHW layout, the dimensions of H and W correspond to index 2 and 3.
if (model_builder.GetPreferredLayout() == DataLayout::NHWC) {
total_padding[i] = strides[i] * (narrow<size_t>(input_shape[i + 1]) - 1) +
output_padding[i] + ((kernel_shape[i] - 1) * dilations[i] + 1) - output_shape[i];
} else {
ORT_RETURN_IF_NOT(model_builder.GetPreferredLayout() == DataLayout::NCHW,
"WebNN GPU backend preferred layout should be NCHW.");
total_padding[i] = strides[i] * (narrow<size_t>(input_shape[i + 2]) - 1) +
output_padding[i] + ((kernel_shape[i] - 1) * dilations[i] + 1) - output_shape[i];
}
}
pads[0] = total_padding[0] - (total_padding[0] / 2);
pads[1] = total_padding[0] / 2;