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.

split: fix cross-device events on the GLM split path

Resynth3a5f76e02627
diff --git a/exllamav3/bridge.py b/exllamav3/bridge.py
index 167d242..2011ada 100644
--- a/exllamav3/bridge.py
+++ b/exllamav3/bridge.py
@@ -113,8 +113,14 @@ class Bridge:
slot.buf = torch.empty(numel, dtype = dtype, pin_memory = True)
slot.cap = numel
slot.read_valid = False
- slot.filled = torch.cuda.Event()
- slot.read = torch.cuda.Event()
+ # CUDA/HIP events belong to the device that records them. The channel is
+ # directional, so create each event on its own endpoint; creating both on
+ # the ambient device fails on the first large (32K) growth with
+ # "Event device N does not match recording stream's device".
+ with torch.cuda.device(ch.src):
+ slot.filled = torch.cuda.Event()
+ with torch.cuda.device(ch.dst):
+ slot.read = torch.cuda.Event()
self.stats["reallocs"] += 1
def _hop(self, t: torch.Tensor, dst: torch.device, chunk: bool) -> torch.Tensor:
@@ -134,21 +140,24 @@ class Bridge:
if numel > slot.cap:
self._grow(ch, slot, t.dtype, numel)
- src_stream = torch.cuda.current_stream(src)
- dst_stream = torch.cuda.current_stream(dst)
+ with torch.cuda.device(src):
+ src_stream = torch.cuda.current_stream(src)
+ with torch.cuda.device(dst):
+ dst_stream = torch.cuda.current_stream(dst)
# Reusing a host slot: the source must not overwrite it until the destination
# finished reading it. This is a cross-device stream wait, not a host wait.
if slot.read_valid:
- src_stream.wait_event(slot.read)
+ with torch.cuda.device(src):
+ src_stream.wait_event(slot.read)
hb = slot.buf[:numel].view(t.shape)
- with torch.cuda.stream(src_stream):
+ with torch.cuda.device(src), torch.cuda.stream(src_stream):
hb.copy_(t, non_blocking = True)
slot.filled.record(src_stream)
- with torch.cuda.stream(dst_stream):
+ with torch.cuda.device(dst), torch.cuda.stream(dst_stream):
dst_stream.wait_event(slot.filled)
out = hb.to(dst, non_blocking = True)
slot.read.record(dst_stream)
diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.py
index 99f0a29..772beb4 100644
--- a/exllamav3/modules/block_sparse_mlp.py
+++ b/exllamav3/modules/block_sparse_mlp.py
@@ -220,7 +220,11 @@ _side_state: dict = {}
def _side(device):
st = _side_state.get(device)
if st is None:
- st = _side_state[device] = (torch.cuda.Stream(device = device), torch.cuda.Event(), torch.cuda.Event())
+ # Events belong to the device that records them. Creating them on the
+ # ambient device breaks a split layer whose side stream is on the other GPU.
+ with torch.cuda.device(device):
+ st = (torch.cuda.Stream(device = device), torch.cuda.Event(), torch.cuda.Event())
+ _side_state[device] = st
return st
diff --git a/exllamav3/modules/embedding.py b/exllamav3/modules/embedding.py
index e893ee2..fa0f136 100644
--- a/exllamav3/modules/embedding.py
+++ b/exllamav3/modules/embedding.py
@@ -223,8 +223,12 @@ class Embedding(Module):
round-to-nearest the host conversion does for bf16 -> fp16). The rows stay cached for the MTP block (forward_device)."""
d = y.to(device, non_blocking = True)
if self._narrow_ev is None:
- self._narrow_ev = torch.cuda.Event()
- self._narrow_ev.record()
+ # The CPU embedding uploads to the first layer's device, which is the
+ # front GPU in a split; the event must belong to that device.
+ with torch.cuda.device(device):
+ self._narrow_ev = torch.cuda.Event()
+ with torch.cuda.device(device):
+ self._narrow_ev.record()
self._dev_rows = (ids.clone(), d) # consumed by forward_device (the MTP block), else replaced by the next chunk
return d if d.dtype == out_dtype else d.to(out_dtype)
diff --git a/exllamav3/serve_split.py b/exllamav3/serve_split.py
index 47bd6a9..6206783 100644
--- a/exllamav3/serve_split.py
+++ b/exllamav3/serve_split.py
@@ -18,6 +18,15 @@ if TYPE_CHECKING:
# Split mode runs eager until per-segment capture lands (Stage 6). A whole-forward
# or per-block graph must never span the host-staged bridge between segments.
+# Deferred prefill residual fusions keep a pending state on the producing stream;
+ # the next block may be on the other device, beyond the bridge. Split mode runs
+ # the unfused path (same numerics, no cross-stream pending tensor).
+SPLIT_UNSAFE_ENV = {
+ "EXL3_PF_HC_FUSE": "0",
+ "EXL3_PF_GR_FUSE": "0",
+ "EXL3_GR_PF_FUSE": "0",
+}
+
GRAPH_ENV_OFF = {
"EXL3_BLOCK_GRAPH": "0",
"EXL3_BLOCK_GRAPH_MLA": "0",
@@ -130,7 +139,8 @@ def apply_split_env(settings: SplitSettings, logger = print) -> list[str]:
preset = SPLIT_PRESETS.get(settings.preset)
if preset is None:
raise ValueError(f"unknown split preset {settings.preset!r}; choose one of {sorted(SPLIT_PRESETS)}")
- forced = dict(GRAPH_ENV_OFF)
+ forced = dict(SPLIT_UNSAFE_ENV)
+ forced.update(GRAPH_ENV_OFF)
forced.update(preset)
if settings.split_graphs == "on":
# Stage 6 has no per-segment capture yet: honouring "on" would let a graph
@@ -139,7 +149,7 @@ def apply_split_env(settings: SplitSettings, logger = print) -> list[str]:
overridden = [f"{k}={os.environ[k]}" for k, v in forced.items()
if os.environ.get(k) not in (None, "", v)]
os.environ.update(forced)
- logger("[split] graphs forced off for split mode (per-segment capture is Stage 6)"
+ logger("[split] graphs and deferred residual fusions forced off for split mode"
+ (f"; overrode {', '.join(overridden)}" if overridden else ""))
return overridden
diff --git a/tests/test_split_plan_cpu.py b/tests/test_split_plan_cpu.py
index 18e9187..28e81f9 100644
--- a/tests/test_split_plan_cpu.py
+++ b/tests/test_split_plan_cpu.py
@@ -375,6 +375,8 @@ def test_adapter_unsafe_env_is_applied_and_logged(split_modules, monkeypatch):
def test_split_settings_off_is_a_noop(split_modules, monkeypatch):
_dm, _bridge, split = split_modules
monkeypatch.setenv("EXL3_BLOCK_GRAPH", "1")
+ for _k in ("EXL3_PF_HC_FUSE", "EXL3_PF_GR_FUSE", "EXL3_GR_PF_FUSE"):
+ monkeypatch.setenv(_k, "1")
off = split.SplitSettings(front_layers = 0)
assert off.enabled is False
assert split.apply_split_env(off) == []
@@ -384,3 +386,7 @@ def test_split_settings_off_is_a_noop(split_modules, monkeypatch):
assert on.enabled is True and on.auto is True
split.apply_split_env(on)
assert os.environ["EXL3_BLOCK_GRAPH"] == "0"
+ # Deferred residual fusions cannot cross the host-staged bridge.
+ assert os.environ["EXL3_PF_HC_FUSE"] == "0"
+ assert os.environ["EXL3_PF_GR_FUSE"] == "0"
+ assert os.environ["EXL3_GR_PF_FUSE"] == "0"