Skip to content

Conversation

@guangyey
Copy link
Collaborator

@guangyey guangyey commented Jul 7, 2025

Stack from ghstack (oldest at bottom):

Motivation

#155451 decoupled torch._C._storage_Use_Count from CUDA and introduced a corresponding unit test:

pytorch/test/test_torch.py

Lines 257 to 262 in 815545f

def test_storage_use_count(self, device):
a = torch.randn(10, device=device)
prev_cf = torch._C._storage_Use_Count(a.untyped_storage()._cdata)
self.assertEqual(prev_cf, 1)
b = a.view(2, 5)
self.assertEqual(torch._C._storage_Use_Count(b.untyped_storage()._cdata), prev_cf + 1)

However, this test fails when PyTorch is built with debug assertions enabled. @clee2000 disabled this UT in #156731. The root cause is that _cdata is obtained from an intrusive_ptr, not a weak_intrusive_ptr. As a result, calling c10::weak_intrusive_ptr::use_count on it triggers the internal assertion:
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(
owning_weak_ptr == NullType::singleton() ||
owning_weak_ptr->weakcount_.load() > 1 ||
(owning_weak_ptr->refcount_.load() == 0 &&
owning_weak_ptr->weakcount_.load() > 0),
"weak_intrusive_ptr: Can only weak_intrusive_ptr::reclaim() owning pointers that were created using weak_intrusive_ptr::release().");

For example:

a = torch.randn(10, device=device) # refcount=1, weakcount=1
prev_cf = torch._C._storage_Use_Count(a.untyped_storage()._cdata) # violate the assertation

This violates the expected invariant inside weak_intrusive_ptr::use_count, which assumes the pointer was originally constructed from a valid weak_intrusive_ptr. Actually, storage_impl is obtained from an intrusive_ptr.

pytorch/torch/csrc/Module.cpp

Lines 2105 to 2109 in 815545f

py_module.def("_storage_Use_Count", [](size_t storage_impl_ptr) {
// NOLINTNEXTLINE(performance-no-int-to-ptr)
c10::StorageImpl* storage_impl = (c10::StorageImpl*)storage_impl_ptr;
return c10::raw::weak_intrusive_ptr::use_count(storage_impl);
});

Solution

Use c10::intrusive_ptr::use_count instead.

@pytorch-bot
Copy link

pytorch-bot bot commented Jul 7, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/157694

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Cancelled Job, 1 Unrelated Failure

As of commit 25db3ac with merge base f8c0a4b (image):

CANCELLED JOB - The following job was cancelled. Please retry:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

guangyey added a commit that referenced this pull request Jul 7, 2025
ghstack-source-id: ca0e9ef
Pull Request resolved: #157694
@guangyey guangyey requested review from albanD and clee2000 July 7, 2025 06:10
@guangyey guangyey added the topic: bug fixes topic category label Jul 7, 2025
@guangyey guangyey added the topic: not user facing topic category label Jul 7, 2025
@guangyey guangyey requested review from EikanWang and gujinghui July 7, 2025 07:03
Copy link
Collaborator

@albanD albanD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

[ghstack-poisoned]
@guangyey
Copy link
Collaborator Author

guangyey commented Jul 8, 2025

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 8, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-jammy-rocm-py3.10 / test (default, 1, 2, linux.rocm.gpu.2)

Details for Dev Infra team Raised by workflow job

@guangyey
Copy link
Collaborator Author

guangyey commented Jul 8, 2025

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-jammy-rocm-py3.10 / test (default, 1, 2, linux.rocm.gpu.2)

Details for Dev Infra team Raised by workflow job

@guangyey
Copy link
Collaborator Author

guangyey commented Jul 8, 2025

@pytorchbot merge -f "skip rocm queued CI"

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@github-actions github-actions bot deleted the gh/guangyey/164/head branch August 8, 2025 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged open source topic: bug fixes topic category topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants