keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.68k stars 19.42k forks source link

Inconsistent execution results by the PyTorch backend #20235

Open MilkFiish opened 2 weeks ago

MilkFiish commented 2 weeks ago

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])
sachinprasadhs commented 1 week ago

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.