mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-14 20:57:59 +00:00
Fix tensor.data_ptr() representation overflow (#135567)
# Motivation fix https://github.com/pytorch/pytorch/issues/135550 In PyTorch, [`tensor.data_ptr()`](e889252493/tools/autograd/templates/python_variable_methods.cpp (L204)) is reinterpreted by a [signed int64](e889252493/torch/csrc/autograd/utils/wrap_outputs.h (L50)) data type, which could result in an **overflow issue**, like below: ```python import torch a = torch.randn(2).to('xpu') a.data_ptr() # one possible output is -23453392437248 # this is inconsistent with storage.data_ptr() a.untyped_storage().data_ptr() # one possible output is 18446720620317114368 ``` This PR aims to fix this representation overflow issue to make `tensor.data_ptr()` consistent with [`tensor.untyped_storage().data_ptr()`](c0d2f991b1/torch/csrc/StorageMethods.cpp (L62)). With this PR, the output will become: ```python import torch a = torch.randn(2).to('xpu') a.data_ptr() # one possible output is 18446720620317114368 # this is consistent with storage.data_ptr() a.untyped_storage().data_ptr() # one possible output is 18446720620317114368 ``` # Solution Use `PyLong_FromVoidPtr` to prevent the overflow issue and fit the semantic of `wrap`. Pull Request resolved: https://github.com/pytorch/pytorch/pull/135567 Approved by: https://github.com/dvrogozh, https://github.com/EikanWang, https://github.com/albanD
This commit is contained in:
parent
95496e4855
commit
2e8d431a8f
2 changed files with 2 additions and 5 deletions
|
|
@ -49,9 +49,6 @@ static PyObject* THPStorage_nbytes(PyObject* self, PyObject* noargs) {
|
|||
|
||||
static PyObject* THPStorage_dataPtr(PyObject* self, PyObject* noargs) {
|
||||
HANDLE_TH_ERRORS
|
||||
// PyLong_FromVoidPtr should not need to mutate the pointer in order
|
||||
// to extract a new long object from it.
|
||||
|
||||
auto self_ = THPStorage_Unpack(self);
|
||||
// See Note [Invalid Python Storages]
|
||||
auto invalid = self_.data() == nullptr &&
|
||||
|
|
@ -59,7 +56,7 @@ static PyObject* THPStorage_dataPtr(PyObject* self, PyObject* noargs) {
|
|||
TORCH_CHECK(
|
||||
!invalid,
|
||||
"Attempted to access the data pointer on an invalid python storage.")
|
||||
return PyLong_FromVoidPtr(self_.mutable_data());
|
||||
return torch::autograd::utils::wrap(self_.mutable_data());
|
||||
END_HANDLE_TH_ERRORS
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ inline PyObject* wrap(c10::complex<double> value) {
|
|||
}
|
||||
|
||||
inline PyObject* wrap(void* value) {
|
||||
return THPUtils_packInt64(reinterpret_cast<intptr_t>(value));
|
||||
return PyLong_FromVoidPtr(value);
|
||||
}
|
||||
|
||||
inline PyObject* wrap(THPDtype* dtype) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue