Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] Make from_numpy method externally visible #8611

Closed
varunagrawal opened this issue Jun 18, 2018 · 10 comments
Closed

[feature request] Make from_numpy method externally visible #8611

varunagrawal opened this issue Jun 18, 2018 · 10 comments

Comments

@varunagrawal
Copy link
Contributor

Issue description

Currently, most Python linters complain that torch has no method from_numpy. Since converting from a numpy array to a Pytorch tensor is pretty common, it would be nice to add from_numpy to the __all__ list in the torch module to remedy this.

I believe this to be an easy fix and not break anything, but rather make production code linters work better with Pytorch.

Code example

x = numpy.rand(3, 3)
t = torch.from_numpy(x)  # pylint complains that torch has no method from_numpy

System Info

PyTorch version: 0.4.0
Is debug build: No
CUDA used to build PyTorch: 8.0.61

OS: Ubuntu 16.04.4 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
CMake version: version 3.5.1

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: 8.0.61
GPU models and configuration: GPU 0: Quadro P5000
Nvidia driver version: 384.111
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.5.1.10
/usr/lib/x86_64-linux-gnu/libcudnn.so.6.0.21
/usr/lib/x86_64-linux-gnu/libcudnn_static.a

Versions of relevant libraries:
[pip3] msgpack-numpy (0.4.1)
[pip3] numpy (1.14.2)
[pip3] tensorboard-pytorch (0.7.1)
[pip3] torch (0.4.0)
[pip3] torchfile (0.1.0)
[pip3] torchvision (0.2.1)
[conda] tensorboard-pytorch 0.7.1
[conda] torch 0.4.0
[conda] torchfile 0.1.0
[conda] torchvision 0.2.1

@fmassa
Copy link
Member

fmassa commented Jun 18, 2018

I think this is an easy fix indeed, but I wonder if from_numpy is not going to be deprecated in favour of as_tensor?

@ssnl
Copy link
Collaborator

ssnl commented Jun 18, 2018

Yeah I think we are deprecating from_numpy, but this should be fixed. The related issue is likely #7318

@varunagrawal
Copy link
Contributor Author

Wouldn't that be a breaking change? I don't quite understand the need to deprecate from_numpy given numpy is the predominant library for numerical computing in Python.
Having as_tensor as an additional method would be great for extensibility.

@ssnl
Copy link
Collaborator

ssnl commented Jun 18, 2018

@varunagrawal Eventually, yes. But we won't suddenly do it. It will be a deprecation warning for a couple versions before we drop it.

@ssnl
Copy link
Collaborator

ssnl commented Jun 18, 2018

I don't remember the exact reasoning for deprecating it though. Maybe @fmassa knows?

@fmassa
Copy link
Member

fmassa commented Jun 18, 2018

All the functionality of from_numpy is present in as_tensor, but as_tensor also supports other data types and thus is more generic. I'm ok if we don't deprecate it, but for new users it might just be simpler for them to learn one API

@varunagrawal
Copy link
Contributor Author

The reasoning seems good. Though a lot of early adopters will need to update their code once as_tensor becomes the standard.

I'm closing this issue in favor of #7318. Thank you for your time @ssnl and @fmassa!

facebook-github-bot pushed a commit that referenced this issue Jan 31, 2019
Summary:
In the warning box on https://pytorch.org/docs/stable/tensors.html#torch.Tensor.new_tensor it says:

> new_tensor() always copies data. [...] If you have a numpy array and want to avoid a copy, use **torch.from_numpy()**.

But then further up the page we have another warning box with the message:

> torch.tensor() always copies data. [...] If you have a numpy array and want to avoid a copy, use **torch.as_tensor()**.

Now I believe this is just a small oversight, since from_numpy is to be deprecated in favour of as_tensor. See for example #6885 and #8611. I suggest to just use **torch.as_tensor()** in both of the warning boxes.

cc gchanan
Pull Request resolved: #16587

Differential Revision: D13897038

Pulled By: gchanan

fbshipit-source-id: 2eb3cd47d2c0b5bf4350f980de3be9fe59b4a846
@daddydrac
Copy link

daddydrac commented Oct 7, 2019

Instead of a numpy array, I have a cupy array (cupy is a gpu enabled drop-in replacement for numpy). I tried both from_numpy and as_tensor and get an error -

cupy_array = cu.array([1,2,3,4,5,6]) print(cupy_array)

tensor_cnv = torch.from_numpy(cupy_array)

from_numpy error:

TypeError                                 Traceback (most recent call last)
<ipython-input-64-a81541e377fc> in <module>
----> 1 tensor_cnv = torch.from_numpy(cupy_array)
      2 print(tensor_cnv)

TypeError: expected np.ndarray (got cupy.core.core.ndarray) 

as_tensor error:

ValueError                                Traceback (most recent call last)
<ipython-input-65-39506a59aab0> in <module>
----> 1 tensor_cnv = torch.as_tensor(cupy_array)
      2 print(tensor_cnv)

ValueError: given array strides not a multiple of the element byte size. Make a copy of the array to reallocate the memory.

@daddydrac
Copy link

daddydrac commented Oct 7, 2019

I fixed it --> When working with #cupy and #pytorch, you might have to do a couple things diff to turn a #numpy / cupy array into a tensor. First you need to copy the numpy (cupy )array, this will allocate new memory for numpy array . This is to prevent negative strides within Ndarray which are the number of locations in memory between beginnings of successive array elements. Next, we flip it with the Axis in the array, which tells which entries are reversed. Lastly, we can finally convert the copied array to a tensor.

import torch
import cupy as cu
torch.as_tensor(cu.flip(cu.copy(cu.array([1,2,3,4,5,6])),axis=0))

@daddydrac
Copy link

daddydrac commented Oct 7, 2019

For a near 100% of computations on GPU, use (optimized version):

torch.tensor(cu.flip(cu.copy(cu.array([1,2,3,4,5,6])),axis=0), dtype=torch.float16, device='cuda:0')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants