okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3ed9dafa1
main
1"""Tests for tools/make_profile.py, without a model: synthetic profiles and routing traces. 2 3python -m unittest tools.test_make_profile 4""" 5from __future__import annotations 6 7import struct 8import sys 9import tempfile 10import unittest 11from collections import defaultdict 12from pathlib import Path 13 14ROOT = Path (__file__ ).resolve ().parents [1 ] 15sys .path .insert (0 ,str (ROOT / "tools" )) 16import make_profile as MP # noqa: E402 17 18NE = 4 # a tiny "experts per layer"; the layer count is fixed at MP.N_LAYER 19 20 21def write_trace (path ,records ): 22"""One record per (layer, [expert ids]); the reader skips k weights after the k ids.""" 23with open (path ,"wb" )as f : 24for layer ,experts in records : 25f .write (struct .pack ("<ii" ,layer ,len (experts ))) 26f .write (struct .pack ("<%di" % len (experts ),* experts )) 27f .write (struct .pack ("<%df" % len (experts ),* ([1.0 ]* len (experts )))) 28 29 30def full_base (): 31"""Every pair, in an order that differs from rank_profile's fill (e-major then layer).""" 32return [(layer ,e )for layer in range (MP .N_LAYER )for e in range (NE )] 33 34 35class RankProfile (unittest .TestCase ): 36def test_a_complete_base_hides_the_trace (self ): 37# the shipped profile's situation: it already ranks every pair, so the trace cannot move anything 38base = full_base () 39freq = defaultdict (int , {(5 ,1 ):10 , (6 ,2 ):7 }) 40ranked ,counts = MP .rank_profile (base ,freq ,NE ) 41self .assertEqual (ranked ,base ) 42self .assertEqual (counts ["trace" ],0 ) 43self .assertEqual (counts ["base" ],MP .N_LAYER * NE ) 44 45def test_reorder_puts_the_traces_first (self ): 46base = full_base () 47freq = defaultdict (int , {(5 ,1 ):10 , (6 ,2 ):7 }) 48ranked ,counts = MP .rank_profile (base ,freq ,NE ,reorder = True ) 49self .assertEqual (ranked [:2 ], [(5 ,1 ), (6 ,2 )]) 50self .assertEqual (set (ranked ),set (base ))# still every pair, once 51self .assertEqual (len (ranked ),len (base )) 52self .assertEqual (counts ["trace" ],2 ) 53self .assertEqual (counts ["base" ],MP .N_LAYER * NE - 2 )# the base fills the rest, minus the two 54 55def test_frequency_and_tie_break_order_the_trace (self ): 56freq = defaultdict (int , {(1 ,0 ):1 , (2 ,0 ):3 , (3 ,0 ):3 }) 57ranked ,_ = MP .rank_profile ([],freq ,NE ,no_base = True )# (2,0)/(3,0) tie -> the smaller pair first 58self .assertEqual (ranked [:3 ], [(2 ,0 ), (3 ,0 ), (1 ,0 )]) 59 60def test_default_appends_only (self ): 61base = [(0 ,0 ), (0 ,1 )] 62freq = defaultdict (int , {(0 ,0 ):5 , (1 ,3 ):9 }) 63ranked ,_ = MP .rank_profile (base ,freq ,NE ) 64self .assertEqual (ranked [:3 ], [(0 ,0 ), (0 ,1 ), (1 ,3 )])# base first; (0,0) already there, not moved 65 66def test_no_base_uses_the_traces_then_the_fill (self ): 67base = [(0 ,0 ), (0 ,1 )] 68freq = defaultdict (int , {(1 ,3 ):9 , (0 ,0 ):5 }) 69ranked ,counts = MP .rank_profile (base ,freq ,NE ,no_base = True ) 70self .assertEqual (ranked [:2 ], [(1 ,3 ), (0 ,0 )]) 71self .assertEqual (counts ["base" ],0 ) 72self .assertEqual (len (ranked ),MP .N_LAYER * NE ) 73 74def test_reorder_without_traces_is_the_default (self ): 75base = full_base () 76self .assertEqual (MP .rank_profile (base ,defaultdict (int ),NE ,reorder = True )[0 ],base ) 77 78 79class Main (unittest .TestCase ): 80def run_main (self ,argv ): 81old ,sys .argv = sys .argv , ["make_profile.py" ]+ argv 82try : 83MP .main () 84finally : 85sys .argv = old 86 87def test_end_to_end_reorder_over_a_complete_base (self ): 88with tempfile .TemporaryDirectory ()as d : 89base = Path (d )/ "base.bin" 90out = Path (d )/ "out.bin" 91trace = Path (d )/ "t.bin" 92MP .write_profile (str (base ),full_base (),NE ) 93write_trace (str (trace ), [(5 , [1 ]), (6 , [2 ]), (6 , [2 ]), (6 , [2 ])]) 94self .run_main ([str (trace ),"--base" ,str (base ),"--reorder" ,"--n-expert" ,str (NE ),"--out" ,str (out )]) 95ranked = MP .read_profile (str (out ),NE ) 96self .assertEqual (ranked [:2 ], [(6 ,2 ), (5 ,1 )])# most frequent first 97self .assertEqual (set (ranked ),set (full_base ())) 98# and without --reorder it is the base's order, untouched 99self .run_main ([str (trace ),"--base" ,str (base ),"--n-expert" ,str (NE ),"--out" ,str (out )]) 100self .assertEqual (MP .read_profile (str (out ),NE ),full_base ()) 101 102 103if __name__ == "__main__" : 104unittest .main ()