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 2 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
6 changes: 6 additions & 0 deletions torch/csrc/autograd/python_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,12 @@ 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);
TORCH_CHECK_TYPE(
var.dtype() == grad.dtype(),
"assigned grad has data of type ",
grad.dtype(),
" that differs from the required type ",
kiersten-stokes marked this conversation as resolved.
Show resolved Hide resolved
var.dtype());
bool gradIsSparse =
(var.dtype() == grad.dtype() &&
var.device().type() == grad.device().type() && grad.layout() == kSparse);
Expand Down