okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
caac71139
main
1"""Tests for setup's UD-* start on a card of about 8 GB (0.1.44): the VRAM estimate, the plan that picks the least harmful 2config that starts, and the words (UD-Q4 is slow here: pick IQ3_XXS / IQ3_S). Measured on an 8 GB Arc A750: setup's 38192 / int8 / reserve 400 start stopped with "no VRAM is left for the expert cache, short 473 MiB". 4 5python -m unittest tools.test_setup_small_card 6""" 7from __future__import annotations 8 9import sys 10import unittest 11from pathlib import Path 12from unittest import mock 13 14ROOT = Path (__file__ ).resolve ().parents [1 ] 15sys .path .insert (0 ,str (ROOT )) 16sys .path .insert (0 ,str (ROOT / "tools" )) 17import setup # noqa: E402 18from test_setup_unsloth import Base # noqa: E402 19 20 21class Estimate (unittest .TestCase ): 22def test_the_a750_measurement (self ): 23# 8192 / int8 / reserve 400 was 473 MiB short; 4608 / q4_0 / reserve 0 started (without the draft layer's slack) 24self .assertAlmostEqual (setup .unsloth_vram_slack_mib (8.0 ,8192 ,"int8" ,400 ),- 473 ,delta = 2 ) 25self .assertGreaterEqual (setup .unsloth_vram_slack_mib (8.0 ,4608 ,"q4_0" ,0 ),0 ) 26 27def test_a_bigger_card_has_room (self ): 28self .assertGreater (setup .unsloth_vram_slack_mib (12.0 ,32768 ,"int8" ,700 ),setup .UD_FIT_MIN_SLACK_MIB ) 29 30def test_cuda_and_hip_pay_for_the_runtime_sycl_does_not (self ): 31self .assertEqual (setup .unsloth_runtime_mib ({"driver" :"i915" }),0 ) 32self .assertEqual (setup .unsloth_runtime_mib ({"driver" :"xe" }),0 ) 33self .assertEqual (setup .unsloth_runtime_mib ({"driver" :"580.97" }),setup .UD_FIT_RUNTIME_MIB ) 34self .assertEqual (setup .unsloth_runtime_mib ({"driver" :"amdgpu" }),setup .UD_FIT_RUNTIME_MIB ) 35 36 37class Plan (unittest .TestCase ): 38def test_a_card_that_fits_is_not_changed (self ): 39self .assertEqual (setup .unsloth_small_card_plan (10.0 ,8192 ,"fp16" ,None )[:3 ], (8192 ,"fp16" ,True )) 40 41def test_8gb_drops_the_draft_layer_first (self ): 42# nothing else frees enough on 8 GB: the context and the KV alone do not fit with the draft layer 43for kv in ("fp16" ,"int8" ): 44plan = setup .unsloth_small_card_plan (8.0 ,8192 ,kv ,300 ) 45self .assertIsNotNone (plan ) 46self .assertFalse (plan [2 ]) 47self .assertEqual (plan [:2 ], (8192 ,kv ))# the context and the KV the user would get are kept 48self .assertGreaterEqual (plan [3 ],setup .UD_FIT_MIN_SLACK_MIB ) 49 50def test_a_card_just_short_changes_the_kv_before_the_context_before_the_draft (self ): 51# ~8.9 GB: the 4-bit KV is enough with the draft layer 52vg = (setup .UD_FIT_FIXED_MIB + setup .UD_FIT_MTP_MIB + 8192 * 13 * 576 / 2 ** 20 + 300 + 80 )/ 1024 53plan = setup .unsloth_small_card_plan (vg ,8192 ,"fp16" ,300 ) 54self .assertEqual (plan [:3 ], (8192 ,"q4_0" ,True )) 55# ... and a smaller context when the user fixed the KV 56vg = (setup .UD_FIT_FIXED_MIB + setup .UD_FIT_MTP_MIB + 4608 * 13 * 2112 / 2 ** 20 + 300 + 80 )/ 1024 57plan = setup .unsloth_small_card_plan (vg ,8192 ,"fp16" ,300 ,kv_fixed = True ) 58self .assertEqual (plan [:3 ], (4608 ,"fp16" ,True )) 59 60def test_what_the_user_chose_is_not_changed (self ): 61plan = setup .unsloth_small_card_plan (8.0 ,32768 ,"int8" ,300 ,ctx_fixed = True ,kv_fixed = True ) 62self .assertEqual (plan [:2 ], (32768 ,"int8" )) 63 64def test_a_6gb_card_has_no_plan (self ): 65self .assertIsNone (setup .unsloth_small_card_plan (6.0 ,8192 ,"int8" ,None )) 66 67def test_a_reserve_the_user_gave_is_counted (self ): 68a = setup .unsloth_small_card_plan (8.0 ,8192 ,"int8" ,0 ) 69b = setup .unsloth_small_card_plan (8.0 ,8192 ,"int8" ,1500 ) 70self .assertGreater (a [3 ],b [3 ]if b else - 1e9 ) 71 72 73class Words (unittest .TestCase ): 74def test_always_says_to_pick_iq3 (self ): 75lines = setup .unsloth_small_card_lines ("UD-Q4_K_XL" ,8.0 , (8192 ,"int8" ,False ,400.0 ),False ) 76text = " " .join (lines ) 77self .assertIn ("very slow" ,text ) 78self .assertIn ("IQ3_XXS or IQ3_S" ,text ) 79 80def test_no_plan_says_the_start_will_probably_stop (self ): 81lines = setup .unsloth_small_card_lines ("UD-Q4_K_XL" ,6.0 ,None ,False ) 82self .assertTrue (any ("will probably" in x for x in lines )) 83 84 85class Main8GB (Base ): 86"""setup.main() for UD-Q4_K_XL on an 8 GB NVIDIA card: the written config is the one that starts.""" 87 88def arg (self ,cfg ,name ): 89return cfg ["args" ][cfg ["args" ].index (name )+ 1 ] 90 91def test_nvidia_8gb_writes_a_config_that_fits_and_says_so (self ): 92code ,out ,cfg = self .main (["--context" ,"8192" ],vram = 8.0 ) 93self .assertEqual (code ,0 ,out ) 94self .assertNotIn ("--mtp" ,cfg ["args" ])# no draft layer on the GPU 95self .assertEqual (self .arg (cfg ,"--max-context" ),"8192" )# the context the user asked for 96self .assertIn ("would not start on this 8 GB card" ,out ) 97self .assertIn ("no MTP draft layer" ,out ) 98self .assertIn ("IQ3_XXS or IQ3_S" ,out ) 99self .assertIn ("very slow" ,out ) 100 101def test_a_context_and_kv_the_user_chose_are_kept (self ): 102code ,out ,cfg = self .main (["--context" ,"16384" ,"--kv" ,"int8" ],vram = 8.0 ) 103self .assertEqual (code ,0 ,out ) 104self .assertEqual (self .arg (cfg ,"--max-context" ),"16384" ) 105self .assertEqual (self .arg (cfg ,"--kv" ),"int8" ) 106 107def test_the_default_context_on_8gb_starts_without_asking (self ): 108code ,out ,cfg = self .main ([],vram = 8.0 ) 109self .assertEqual (code ,0 ,out ) 110self .assertEqual (self .arg (cfg ,"--max-context" ),"8192" ) 111self .assertNotIn ("--mtp" ,cfg ["args" ]) 112 113def test_a_12gb_card_is_untouched (self ): 114code ,out ,cfg = self .main (["--context" ,"8192" ],vram = 11.9 ) 115self .assertEqual (code ,0 ,out ) 116self .assertIn ("--mtp" ,cfg ["args" ]) 117self .assertNotIn ("would not start" ,out ) 118self .assertNotIn ("very slow" ,out ) 119 120def test_amd_8gb (self ): 121r = [{"index" :0 ,"name" :"AMD Radeon RX 7600" ,"vram_gb" :8.0 ,"arch" :"gfx1102" ,"driver" :"amdgpu" }] 122with mock .patch .object (setup ,"get_prebuilt_hip" ,lambda * a ,** k :self .t / "engine" ), \ 123mock .patch .object (setup ,"build_engine_hip" ,lambda * a ,** k :self .t / "engine" ),mock .patch .object (setup ,"hip_card" ,lambda eng ,gpu ,listed :gpu ): 124code ,out ,cfg = self .main (["--context" ,"8192" ,"--backend" ,"hip" ,"--vision" ,"no" ],amd = r ) 125self .assertEqual (code ,0 ,out ) 126self .assertNotIn ("--mtp" ,cfg ["args" ]) 127self .assertIn ("IQ3_XXS or IQ3_S" ,out ) 128 129 130if __name__ == "__main__" : 131unittest .main ()