|
| 1 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +"""segment_max / segment_softmax (NeighborGraph PR-D segment toolkit).""" |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from deepmd.dpmodel.utils.neighbor_graph import ( |
| 7 | + segment_max, |
| 8 | + segment_softmax, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class TestSegmentMax: |
| 13 | + def test_basic(self) -> None: |
| 14 | + data = np.array([1.0, 5.0, 2.0, -3.0]) |
| 15 | + ids = np.array([0, 0, 2, 2], dtype=np.int64) |
| 16 | + out = segment_max(data, ids, 3) |
| 17 | + assert out[0] == 5.0 |
| 18 | + assert np.isneginf(out[1]) # empty segment |
| 19 | + assert out[2] == 2.0 |
| 20 | + |
| 21 | + def test_trailing_dims(self) -> None: |
| 22 | + data = np.array([[1.0, -2.0], [3.0, -4.0], [0.0, 9.0]]) |
| 23 | + ids = np.array([1, 1, 0], dtype=np.int64) |
| 24 | + out = segment_max(data, ids, 2) |
| 25 | + np.testing.assert_allclose(out[0], [0.0, 9.0]) |
| 26 | + np.testing.assert_allclose(out[1], [3.0, -2.0]) |
| 27 | + |
| 28 | + def test_torch_matches_numpy(self) -> None: |
| 29 | + import torch |
| 30 | + |
| 31 | + data = np.array([0.3, 1.2, -0.7, 2.0]) |
| 32 | + ids = np.array([0, 0, 1, 1], dtype=np.int64) |
| 33 | + ref = segment_max(data, ids, 2) |
| 34 | + out = segment_max(torch.from_numpy(data), torch.from_numpy(ids), 2) |
| 35 | + np.testing.assert_allclose(out.numpy(), ref) |
| 36 | + |
| 37 | + |
| 38 | +class TestSegmentSoftmax: |
| 39 | + def test_matches_dense(self) -> None: |
| 40 | + logits = np.array([1.0, 2.0, 0.5, -1.0]) |
| 41 | + ids = np.array([0, 0, 0, 1], dtype=np.int64) |
| 42 | + w = segment_softmax(logits, ids, 2) |
| 43 | + ref0 = np.exp(np.array([1.0, 2.0, 0.5]) - 2.0) |
| 44 | + ref0 = ref0 / ref0.sum() |
| 45 | + np.testing.assert_allclose(w[:3], ref0, atol=1e-12) |
| 46 | + np.testing.assert_allclose(w[3], 1.0, atol=1e-12) |
| 47 | + |
| 48 | + def test_stable_large_logits(self) -> None: |
| 49 | + logits = np.array([1e30, 1e30 + 1.0]) |
| 50 | + ids = np.array([0, 0], dtype=np.int64) |
| 51 | + w = segment_softmax(logits, ids, 1) |
| 52 | + assert not np.any(np.isnan(w)) |
| 53 | + np.testing.assert_allclose(w.sum(), 1.0, atol=1e-12) |
| 54 | + |
| 55 | + def test_masked_entries_zero(self) -> None: |
| 56 | + logits = np.array([1.0, 2.0, 3.0]) |
| 57 | + ids = np.array([0, 0, 0], dtype=np.int64) |
| 58 | + mask = np.array([True, False, True]) |
| 59 | + w = segment_softmax(logits, ids, 1, mask=mask) |
| 60 | + assert w[1] == 0.0 |
| 61 | + np.testing.assert_allclose(w.sum(), 1.0, atol=1e-12) |
| 62 | + # masked entry excluded from the denominator too |
| 63 | + ref = np.exp(np.array([1.0, 3.0]) - 3.0) |
| 64 | + ref = ref / ref.sum() |
| 65 | + np.testing.assert_allclose(w[[0, 2]], ref, atol=1e-12) |
| 66 | + |
| 67 | + def test_all_masked_segment_is_zero_no_nan(self) -> None: |
| 68 | + logits = np.array([1.0, 2.0, 5.0]) |
| 69 | + ids = np.array([0, 0, 1], dtype=np.int64) |
| 70 | + mask = np.array([True, True, False]) |
| 71 | + w = segment_softmax(logits, ids, 2, mask=mask) |
| 72 | + assert not np.any(np.isnan(w)) |
| 73 | + assert w[2] == 0.0 |
| 74 | + |
| 75 | + def test_empty_segment_no_nan(self) -> None: |
| 76 | + logits = np.array([1.0, 2.0]) |
| 77 | + ids = np.array([0, 0], dtype=np.int64) |
| 78 | + w = segment_softmax(logits, ids, 3) |
| 79 | + assert not np.any(np.isnan(w)) |
| 80 | + |
| 81 | + def test_torch_matches_numpy(self) -> None: |
| 82 | + import torch |
| 83 | + |
| 84 | + logits = np.array([0.3, 1.2, -0.7, 2.0]) |
| 85 | + ids = np.array([0, 0, 1, 1], dtype=np.int64) |
| 86 | + mask = np.array([True, True, True, False]) |
| 87 | + ref = segment_softmax(logits, ids, 2, mask=mask) |
| 88 | + out = segment_softmax( |
| 89 | + torch.from_numpy(logits), |
| 90 | + torch.from_numpy(ids), |
| 91 | + 2, |
| 92 | + mask=torch.from_numpy(mask), |
| 93 | + ) |
| 94 | + np.testing.assert_allclose(out.numpy(), ref, atol=1e-12) |
0 commit comments