When using keras.layers.MaxPooling2D with PyTorch backend, there is an inconsistent execution result between static inference shape and dynamic results.

import os
import re
import torch
import numpy as np
os.environ['KERAS_BACKEND']='torch'
import keras

layer = keras.layers.MaxPooling2D(
    pool_size=[ 2, 3 ],
    strides=[ 3, 3 ],
    padding="same",
    data_format="channels_first",
    trainable=True,
    autocast=True,
)

result_static = layer.compute_output_shape([1, 5, 5, 4])

result_dynamic = layer(
    inputs=np.random.rand(*[1, 5, 5, 4]),
)

The version is keras 3.5.0 with PyTorch 2.4.0 And I got the results below

result_static: 
(1, 5, 2, 2)

result_dynamic.shape:
torch.Size([1, 5, 2, 1])

Comment From: sachinprasadhs

Thanks for reporting the issue, on investigation it was found out that this happens with all the layers in torch which uses padding="same". It was kind of edge case scenario with only certain combination like you provided would catch the error. Created a PR to fix the same.

Comment From: dhantule

Hi @MilkFiish, thanks for reporting this.

This issues seems to be fixed in this PR, I have tested your code with the keras 3.10.0 and it's working fine in this gist.

Comment From: github-actions[bot]

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.