Skip to content

Commit 02c24b0

Browse files
hkmatsumotopytorchmergebot
authored andcommitted
Add Python binding resizable to class {Untyped,Typed}Storage (pytorch#119286)
This PR exposes `resizable` method of `StorageImpl` to Python frontend to make it accessible for users. Fixes pytorch#119233 Pull Request resolved: pytorch#119286 Approved by: https://github.com/ezyang, https://github.com/mikaylagawarecki
1 parent d054cd3 commit 02c24b0

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

test/test_torch.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8305,6 +8305,13 @@ def test_sizeof(self) -> None:
83058305
self.assertEqual((sizeof_100 - sizeof_empty) // (sizeof_10 - sizeof_empty), 10)
83068306
self.assertEqual((sizeof_100 - sizeof_empty) % (sizeof_10 - sizeof_empty), 0)
83078307

8308+
@skipIfTorchDynamo("Not a suitable test for TorchDynamo")
8309+
def test_resizable(self) -> None:
8310+
x = torch.randn(5)
8311+
self.assertTrue(x.storage().resizable())
8312+
x.numpy()
8313+
self.assertFalse(x.storage().resizable())
8314+
83088315
def test_iter(self) -> None:
83098316
x = torch.randn(5, 5)
83108317
for i, sub in enumerate(x):

torch/csrc/StorageMethods.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ static PyObject* THPStorage_dataPtr(PyObject* self, PyObject* noargs) {
6363
END_HANDLE_TH_ERRORS
6464
}
6565

66+
static PyObject* THPStorage_resizable(PyObject* self, PyObject* noargs) {
67+
HANDLE_TH_ERRORS
68+
THPStorage_assertNotNull(self);
69+
return PyBool_FromLong(THPStorage_Unpack(self).resizable());
70+
END_HANDLE_TH_ERRORS
71+
}
72+
6673
static PyObject* THPStorage_copy_(
6774
PyObject* self,
6875
PyObject* args,
@@ -668,6 +675,7 @@ static PyMethodDef THPStorage_methods[] = {
668675
{"resize_", THPStorage_resize_, METH_O, nullptr},
669676
{"nbytes", THPStorage_nbytes, METH_NOARGS, nullptr},
670677
{"data_ptr", THPStorage_dataPtr, METH_NOARGS, nullptr},
678+
{"resizable", THPStorage_resizable, METH_NOARGS, nullptr},
671679
{"_write_file", THPStorage_writeFile, METH_VARARGS, nullptr},
672680
{"_new_with_file",
673681
THPStorage_newWithFile,

torch/storage.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def get_device(self) -> int:
4747

4848
def data_ptr(self) -> int: ... # type: ignore[empty-body] # noqa: E704
4949

50+
def resizable(self) -> bool: ... # type: ignore[empty-body] # noqa: E704
51+
5052
# Defined in torch/csrc/generic/StorageSharing.cpp
5153
def _share_filename_cpu_(self, *args, **kwargs): ... # noqa: E704
5254
def _share_fd_cpu_(self, *args, **kwargs): ... # noqa: E704
@@ -957,6 +959,10 @@ def data_ptr(self):
957959
def _data_ptr(self):
958960
return self._untyped_storage.data_ptr()
959961

962+
def resizable(self):
963+
_warn_typed_storage_removal()
964+
return self._untyped_storage.resizable()
965+
960966
def resize_(self, size):
961967
_warn_typed_storage_removal()
962968
self._resize_(size)

0 commit comments

Comments
 (0)