okami.mom/strata

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

Niko1221merge f-145-int 194c26881 (model-card sampling defaults, web sampling, answer room, thinking budget) into f-split-int07fe758ac

main
4.3 KiB108 linesraw
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
7    python -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):
25    def generate(self, ids, max_new, sampling, cancel, embeddings=None):
26        self.sampling = dict(sampling or {})
27        yield from super().generate(ids, max_new, sampling, cancel, embeddings)
28
29
30class ModelCardSampling(unittest.TestCase):
31    @classmethod
32    def setUpClass(cls):
33        cls.tok = ByteTokenizer()
34        cls.engine = Recording(cls.tok, "<think>\nhm\n</think>\n\nok", max_context=8192)
35        cls.svc = Service(cls.engine, cls.tok, ChatTemplate(ROOT / "serve/chat_template.jinja"))
36        cls.httpd = serve(cls.svc, port=0)
37        cls.base = f"http://127.0.0.1:{cls.httpd.server_address[1]}"
38
39    @classmethod
40    def tearDownClass(cls):
41        cls.httpd.shutdown()
42        cls.httpd.server_close()
43
44    def setUp(self):
45        self.svc.model_arch = "qwen4exp"
46        self.svc.sampling_defaults = {}
47        self.svc.shared = {}
48        self.svc.config_effort = None
49
50    def tearDown(self):
51        self.svc.model_arch = None
52
53    def keys(self, **body):
54        body = {"model": "m", "messages": [{"role": "user", "content": "hi"}], "max_tokens": 8, **body}
55        self.engine.sampling = {}
56        req = urllib.request.Request(self.base + "/v1/chat/completions", data=json.dumps(body).encode(),
57                                     headers={"Content-Type": "application/json"})
58        urllib.request.urlopen(req, timeout=10).read()
59        return StrataEngine.sampling_keys(self.engine.sampling)
60
61    def test_tables(self):
62        self.assertEqual(model_card_sampling("qwen4exp", True), {"temperature": 1.0, "top_p": 0.95, "top_k": 20})
63        self.assertEqual(model_card_sampling("qwen4exp", False),
64                         {"temperature": 0.7, "top_p": 0.8, "top_k": 20, "presence_penalty": 1.5})
65        self.assertEqual(model_card_sampling("qwen35moe", True)["presence_penalty"], 1.5)
66        self.assertEqual(model_card_sampling(None, True), {})
67        self.assertEqual(model_card_sampling("unknown", False), {})
68
69    def test_thinking_default(self):
70        k = self.keys()
71        self.assertIn("temperature=1.0", k)
72        self.assertIn("top_p=0.95", k)
73        self.assertIn("top_k=20", k)
74        self.assertNotIn("penalty_present", k)
75
76    def test_non_thinking_default(self):
77        k = self.keys(reasoning_effort="none")
78        self.assertIn("temperature=0.7", k)
79        self.assertIn("top_p=0.8", k)
80        self.assertIn("penalty_present=1.5", k)
81
82    def test_request_wins_and_zero_is_greedy(self):
83        k = self.keys(temperature=0)
84        self.assertNotIn("temperature", k)
85        self.assertIn("top_k=20", k)
86        k = self.keys(temperature=0.3, top_p=0.5)
87        self.assertIn("temperature=0.3", k)
88        self.assertIn("top_p=0.5", k)
89
90    def test_config_block_and_shared_settings_win_over_the_card(self):
91        self.svc.sampling_defaults = {"temperature": 0.6, "top_k": 40}
92        k = self.keys()
93        self.assertIn("temperature=0.6", k)
94        self.assertIn("top_k=40", k)
95        self.assertIn("top_p=0.95", k)
96        self.svc.shared = {"temperature": 0.2}
97        self.assertIn("temperature=0.2", self.keys())
98        self.svc.sampling_defaults = {"temperature": 0}
99        self.svc.shared = {}
100        self.assertNotIn("temperature", self.keys())     # a config that asks for greedy gets it
101
102    def test_no_family_no_defaults(self):
103        self.svc.model_arch = None
104        self.assertEqual(self.keys(), "")
105
106
107if __name__ == "__main__":
108    unittest.main()