|
| 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 PBI |
| 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 _uniform(values: Tensor) -> Tensor: |
| 17 | + """Uniform preference vector matching the shape of `values`.""" |
| 18 | + return torch.full_like(values, 1.0 / values.numel()) |
| 19 | + |
| 20 | + |
| 21 | +def test_value() -> None: |
| 22 | + # direction = [1, 1] / sqrt(2). For [2, 0]: d1 = sqrt(2), perpendicular = [1, -1] so |
| 23 | + # d2 = sqrt(2), and d1 + theta * d2 = 2 * sqrt(2). |
| 24 | + out = PBI(theta=1.0, weights=tensor_([1.0, 1.0]))(tensor_([2.0, 0.0])) |
| 25 | + torch.testing.assert_close(out, tensor_(2.0) * tensor_(2.0).sqrt()) |
| 26 | + |
| 27 | + |
| 28 | +def test_theta_zero_is_projection() -> None: |
| 29 | + # With theta = 0 only the projection d1 remains. For [2, 0] onto [1, 1] / sqrt(2): d1 = sqrt(2). |
| 30 | + out = PBI(theta=0.0, weights=tensor_([1.0, 1.0]))(tensor_([2.0, 0.0])) |
| 31 | + torch.testing.assert_close(out, tensor_(2.0).sqrt()) |
| 32 | + |
| 33 | + |
| 34 | +def test_reference_shifts_values() -> None: |
| 35 | + # Subtracting the reference [1, 1] from [3, 1] gives [2, 0], matching the no-reference case. |
| 36 | + with_reference = PBI(theta=1.0, weights=tensor_([1.0, 1.0]), reference=tensor_([1.0, 1.0])) |
| 37 | + out = with_reference(tensor_([3.0, 1.0])) |
| 38 | + expected = PBI(theta=1.0, weights=tensor_([1.0, 1.0]))(tensor_([2.0, 0.0])) |
| 39 | + torch.testing.assert_close(out, expected) |
| 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 | + reference = tensor_([0.5, 0.5, 0.5]) |
| 46 | + theta = 5.0 |
| 47 | + shifted = values - reference |
| 48 | + direction = weights / weights.norm() |
| 49 | + d1 = (shifted * direction).sum() |
| 50 | + d2 = (shifted - d1 * direction).norm() |
| 51 | + expected = d1 + theta * d2 |
| 52 | + torch.testing.assert_close(PBI(theta, weights=weights, reference=reference)(values), expected) |
| 53 | + |
| 54 | + |
| 55 | +def test_finite_when_values_on_preference_ray() -> None: |
| 56 | + # When the values lie exactly on the preference direction, d2 = 0. The constant under the square |
| 57 | + # root keeps both the value and the gradient finite (no nan), which is the whole point of the |
| 58 | + # stabilization. |
| 59 | + weights = tensor_([1.0, 2.0]) |
| 60 | + leaf = weights.detach().clone().requires_grad_() # values == weights, so they are on the ray. |
| 61 | + out = PBI(theta=5.0, weights=weights)(leaf) |
| 62 | + out.backward() |
| 63 | + assert out.isfinite() |
| 64 | + assert leaf.grad is not None |
| 65 | + assert leaf.grad.isfinite().all() |
| 66 | + |
| 67 | + |
| 68 | +@mark.parametrize("values", all_inputs) |
| 69 | +def test_expected_structure(values: Tensor) -> None: |
| 70 | + assert_returns_scalar(PBI(theta=5.0, weights=_uniform(values)), values) |
| 71 | + |
| 72 | + |
| 73 | +@mark.parametrize("values", all_inputs) |
| 74 | +def test_grad_flow(values: Tensor) -> None: |
| 75 | + assert_grad_flow(PBI(theta=5.0, weights=_uniform(values)), values) |
| 76 | + |
| 77 | + |
| 78 | +@mark.parametrize("values", all_inputs) |
| 79 | +def test_permutation_invariant(values: Tensor) -> None: |
| 80 | + # With uniform weights and no reference, both d1 and d2 are symmetric in the inputs. |
| 81 | + assert_permutation_invariant(PBI(theta=5.0, weights=_uniform(values)), values) |
| 82 | + |
| 83 | + |
| 84 | +@mark.parametrize("theta", [-1.0, -0.5]) |
| 85 | +def test_raises_on_negative_theta(theta: float) -> None: |
| 86 | + with raises(ValueError): |
| 87 | + PBI(theta=theta, weights=tensor_([0.5, 0.5])) |
| 88 | + |
| 89 | + |
| 90 | +def test_raises_on_weights_shape_mismatch() -> None: |
| 91 | + scalarizer = PBI(theta=5.0, weights=tensor_([1.0, 1.0, 1.0])) |
| 92 | + with raises(ValueError): |
| 93 | + scalarizer(tensor_([1.0, 1.0])) |
| 94 | + |
| 95 | + |
| 96 | +def test_raises_on_reference_shape_mismatch() -> None: |
| 97 | + scalarizer = PBI(theta=5.0, weights=tensor_([1.0, 1.0]), reference=tensor_([0.0, 0.0, 0.0])) |
| 98 | + with raises(ValueError): |
| 99 | + scalarizer(tensor_([1.0, 1.0])) |
| 100 | + |
| 101 | + |
| 102 | +def test_representations() -> None: |
| 103 | + s = PBI(theta=5.0, weights=torch.tensor([0.5, 0.5])) |
| 104 | + assert repr(s) == "PBI(theta=5.0, weights=tensor([0.5000, 0.5000]), reference=None)" |
| 105 | + assert str(s) == "PBI" |
0 commit comments