Skip to content

Commit

Permalink
[WebNN EP] Fix bug of padding in Op ConvTranspose (#18577)
Browse files Browse the repository at this point in the history
Get the dimensions of H and W according to the layout.
  • Loading branch information
zesongw authored Nov 30, 2023
1 parent b1e749e commit 4025bd8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions onnxruntime/core/providers/webnn/builders/impl/conv_op_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4025bd8

Please sign in to comment.