From a45b79d23fbf3586d81ecbaf6881863da1d86779 Mon Sep 17 00:00:00 2001 From: Will Feng Date: Sat, 30 Mar 2019 11:28:44 -0700 Subject: [PATCH] Fix wrap(at::Scalar) (#18632) 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torch/csrc/autograd/utils/wrap_outputs.h b/torch/csrc/autograd/utils/wrap_outputs.h index 0e9612e..0819d76 100644 --- a/torch/csrc/autograd/utils/wrap_outputs.h +++ b/torch/csrc/autograd/utils/wrap_outputs.h @@ -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 tensors) { -- 2.7.4