Handling overflow for long int overflow for the product of kernel_hei… (#155989)

…ght and kernel_width that overflows to be exactly 0

Fixes [#155981](https://github.com/pytorch/pytorch/issues/155981)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/155989
Approved by: https://github.com/malfet
This commit is contained in:
arkadip-maitra
2025-09-19 18:14:57 +00:00
committed by PyTorch MergeBot
parent 607469bdad
commit a0d2d84846
2 changed files with 17 additions and 0 deletions

View File

@ -7111,6 +7111,14 @@ tensor(..., device='meta', size=(1,), requires_grad=True)""")
unfold = nn.Unfold(kernel_size=(1, 3), padding=(1, 1), dilation=(1, 2))
unfold(torch.randn(1, 2, 2, 2))
with self.assertRaisesRegex(RuntimeError, r"the product of kernel_width and kernel_height overflowed"):
tensor_data = torch.tensor([
[1.4009e-03, -1.3341e-32, -1.3334e-32, -1.3341e-32, 1.2723e-38, 3.6334e+00, 1.5374e-02],
[-1.5525e-02, 9.2391e-29, -2.5615e-13, -1.3322e-32, -1.3341e-32, -1.3341e-32, -1.3341e-32],
[-1.3341e-32, -1.3341e-32, -1.3341e-32, 3.0466e+14, 2.3677e+14, 2.3677e+14, 2.3677e+14],
])
F.fold(tensor_data, 16, 7318349394477056)
def test_softmin(self):
x = torch.randn(2, 16)
self.assertEqual(F.softmin(x, 1), F.softmax(-x, 1))