@@ -0,0 +1,81 @@ |
| 1 | +"""Static mean-gate fallback for Ollama export.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from dlm.export.gate_fallback import mean_gate_weights, uniform_adapter_mix |
| 8 | +from dlm.train.gate.module import Gate, GateMetadata |
| 9 | + |
| 10 | + |
| 11 | +class TestUniformAdapterMix: |
| 12 | + def test_three_adapters_third_each(self) -> None: |
| 13 | + mix = uniform_adapter_mix(("a", "b", "c")) |
| 14 | + assert mix == [("a", 1 / 3), ("b", 1 / 3), ("c", 1 / 3)] |
| 15 | + |
| 16 | + def test_empty_tuple(self) -> None: |
| 17 | + assert uniform_adapter_mix(()) == [] |
| 18 | + |
| 19 | + |
| 20 | +class TestMeanGateWeights: |
| 21 | + def _gate_and_meta(self) -> tuple[Gate, GateMetadata]: |
| 22 | + gate = Gate(input_dim=8, hidden_proj_dim=4, n_adapters=2) |
| 23 | + meta = GateMetadata( |
| 24 | + input_dim=8, |
| 25 | + hidden_proj_dim=4, |
| 26 | + adapter_names=("a", "b"), |
| 27 | + mode="trained", |
| 28 | + ) |
| 29 | + return gate, meta |
| 30 | + |
| 31 | + def test_empty_corpus_refused(self) -> None: |
| 32 | + gate, meta = self._gate_and_meta() |
| 33 | + with pytest.raises(ValueError, match=">= 1 prompt embedding"): |
| 34 | + mean_gate_weights(gate, meta, []) |
| 35 | + |
| 36 | + def test_weights_shape_and_sum_to_one(self) -> None: |
| 37 | + import torch |
| 38 | + |
| 39 | + gate, meta = self._gate_and_meta() |
| 40 | + embeddings = [torch.randn(8) for _ in range(16)] |
| 41 | + mix = mean_gate_weights(gate, meta, embeddings) |
| 42 | + assert [name for name, _ in mix] == ["a", "b"] |
| 43 | + total = sum(w for _, w in mix) |
| 44 | + assert total == pytest.approx(1.0, abs=1e-5) |
| 45 | + for _, w in mix: |
| 46 | + assert 0.0 <= w <= 1.0 |
| 47 | + |
| 48 | + def test_dim_mismatch_refused(self) -> None: |
| 49 | + import torch |
| 50 | + |
| 51 | + gate, meta = self._gate_and_meta() |
| 52 | + # Wrong-dim embedding. |
| 53 | + with pytest.raises(ValueError, match="input_dim"): |
| 54 | + mean_gate_weights(gate, meta, [torch.randn(4)]) |
| 55 | + |
| 56 | + def test_mean_reflects_per_prompt_skew(self) -> None: |
| 57 | + """Ten prompts near cluster A + one prompt near cluster B should |
| 58 | + average out to favor A. Sanity check that mean_gate_weights isn't |
| 59 | + just emitting uniform.""" |
| 60 | + import torch |
| 61 | + |
| 62 | + gate = Gate(input_dim=8, hidden_proj_dim=8, n_adapters=2) |
| 63 | + meta = GateMetadata( |
| 64 | + input_dim=8, |
| 65 | + hidden_proj_dim=8, |
| 66 | + adapter_names=("a", "b"), |
| 67 | + mode="trained", |
| 68 | + ) |
| 69 | + # Force the gate weights so it's (almost) deterministic: class-a |
| 70 | + # embeddings near +1, class-b near -1. |
| 71 | + torch.manual_seed(0) |
| 72 | + a_embeddings = [torch.ones(8) + 0.01 * torch.randn(8) for _ in range(10)] |
| 73 | + b_embedding = -torch.ones(8) |
| 74 | + # We won't train here — untrained gate may or may not favor A. |
| 75 | + # The point is only that the mean is a real average (not uniform |
| 76 | + # or fixed), which we check by comparing against a single-prompt |
| 77 | + # case. |
| 78 | + mix_mixed = mean_gate_weights(gate, meta, a_embeddings + [b_embedding]) |
| 79 | + mix_single_a = mean_gate_weights(gate, meta, [a_embeddings[0]]) |
| 80 | + # Different input distributions → different averaged outputs. |
| 81 | + assert mix_mixed != mix_single_a |