Skip to content

Commit e951575

Browse files
authored
fix(autojac): Fix Grad behavior on empty outputs (#272)
* Make Grad._differentiate return a tuple of zero tensors when its outputs parameter is of length 0
1 parent dd2325d commit e951575

File tree

1 file changed

+1
-6
lines changed
  • src/torchjd/autojac/_transform

1 file changed

+1
-6
lines changed

src/torchjd/autojac/_transform/grad.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ def _differentiate(self, grad_outputs: Sequence[Tensor]) -> tuple[Tensor, ...]:
3737
return tuple()
3838

3939
if len(outputs) == 0:
40-
return tuple(
41-
[
42-
torch.empty(input.shape, device=input.device, dtype=input.dtype)
43-
for input in inputs
44-
]
45-
)
40+
return tuple([torch.zeros_like(input) for input in inputs])
4641

4742
optional_grads = torch.autograd.grad(
4843
outputs,

0 commit comments

Comments
 (0)