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.

hyperconnections: KYOJIN_HIP_GLUE defers the decode residual apply into the next mix_norm

YanissAmza02be522b14b
diff --git a/exllamav3/modules/hip_glue.py b/exllamav3/modules/hip_glue.py
new file mode 100644
index 0000000..4099441
--- /dev/null
+++ b/exllamav3/modules/hip_glue.py
@@ -0,0 +1,41 @@
+"""KYOJIN_HIP_GLUE: one run-time switch for the decode launch-reduction levers on ROCm.
+
+The first lever is the decode-width site fusion of the hyper-connection pair that every HC
+transformer runs twice per layer: the sublayer's residual apply (ext.hc_apply) is deferred
+into the next site's fused mix+norm (ext.hc_pf_apply_mix_norm), so one launch replaces two.
+The kernel is the prefill one (EXL3_PF_HC_FUSE), whose phase A replays hc_apply_kernel's exact
+per-thread loop and whose phase P replays hc_mix_norm_1pass's partials loop verbatim: at a row
+count in the single-chunk regime it is bit-identical to the unfused pair. Prefill takes it on
+both sides of the knob; decode rows (1 .. 256) only when the knob is on, because that regime
+was not measured for the prefill path.
+
+Tests: tests/test_hip_glue.py (no GPU: the range decision and the knob wiring only).
+"""
+import os
+
+import torch
+
+from ..ext import exllamav3_ext as ext
+from ..util.knobs import knob_on
+
+HC_FUSE_MIN_ROWS_PREFILL = 257 # the measured prefill regime (single-chunk, R > 256)
+HC_FUSE_MIN_ROWS_DECODE = 1 # decode/verify rows, KYOJIN_HIP_GLUE=1 only
+HC_FUSE_MAX_ROWS = 65536 # = HC_MAX_TICKETS in hc_mix.cu
+
+
+def glue_on() -> bool:
+ """KYOJIN_HIP_GLUE, read per call (default off until the lead's gate)."""
+ return knob_on("KYOJIN_HIP_GLUE", "0")
+
+
+def hc_fuse_rows_ok(rows: int, glue: bool | None = None) -> bool:
+ """Row counts the fused apply+mix_norm kernel takes. Prefill rows keep the shipped range;
+ decode rows (up to 256) join it only when the glue knob is on. Zero rows never fuse."""
+ if glue is None:
+ glue = glue_on()
+ lo = HC_FUSE_MIN_ROWS_DECODE if glue else HC_FUSE_MIN_ROWS_PREFILL
+ return lo <= rows <= HC_FUSE_MAX_ROWS
+
+
+def available() -> bool:
+ return hasattr(ext, "hc_pf_apply_mix_norm")
diff --git a/exllamav3/modules/hyperconnections.py b/exllamav3/modules/hyperconnections.py
index d83b910..143aaec 100644
--- a/exllamav3/modules/hyperconnections.py
+++ b/exllamav3/modules/hyperconnections.py
@@ -329,7 +329,9 @@ class HyperConnection(Module):
if post is None or comb is None: # GatedResidual-style sites (Qwen) carry no post/comb
return False
b, s, H, D = x.shape
- return (b * s > 256 and b * s <= 65536 and H == 4 and D % 4 == 0
+ # KYOJIN_HIP_GLUE: decode rows join the fused site pair (see modules/hip_glue.py)
+ from .hip_glue import hc_fuse_rows_ok
+ return (hc_fuse_rows_ok(b * s) and H == 4 and D % 4 == 0
and x.dtype == torch.float and x.is_contiguous()
and y.dtype in (torch.float, torch.half) and y.is_contiguous() and y.numel() == b * s * D
and post.dtype == torch.float and post.is_contiguous()
@@ -362,7 +364,8 @@ class HyperConnection(Module):
and D <= 8192 and streams.is_contiguous() and _hc_mix_supported(streams.device)):
return None
R = b * s
- if R <= 256 or R > 65536: # = HC_MAX_TICKETS (hc_mix.cu)
+ from .hip_glue import hc_fuse_rows_ok
+ if not hc_fuse_rows_ok(R): # prefill rows, plus decode rows under KYOJIN_HIP_GLUE
return None
if ext.hc_mix_num_chunks(R, H * D) != 1:
return None
diff --git a/exllamav3/util/knobs.py b/exllamav3/util/knobs.py
index 6f11ad6..18f45a4 100644
--- a/exllamav3/util/knobs.py
+++ b/exllamav3/util/knobs.py
@@ -11,6 +11,7 @@ from typing import Callable, Optional
# name -> default (str). The call sites pass the same default to knob(); tests/test_knobs.py checks the two agree.
KNOBS = {
+ "KYOJIN_HIP_GLUE": "0",
"EXL3_DEQ_WINDOW_TRITON": "1",
"EXL3_GEMV_TRI": "1",
"EXL3_GEMV_R_ARR1": "1",
diff --git a/tests/test_hip_glue.py b/tests/test_hip_glue.py
new file mode 100644
index 0000000..01102d5
--- /dev/null
+++ b/tests/test_hip_glue.py
@@ -0,0 +1,77 @@
+"""KYOJIN_HIP_GLUE: the glue knob is registered, off by default, and its row gate switches the
+decode site fusion on without touching the prefill range. CPU-only: no extension, no GPU.
+
+The lever it gates (hyperconnections.HyperConnection.pf_can_defer -> _pf_apply_mix_norm,
+ext.hc_pf_apply_mix_norm) is exercised on the device by tools/depth/glueab.py; these tests pin
+the gate and the default so a later edit cannot turn the decode fusion on by accident.
+"""
+import inspect
+import os
+
+import pytest
+
+from exllamav3.util import knobs
+
+
[email protected](autouse = True)
+def clean_env():
+ saved = {n: os.environ.get(n) for n in knobs.KNOBS}
+ os.environ.pop("KYOJIN_HIP_GLUE", None)
+ yield
+ for n, v in saved.items():
+ if v is None:
+ os.environ.pop(n, None)
+ else:
+ os.environ[n] = v
+ knobs.invalidate()
+
+
+def _glue():
+ from exllamav3.modules import hip_glue
+ return hip_glue
+
+
+def test_knob_registered_and_off():
+ assert "KYOJIN_HIP_GLUE" in knobs.KNOBS
+ assert knobs.KNOBS["KYOJIN_HIP_GLUE"] == "0"
+ assert _glue().glue_on() is False
+
+
+def test_knob_reads_the_environment_per_call():
+ g = _glue()
+ knobs.set_knobs({"KYOJIN_HIP_GLUE": "1"})
+ assert g.glue_on() is True
+ assert g.hc_fuse_rows_ok(1) is True
+ knobs.set_knobs({"KYOJIN_HIP_GLUE": "0"})
+ assert g.glue_on() is False
+ assert g.hc_fuse_rows_ok(1) is False
+
+
+def test_prefill_range_is_the_same_on_both_sides():
+ g = _glue()
+ for rows in (257, 300, 4096, g.HC_FUSE_MAX_ROWS):
+ assert g.hc_fuse_rows_ok(rows, glue = False) is True
+ assert g.hc_fuse_rows_ok(rows, glue = True) is True
+
+
+def test_decode_range_only_with_glue():
+ g = _glue()
+ for rows in (1, 2, 5, 256):
+ assert g.hc_fuse_rows_ok(rows, glue = False) is False
+ assert g.hc_fuse_rows_ok(rows, glue = True) is True
+
+
+def test_zero_and_oversized_rows_never_fuse():
+ g = _glue()
+ for glue in (False, True):
+ assert g.hc_fuse_rows_ok(0, glue = glue) is False
+ assert g.hc_fuse_rows_ok(g.HC_FUSE_MAX_ROWS + 1, glue = glue) is False
+
+
+def test_the_site_pair_goes_through_the_gate():
+ """pf_can_defer and _pf_apply_mix_norm must decide through hc_fuse_rows_ok: a copy of the old
+ `b * s > 256` literal would leave the knob inert on decode."""
+ from exllamav3.modules import hyperconnections as hc
+ assert "hc_fuse_rows_ok" in inspect.getsource(hc.HyperConnection.pf_can_defer)
+ assert "hc_fuse_rows_ok" in inspect.getsource(hc.HyperConnection._pf_apply_mix_norm)
+ assert "b * s > 256" not in inspect.getsource(hc.HyperConnection.pf_can_defer)