|
| 1 | +""" |
| 2 | +The cross-evaluation memo for linear-func operated mapping matrices in the |
| 3 | +numba CPU sparse inversion (`imaging_numba/sparse.py`). |
| 4 | +
|
| 5 | +The memo must: reuse the matrix when a fresh linear-func object fingerprints |
| 6 | +identically to a previous evaluation's (the fixed-MGE campaign case); recompute |
| 7 | +when any state differs (free profile parameters); fall back to the uncached |
| 8 | +parent computation when an object cannot be fingerprinted or the memo is |
| 9 | +disabled; and never hand out writeable buffers. |
| 10 | +""" |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +import pytest |
| 14 | + |
| 15 | +from autoarray.inversion.inversion.imaging_numba import sparse as sparse_module |
| 16 | +from autoarray.inversion.inversion.imaging_numba.sparse import ( |
| 17 | + InversionImagingSparseNumba, |
| 18 | + _operated_mapping_matrix_memo, |
| 19 | + _operated_mapping_matrix_memo_key, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +class FakeLinearFunc: |
| 24 | + """Stands in for an MGE linear-func bundle: `values` plays the role of the |
| 25 | + profile parameters, and computing the override is counted class-wide so |
| 26 | + tests can assert whether the convolution work actually ran.""" |
| 27 | + |
| 28 | + compute_count = 0 |
| 29 | + |
| 30 | + def __init__(self, values): |
| 31 | + self.values = np.array(values, dtype=float) |
| 32 | + |
| 33 | + @property |
| 34 | + def operated_mapping_matrix_override(self): |
| 35 | + type(self).compute_count += 1 |
| 36 | + return np.outer(self.values, np.arange(1.0, 4.0)) |
| 37 | + |
| 38 | + |
| 39 | +class UnpicklableLinearFunc(FakeLinearFunc): |
| 40 | + def __init__(self, values): |
| 41 | + super().__init__(values) |
| 42 | + self.blocker = lambda: None # lambdas cannot be pickled |
| 43 | + |
| 44 | + |
| 45 | +class StubInversion(InversionImagingSparseNumba): |
| 46 | + """Bypasses the real constructor; the property under test only needs |
| 47 | + `cls_list_from` (and instance-dict storage for its cached_property).""" |
| 48 | + |
| 49 | + def __init__(self, linear_func_list): |
| 50 | + self._stub_linear_func_list = list(linear_func_list) |
| 51 | + |
| 52 | + def cls_list_from(self, cls): |
| 53 | + return self._stub_linear_func_list |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture(autouse=True) |
| 57 | +def _clean_memo(): |
| 58 | + _operated_mapping_matrix_memo.clear() |
| 59 | + FakeLinearFunc.compute_count = 0 |
| 60 | + yield |
| 61 | + _operated_mapping_matrix_memo.clear() |
| 62 | + |
| 63 | + |
| 64 | +def test__memo_key__stable_for_equal_state__distinct_for_different_state(): |
| 65 | + key_a = _operated_mapping_matrix_memo_key(FakeLinearFunc([1.0, 2.0])) |
| 66 | + key_b = _operated_mapping_matrix_memo_key(FakeLinearFunc([1.0, 2.0])) |
| 67 | + key_c = _operated_mapping_matrix_memo_key(FakeLinearFunc([1.0, 2.5])) |
| 68 | + |
| 69 | + assert key_a == key_b |
| 70 | + assert key_a != key_c |
| 71 | + |
| 72 | + |
| 73 | +def test__memo_key__unpicklable_state_returns_none(): |
| 74 | + assert _operated_mapping_matrix_memo_key(UnpicklableLinearFunc([1.0])) is None |
| 75 | + |
| 76 | + |
| 77 | +def test__identical_state_across_fresh_objects__computes_once(): |
| 78 | + func_eval_0 = FakeLinearFunc([1.0, 2.0]) |
| 79 | + dict_0 = StubInversion([func_eval_0]).linear_func_operated_mapping_matrix_dict |
| 80 | + |
| 81 | + # A sampler's next evaluation builds a FRESH object with identical state. |
| 82 | + func_eval_1 = FakeLinearFunc([1.0, 2.0]) |
| 83 | + dict_1 = StubInversion([func_eval_1]).linear_func_operated_mapping_matrix_dict |
| 84 | + |
| 85 | + assert FakeLinearFunc.compute_count == 1 |
| 86 | + assert np.array_equal(dict_0[func_eval_0], dict_1[func_eval_1]) |
| 87 | + assert not dict_1[func_eval_1].flags.writeable |
| 88 | + |
| 89 | + |
| 90 | +def test__changed_state__recomputes_and_matches_uncached_result(): |
| 91 | + StubInversion([FakeLinearFunc([1.0, 2.0])]).linear_func_operated_mapping_matrix_dict |
| 92 | + |
| 93 | + func_changed = FakeLinearFunc([1.0, 3.0]) |
| 94 | + result = StubInversion([func_changed]).linear_func_operated_mapping_matrix_dict[ |
| 95 | + func_changed |
| 96 | + ] |
| 97 | + |
| 98 | + assert FakeLinearFunc.compute_count == 2 |
| 99 | + assert np.array_equal(result, np.outer([1.0, 3.0], np.arange(1.0, 4.0))) |
| 100 | + |
| 101 | + |
| 102 | +def test__cached_property__single_dict_build_per_inversion(): |
| 103 | + inversion = StubInversion([FakeLinearFunc([1.0, 2.0])]) |
| 104 | + |
| 105 | + dict_first = inversion.linear_func_operated_mapping_matrix_dict |
| 106 | + dict_second = inversion.linear_func_operated_mapping_matrix_dict |
| 107 | + |
| 108 | + assert dict_first is dict_second |
| 109 | + |
| 110 | + |
| 111 | +def test__unpicklable_func__falls_back_to_uncached_parent_and_stores_nothing(): |
| 112 | + func = UnpicklableLinearFunc([1.0, 2.0]) |
| 113 | + |
| 114 | + result = StubInversion([func]).linear_func_operated_mapping_matrix_dict[func] |
| 115 | + |
| 116 | + assert np.array_equal(result, np.outer([1.0, 2.0], np.arange(1.0, 4.0))) |
| 117 | + assert len(_operated_mapping_matrix_memo) == 0 |
| 118 | + |
| 119 | + |
| 120 | +def test__env_var_disables_memo(monkeypatch): |
| 121 | + monkeypatch.setenv("AUTOARRAY_NUMBA_OPERATED_MEMO", "0") |
| 122 | + |
| 123 | + func = FakeLinearFunc([1.0, 2.0]) |
| 124 | + result = StubInversion([func]).linear_func_operated_mapping_matrix_dict[func] |
| 125 | + |
| 126 | + assert np.array_equal(result, np.outer([1.0, 2.0], np.arange(1.0, 4.0))) |
| 127 | + assert len(_operated_mapping_matrix_memo) == 0 |
| 128 | + |
| 129 | + |
| 130 | +def test__memo_eviction__bounded_size(): |
| 131 | + for value in range(sparse_module._OPERATED_MAPPING_MATRIX_MEMO_MAX_ENTRIES + 3): |
| 132 | + func = FakeLinearFunc([float(value)]) |
| 133 | + StubInversion([func]).linear_func_operated_mapping_matrix_dict |
| 134 | + |
| 135 | + assert ( |
| 136 | + len(_operated_mapping_matrix_memo) |
| 137 | + == sparse_module._OPERATED_MAPPING_MATRIX_MEMO_MAX_ENTRIES |
| 138 | + ) |
0 commit comments