|
| 1 | +import torch |
| 2 | +from pytest import mark, raises |
| 3 | +from torch import Tensor |
| 4 | +from torch.nn.functional import cosine_similarity |
| 5 | +from utils.tensors import tensor_ |
| 6 | + |
| 7 | +from torchjd.scalarization import COSMOS |
| 8 | + |
| 9 | +from ._asserts import ( |
| 10 | + assert_grad_flow, |
| 11 | + assert_permutation_invariant, |
| 12 | + assert_returns_scalar, |
| 13 | +) |
| 14 | +from ._inputs import all_inputs |
| 15 | + |
| 16 | + |
| 17 | +def _uniform(values: Tensor) -> Tensor: |
| 18 | + """Uniform preference vector matching the shape of `values`.""" |
| 19 | + return torch.full_like(values, 1.0 / values.numel()) |
| 20 | + |
| 21 | + |
| 22 | +def test_value_aligned_gives_zero() -> None: |
| 23 | + # Uniform weights on equal values are perfectly aligned, so cos(r, L) = 1. The result is the |
| 24 | + # weighted sum (1) minus lambda (1): 0. |
| 25 | + out = COSMOS(lambda_=1.0, weights=tensor_([0.5, 0.5]))(tensor_([1.0, 1.0])) |
| 26 | + torch.testing.assert_close(out, tensor_(0.0)) |
| 27 | + |
| 28 | + |
| 29 | +def test_value_lambda_zero_is_linear_scalarization() -> None: |
| 30 | + # With lambda = 0 there is no cosine penalty, so COSMOS is just the weighted sum. |
| 31 | + weights = tensor_([1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0]) |
| 32 | + out = COSMOS(lambda_=0.0, weights=weights)(tensor_([1.0, 2.0, 4.0])) |
| 33 | + torch.testing.assert_close(out, tensor_(7.0 / 3.0)) |
| 34 | + |
| 35 | + |
| 36 | +def test_value_with_weights() -> None: |
| 37 | + # With lambda = 0, only the linear term remains: 2*3 + 1*4 = 10. |
| 38 | + out = COSMOS(lambda_=0.0, weights=tensor_([2.0, 1.0]))(tensor_([3.0, 4.0])) |
| 39 | + torch.testing.assert_close(out, tensor_(10.0)) |
| 40 | + |
| 41 | + |
| 42 | +def test_full_formula() -> None: |
| 43 | + values = tensor_([1.0, 2.0, 4.0]) |
| 44 | + weights = tensor_([0.5, 0.3, 0.2]) |
| 45 | + lambda_ = 2.0 |
| 46 | + expected = (weights * values).sum() - lambda_ * cosine_similarity(weights, values, dim=0) |
| 47 | + torch.testing.assert_close(COSMOS(lambda_, weights=weights)(values), expected) |
| 48 | + |
| 49 | + |
| 50 | +@mark.parametrize("values", all_inputs) |
| 51 | +def test_expected_structure(values: Tensor) -> None: |
| 52 | + assert_returns_scalar(COSMOS(lambda_=1.0, weights=_uniform(values)), values) |
| 53 | + |
| 54 | + |
| 55 | +@mark.parametrize("values", all_inputs) |
| 56 | +def test_grad_flow(values: Tensor) -> None: |
| 57 | + assert_grad_flow(COSMOS(lambda_=1.0, weights=_uniform(values)), values) |
| 58 | + |
| 59 | + |
| 60 | +@mark.parametrize("values", all_inputs) |
| 61 | +def test_permutation_invariant(values: Tensor) -> None: |
| 62 | + # With uniform weights, both the weighted sum and the cosine term are symmetric in the inputs. |
| 63 | + assert_permutation_invariant(COSMOS(lambda_=1.0, weights=_uniform(values)), values) |
| 64 | + |
| 65 | + |
| 66 | +def test_zero_values_returns_zero() -> None: |
| 67 | + # `cosine_similarity` is numerically stable for the zero vector, so all-zero values give 0 (no |
| 68 | + # nan), regardless of lambda. |
| 69 | + out = COSMOS(lambda_=1.0, weights=tensor_([0.5, 0.5]))(tensor_([0.0, 0.0])) |
| 70 | + torch.testing.assert_close(out, tensor_(0.0)) |
| 71 | + |
| 72 | + |
| 73 | +@mark.parametrize("lambda_", [-1.0, -0.5]) |
| 74 | +def test_raises_on_negative_lambda(lambda_: float) -> None: |
| 75 | + with raises(ValueError): |
| 76 | + COSMOS(lambda_=lambda_, weights=tensor_([0.5, 0.5])) |
| 77 | + |
| 78 | + |
| 79 | +def test_raises_on_weights_shape_mismatch() -> None: |
| 80 | + scalarizer = COSMOS(lambda_=1.0, weights=tensor_([1.0, 1.0, 1.0])) |
| 81 | + with raises(ValueError): |
| 82 | + scalarizer(tensor_([1.0, 1.0])) |
| 83 | + |
| 84 | + |
| 85 | +def test_representations() -> None: |
| 86 | + s = COSMOS(lambda_=0.5, weights=torch.tensor([0.5, 0.5])) |
| 87 | + assert repr(s) == "COSMOS(lambda_=0.5, weights=tensor([0.5000, 0.5000]))" |
| 88 | + assert str(s) == "COSMOS" |
0 commit comments