Implement “Soft Pooling”

In the traditional pooling layer, the side length during pooling is fixed.
At present, I want to try to implement pooling with variable side length, tentatively called “soft pooling”.

The details are as follows.

traditional pooling

“soft pooling”

How can I implement such a model layer?

How would this approach work, if more than two windows fit into each spatial dimension?
If the 3x3 kernels stays constant and just the last part if smaller, you could probably add padding on the right and down border of the input.