Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Hy4 1.9.3: ship the depth gate (learn mode) and row-batched MLA decode attention on by default

YanissAmza1b7a9083e2e
diff --git a/tests/test_hy4_gate_defaults.py b/tests/test_hy4_gate_defaults.py
new file mode 100644
index 0000000..f0ad644
--- /dev/null
+++ b/tests/test_hy4_gate_defaults.py
@@ -0,0 +1,46 @@
+"""zero-GPU check: 1.9.3 ships the depth gate (learn mode) and the row-batched MLA decode attention on; the gate steps aside where it cannot run
+(slots > 1, num-draft != 3, a draft policy); an explicit env value wins; the engine boots with the shipped gate."""
+import os
+import sys
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+TREE = os.path.dirname(HERE)
+sys.path.insert(0, os.path.join(TREE, "tools", "hy4"))
+KEYS = ("HY4_SPEC2_GATE", "HY4_SPEC2_MODE", "HY4_MLA_ROWS2", "HY4_DRAFT_POLICY", "HY4_SPEC2_KB")
+
+
+def _apply(env, **kw):
+ for k in KEYS:
+ os.environ.pop(k, None)
+ os.environ.update(env)
+ import serve
+ serve.apply_env(16384, **kw)
+ return {k: os.environ.get(k) for k in KEYS}
+
+
+def test_defaults_on():
+ e = _apply({})
+ assert (e["HY4_SPEC2_GATE"], e["HY4_SPEC2_MODE"], e["HY4_MLA_ROWS2"]) == ("1", "learn", "1")
+
+
+def test_gate_and_mla2_off_with_slots():
+ e = _apply({}, slots=2)
+ assert (e["HY4_SPEC2_GATE"], e["HY4_MLA_ROWS2"]) == ("0", "0")
+
+
+def test_gate_off_when_it_cannot_run():
+ assert _apply({}, num_draft=4)["HY4_SPEC2_GATE"] == "0"
+ assert _apply({"HY4_DRAFT_POLICY": "cost"})["HY4_SPEC2_GATE"] == "0"
+ assert _apply({}, num_draft=4)["HY4_MLA_ROWS2"] == "1"
+
+
+def test_explicit_env_wins():
+ e = _apply({"HY4_SPEC2_GATE": "0", "HY4_MLA_ROWS2": "0"})
+ assert (e["HY4_SPEC2_GATE"], e["HY4_MLA_ROWS2"]) == ("0", "0")
+
+
+def test_engine_builds_with_the_shipped_gate():
+ _apply({})
+ from engine import Hy4Engine
+ e = Hy4Engine(TREE, "/nonexistent.gguf", ctx=16384, k=3)
+ assert e.gate is not None and e.gate_kb == 2
diff --git a/tools/hy4/serve.py b/tools/hy4/serve.py
index 36f14f9..ed8a7f7 100644
--- a/tools/hy4/serve.py
+++ b/tools/hy4/serve.py
@@ -56,6 +56,7 @@ SHIPPED_ENV = {
"HY4_LC_SPARSE": "1", "HY4_LC_PRIME": "1", # long context: exact top-k sparse decode attend (NLL inside the dense-vs-null band at 8K / 16K), draft-cache fill selects with the prefill scorer
"HY4_PF_SPARSE": "1", "HY4_PF_SPARSE_MINN": "2048", # sparse prefill attention (HIP kernel via SP_SO, Triton fallback) from 2048 keys (first measured from 16K keys): 33K prefill 60.6 -> 42 s, 128K flat ~790 tok/s; NLL 32K 4.5226 vs dense 4.5231 (nulls 4.511-4.515); below 16K dense CK (KL at 4096 with sparse 0.1089 vs nulls 0.0988-0.1054)
"HY4_ENGRAM_NPHASH": "1", "HY4_ENGRAM_NATIVE_IO": "1", "HY4_ENGRAM_PF_WORKERS": "1", "HY4_PF_G_SWIGLU": "1", "HY4_PF_G_ADD": "1", "HY4_PF_MM_FINE": "1", # prefill levers (box A/B 20/11 pf2_A / pf2_B: prefill +11 / +7.6 / +6.5 % at 8K / 32K / 64K, passkey 3/3, KL 0.108 vs noise nulls 0.103-0.108)
+ "HY4_SPEC2_GATE": "1", "HY4_SPEC2_MODE": "learn", "HY4_MLA_ROWS2": "1", # 1.9.3: depth gate (learn mode) + row-batched MLA decode attention, paired A/B wins (51b: B2 +3.9 %, B4 +11.5 %, every class > 0)
"HY4_MLA_HB": "1", "HY4_BG_DOT": "1", "HY4_HEAD_DOT": "1", "HY4_RG_F16": "1", # decode levers (box A/B 20/11, 256 tok x2: verify round -7 % at 4K, -4 % at 32K, plain flat; qual6 KL 0.94 x noise null)
"HY4_PF_KV_HM": "1", "HY4_PF_QO_HM": "1", "HY4_PF_HC2": "1", "HY4_PF_ROUTER16": "1", # prefill set 3 (1.9.1): head-major CK attention operands, lazy layer residual (3 stream passes per layer), fp16 router logits of prefill rows (only on the HY4_REF_FUSED=0 router path)
"HY4_PF_Q3K_V2": "1", # Q3_K prefill gate-up kernel with packed fp16 decode + aligned dword loads (bit-identical to the old kernel, -0.26 s per 8K chunk; needs the rebuilt gm_ext, else falls back; old kernel when K / 256 is odd)
@@ -87,15 +88,16 @@ def check_kernels() -> None:
print(f"hy4 serve: WARNING {m}: slow path (HY4_ALLOW_SLOW=1)", file=sys.stderr, flush=True)
-SLOTS_OFF = ("HY4_LC_SPARSE",) # shipped long-context levers that act on the single-session K / indexer buffers: mu_engine.MuEngine refuses them with --slots > 1
+SLOTS_OFF = ("HY4_LC_SPARSE", "HY4_SPEC2_GATE", "HY4_MLA_ROWS2") # shipped long-context levers that act on the single-session K / indexer buffers: mu_engine.MuEngine refuses them with --slots > 1
-def apply_env(ctx: int, model: str = "", slots: int = 1) -> None:
+def apply_env(ctx: int, model: str = "", slots: int = 1, num_draft: int = 3) -> None:
if os.environ.get("HY4_KV_COMPACT", "0") == "1": # long lane footprint (about 9 KB per token, doc/hy4.md): fp8 latent K for the trunk and the draft layer, window layers in a 1024-row ring, fp16 indexer pool, rope rows from the positions (no per-token tables); each can still be set by hand
for k, v in (("HY4_KV_FP8", "1"), ("HY4_KV_FP8_MTP", "1"), ("HY4_SWA_RING", "1024"), ("HY4_IX_POOL16", "1"), ("HY4_ROPE_POS", "1")):
os.environ.setdefault(k, v)
for k, v in SHIPPED_ENV.items():
- os.environ.setdefault(k, "0" if slots > 1 and k in SLOTS_OFF else v) # an explicit value still wins (and MuEngine refuses it)
+ off = (slots > 1 and k in SLOTS_OFF) or (k == "HY4_SPEC2_GATE" and (num_draft != 3 or os.environ.get("HY4_DRAFT_POLICY"))) # the depth gate drafts 2 steps + 1 tail (num-draft 3) and excludes the draft policy
+ os.environ.setdefault(k, "0" if off else v) # an explicit value still wins (and MuEngine refuses it)
os.environ.setdefault("HY4_NOSYNC_CAP", str(((ctx + 64 + 511) // 512) * 512)) # K buffers / token history: fixed for the life of the process
# the measured line also runs with frozen TunableOp results for the prefill GEMMs and a start-up request that compiles the prefill kernels and captures the decode graphs
tune = os.path.join(TREE, "tools", "hy4", "tunableop_gfx1151.csv")
@@ -521,7 +523,7 @@ def main(argv=None):
args = ap.parse_args(argv)
if args.default_max_tokens < 0:
ap.error("--default-max-tokens must be 0 (all the room left in the context window) or a positive number of tokens")
- apply_env(args.max_ctx, args.model, args.slots)
+ apply_env(args.max_ctx, args.model, args.slots, args.num_draft)
check_kernels()
st = State(args)
app = build_app(st)