Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Fix AvgPool and MaxPool for 1x1 kernels #6969

Merged
merged 11 commits into from
Oct 24, 2022

Conversation

mattsoulanille
Copy link
Member

@mattsoulanille mattsoulanille commented Oct 22, 2022

XNNPack does not support 1x1 kernels for AvgPool or MaxPool. Implement these cases manually, including support for strides.

Support for padding is still TODO.

#6867 could be fixed by this issue, but I've yet to confirm that.

To see the logs from the Cloud Build CI, please join either our discussion or announcement mailing list.


This change is Reviewable

Copy link
Collaborator

@Linchenn Linchenn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just some nits. Thank you!

tfjs-backend-wasm/src/cc/util.cc Outdated Show resolved Hide resolved
@@ -152,6 +152,15 @@ const std::vector<size_t> assert_and_get_broadcast_shape(
const std::vector<size_t> get_broadcast_dims(
const std::vector<size_t> in_shape, const std::vector<size_t> out_shape);

// Generates the output for AvgPool, MaxPool, etc where xnnpack does not support
// a 1x1 filter. Applies batching, channels, and strides.
// TODO(mattsoulanille): Support padding.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we either add an error in identity_pool or add a condition in the references for cases padding != 0? Otherwise, this will silently produce a wrong result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some more reading on AvgPool padding, and it seems like it's meaningless for the 1x1 kernel case.

Unlike for convolution ops, padding for AvgPool and MaxPool does not take into account the padded values when computing the result at a given index. It only uses the values that are part of the original tensor (and padding just allows it to compute at a given index instead of returning a tensor of smaller dimension than the input). Pooling with a 1x1 kernel will always output the same dimension as the input, so padding should not affect the result.

I also think I discovered a bug in the CPU backend; Padding a 1x1 kernel can result in NaNs in the output tensor.

As far as I can tell right now, the wasm version is actually correct to completely ignore the padding (for 1x1), so I'll remove the TODO.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Thank you!

Copy link
Collaborator

@jinjingforever jinjingforever left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you fixing the bug and adding tests!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants