mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-21 05:34:18 +08:00
Fixed a memory leak when calling from_numpy on a numpy array with an … (#121156)
…unsupported dtype. Fixes #121138. The lambda function that DECREFs the object is not called when the dtype conversion functions throws. This PR moves the conversion before the INCREF, which prevents the memory leak. Pull Request resolved: https://github.com/pytorch/pytorch/pull/121156 Approved by: https://github.com/soulitzer, https://github.com/albanD
This commit is contained in:
committed by
PyTorch MergeBot
parent
360761f7d0
commit
76f3663efe
@ -6,6 +6,7 @@ import torch
|
||||
import numpy as np
|
||||
|
||||
from itertools import product
|
||||
import sys
|
||||
|
||||
from torch.testing._internal.common_utils import \
|
||||
(skipIfTorchDynamo, TestCase, run_tests)
|
||||
@ -257,6 +258,18 @@ class TestNumPyInterop(TestCase):
|
||||
x.strides = (3,)
|
||||
self.assertRaises(ValueError, lambda: torch.from_numpy(x))
|
||||
|
||||
@skipIfTorchDynamo("No need to test invalid dtypes that should fail by design.")
|
||||
def test_from_numpy_no_leak_on_invalid_dtype(self):
|
||||
# This used to leak memory as the `from_numpy` call raised an exception and didn't decref the temporary
|
||||
# object. See https://github.com/pytorch/pytorch/issues/121138
|
||||
x = np.array("value".encode('ascii'))
|
||||
for _ in range(1000):
|
||||
try:
|
||||
torch.from_numpy(x)
|
||||
except TypeError:
|
||||
pass
|
||||
self.assertTrue(sys.getrefcount(x) == 2)
|
||||
|
||||
@skipMeta
|
||||
def test_from_list_of_ndarray_warning(self, device):
|
||||
warning_msg = r"Creating a tensor from a list of numpy.ndarrays is extremely slow"
|
||||
|
Reference in New Issue
Block a user