diff --git a/docs/models/inkling.md b/docs/models/inkling.md index e2d87f09e..6130660a4 100644 --- a/docs/models/inkling.md +++ b/docs/models/inkling.md @@ -53,10 +53,18 @@ only part of the experts was not measured. Three runs each, 32 tokens written af On a laptop with an RTX 4070 Laptop (8 GB), a Core i7-14700HX and 32 GB of RAM, on Windows, UD-Q3_K_XL has 1,216 of its 3,072 experts on the card and the rest are computed on the CPU. On a card that does not hold every expert, setup -gives Inkling 2 Mini four settings that read fewer bytes for each token: `--attn-requant q5_0` (the attention's -matrices in 5 bits instead of 8), `--loop-bf16` (the loop controllers in BF16 instead of F32), `--tail-skip 4` and -`--expert-swaps 0`. A card that holds the whole model gets none of them. The fastest of three runs of the README's -benchmark: +asks whether to use three small-card settings that read fewer bytes for each token and lower answer quality: +`--attn-requant q5_0` (the attention's matrices in 5 bits instead of 8), `--loop-bf16` (the loop controllers in BF16 +instead of F32) and `--tail-skip 4` (a token's experts ranked fifth to eighth that are not on the card are not +computed). The default is no, and `--yes` takes no. `--inkling-small-card on` or `off` answers it without asking, and +a later setup run keeps the answer (`"small_card"` in the model's run config). The first two round more coarsely and +the third skips computation. With the three, the same most likely token comes at 94.1-97.0% of the steps (IQ4_NL and +UD-Q2_K_XL on an RTX 2070 Super 8 GB and a Quadro RTX 4000 8 GB), against 98.3-99.3% for a second run without them +([one setting at a time](#setups-four-settings-one-at-a-time)). A card that holds the whole model is not asked and +gets none of them. Expert swaps stay on: with `--expert-swaps 0` the experts the +card holds stay the ones it loaded, fewer of a token's experts are found there, and the CPU computes more of them +(16-23% slower writing after a 302-token prompt on the 8 and 16 GB cards measured, about the same after 8,192). +The fastest of three runs of the README's benchmark, measured when setup also gave `--expert-swaps 0`: | UD-Q3_K_XL | Writes in a short chat | Reads a 30,902-token prompt | Writes after it | | --- | ---: | ---: | ---: | @@ -106,7 +114,9 @@ for this model, and the installer writes `"parallel": 1`. Its license is Apache ./setup.sh --family inkling ``` -Add `--model UD-Q3_K_XL` or `--model UD-Q2_K_XL` for a smaller size. +Add `--model UD-Q3_K_XL` or `--model UD-Q2_K_XL` for a smaller size. On a card that does not hold every expert, +setup asks about the faster small-card settings, which lower answer quality (off unless you say yes; +`--inkling-small-card on|off`, see [How fast is it?](#how-fast-is-it)). ## Which model should I pick? diff --git a/terminal/setup.py b/terminal/setup.py index bbdfcf1be..b50b7ed81 100644 --- a/terminal/setup.py +++ b/terminal/setup.py @@ -37,7 +37,7 @@ --models-dir DIR, --gguf-dir DIR (use GGUF files you already have), --build (compile instead of the ready-made engine), --max-compatibility (with --build on Windows: an engine that also runs on other PCs), --check (only check this PC), --resident-budget-gib N (UD-Q4_K_XL's or UD-IQ4_XS's experts in RAM), ---kv-streaming on|off|auto. +--kv-streaming on|off|auto, --inkling-small-card on|off. Setup recommends, it never forces: the recommended answers are the defaults (--yes, or Enter), and a bigger choice than it recommends - a longer context, more GPUs, a bigger RAM budget, a size it thinks will not fit - is kept, with @@ -879,7 +879,7 @@ def ms_meta(repo: str, path: str): # for the family whose answers it moves no more than the Strata project's default moves its own (docs/BACKENDS.md). # "small_card_args": engine flags for a card that does not hold every expert of the size (poly_held_gb()), where a # token is bound by the bytes it reads from VRAM and by the experts the CPU computes; a card that holds them all - # runs as before. + # runs as before. They change the answers, so setup asks first (off unless chosen, poly_small_card()). # "laptop_tps": tokens/s written after a prompt that fills each context on the 8 GB laptop, where the family's # differ from the others'. # "batch_gb": the VRAM the engine's prompt batch takes for a second 1,024 tokens, measured on the RTX 5090; a card @@ -947,7 +947,10 @@ def ms_meta(repo: str, path: str): # UD-Q3_K_XL on the RTX 4070 Laptop (8 GB), the README's benchmark, medians of 3 rounds: 43.7 -> 55.8 # tokens/s written in the short chat, 439 -> 466 read and 33.6 -> 41.9 written after the long prompt; # the same most likely token as without them at 96.7% of 1,280 steps, the default run again at 98.3% - "small_card_args": ["--attn-requant", "q5_0", "--loop-bf16", "--tail-skip", "4", "--expert-swaps", "0"], + # (measured with --expert-swaps 0 as well, since dropped: it keeps the experts held as they were loaded, + # so the CPU computes more of a token's experts; 16-23% slower writing after a 302-token prompt on the + # 8 and 16 GB cards measured) + "small_card_args": ["--attn-requant", "q5_0", "--loop-bf16", "--tail-skip", "4"], "models": {"UD-Q4_K_XL": {"about": "4-bit experts (NVFP4, Unsloth Dynamic), 8-bit where they write", "download_gb": 17.6, "ram_gb": 21, "arena_gb": 15.797846016, "vram_beside_gb": 3.7}, @@ -8255,7 +8258,7 @@ def write_config(path: Path, cfg: dict): # then applies the person's options over the generated ones SETUP_KEYS = frozenset({"exe", "args", "cwd", "tokenizer", "model_name", "log", "lib_dirs", "port", "backend", "env", "gpu", "gpus_asked", "gpus_by", "layer_split", "host", "api_key", "draft_vocab", "vision", "args_written", - "display_name", "cuda", "ui_budget_work_gib", "shape_title"}) + "display_name", "cuda", "ui_budget_work_gib", "shape_title", "small_card"}) SETUP_ENV = frozenset({"STRATA_HIPBLASLT_TUNING", "STRATA_ROCBLAS_TUNING", "STRATA_RESIDENT_PIN", "STRATA_NO_ARENA_THP", "STRATA_MMQ_KQ", "STRATA_HC_Q8", "STRATA_ROUTE_TAIL_SKIP"}) | frozenset(GFX1103_SWITCHES) | frozenset(GFX1150_SWITCHES) # the "env" entries setup writes (the gfx1103 switches are setup's too: a re-run refreshes them, they do not linger) # 0.1.44 added these three, written for Unsloth's files only (unsloth_default_env). In any other model's config they are the @@ -10150,6 +10153,45 @@ def poly_saved_conv(paths) -> int | str | None: return None +def poly_saved_small_card(paths) -> str | None: + """"small_card" of an earlier run config of this model (the first of `paths` there is): "on" or "off"; None + without one, or when it has no answer (a config of an older setup, which added the settings without asking).""" + for p in paths: + if p is None or not p.is_file(): + continue + try: + v = json.loads(p.read_text(encoding="utf-8-sig")).get("small_card") + except (OSError, ValueError, AttributeError): + continue + return v if v in ("on", "off") else None + return None + + +def poly_small_card(a, fam, model, d, vram, multi, paths) -> str | None: + """A family's "small_card_args" on one card that does not hold every expert of the size: "on" or "off". They + are faster there and change the answers (lossy rounding, and --tail-skip leaves some of a token's experts out: + docs/models/inkling.md), so they are a choice: --inkling-small-card, else the earlier run config's answer, else + asked (Enter and --yes: off). None where the question does not apply.""" + asked = getattr(a, "inkling_small_card", None) + if not fam.get("small_card_args"): + return None + if multi or poly_held_gb(d, vram) >= d["arena_gb"]: + if asked == "on": + say(f" --inkling-small-card on: {'these cards hold' if multi else 'this card holds'} what {model} " + "needs without them; they are left out") + return None + if asked: + return asked + saved = poly_saved_small_card(paths) + if saved: + return saved + say() + say(f" Your card can't hold every expert of {model}. The small-card settings make it faster here, but they lower") + say(" answer quality: they round the attention and loop weights more coarsely and skip some experts per token") + say(" (docs/models/inkling.md). --inkling-small-card on|off answers this without asking.") + return "on" if ask("Use the faster small-card settings?", ["y", "n"], "n", a.yes) == "y" else "off" + + def poly_asset() -> str: """The ready-made strata-poly's asset for this checkout: the engine and the image encoder are both in it.""" return POLY_ASSET.format(src=source_hash(ENGINE_SOURCES + VISION_SOURCES)) @@ -10514,6 +10556,7 @@ def poly_setup(a, family, gpu, hip, cuda_tk, multi, ram, data, elsewhere, port, ("--vision-tokens", not sees and a.vision_tokens is not None), ("--kv", a.kv and a.kv != "fp16" and family not in POLY_KV.get(a.kv, ())), ("--kv-streaming", a.kv_streaming != "auto" and family not in POLY_KV_STREAMING), + ("--inkling-small-card", getattr(a, "inkling_small_card", None) and not fam.get("small_card_args")), ("--rope-scaling", a.rope_scaling), ("--rope-scale", a.rope_scale is not None), ("--low-ram", a.low_ram != "auto"), ("--resident-budget-gib", a.resident_budget_gib is not None), @@ -10641,6 +10684,9 @@ def poly_setup(a, family, gpu, hip, cuda_tk, multi, ram, data, elsewhere, port, ok(f"conversation cache: 40% of the RAM free once the model is loaded, up to {POLY_CONV_CACHE_MIB} MiB") else: ok("conversation cache: " + (f"up to {conv} MiB of RAM" if conv else "off (the last conversation is kept)")) + # Inkling 2's small-card settings: asked once, then kept as the model's run config has it ("small_card") + small = poly_small_card(a, fam, model, d, vram, multi, + [cfg_path, adopted if adopted is not None and adopted.name == cfg_path.name else None]) # one file, or the numbered files of a split GGUF: the engine is given the first and finds the others by name models_dir = Path(a.gguf_dir) if a.gguf_dir else Path(a.models_dir) / tag @@ -10798,10 +10844,13 @@ def _skip_or_verify(path, sha_key): "that much less)") if fam.get("tail_skip"): args += ["--tail-skip", str(fam["tail_skip"])] - if fam.get("small_card_args") and not multi and poly_held_gb(d, vram) < d["arena_gb"]: + if small == "on": args += fam["small_card_args"] - ok(f"this card holds part of {model}'s experts: {' '.join(fam['small_card_args'])} (fewer bytes a token, " - "and the answers move a little)") + ok(f"this card holds part of {model}'s experts: {' '.join(fam['small_card_args'])} (faster, lower answer " + "quality; --inkling-small-card off takes them out)") + elif small == "off": + ok(f"this card holds part of {model}'s experts: the small-card settings are off (every expert is computed; " + "--inkling-small-card on: faster, lower answer quality)") if kv: args += ["--kv", kv] ok(f"attention caches stored as {kv} (--kv in the config, docs/Q8_KV.md; --kv fp16 at setup takes it out)") @@ -10831,6 +10880,8 @@ def _skip_or_verify(path, sha_key): "model_name": base.lower() if base else f"{fam['name']}-{model.lower()}", "display_name": base if base else f"{title} {model}", "log": str(ROOT / f"strata-{tag.lower()}.log"), "lib_dirs": lib_dirs, "port": port} + if small: + cfg["small_card"] = small # the answer, so a setup run again keeps it (poly_saved_small_card) if fam.get("tool_call_recovery"): cfg["tool_call_recovery"] = True ok("its tool calls are JSON inside the wrapper: they are read with the recovery (tool_call_recovery in the " @@ -12533,6 +12584,11 @@ def main() -> int: ap.add_argument("--kv-streaming", choices=["auto", "on", "off"], default="auto", help="from a 64K context: keep the KV cache in RAM and only the attention's window in VRAM (more " "experts fit on the GPU); auto: when the RAM has room for it") + ap.add_argument("--inkling-small-card", choices=["on", "off"], + help="Inkling 2 Mini on a card that does not hold every expert of the size: on = the faster " + "small-card settings (--attn-requant q5_0 --loop-bf16 --tail-skip 4), which lower answer " + "quality; off = none of them (the default). Without it setup asks, and a later setup run " + "keeps the answer (\"small_card\" in the run config)") ap.add_argument("--backend", choices=["cuda", "hip", "sycl"], help="cuda = NVIDIA (default), hip = AMD RX 7900 / 7800 / 7700 XT, RX 9060 XT / 9070 / AI PRO R9700 on " "Linux or Windows (chosen by itself when the PC has no NVIDIA card PolyStrata can use), " diff --git a/terminal/tools/test_setup_inkling.py b/terminal/tools/test_setup_inkling.py index 7de83a407..31585b5a5 100644 --- a/terminal/tools/test_setup_inkling.py +++ b/terminal/tools/test_setup_inkling.py @@ -64,17 +64,110 @@ def test_install_each_size(self): self.assertNotIn("vision", cfg) self.assertNotIn("--parallel", out) # not offered - def test_a_card_that_holds_part_of_the_experts_gets_the_small_card_flags(self): - # an 8 GB card: Q5_0 attention, BF16 loop controllers, tail skip 4, no expert swaps - code, out, _ = self.main(family="inkling", model="UD-Q3_K_XL", ram=32.0, vram=8188 / 1024) + SMALL = ("--attn-requant", "--loop-bf16", "--tail-skip", "--expert-swaps") + CFG = "strata-inkling-ud-q3_k_xl.json" + + def small(self, argv=(), answers=None): + """UD-Q3_K_XL on an 8 GB card, which holds part of its experts: the run config's args and "small_card".""" + code, out, _ = self.main(argv, family="inkling", model="UD-Q3_K_XL", ram=32.0, vram=8188 / 1024, + answers=answers) self.assertEqual(code, 0, out) - args = json.loads((self.t / "strata-inkling-ud-q3_k_xl.json").read_text())["args"] + cfg = json.loads((self.t / self.CFG).read_text()) + return cfg["args"], cfg.get("small_card"), out + + def assert_on(self, args): + # Q5_0 attention, BF16 loop controllers, tail skip 4; expert swaps stay on (the default) self.assertEqual(args[args.index("--attn-requant") + 1], "q5_0") self.assertIn("--loop-bf16", args) self.assertEqual(args[args.index("--tail-skip") + 1], "4") - self.assertEqual(args[args.index("--expert-swaps") + 1], "0") self.assertEqual(args.count("--tail-skip"), 1) - self.assertIn("holds part of", out) + self.assertNotIn("--expert-swaps", args) + + def assert_off(self, args): + for flag in self.SMALL: + self.assertNotIn(flag, args) + + def asked_small(self): + return [q for q in self.asked if "small-card" in q] + + def test_small_card_asked_yes(self): + args, small, out = self.small(answers={"small-card": "y"}) + self.assertEqual(len(self.asked_small()), 1) + self.assert_on(args) + self.assertEqual(small, "on") + self.assertIn("lower answer quality", out) + + def test_small_card_asked_no(self): + args, small, _ = self.small(answers={"small-card": "n"}) + self.assertEqual(len(self.asked_small()), 1) + self.assert_off(args) + self.assertEqual(small, "off") + + def test_small_card_asked_enter_is_off(self): + args, small, _ = self.small(answers={}) + self.assertEqual(len(self.asked_small()), 1) + self.assert_off(args) + self.assertEqual(small, "off") + + def test_small_card_yes_is_off(self): + # --yes: not asked (the harness fails on any question), the recommended answer is off + args, small, out = self.small() + self.assert_off(args) + self.assertEqual(small, "off") + self.assertIn("small-card settings are off", out) + + def test_small_card_flag(self): + for choice, check in (("on", self.assert_on), ("off", self.assert_off)): + with self.subTest(choice): + (self.t / self.CFG).unlink(missing_ok=True) + args, small, _ = self.small(("--inkling-small-card", choice), answers={"small-card": "AskedAnyway"}) + self.assertEqual(self.asked_small(), []) # the flag is the answer: not asked + check(args) + self.assertEqual(small, choice) + + def test_small_card_rerun_keeps_the_answer(self): + for first, check in (("y", self.assert_on), ("n", self.assert_off)): + with self.subTest(first): + (self.t / self.CFG).unlink(missing_ok=True) + self.small(answers={"small-card": first}) + for argv, answers in (((), {}), ((), None)): # asked the other questions, and --yes + args, small, _ = self.small(argv, answers=answers) + if answers is not None: + self.assertEqual(self.asked_small(), []) + check(args) + self.assertEqual(small, "on" if first == "y" else "off") + # the flag changes a kept answer, and the new one is kept + args, small, _ = self.small(("--inkling-small-card", "on")) + self.assert_on(args) + args, small, _ = self.small() + self.assert_on(args) + self.assertEqual(small, "on") + + def test_small_card_config_of_an_older_setup_is_asked_again(self): + # a run config with the settings and no "small_card" (setup added them without asking): asked, off by default + self.small(("--inkling-small-card", "on")) + cfg = json.loads((self.t / self.CFG).read_text()) + del cfg["small_card"] + (self.t / self.CFG).write_text(json.dumps(cfg)) + args, small, _ = self.small(answers={}) + self.assertEqual(len(self.asked_small()), 1) + self.assert_off(args) + self.assertEqual(small, "off") + + def test_small_card_not_asked_on_a_card_that_holds_every_expert(self): + code, out, _ = self.main(("--inkling-small-card", "on"), family="inkling", model="UD-Q3_K_XL", ram=64.0, + vram=32607 / 1024, answers={}) + self.assertEqual(code, 0, out) + self.assertEqual(self.asked_small(), []) + cfg = json.loads((self.t / self.CFG).read_text()) + self.assert_off(cfg["args"]) + self.assertNotIn("small_card", cfg) + self.assertIn("they are left out", out) + + def test_small_card_flag_of_another_family_is_not_used(self): + code, out, cfg = self.main(("--inkling-small-card", "on")) # GLM, the harness's family + self.assertEqual(code, 0, out) + self.assertRegex(out, r"not used with .*--inkling-small-card") def test_requests_at_once_are_not_taken(self): code, out, _ = self.main(("--parallel", "4"), family="inkling", model="UD-Q3_K_XL", ram=64.0, vram=32607 / 1024)