okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
07fe758ac
main
1"""serve/test_model_card_sampling.py - a request that sends no sampling gets the model card's, not greedy (mock engine). 2 3Qwen's "Best Practices": thinking mode temperature 1.0 / top_p 0.95 / top_k 20, non-thinking 0.7 / 0.8 / 20 with 4presence_penalty 1.5. The request's own fields win, then the Chat settings shared with apps, then the config's 5`sampling` block, then the card; temperature 0 asked for is still greedy. 6 7python -m unittest serve.test_model_card_sampling -v 8""" 9from __future__import annotations 10 11import json 12import sys 13import unittest 14import urllib .request 15from pathlib import Path 16 17sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 18from serve .frontend import ChatTemplate # noqa: E402 19from serve .server import ByteTokenizer ,MockEngine ,Service ,StrataEngine ,model_card_sampling ,serve # noqa: E402 20 21ROOT = Path (__file__ ).resolve ().parents [1 ] 22 23 24class Recording (MockEngine ): 25def generate (self ,ids ,max_new ,sampling ,cancel ,embeddings = None ): 26self .sampling = dict (sampling or {}) 27yield from super ().generate (ids ,max_new ,sampling ,cancel ,embeddings ) 28 29 30class ModelCardSampling (unittest .TestCase ): 31@classmethod 32def setUpClass (cls ): 33cls .tok = ByteTokenizer () 34cls .engine = Recording (cls .tok ,"<think>\nhm\n</think>\n\nok" ,max_context = 8192 ) 35cls .svc = Service (cls .engine ,cls .tok ,ChatTemplate (ROOT / "serve/chat_template.jinja" )) 36cls .httpd = serve (cls .svc ,port = 0 ) 37cls .base = f"http://127.0.0.1: { cls . httpd . server_address [ 1 ] } " 38 39@classmethod 40def tearDownClass (cls ): 41cls .httpd .shutdown () 42cls .httpd .server_close () 43 44def setUp (self ): 45self .svc .model_arch = "qwen4exp" 46self .svc .sampling_defaults = {} 47self .svc .shared = {} 48self .svc .config_effort = None 49 50def tearDown (self ): 51self .svc .model_arch = None 52 53def keys (self ,** body ): 54body = {"model" :"m" ,"messages" : [{"role" :"user" ,"content" :"hi" }],"max_tokens" :8 ,** body } 55self .engine .sampling = {} 56req = urllib .request .Request (self .base + "/v1/chat/completions" ,data = json .dumps (body ).encode (), 57headers = {"Content-Type" :"application/json" }) 58urllib .request .urlopen (req ,timeout = 10 ).read () 59return StrataEngine .sampling_keys (self .engine .sampling ) 60 61def test_tables (self ): 62self .assertEqual (model_card_sampling ("qwen4exp" ,True ), {"temperature" :1.0 ,"top_p" :0.95 ,"top_k" :20 }) 63self .assertEqual (model_card_sampling ("qwen4exp" ,False ), 64 {"temperature" :0.7 ,"top_p" :0.8 ,"top_k" :20 ,"presence_penalty" :1.5 }) 65self .assertEqual (model_card_sampling ("qwen35moe" ,True )["presence_penalty" ],1.5 ) 66self .assertEqual (model_card_sampling (None ,True ), {}) 67self .assertEqual (model_card_sampling ("unknown" ,False ), {}) 68 69def test_thinking_default (self ): 70k = self .keys () 71self .assertIn ("temperature=1.0" ,k ) 72self .assertIn ("top_p=0.95" ,k ) 73self .assertIn ("top_k=20" ,k ) 74self .assertNotIn ("penalty_present" ,k ) 75 76def test_non_thinking_default (self ): 77k = self .keys (reasoning_effort = "none" ) 78self .assertIn ("temperature=0.7" ,k ) 79self .assertIn ("top_p=0.8" ,k ) 80self .assertIn ("penalty_present=1.5" ,k ) 81 82def test_request_wins_and_zero_is_greedy (self ): 83k = self .keys (temperature = 0 ) 84self .assertNotIn ("temperature" ,k ) 85self .assertIn ("top_k=20" ,k ) 86k = self .keys (temperature = 0.3 ,top_p = 0.5 ) 87self .assertIn ("temperature=0.3" ,k ) 88self .assertIn ("top_p=0.5" ,k ) 89 90def test_config_block_and_shared_settings_win_over_the_card (self ): 91self .svc .sampling_defaults = {"temperature" :0.6 ,"top_k" :40 } 92k = self .keys () 93self .assertIn ("temperature=0.6" ,k ) 94self .assertIn ("top_k=40" ,k ) 95self .assertIn ("top_p=0.95" ,k ) 96self .svc .shared = {"temperature" :0.2 } 97self .assertIn ("temperature=0.2" ,self .keys ()) 98self .svc .sampling_defaults = {"temperature" :0 } 99self .svc .shared = {} 100self .assertNotIn ("temperature" ,self .keys ())# a config that asks for greedy gets it 101 102def test_no_family_no_defaults (self ): 103self .svc .model_arch = None 104self .assertEqual (self .keys (),"" ) 105 106 107if __name__ == "__main__" : 108unittest .main ()