|
| 1 | +import torch |
| 2 | +from pytest import mark, raises |
| 3 | +from torch import Tensor |
| 4 | +from utils.tensors import tensor_ |
| 5 | + |
| 6 | +from torchjd.scalarization import STCH |
| 7 | + |
| 8 | +from ._asserts import ( |
| 9 | + assert_grad_flow, |
| 10 | + assert_permutation_invariant, |
| 11 | + assert_returns_scalar, |
| 12 | +) |
| 13 | +from ._inputs import all_inputs |
| 14 | + |
| 15 | + |
| 16 | +def test_value_default() -> None: |
| 17 | + # Uniform weights, no reference: mu * logsumexp([0, 0]) = log(2). |
| 18 | + out = STCH(mu=1.0)(tensor_([0.0, 0.0])) |
| 19 | + torch.testing.assert_close(out, torch.log(tensor_(2.0))) |
| 20 | + |
| 21 | + |
| 22 | +def test_value_with_weights() -> None: |
| 23 | + # weights = [1, 1] on values [1, 1]: mu * logsumexp([1, 1]) = 1 + log(2). |
| 24 | + out = STCH(mu=1.0, weights=tensor_([1.0, 1.0]))(tensor_([1.0, 1.0])) |
| 25 | + torch.testing.assert_close(out, 1.0 + torch.log(tensor_(2.0))) |
| 26 | + |
| 27 | + |
| 28 | +def test_value_with_reference() -> None: |
| 29 | + # reference shifts values to [0, 0], so the result collapses back to log(2). |
| 30 | + out = STCH(mu=1.0, weights=tensor_([1.0, 1.0]), reference=tensor_([1.0, 1.0]))( |
| 31 | + tensor_([1.0, 1.0]) |
| 32 | + ) |
| 33 | + torch.testing.assert_close(out, torch.log(tensor_(2.0))) |
| 34 | + |
| 35 | + |
| 36 | +@mark.parametrize("losses", all_inputs) |
| 37 | +def test_expected_structure(losses: Tensor) -> None: |
| 38 | + assert_returns_scalar(STCH(mu=1.0), losses) |
| 39 | + |
| 40 | + |
| 41 | +@mark.parametrize("losses", all_inputs) |
| 42 | +def test_grad_flow(losses: Tensor) -> None: |
| 43 | + assert_grad_flow(STCH(mu=1.0), losses) |
| 44 | + |
| 45 | + |
| 46 | +@mark.parametrize("losses", all_inputs) |
| 47 | +def test_permutation_invariant(losses: Tensor) -> None: |
| 48 | + # With uniform weights and no reference, STCH is symmetric in its inputs. |
| 49 | + assert_permutation_invariant(STCH(mu=1.0), losses) |
| 50 | + |
| 51 | + |
| 52 | +def test_does_not_overflow_for_large_values_and_small_mu() -> None: |
| 53 | + # `weights * values / mu` would overflow to inf before logsumexp can stabilize it. The |
| 54 | + # value-preserving centering keeps the result finite and equal to the dominant (max) term. |
| 55 | + values = tensor_([1e30, 2e30, 3e30]) |
| 56 | + out = STCH(mu=1e-10)(values) |
| 57 | + assert out.isfinite() |
| 58 | + torch.testing.assert_close(out, tensor_(1e30)) # 3e30 weighted by the uniform 1/3. |
| 59 | + |
| 60 | + |
| 61 | +@mark.parametrize("mu", [0.0, -1.0]) |
| 62 | +def test_raises_on_non_positive_mu(mu: float) -> None: |
| 63 | + with raises(ValueError): |
| 64 | + STCH(mu=mu) |
| 65 | + |
| 66 | + |
| 67 | +def test_raises_on_weights_shape_mismatch() -> None: |
| 68 | + scalarizer = STCH(mu=1.0, weights=tensor_([1.0, 1.0, 1.0])) |
| 69 | + with raises(ValueError): |
| 70 | + scalarizer(tensor_([1.0, 1.0])) |
| 71 | + |
| 72 | + |
| 73 | +def test_raises_on_reference_shape_mismatch() -> None: |
| 74 | + scalarizer = STCH(mu=1.0, reference=tensor_([1.0, 1.0, 1.0])) |
| 75 | + with raises(ValueError): |
| 76 | + scalarizer(tensor_([1.0, 1.0])) |
| 77 | + |
| 78 | + |
| 79 | +def test_representations() -> None: |
| 80 | + s = STCH(mu=0.5) |
| 81 | + assert repr(s) == "STCH(mu=0.5, weights=None, reference=None)" |
| 82 | + assert str(s) == "STCH" |
0 commit comments