Skip to content

Commit

Permalink
Add Python binding resizable to class {Untyped,Typed}Storage (pyt…
Browse files Browse the repository at this point in the history
…orch#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
  • Loading branch information
hkmatsumoto authored and pytorchmergebot committed Feb 7, 2024
1 parent d054cd3 commit 02c24b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8305,6 +8305,13 @@ def test_sizeof(self) -> None:
self.assertEqual((sizeof_100 - sizeof_empty) // (sizeof_10 - sizeof_empty), 10)
self.assertEqual((sizeof_100 - sizeof_empty) % (sizeof_10 - sizeof_empty), 0)

@skipIfTorchDynamo("Not a suitable test for TorchDynamo")
def test_resizable(self) -> None:
x = torch.randn(5)
self.assertTrue(x.storage().resizable())
x.numpy()
self.assertFalse(x.storage().resizable())

def test_iter(self) -> None:
x = torch.randn(5, 5)
for i, sub in enumerate(x):
Expand Down
8 changes: 8 additions & 0 deletions torch/csrc/StorageMethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ static PyObject* THPStorage_dataPtr(PyObject* self, PyObject* noargs) {
END_HANDLE_TH_ERRORS
}

static PyObject* THPStorage_resizable(PyObject* self, PyObject* noargs) {
HANDLE_TH_ERRORS
THPStorage_assertNotNull(self);
return PyBool_FromLong(THPStorage_Unpack(self).resizable());
END_HANDLE_TH_ERRORS
}

static PyObject* THPStorage_copy_(
PyObject* self,
PyObject* args,
Expand Down Expand Up @@ -668,6 +675,7 @@ static PyMethodDef THPStorage_methods[] = {
{"resize_", THPStorage_resize_, METH_O, nullptr},
{"nbytes", THPStorage_nbytes, METH_NOARGS, nullptr},
{"data_ptr", THPStorage_dataPtr, METH_NOARGS, nullptr},
{"resizable", THPStorage_resizable, METH_NOARGS, nullptr},
{"_write_file", THPStorage_writeFile, METH_VARARGS, nullptr},
{"_new_with_file",
THPStorage_newWithFile,
Expand Down
6 changes: 6 additions & 0 deletions torch/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def get_device(self) -> int:

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

def resizable(self) -> bool: ... # type: ignore[empty-body] # noqa: E704

# Defined in torch/csrc/generic/StorageSharing.cpp
def _share_filename_cpu_(self, *args, **kwargs): ... # noqa: E704
def _share_fd_cpu_(self, *args, **kwargs): ... # noqa: E704
Expand Down Expand Up @@ -957,6 +959,10 @@ def data_ptr(self):
def _data_ptr(self):
return self._untyped_storage.data_ptr()

def resizable(self):
_warn_typed_storage_removal()
return self._untyped_storage.resizable()

def resize_(self, size):
_warn_typed_storage_removal()
self._resize_(size)
Expand Down

0 comments on commit 02c24b0

Please sign in to comment.