summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorgchanan <gregchanan@gmail.com>2018-02-09 10:59:15 -0500
committerGitHub <noreply@github.com>2018-02-09 10:59:15 -0500
commit6a9b7132ec4be0c0efe330aad9fcc5e1342c2dd1 (patch)
tree3557cd89a771586f571fe9be6ebf365dcdd46457 /tools
parent65fb885467c07daadce383345d385ff45bd3db2a (diff)
downloadpytorch-6a9b7132ec4be0c0efe330aad9fcc5e1342c2dd1.tar.gz
pytorch-6a9b7132ec4be0c0efe330aad9fcc5e1342c2dd1.tar.bz2
pytorch-6a9b7132ec4be0c0efe330aad9fcc5e1342c2dd1.zip
Add a new_tensor instance method to Variable that takes only data. (#5144)
* Add a new_tensor instance method to Variable that takes only data. This is to work around the legacy problems of new, where e.g. new(5) will give you an unfilled tensor rather than a scalar. * Remove double return. * Fix cuda scalar code path. * Work around lack of WITH_SCALARS.
Diffstat (limited to 'tools')
-rw-r--r--tools/autograd/templates/python_torch_functions.cpp2
-rw-r--r--tools/autograd/templates/python_variable_methods.cpp12
2 files changed, 12 insertions, 2 deletions
diff --git a/tools/autograd/templates/python_torch_functions.cpp b/tools/autograd/templates/python_torch_functions.cpp
index fb5c801261..0490af5ab3 100644
--- a/tools/autograd/templates/python_torch_functions.cpp
+++ b/tools/autograd/templates/python_torch_functions.cpp
@@ -77,7 +77,7 @@ static PyObject * THPVariable_from_numpy(PyObject* module, PyObject* arg)
static PyObject * THPVariable_variable(PyObject* self, PyObject* args, PyObject* kwargs)
{
HANDLE_TH_ERRORS
- return THPVariable_Wrap(torch::utils::variable_data_factory(default_type(), args, kwargs));
+ return THPVariable_Wrap(torch::utils::new_tensor(default_type(), args, kwargs));
END_HANDLE_TH_ERRORS
}
diff --git a/tools/autograd/templates/python_variable_methods.cpp b/tools/autograd/templates/python_variable_methods.cpp
index c13d561dd0..5d60deea59 100644
--- a/tools/autograd/templates/python_variable_methods.cpp
+++ b/tools/autograd/templates/python_variable_methods.cpp
@@ -485,7 +485,16 @@ static PyObject * THPVariable_new(PyObject* self, PyObject* args, PyObject* kwar
HANDLE_TH_ERRORS
auto& self_ = reinterpret_cast<THPVariable*>(self)->cdata;
AutoGPU auto_gpu(self_);
- return THPVariable_Wrap(torch::utils::tensor_new(self_.type(), args, kwargs));
+ return THPVariable_Wrap(torch::utils::legacy_tensor_ctor(self_.type(), args, kwargs));
+ END_HANDLE_TH_ERRORS
+}
+
+static PyObject * THPVariable_new_tensor(PyObject* self, PyObject* args, PyObject* kwargs)
+{
+ HANDLE_TH_ERRORS
+ auto& self_ = reinterpret_cast<THPVariable*>(self)->cdata;
+ AutoGPU auto_gpu(self_);
+ return THPVariable_Wrap(torch::utils::new_tensor(self_.type(), args, kwargs));
END_HANDLE_TH_ERRORS
}
@@ -592,6 +601,7 @@ PyMethodDef variable_methods[] = {
{"ndimension", (PyCFunction)THPVariable_dim, METH_NOARGS, NULL},
{"nelement", (PyCFunction)THPVariable_numel, METH_NOARGS, NULL},
{"new", (PyCFunction)THPVariable_new, METH_VARARGS | METH_KEYWORDS, NULL},
+ {"new_tensor", (PyCFunction)THPVariable_new_tensor, METH_VARARGS | METH_KEYWORDS, NULL},
{"numpy", (PyCFunction)THPVariable_numpy, METH_NOARGS, NULL},
{"record_stream", (PyCFunction)THPVariable_record_stream, METH_O, NULL},
{"short", (PyCFunction)THPVariable_short, METH_NOARGS, NULL},