Fix wrap(at::Scalar) (#18632)
authorWill Feng <willfeng@fb.com>
Sat, 30 Mar 2019 18:28:44 +0000 (11:28 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 30 Mar 2019 18:36:11 +0000 (11:36 -0700)
Summary:
Problem:
```cpp
// This function expects a `Variable` as input
inline PyObject* wrap(at::Tensor tensor) {
  return THPVariable_Wrap(Variable(std::move(tensor)));
}

inline PyObject* wrap(at::Scalar scalar) {
  // This function calls `wrap(at::Tensor tensor)` (the function above), but since
  // `scalar_to_tensor(...)` returns a `Tensor` and not a `Variable`, the call to
  // `wrap(at::Tensor tensor)` will fail with "Tensor that was converted to Variable
  // was not actually a Variable", which is not what we want.
  return wrap(scalar_to_tensor(scalar));
}
```

The right fix is to call `make_variable(...)` with the tensor returned from `scalar_to_tensor(scalar)`.

This unblocks https://github.com/pytorch/pytorch/pull/18230 as it is the only patch that hits this code path now. All other native functions that return Scalar (such as `item()` or `_local_scalar_dense()`) either has custom-defined implementation that doesn't go through this path, or is not exposed to Python at all.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18632

Differential Revision: D14689293

Pulled By: yf225

fbshipit-source-id: be7ba5d3de83a69533a2997de97ad92989ff78ee

torch/csrc/autograd/utils/wrap_outputs.h

index 0e9612e..0819d76 100644 (file)
@@ -55,7 +55,7 @@ inline PyObject* wrap(at::Tensor tensor) {
 }
 
 inline PyObject* wrap(at::Scalar scalar) {
-  return wrap(scalar_to_tensor(scalar));
+  return wrap(make_variable(scalar_to_tensor(scalar)));
 }
 
 inline PyObject* wrap(std::tuple<at::Tensor, at::Tensor> tensors) {