Skip to content

Commit 98afdcf

Browse files
apaszkesoumith
authored andcommitted
Accept None values returned from grad hooks
1 parent ef32e96 commit 98afdcf

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

torch/csrc/autograd/python_hook.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ static void check_result(PyObject* prev, PyObject* result, PyObject* hook) {
146146
}
147147

148148
static void check_single_result(PyObject* _original, PyObject* _result, PyObject* hook) {
149+
if (_result == Py_None) return;
150+
151+
if (_original == Py_None) {
152+
throw std::runtime_error("can't replace a None gradient with a non-None value");
153+
}
154+
149155
if (!PyObject_IsInstance(_result, THPVariableClass)) {
150156
PyErr_Format(PyExc_TypeError, "expected Variable, but hook returned '%s'",
151157
THPUtils_typename(_result));

0 commit comments

Comments
 (0)