okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-gh-ctl: GitHub setup, Docker, docs and bench items for 0.2.7538acd098

main
7.1 KiB136 linesraw
1"""Tests for setup.py's automatic rope configuration (§4 + §6): the factor derives from the FINAL context
2actually served - RAM reductions first, then final / trained 262144 clamped at 1 - explicit
3--rope-scaling/--rope-scale choices win verbatim, an explicit none is refused past the trained range, and
4nothing turns on by itself inside it.  Pure functions - no GPU, no downloads, no prompts.
5
6    python -m unittest tools.test_setup_rope
7"""
8from __future__ import annotations
9
10import sys
11import unittest
12from pathlib import Path
13
14ROOT = Path(__file__).resolve().parents[1]
15sys.path.insert(0, str(ROOT))
16import setup  # noqa: E402
17
18
19class DerivedFactor(unittest.TestCase):
20    """The formula itself: max(1.0, final / trained)."""
21
22    def test_the_task_table(self):
23        self.assertEqual(setup.derived_factor(131072), 1.0)        # below the trained range: clamped at 1
24        self.assertEqual(setup.derived_factor(262144), 1.0)        # exactly the trained range: 1
25        self.assertEqual(setup.derived_factor(393216), 1.5)        # 384K
26        self.assertEqual(setup.derived_factor(524288), 2.0)        # 512K
27
28    def test_free_form_contexts_get_the_exact_ratio(self):
29        # /262144 = /2**18 is exact in binary floating point, so no dust
30        self.assertEqual(setup.derived_factor(600000), 600000 / 262144)
31        self.assertGreater(setup.derived_factor(600000), 2.0)
32
33    def test_the_clamp_is_what_keeps_128k_at_one(self):
34        self.assertEqual(setup.derived_factor(131072), 1.0)        # NOT the raw 0.5
35
36
37class ResolveRope(unittest.TestCase):
38    def test_invalid_explicit_factors_are_rejected(self):
39        for ctx in (131072, 262144, 1048576):
40            for method in (None, "none", "linear", "yarn"):
41                for scale in (float("nan"), float("inf"), float("-inf"), -1.0, 0.0, 0.5):
42                    with self.subTest(ctx=ctx, method=method, scale=scale):
43                        with self.assertRaisesRegex(ValueError, r"--rope-scale.*finite.*>= 1"):
44                            setup.resolve_rope(ctx, method, scale)
45
46    def test_valid_explicit_factors_remain_verbatim(self):
47        for ctx in (131072, 262144, 1048576):
48            for method in ("linear", "yarn"):
49                for scale in (1.0, 1.5, 4.0):
50                    with self.subTest(ctx=ctx, method=method, scale=scale):
51                        self.assertEqual(setup.resolve_rope(ctx, method, scale), (method, scale))
52
53    # ---- the automatic flow the docs promise (an omitted flag, --yes or the interactive default)
54    def test_omitted_384k_gets_yarn_and_1_5(self):
55        self.assertEqual(setup.resolve_rope(393216, None, None), ("yarn", 1.5))
56
57    def test_omitted_512k_gets_yarn_and_2(self):
58        self.assertEqual(setup.resolve_rope(524288, None, None), ("yarn", 2.0))
59
60    def test_deterministic_for_yes(self):
61        # --yes never prompts, so the resolution must be a pure function of its inputs
62        for _ in range(3):
63            self.assertEqual(setup.resolve_rope(524288, None, None), ("yarn", 2.0))
64
65    def test_omitted_method_with_an_explicit_factor(self):
66        # --rope-scale alone still expresses the intent to extend: the method defaults, the factor is kept
67        self.assertEqual(setup.resolve_rope(393216, None, 1.8), ("yarn", 1.8))
68
69    # ---- explicit selections are respected, verbatim
70    def test_explicit_method_gets_the_derived_factor(self):
71        self.assertEqual(setup.resolve_rope(524288, "linear", None), ("linear", 2.0))
72
73    def test_explicit_method_and_factor_are_kept_verbatim(self):
74        self.assertEqual(setup.resolve_rope(393216, "linear", 2.0), ("linear", 2.0))
75        self.assertEqual(setup.resolve_rope(524288, "yarn", 1.5), ("yarn", 1.5))
76
77    def test_an_explicit_override_survives_a_context_reduction(self):
78        # §4: the RAM check reduced the final context to 128K - a user-supplied factor is NOT recalculated
79        self.assertEqual(setup.resolve_rope(131072, "yarn", 2.0), ("yarn", 2.0))
80        self.assertEqual(setup.resolve_rope(131072, "linear", 1.75), ("linear", 1.75))
81
82    # ---- an explicit none past the trained range is refused with an explanation, not overridden
83    def test_explicit_none_past_trained_is_refused(self):
84        with self.assertRaises(ValueError) as cm:
85            setup.resolve_rope(524288, "none", None)
86        self.assertIn("262144", str(cm.exception))
87        self.assertIn("yarn", str(cm.exception))                   # the message names the way out
88
89    def test_explicit_none_within_trained_is_the_stock_model(self):
90        self.assertEqual(setup.resolve_rope(131072, "none", None), (None, None))
91
92    # ---- inside the trained range nothing turns on by itself; a chosen method gets factor 1
93    def test_omitted_within_trained_adds_nothing(self):
94        self.assertEqual(setup.resolve_rope(262144, None, None), (None, None))
95        self.assertEqual(setup.resolve_rope(131072, None, None), (None, None))
96
97    def test_a_scale_alone_within_trained_is_rejected(self):
98        with self.assertRaises(ValueError):
99            setup.resolve_rope(131072, None, 2.0)
100
101    def test_a_chosen_method_within_trained_gets_factor_1(self):
102        # no more unconditional factor 2: 1 = the trained angles, no automatic expansion
103        self.assertEqual(setup.resolve_rope(131072, "yarn", None), ("yarn", 1.0))
104        self.assertEqual(setup.resolve_rope(262144, "linear", None), ("linear", 1.0))
105
106    # ---- §4 coordination: the RAM reduction lands BEFORE the rope config, so the factor matches the
107    # context actually served - the 512K-on-IQ3_S-with-under-90-GB case from the task, end to end
108    def test_512k_on_a_small_ram_machine_resolves_to_128k_factor_1(self):
109        # asked 524288 with --rope-scaling yarn and no factor; the RAM check brings the final context to 131072
110        self.assertEqual(setup.resolve_rope(131072, "yarn", None), ("yarn", 1.0))
111
112    def test_no_reduction_384k_and_512k_resolve_as_documented(self):
113        self.assertEqual(setup.resolve_rope(393216, "yarn", None), ("yarn", 1.5))
114        self.assertEqual(setup.resolve_rope(524288, "yarn", None), ("yarn", 2.0))
115
116    # ---- the short-context item's own table: no factor-2 fallback inside the trained window, for either
117    # method, with the explicit override column kept verbatim
118    def test_the_short_context_table(self):
119        cases = [
120            (131072, None, "yarn", 1.0),                   # --context 131072 --rope-scaling yarn
121            (262144, None, "yarn", 1.0),                   # --context 262144 --rope-scaling yarn
122            (393216, None, "yarn", 1.5),
123            (524288, None, "yarn", 2.0),
124            (131072, 2.0, "yarn", 2.0),                    # --rope-scale 2 is kept inside the window
125        ]
126        for ctx, scale, method, want in cases:
127            self.assertEqual(setup.resolve_rope(ctx, method, scale), (method, want), f"ctx {ctx}")
128
129    def test_linear_gets_the_same_rule_as_yarn(self):
130        # the old fallback applied to both methods, so the fix must too
131        self.assertEqual(setup.resolve_rope(131072, "linear", None), ("linear", 1.0))
132        self.assertEqual(setup.resolve_rope(393216, "linear", None), ("linear", 1.5))
133
134
135if __name__ == "__main__":
136    unittest.main()