Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions test/test_autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,14 @@ def forward(ctx, x):
def backward(ctx, grad):
return grad * 2

v = torch.ones(1, requires_grad=True)
MyFunction.apply(v).backward()
self.assertEqual(v.grad.data.tolist(), [2])

v.grad.data.zero_()
MyFunction.apply(v.clone()).backward()
self.assertEqual(v.grad.data.tolist(), [2])
for shape in [(1,), ()]:
v = torch.ones(shape, requires_grad=True)
MyFunction.apply(v).backward()
self.assertEqual(v.grad, torch.full(shape, 2))

v.grad.data.zero_()
MyFunction.apply(v.clone()).backward()
self.assertEqual(v.grad, torch.full(shape, 2))

def test_legacy_function_none_grad(self):
class MyFunction(Function):
Expand Down
4 changes: 2 additions & 2 deletions torch/csrc/autograd/python_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ static void _wrap_outputs(THPFunction *self,
} else if (is_input) {
// An input has been returned, but it wasn't modified. Return it as a view
// so that we can attach a new grad_fn to the Variable.
var = var.slice();
var = var.view_as(var);

This comment was marked as off-topic.

var.set_gradient_edge({cdata, output_nr});
} else if (cdata) {
var.set_gradient_edge({cdata, output_nr});
Expand Down Expand Up @@ -602,7 +602,7 @@ static void _trace_post_record(

auto state_lock = trace_info.state->lock();
trace_info.n->i_(attr::inplace, is_inplace);

}

PyObject* process_outputs(PyObject *op_obj, THPFunction* grad_fn, const UnpackedInput& unpacked,
Expand Down