Skip to content

Commit c3320ed

Browse files
fxdawnnpytorchmergebot
authored andcommitted
[3.14] Add python version adjustment for frame count changes (pytorch#168190)
``sys.getrefcount(lib)`` got impacted due to python3.13 optimization. ``sys.getrefcount(lib._op_impls)`` and others remain the same. Test plan: ``python test/test_python_dispatch.py TestPythonRegistration.test_finalizer`` in local ``python=3.14`` env Pull Request resolved: pytorch#168190 Approved by: https://github.com/williamwen42, https://github.com/azahed98
1 parent bb4009a commit c3320ed

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

test/test_python_dispatch.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,12 @@ def test_finalizer(self):
299299
lib = Library(self.test_ns, "FRAGMENT") # noqa: TOR901
300300
lib.define("foo123(Tensor x) -> Tensor")
301301

302-
# 1 for `lib`, 1 for sys.getrefcount
303-
self.assertEqual(sys.getrefcount(lib), 2)
302+
# 1 for `lib`, 1 for sys.getrefcount' for previous python version (<=3.12)
303+
# In Python 3.13+, sys.getrefcount() was optimized to not create
304+
# a temporary reference, so expected counts are 1 less than before
305+
expected_refcount = 1 if sys.version_info >= (3, 14) else 2
306+
self.assertEqual(sys.getrefcount(lib), expected_refcount)
307+
304308
# We gained an additional reference that gets cleared when the finalizer runs
305309
self.assertEqual(sys.getrefcount(torch.library._impls), impls_refcnt + 1)
306310
# 1 for `lib`
@@ -318,15 +322,15 @@ def foo123(x):
318322
saved_op_impls = lib._op_impls
319323

320324
# del will definitely work if the following passes
321-
self.assertEqual(sys.getrefcount(lib), 2)
325+
self.assertEqual(sys.getrefcount(lib), expected_refcount)
322326
del lib
323327

324328
# 1 for saved_op_impls
325329
# 1 for sys.getrefcount
326330
# This function should be the last user of lib._op_impls:
327331
# - lib should not have a reference anymore (it was del'ed)
328332
# - lib's finalizer should not have a reference anymore
329-
self.assertEqual(sys.getrefcount(saved_op_impls), 2)
333+
self.assertEqual(sys.getrefcount(saved_op_impls), expected_refcount)
330334

331335
self.assertTrue(key not in torch.library._impls)
332336

0 commit comments

Comments
 (0)