From 2e8d431a8fbfdbdb07448195f16afa9e101188ac Mon Sep 17 00:00:00 2001 From: "Yu, Guangye" Date: Tue, 10 Sep 2024 14:13:47 +0000 Subject: [PATCH] Fix tensor.data_ptr() representation overflow (#135567) # Motivation fix https://github.com/pytorch/pytorch/issues/135550 In PyTorch, [`tensor.data_ptr()`](https://github.com/pytorch/pytorch/blob/e889252493558a56263618faae9a9ef421c2a47d/tools/autograd/templates/python_variable_methods.cpp#L204) is reinterpreted by a [signed int64](https://github.com/pytorch/pytorch/blob/e889252493558a56263618faae9a9ef421c2a47d/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()`](https://github.com/pytorch/pytorch/blob/c0d2f991b14d50f8081d788d4a3dc6584ee15502/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 --- torch/csrc/StorageMethods.cpp | 5 +---- torch/csrc/autograd/utils/wrap_outputs.h | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/torch/csrc/StorageMethods.cpp b/torch/csrc/StorageMethods.cpp index 19f02b46453..a9d3f64f914 100644 --- a/torch/csrc/StorageMethods.cpp +++ b/torch/csrc/StorageMethods.cpp @@ -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 } diff --git a/torch/csrc/autograd/utils/wrap_outputs.h b/torch/csrc/autograd/utils/wrap_outputs.h index 5d1d17c597a..72d7a6c76d7 100644 --- a/torch/csrc/autograd/utils/wrap_outputs.h +++ b/torch/csrc/autograd/utils/wrap_outputs.h @@ -47,7 +47,7 @@ inline PyObject* wrap(c10::complex value) { } inline PyObject* wrap(void* value) { - return THPUtils_packInt64(reinterpret_cast(value)); + return PyLong_FromVoidPtr(value); } inline PyObject* wrap(THPDtype* dtype) {