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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages in THPVariable_set_grad #100683

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 31 additions & 13 deletions torch/csrc/autograd/python_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,23 +914,41 @@ int THPVariable_set_grad(THPVariable* self, PyObject* py_grad, void* unused) {
"can't assign Variable as its own grad");

const auto& grad = THPVariable_Unpack(py_grad);
bool gradIsSparse =
(var.dtype() == grad.dtype() &&
var.device().type() == grad.device().type() && grad.layout() == kSparse);
THPUtils_assertRet(
-1,
grad.options().type_equal(var.options()) || gradIsSparse,
"assigned grad has data of a different type");
TORCH_CHECK(
var.dtype() == grad.dtype(),
"attempting to assign a gradient with dtype '",
grad.dtype(),
"' to a tensor with dtype '",
var.dtype(),
"'. Please ensure that the gradient and the tensor have the same dtype");
TORCH_CHECK(
var.device().type() == grad.device().type(),
"attempting to assign a gradient with device type '",
grad.device().type(),
"' to a tensor with device type '",
var.device().type(),
"'. Please ensure that the gradient and the tensor are on the same device");
if (grad.layout() != kSparse) {
TORCH_CHECK(
grad.options().type_equal(var.options()),
"attempting to assign a gradient to a tensor that has data of a different type");
}
if (var.is_cuda()) {
THPUtils_assertRet(
-1,
TORCH_CHECK(
grad.get_device() == var.get_device(),
"assigned grad has data located on a different device");
"attempting to assign a gradient located on device with index '",
grad.get_device(),
"' to a tensor located on device with index '",
var.get_device(),
"'. Please ensure that the gradient and the tensor are on the same device");
}
THPUtils_assertRet(
-1,
TORCH_CHECK(
grad.sym_sizes().equals(var.sym_sizes()),
"assigned grad has data of a different size");
"attempting to assign a gradient of size '",
grad.sym_sizes(),
"' to a tensor of size '",
var.sym_sizes(),
"'. Please ensure that the gradient and the tensor are the same size");

var.mutable_grad() = grad;
return 0;
Expand Down