Skip to content

Commit

Permalink
Make PyTorch compilable against upcoming Numpy-2.0 (#121880) (#123380)
Browse files Browse the repository at this point in the history
Test plan:
```
% python -c "import torch;import numpy;print(numpy.__version__, torch.tensor(numpy.arange(3, 10)))"
2.1.0.dev0+git20240312.9de8a80 tensor([3, 4, 5, 6, 7, 8, 9])
% python -c "import torch;print(torch.rand(3, 3).numpy())"
[[0.0931946  0.44874293 0.8480404 ]
 [0.93877375 0.10188377 0.67375803]
 [0.02520031 0.89019287 0.5691561 ]]

```
Fixes #121798

Pull Request resolved: #121880
Approved by: https://github.com/albanD

(cherry picked from commit 38d9bb5)

Co-authored-by: Nikita Shulga <2453524+malfet@users.noreply.github.com>
  • Loading branch information
pytorchbot and malfet committed Apr 4, 2024
1 parent b67b277 commit a8b0091
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions torch/csrc/utils/tensor_numpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@ at::Tensor tensor_from_cuda_array_interface(PyObject* obj) {
TORCH_CHECK_VALUE(
PyArray_DescrConverter(py_typestr, &descr), "cannot parse `typestr`");
dtype = numpy_dtype_to_aten(descr->type_num);
#if NPY_ABI_VERSION >= 0x02000000
dtype_size_in_bytes = PyDataType_ELSIZE(descr);
#else
dtype_size_in_bytes = descr->elsize;
#endif
TORCH_INTERNAL_ASSERT(dtype_size_in_bytes > 0);
}

Expand Down

0 comments on commit a8b0091

Please sign in to comment.