Skip to content

2D linear upsample with align_corners=False doesn't match PyTorch. #8897

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

Open
ysiraichi opened this issue Mar 27, 2025 · 3 comments
Open

2D linear upsample with align_corners=False doesn't match PyTorch. #8897

ysiraichi opened this issue Mar 27, 2025 · 3 comments
Labels
pytorch divergence XLA behavior doesn't match Pytorch eager frontend xla:gpu

Comments

@ysiraichi
Copy link
Collaborator

🐛 Bug

Running a 2D linear upsample yields in different results when comparing with PyTorch (both CPU and CUDA). That, however, only happens when align_corners=False.

def run(device):
    a = torch.arange(9, dtype=torch.float32, device=device).view(1, 1, 3, 3)
    m = Upsample(a, scale_factor=1.5, mode="bilinear", align_corners=False)
    return m(a)

>>> run("cpu")
tensor([[[[0.0000, 0.5000, 1.1667, 1.8333],
          [1.5000, 2.0000, 2.6667, 3.3333],
          [3.5000, 4.0000, 4.6667, 5.3333],
          [5.5000, 6.0000, 6.6667, 7.3333]]]],
       grad_fn=<UpsampleBilinear2DBackward0>)

>>> run("xla")
tensor([[[[0.0000, 0.6250, 1.3750, 2.0000],
          [1.8750, 2.5000, 3.2500, 3.8750],
          [4.1250, 4.7500, 5.5000, 6.1250],
          [6.0000, 6.6250, 7.3750, 8.0000]]]], device='xla:0',
       grad_fn=<UpsampleBilinear2DBackward0>)

PyTorch/XLA result seems to align with tensorflow (makes sense, since we took it from there), and OpenCV. However, PyTorch does seem to be the different one here.

Environment

  • Reproducible on XLA backend [CPU/TPU/CUDA]: CUDA
  • torch_xla version: 3808df0

Additional context

Found this when lowering the backwards (issue #8754). I tried taking the lowering from the same place and it looks like the gradient also matches only when align_corners=True.

This might not affect XLA:TPU, since it uses a custom lowering.

@ysiraichi ysiraichi added pytorch divergence XLA behavior doesn't match Pytorch eager frontend xla:gpu labels Mar 27, 2025
@ysiraichi
Copy link
Collaborator Author

Question: are all of these correct results of a different interpretation?

@ysiraichi
Copy link
Collaborator Author

ysiraichi commented Mar 27, 2025

Apparently, this mismatch is due to PyTorch using the scale_factor, while tensorflow (and OpenCV) uses the ratio between output and input sizes. If we call Upsample(..., recompute_scale_factor=True) we get the same result.

Thank you @vfdev-5 for helping me with this.

@ysiraichi
Copy link
Collaborator Author

I'm opening this issue, since with recompute_scale_factor=False, we still get different results.

@ysiraichi ysiraichi reopened this Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pytorch divergence XLA behavior doesn't match Pytorch eager frontend xla:gpu
Projects
None yet
Development

No branches or pull requests

1 participant