okami.mom/strata

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

Niko1221Merge branch 'f-0212-int' into f-0212-splitdf2c65b9f8be

main
5.8 KiB101 linesraw
1"""The NVIDIA split's prompt-path defaults (src/split), as pictures of the sources (no GPU).
2
30.2.10.1 (owner 11-03, "do all 5"):
4  * FNX_PF_ROWS: 8192 prompt rows a chunk on NVIDIA (the expert kernels cost ~30 percent less a token); FNX_PF_ROWS=4096 is the old chunk.
5  * FNX_PF_HCSPLIT: the hyper-connection read of a chunk runs on one card's rows each. Mode 2 is the default, used only when every card's start-up calibration
6    found the cublasLt algorithm (else the read stays on both cards); mode 1 is bit-identical to the unsplit read (whole 4096-row chunks only); 0 turns it off.
7  * FNX_PFX_ATTN_I8 on, FNX_PFX_GDN_TC on except for the IQ3_S-type files (routed experts in IQ2_S/IQ3_XXS/IQ3_S/Q2_0), decided in the engine from the file;
8    the variables override both ways.
9"""
10
11from __future__ import annotations
12
13import re
14import unittest
15from pathlib import Path
16
17ROOT = Path(__file__).resolve().parents[2]
18SRC = ROOT / "src" / "split" / "src"
19
20
21def text(name: str) -> str:
22    return (SRC / name).read_text(encoding="utf-8")
23
24
25class HcSplit(unittest.TestCase):
26    def test_default_is_on_and_needs_the_calibrated_algorithm(self):
27        s = text("prefill.cu")
28        self.assertRegex(s, r'p->hcs_mode = \(int\) env_int\("FNX_PF_HCSPLIT", 2\)')
29        # the split runs on every card or on none, and only with the calibrated algorithm on every card (either mode)
30        self.assertRegex(s, r"all = all && pf_\[c\]->hcs && pf_\[c\]->down_lt;")
31        # mode 1 (exact): a pass is split only when every chunk is a whole 4096 rows, the sizes the algorithm was calibrated at
32        self.assertRegex(s, r"hcs_mode == 1 && \(!pf_\[0\]->down_lt \|\| \(int\) ck\.tokens\.size\(\) != 4096\)")
33        self.assertIn("if (n != 4096 && n != 2048) return false;", s)
34        self.assertRegex(s, r'env_int\("FNX_PF_HCSPLIT_MIN", 3\)')   # fewer than 3 chunks in a pass: not split
35
36    def test_nvidia_only(self):
37        s = text("prefill.cu")
38        i = s.index('p->hcs_mode = (int) env_int("FNX_PF_HCSPLIT"')
39        self.assertIn("#if !FNX_HIP", s[i - 200:i])   # the AMD build never splits the read
40        self.assertIn("NCARD == 2", s[i:i + 400])
41        self.assertIn("CUDA::cublasLt", (ROOT / "src" / "split" / "CMakeLists.txt").read_text(encoding="utf-8"))
42
43    def test_chunk_of_8192_rows_on_nvidia_only_and_sized_to_the_free_memory(self):
44        e = text("engine.cu")
45        i = e.index("        rows = 8192;")
46        self.assertIn("#if !FNX_HIP", e[i - 80:i])                       # the AMD build keeps 4096
47        self.assertIn('env_int("FNX_PF_ROWS", rows)', e)                  # set by hand: used as given
48        # the sizes tried, largest first, with the bytes they take per card (measured): UD-Q4_K_XL on 2 x 48 GB keeps only 6.5 GB and must not take 8192 x 4
49        self.assertIn("{{8192, 4, 4148}, {8192, 2, 3028}, {4096, 4, 1673}, {4096, 2, 1112}}", e)
50        self.assertIn("pf_pass_pick_ > 0 ? pf_pass_pick_", text("prefill.cu"))
51
52    def test_the_sum_before_a_split_read_is_a_reduce_scatter_only(self):
53        s = text("pf_common.cuh")
54        self.assertIn("bool own_out = false", s)
55        self.assertRegex(s, r"o\.k == PfRingOp::PASS \|\| o\.k == PfRingOp::SIGNALA \|\| o\.k == PfRingOp::WAITA")
56
57    def test_passes_stay_short_while_other_requests_decode(self):
58        sf = (ROOT / "src" / "split" / "tools" / "sfnx.cpp").read_text(encoding="utf-8")
59        self.assertEqual(len(re.findall(r"e\.set_pass_rows\(any_decoding\(\) \? 8192 : 0\)", sf)), 2)   # solo and batch admissions
60        self.assertIn("void set_pass_rows(int rows)", text("engine.hpp"))
61
62
63class PerFileSwitches(unittest.TestCase):
64    def test_attention_int8_is_decided_by_the_files_expert_types(self):
65        k, e = text("kernels.cu"), text("engine.cu")
66        # IQ3_S-type files: on (the suite shows no loss); the others (UD-Q4_K_XL lost agent-A8 and A10 with it): off; the variable overrides
67        self.assertRegex(k, r'i8_env = \(int\) env_int\("FNX_PFX_ATTN_I8", -1\)')
68        self.assertRegex(k, r"i8_env >= 0 \? i8_env != 0 : attn_i8_default\(\)")
69        self.assertIn("attn_i8_set_default(m_.g.iq_experts());", e)
70
71    def test_gdn_scan_is_decided_by_the_files_expert_types(self):
72        k, g, m = text("kernels.cu"), text("gguf.cpp"), text("engine.cu")
73        # the rule: IQ-type routed experts (the set convert_small_tensors converts) -> off; the variable overrides
74        self.assertRegex(g, r"mixed \|= kv\.second\.type == T_IQ2_S \|\| kv\.second\.type == T_IQ3_XXS \|\| kv\.second\.type == T_IQ3_S \|\| kv\.second\.type == T_Q2_0;")
75        self.assertIn("iq_experts_ = mixed;", g)
76        self.assertIn("gdn_tc_set_default(!m_.g.iq_experts());", m)
77        self.assertRegex(k, r'gdn_tc_env = \(int\) fnx::env_int\("FNX_PFX_GDN_TC", -1\)')
78        self.assertRegex(k, r"gdn_tc_env >= 0 \? gdn_tc_env != 0 : g_gdn_tc_default\.load\(\)")
79        self.assertIn("g_gdn_tc_default{true}", k)
80
81    def test_the_rule_in_python_terms(self):
82        """The same rule as a function, over the type names a GGUF's routed experts carry (what strata_control/split_engine.py reads)."""
83        iq = {"IQ2_S", "IQ3_XXS", "IQ3_S", "Q2_0"}
84
85        def gdn_tc(expert_types, env=None):
86            if env is not None:
87                return env != "0"
88            return not (set(expert_types) & iq)
89
90        self.assertFalse(gdn_tc({"IQ4_NL", "IQ2_S", "IQ3_XXS", "IQ3_S", "Q4_0"}))   # the ISTA IQ3_S set
91        self.assertTrue(gdn_tc({"Q4_K", "Q5_K", "Q5_1", "Q8_0"}))                    # UD-Q4_K_XL
92        self.assertTrue(gdn_tc({"Q8_0"}))
93        self.assertTrue(gdn_tc({"IQ3_S"}, env="1"))
94        self.assertFalse(gdn_tc({"Q4_K"}, env="0"))
95
96    def test_one_hop_stays_a_screen_switch(self):
97        self.assertRegex(text("engine.cu"), r'env_int\("FNX_ONEHOP", 1\) == 2')
98
99
100if __name__ == "__main__":
101    unittest.main()