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

Use variable_data() in tensor_to_numpy #22214

Closed
wants to merge 2 commits into from

Conversation

yf225
Copy link
Contributor

@yf225 yf225 commented Jun 25, 2019

As part of the Variable/Tensor merge, we want to gradually remove call sites of tensor_data() and the API itself, and instead uses variable_data(). This PR removes the tensor_data() call in the tensor_to_numpy conversion path.

@yf225 yf225 requested a review from gchanan June 25, 2019 17:58
@pytorchbot pytorchbot added module: internals Related to internal abstractions in c10 and ATen module: numpy Related to numpy support, and also numpy compatibility of our operators module: pybind Related to our Python bindings / interactions with other Python libraries labels Jun 25, 2019
@@ -110,7 +115,7 @@ PyObject* tensor_to_numpy(const at::Tensor& tensor) {
// object of the ndarray to the tensor and disabling resizes on the storage.
// This is not sufficient. For example, the tensor's storage may be changed
// via Tensor.set_, which can free the underlying memory.
PyObject* py_tensor = THPVariable_Wrap(make_variable(tensor, false));
PyObject* py_tensor = THPVariable_Wrap(as_variable_ref(tensor).variable_data());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add an explicit check for tensor.is_variable() here, but I think as_variable_ref(tensor) already checks whether tensor is a Variable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the variable_data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and we don't need to use variable_data here - resizing is not supported on either the PyTorch tensor or the numpy array, so creating a shallow-copy of the tensor being converted is not necessary in any case.

Copy link
Contributor

@gchanan gchanan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yf225 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@yf225 merged this pull request in 5f84f37.

@@ -85,6 +85,11 @@ PyObject* tensor_to_numpy(const at::Tensor& tensor) {
if (tensor.type().backend() != Backend::CPU) {
throw TypeError("NumPy conversion for %s is not supported", tensor.type().toString().c_str());
}
if (tensor.requires_grad()) {
throw std::runtime_error(
"Can't call numpy() on Variable that requires grad. "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message is outdated. There are no more Variables in PyTorch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are in C++ still, see #13638. We should fix these once that proposal is implemented, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged module: internals Related to internal abstractions in c10 and ATen module: numpy Related to numpy support, and also numpy compatibility of our operators module: pybind Related to our Python bindings / interactions with other Python libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants