Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10057,6 +10057,19 @@ def __del__(self):
self.assertEqual(MyStorage.finalized_count, 1)
self.assertTrue(m[0])

@unittest.skipIf(not torch.cuda.is_available(), "_storage_Use_Count only registered when CUDA is available")
def test_storage__use_count_APIs_align(self):
a = torch.rand(2, 3)
s = a.untyped_storage()
init_use_count = s._use_count()
self.assertEqual(init_use_count, torch._C._storage_Use_Count(s._cdata))
b = a.t()
self.assertEqual(s._use_count(), init_use_count + 1)
self.assertEqual(s._use_count(), torch._C._storage_Use_Count(s._cdata))
del b
self.assertEqual(s._use_count(), init_use_count)
self.assertEqual(s._use_count(), torch._C._storage_Use_Count(s._cdata))

def test_tensor_ressurecting_clear(self):
# Regression test for https://github.com/pytorch/pytorch/issues/136358
# A Tensor with custom __dict__
Expand Down
8 changes: 8 additions & 0 deletions torch/csrc/StorageMethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ static PyObject* THPStorage_nbytes(PyObject* self, PyObject* noargs) {
END_HANDLE_TH_ERRORS
}

static PyObject* THPStorage__useCount(PyObject* self, PyObject* noargs) {
HANDLE_TH_ERRORS
THPStorage_assertNotNull(self);
return py::cast(THPStorage_Unpack(self).use_count()).release().ptr();
END_HANDLE_TH_ERRORS
}

static PyObject* THPStorage_dataPtr(PyObject* self, PyObject* noargs) {
HANDLE_TH_ERRORS
// PyLong_FromVoidPtr should not need to mutate the pointer in order
Expand Down Expand Up @@ -620,6 +627,7 @@ static PyMethodDef THPStorage_methods[] = {
METH_VARARGS | METH_STATIC,
nullptr},
{"_set_from_file", THPStorage_setFromFile, METH_VARARGS, nullptr},
{"_use_count", THPStorage__useCount, METH_NOARGS, nullptr},
{"from_buffer",
castPyCFunctionWithKeywords(THPStorage_fromBuffer),
METH_VARARGS | METH_KEYWORDS | METH_STATIC,
Expand Down
Loading