Skip to content

Support divisor_override for avg_pool2d and avg_pool3d (fixes #2745)#2748

Open
ArsalanShakil wants to merge 1 commit into
apple:mainfrom
ArsalanShakil:avg-pool-divisor-override
Open

Support divisor_override for avg_pool2d and avg_pool3d (fixes #2745)#2748
ArsalanShakil wants to merge 1 commit into
apple:mainfrom
ArsalanShakil:avg-pool-divisor-override

Conversation

@ArsalanShakil

Copy link
Copy Markdown

Summary

Fixes #2745.

The PyTorch frontend previously rejected avg_pool2d and avg_pool3d whenever divisor_override was provided, raising:

ValueError: divisor_override is not supported for avg_pool2d

This PR adds support for a static, positive divisor_override.

Approach

MIL's avg_pool has no divisor_override option, so the value is reconstructed:

  1. Average over the full kernel (padding counted as zeros via exclude_padding_from_average=False), which makes the divisor a known constant equal to the kernel volume. The window sum is therefore avg_pool_result * num_kernel_elements.
  2. Rescale by num_kernel_elements / divisor_override, reproducing PyTorch's window_sum / divisor_override semantics.

A non-positive divisor_override raises a clear ValueError.

Tests

Added test_avg_pool2d_divisor_override and test_avg_pool3d_divisor_override, parametrized over kernel/stride/padding combinations and divisor values, across the TorchScript, TorchExport, and ExecuTorch frontends and the fp16/fp32 backends. All new and existing TestAvgPool cases pass and numerically match PyTorch.

)

The PyTorch frontend previously raised a ValueError whenever avg_pool2d or
avg_pool3d was given a divisor_override. Implement support by averaging over
the full kernel (counting padding as zeros, so the divisor is the constant
kernel volume) and rescaling the result by num_kernel_elements /
divisor_override, which reproduces PyTorch's window_sum / divisor_override
semantics. Add converter tests covering 2D/3D across the TorchScript,
TorchExport and ExecuTorch frontends.
@TobyRoseman

Copy link
Copy Markdown
Collaborator

CI: https://gitlab.com/coremltools1/coremltools/-/pipelines/2621404318

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.

[PyTorch] Support divisor_override for avg_pool2d and avg_pool3d

2 participants