diff --git a/CHANGELOG.md b/CHANGELOG.md index 440d066d..b388231f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,11 @@ changelog does not include internal changes that do not affect the user. - Fixed `jac_to_grad` failing with an `AttributeError` (or, with `retain_jac=True`, accumulating a wrong gradient) when a tensor is repeated in `tensors`. Repeated tensors are now only considered once. +- Fixed `backward` and `mtl_backward` failing with a `RuntimeError` when the `.jac` or `.grad` + field held an expanded tensor (e.g. when a parameter is directly summed into a loss) and was then + accumulated into by another call, or when such a `.grad` was modified in-place (e.g. with + `clip_grad_norm_`). Expanded gradients are now materialized before being stored in `.grad`, and + an expanded `.jac` is now only materialized when another Jacobian is accumulated into it. ## [0.18.1] - 2026-11-02 diff --git a/src/torchjd/autojac/_accumulation.py b/src/torchjd/autojac/_accumulation.py index 84e0702a..17763e89 100644 --- a/src/torchjd/autojac/_accumulation.py +++ b/src/torchjd/autojac/_accumulation.py @@ -1,6 +1,7 @@ from collections.abc import Iterable from typing import TypeGuard +import torch from torch import Tensor @@ -31,6 +32,7 @@ def accumulate_jacs(params: Iterable[Tensor], jacobians: Iterable[Tensor]) -> No ) if is_tensor_with_jac(param): + param.jac = _materialize_if_expanded(param.jac) param.jac += jac else: # We do not clone the value to save memory and time, so subsequent modifications of @@ -50,7 +52,15 @@ def accumulate_grads(params: Iterable[Tensor], gradients: Iterable[Tensor]) -> N if hasattr(param, "grad") and param.grad is not None: param.grad += grad else: - param.grad = grad + param.grad = _materialize_if_expanded(grad) + + +def _materialize_if_expanded(tensor: Tensor) -> Tensor: + if tensor.layout == torch.strided and any( + stride == 0 and size > 1 for stride, size in zip(tensor.stride(), tensor.shape, strict=True) + ): + return tensor.contiguous() + return tensor def _check_expects_grad(tensor: Tensor, field_name: str) -> None: diff --git a/tests/unit/autojac/test_backward.py b/tests/unit/autojac/test_backward.py index 893ad1a3..51e42e3b 100644 --- a/tests/unit/autojac/test_backward.py +++ b/tests/unit/autojac/test_backward.py @@ -4,6 +4,7 @@ from utils.tensors import eye_, randn_, tensor_ from torchjd.autojac import backward +from torchjd.autojac._accumulation import is_tensor_with_jac from torchjd.autojac._backward import _create_transform from torchjd.autojac._transform import OrderedSet from torchjd.autojac._utils import create_jac_dict @@ -55,6 +56,39 @@ def test_jac_is_populated() -> None: assert_has_jac(a) +def test_repeated_calls_with_expanded_jacobian() -> None: + """ + Tests that backward correctly accumulates the Jacobians across several calls when the Jacobian + of the tensor with respect to the input is an expanded tensor (e.g. because the input is + directly summed). + """ + + a = tensor_([1.0, 2.0], requires_grad=True) + + backward(a.sum()) + backward(2.0 * a.sum()) + + assert_jac_close(a, tensor_([[3.0, 3.0]])) + + +def test_expanded_jacobian_is_not_copied() -> None: + """ + Tests that backward stores an expanded Jacobian in the .jac field without copying it, and that + a second call can still accumulate into it. + """ + + a = tensor_([1.0, 2.0], requires_grad=True) + + backward(a.sum()) + + assert is_tensor_with_jac(a) + assert 0 in a.jac.stride() + + backward(a.sum()) + + assert_jac_close(a, tensor_([[2.0, 2.0]])) + + @mark.parametrize("shape", [(1, 3), (2, 3), (2, 6), (5, 8), (20, 55)]) @mark.parametrize("manually_specify_inputs", [True, False]) @mark.parametrize("chunk_size", [1, 2, None]) diff --git a/tests/unit/autojac/test_mtl_backward.py b/tests/unit/autojac/test_mtl_backward.py index 06dca9ed..c92e2278 100644 --- a/tests/unit/autojac/test_mtl_backward.py +++ b/tests/unit/autojac/test_mtl_backward.py @@ -13,6 +13,7 @@ from utils.tensors import arange_, ones_, rand_, randn_, tensor_ from torchjd.autojac import mtl_backward +from torchjd.autojac._accumulation import is_tensor_with_jac from torchjd.autojac._mtl_backward import _create_grad_tensors_dict, _create_transform from torchjd.autojac._transform import OrderedSet @@ -144,6 +145,53 @@ def test_single_task() -> None: assert_has_grad(p1) +def test_repeated_calls_with_expanded_task_gradient() -> None: + """ + Tests that mtl_backward correctly accumulates the gradients of the task-specific parameters + across several calls when these gradients are expanded tensors (e.g. because the task-specific + parameters are directly summed). + """ + + p0 = tensor_([1.0, 2.0], requires_grad=True) + p1 = tensor_([3.0, 4.0], requires_grad=True) + p2 = tensor_([5.0, 6.0], requires_grad=True) + + for _ in range(2): + f = tensor_([-1.0, 1.0]) @ p0 + y1 = f + p1.sum() + y2 = 2.0 * f + p2.sum() + mtl_backward([y1, y2], features=f) + + assert_jac_close(p0, tensor_([[-2.0, 2.0], [-4.0, 4.0]])) + assert_grad_close(p1, tensor_([2.0, 2.0])) + assert_grad_close(p2, tensor_([2.0, 2.0])) + + +def test_expanded_jac_is_not_copied_but_expanded_grad_is() -> None: + """ + Tests that mtl_backward stores an expanded Jacobian in the .jac field of the shared parameters + without copying it, but materializes an expanded gradient before storing it in the .grad field + of the task-specific parameters. Also tests that a second call accumulates correctly into both. + """ + + p0 = tensor_([1.0, 2.0], requires_grad=True) + p1 = tensor_([3.0, 4.0], requires_grad=True) + + f = p0.sum() + mtl_backward([f + p1.sum()], features=f) + + assert is_tensor_with_jac(p0) + assert 0 in p0.jac.stride() + assert p1.grad is not None + assert 0 not in p1.grad.stride() + + f = p0.sum() + mtl_backward([f + p1.sum()], features=f) + + assert_jac_close(p0, tensor_([[2.0, 2.0]])) + assert_grad_close(p1, tensor_([2.0, 2.0])) + + def test_incoherent_task_number_fails() -> None: """ Tests that mtl_backward raises an error when called with the number of tasks losses different