okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
538acd098
main
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 6python -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 22def test_the_task_table (self ): 23self .assertEqual (setup .derived_factor (131072 ),1.0 )# below the trained range: clamped at 1 24self .assertEqual (setup .derived_factor (262144 ),1.0 )# exactly the trained range: 1 25self .assertEqual (setup .derived_factor (393216 ),1.5 )# 384K 26self .assertEqual (setup .derived_factor (524288 ),2.0 )# 512K 27 28def test_free_form_contexts_get_the_exact_ratio (self ): 29# /262144 = /2**18 is exact in binary floating point, so no dust 30self .assertEqual (setup .derived_factor (600000 ),600000 / 262144 ) 31self .assertGreater (setup .derived_factor (600000 ),2.0 ) 32 33def test_the_clamp_is_what_keeps_128k_at_one (self ): 34self .assertEqual (setup .derived_factor (131072 ),1.0 )# NOT the raw 0.5 35 36 37class ResolveRope (unittest .TestCase ): 38def test_invalid_explicit_factors_are_rejected (self ): 39for ctx in (131072 ,262144 ,1048576 ): 40for method in (None ,"none" ,"linear" ,"yarn" ): 41for scale in (float ("nan" ),float ("inf" ),float ("-inf" ),- 1.0 ,0.0 ,0.5 ): 42with self .subTest (ctx = ctx ,method = method ,scale = scale ): 43with self .assertRaisesRegex (ValueError ,r"--rope-scale.*finite.*>= 1" ): 44setup .resolve_rope (ctx ,method ,scale ) 45 46def test_valid_explicit_factors_remain_verbatim (self ): 47for ctx in (131072 ,262144 ,1048576 ): 48for method in ("linear" ,"yarn" ): 49for scale in (1.0 ,1.5 ,4.0 ): 50with self .subTest (ctx = ctx ,method = method ,scale = scale ): 51self .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) 54def test_omitted_384k_gets_yarn_and_1_5 (self ): 55self .assertEqual (setup .resolve_rope (393216 ,None ,None ), ("yarn" ,1.5 )) 56 57def test_omitted_512k_gets_yarn_and_2 (self ): 58self .assertEqual (setup .resolve_rope (524288 ,None ,None ), ("yarn" ,2.0 )) 59 60def test_deterministic_for_yes (self ): 61# --yes never prompts, so the resolution must be a pure function of its inputs 62for _ in range (3 ): 63self .assertEqual (setup .resolve_rope (524288 ,None ,None ), ("yarn" ,2.0 )) 64 65def 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 67self .assertEqual (setup .resolve_rope (393216 ,None ,1.8 ), ("yarn" ,1.8 )) 68 69# ---- explicit selections are respected, verbatim 70def test_explicit_method_gets_the_derived_factor (self ): 71self .assertEqual (setup .resolve_rope (524288 ,"linear" ,None ), ("linear" ,2.0 )) 72 73def test_explicit_method_and_factor_are_kept_verbatim (self ): 74self .assertEqual (setup .resolve_rope (393216 ,"linear" ,2.0 ), ("linear" ,2.0 )) 75self .assertEqual (setup .resolve_rope (524288 ,"yarn" ,1.5 ), ("yarn" ,1.5 )) 76 77def 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 79self .assertEqual (setup .resolve_rope (131072 ,"yarn" ,2.0 ), ("yarn" ,2.0 )) 80self .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 83def test_explicit_none_past_trained_is_refused (self ): 84with self .assertRaises (ValueError )as cm : 85setup .resolve_rope (524288 ,"none" ,None ) 86self .assertIn ("262144" ,str (cm .exception )) 87self .assertIn ("yarn" ,str (cm .exception ))# the message names the way out 88 89def test_explicit_none_within_trained_is_the_stock_model (self ): 90self .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 93def test_omitted_within_trained_adds_nothing (self ): 94self .assertEqual (setup .resolve_rope (262144 ,None ,None ), (None ,None )) 95self .assertEqual (setup .resolve_rope (131072 ,None ,None ), (None ,None )) 96 97def test_a_scale_alone_within_trained_is_rejected (self ): 98with self .assertRaises (ValueError ): 99setup .resolve_rope (131072 ,None ,2.0 ) 100 101def test_a_chosen_method_within_trained_gets_factor_1 (self ): 102# no more unconditional factor 2: 1 = the trained angles, no automatic expansion 103self .assertEqual (setup .resolve_rope (131072 ,"yarn" ,None ), ("yarn" ,1.0 )) 104self .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 108def 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 110self .assertEqual (setup .resolve_rope (131072 ,"yarn" ,None ), ("yarn" ,1.0 )) 111 112def test_no_reduction_384k_and_512k_resolve_as_documented (self ): 113self .assertEqual (setup .resolve_rope (393216 ,"yarn" ,None ), ("yarn" ,1.5 )) 114self .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 118def test_the_short_context_table (self ): 119cases = [ 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 ] 126for ctx ,scale ,method ,want in cases : 127self .assertEqual (setup .resolve_rope (ctx ,method ,scale ), (method ,want ),f"ctx { ctx } " ) 128 129def test_linear_gets_the_same_rule_as_yarn (self ): 130# the old fallback applied to both methods, so the fix must too 131self .assertEqual (setup .resolve_rope (131072 ,"linear" ,None ), ("linear" ,1.0 )) 132self .assertEqual (setup .resolve_rope (393216 ,"linear" ,None ), ("linear" ,1.5 )) 133 134 135if __name__ == "__main__" : 136unittest .main ()