|
| 1 | +"""š§Ŗ regression tests for branching update behavior.""" |
| 2 | +# ruff: noqa: S101, D103, ANN001, ANN201 |
| 3 | + |
| 4 | +from collections import Counter |
| 5 | + |
| 6 | +from streamgen.nodes import TransformNode |
| 7 | +from streamgen.parameter import Parameter |
| 8 | +from streamgen.samplers.tree import SamplingTree |
| 9 | +from streamgen.transforms import noop |
| 10 | + |
| 11 | + |
| 12 | +# ---------------------------------------------------------------------------- # |
| 13 | +# * helper functions # |
| 14 | +# ---------------------------------------------------------------------------- # |
| 15 | + |
| 16 | + |
| 17 | +def _build_minimal_branch_tree() -> SamplingTree: |
| 18 | + """Builds a small tree where one scoped parameter is attached multiple times. |
| 19 | +
|
| 20 | + Layout: |
| 21 | + root -> BranchingNode(left|right) -> shared_scope |
| 22 | +
|
| 23 | + The node after the branching node is deep-copied into each branch by |
| 24 | + streamgen's tree construction shorthand. All copies fetch params from the |
| 25 | + same scope name ("shared_scope"). |
| 26 | + """ |
| 27 | + nodes = [ |
| 28 | + TransformNode(noop, name="root"), |
| 29 | + { |
| 30 | + "name": "selector", |
| 31 | + "left": [TransformNode(noop, name="left_leaf")], |
| 32 | + "right": [TransformNode(noop, name="right_leaf")], |
| 33 | + }, |
| 34 | + TransformNode(noop, name="shared_scope"), |
| 35 | + ] |
| 36 | + |
| 37 | + params = { |
| 38 | + "shared_scope": { |
| 39 | + "sweep": { |
| 40 | + "schedule": [10, 20, 30], |
| 41 | + "strategy": "hold", |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + return SamplingTree(nodes=nodes, params=params, rng=0) |
| 47 | + |
| 48 | + |
| 49 | +def _collect_schedule_values(mode: str) -> list[int]: |
| 50 | + """Collects two update steps from either tree.update or tree.params.update.""" |
| 51 | + tree = _build_minimal_branch_tree() |
| 52 | + parameter = tree.get_params()["shared_scope.sweep"] |
| 53 | + values = [int(parameter.value)] |
| 54 | + |
| 55 | + for _ in range(2): |
| 56 | + if mode == "tree": |
| 57 | + tree.update() |
| 58 | + elif mode == "params": |
| 59 | + tree.params.update() |
| 60 | + else: |
| 61 | + raise ValueError(f"unknown mode: {mode}") |
| 62 | + values.append(int(parameter.value)) |
| 63 | + |
| 64 | + return values |
| 65 | + |
| 66 | + |
| 67 | +# ---------------------------------------------------------------------------- # |
| 68 | +# * tests # |
| 69 | +# ---------------------------------------------------------------------------- # |
| 70 | + |
| 71 | + |
| 72 | +def test_tree_update_calls_each_parameter_once_in_branching_tree(monkeypatch) -> None: |
| 73 | + """Tests that tree.update touches each parameter object once.""" |
| 74 | + tree = _build_minimal_branch_tree() |
| 75 | + target = tree.get_params()["shared_scope.sweep"] |
| 76 | + |
| 77 | + call_counts: Counter[int] = Counter() |
| 78 | + original_update = Parameter.update |
| 79 | + |
| 80 | + def wrapped_update(self, *args, **kwargs): |
| 81 | + call_counts[id(self)] += 1 |
| 82 | + return original_update(self, *args, **kwargs) |
| 83 | + |
| 84 | + monkeypatch.setattr(Parameter, "update", wrapped_update) |
| 85 | + tree.update() |
| 86 | + |
| 87 | + assert call_counts[id(target)] == 1 |
| 88 | + |
| 89 | + |
| 90 | +def test_parameter_store_update_calls_each_parameter_once(monkeypatch) -> None: |
| 91 | + """Tests that ParameterStore.update touches each parameter object once.""" |
| 92 | + tree = _build_minimal_branch_tree() |
| 93 | + target = tree.get_params()["shared_scope.sweep"] |
| 94 | + |
| 95 | + call_counts: Counter[int] = Counter() |
| 96 | + original_update = Parameter.update |
| 97 | + |
| 98 | + def wrapped_update(self, *args, **kwargs): |
| 99 | + call_counts[id(self)] += 1 |
| 100 | + return original_update(self, *args, **kwargs) |
| 101 | + |
| 102 | + monkeypatch.setattr(Parameter, "update", wrapped_update) |
| 103 | + tree.params.update() |
| 104 | + |
| 105 | + assert call_counts[id(target)] == 1 |
| 106 | + |
| 107 | + |
| 108 | +def test_tree_update_matches_parameter_store_update_progression() -> None: |
| 109 | + """Tests that tree.update progresses one step and matches params.update.""" |
| 110 | + tree_values = _collect_schedule_values("tree") |
| 111 | + params_values = _collect_schedule_values("params") |
| 112 | + |
| 113 | + assert tree_values == [10, 20, 30] |
| 114 | + assert params_values == [10, 20, 30] |
| 115 | + assert tree_values == params_values |
0 commit comments