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: make bridge-off fallback use host bounce

Resynth9c2aaec062a8
diff --git a/doc/split.md b/doc/split.md
index 24b6941..9419e36 100644
--- a/doc/split.md
+++ b/doc/split.md
@@ -23,7 +23,10 @@ device may serve either part. Earlier revisions, the roadmap and
Both legs are enqueued on the two devices' own compute streams; the host never blocks. The
destination tensor is a fresh allocation, so callers may keep using it after the slot cycles.
-`KYOJIN_BRIDGE=0` falls back to `to_device` (split still runs, crossings block).
+`KYOJIN_BRIDGE=0` falls back to `to_device` (split still runs, crossings block). With the
+bridge disabled, cross-device moves are forced through host memory (a blocking bounce), so
+the fallback does not probe or use the direct peer path; an explicit
+`EXLLAMA_NO_P2P_COPY` still wins.
Devices are not tied to a role. Either device may serve `part0` or `part1`, and the split
is not restricted to a particular architecture pair. Kernel support remains per-arch: the
diff --git a/exllamav3/bridge.py b/exllamav3/bridge.py
index 5adda71..65e8277 100644
--- a/exllamav3/bridge.py
+++ b/exllamav3/bridge.py
@@ -194,6 +194,12 @@ def install_bridge(config, *, part1: torch.device, part0: torch.device | None) -
os.environ["KYOJIN_SPLIT_ACTIVE"] = "1"
existing = getattr(config, "device_bridge", None)
if not bridge_enabled():
+ # The direct P2P fallback can poison the context on links that only pretend to
+ # support peer copies. With the bridge explicitly disabled, keep the fallback
+ # safe by forcing a host bounce; an explicit EXLLAMA_NO_P2P_COPY still wins.
+ from .util import device_copy
+ if device_copy.FORCED is None:
+ device_copy.FORCED = True
return existing
if existing is not None and existing.pair == (torch.device(part1), torch.device(part0)):
return existing
diff --git a/exllamav3/util/device_copy.py b/exllamav3/util/device_copy.py
index 6fb11a3..f3f928f 100644
--- a/exllamav3/util/device_copy.py
+++ b/exllamav3/util/device_copy.py
@@ -20,6 +20,9 @@ whether the copy can go direct or has to bounce through host memory:
EXLLAMA_NO_P2P_COPY=0 -> always direct, never probe
Both directions of a pair are probed together, since the fabric can fail one way only.
+A probe that raises (some drivers reject the peer copy instead of corrupting) counts as
+a failed verdict and the pair bounces through host memory. A later direct-copy failure
+also caches the bounce verdict and retries through the host.
"""
_env = os.environ.get("EXLLAMA_NO_P2P_COPY")
@@ -32,16 +35,33 @@ stats = {"direct": 0, "bounced": 0, "probes": 0}
def _probe_direct(src: torch.device, dst: torch.device) -> tuple[bool, bool]:
"""Move random data src -> dst -> src. Returns (src->dst ok, dst->src ok), each judged on
- the host so the verdict never depends on a peer copy itself."""
- a = torch.rand(64, dtype = torch.float32, device = src)
- b = a.to(dst)
- c = b.to(src)
- torch.cuda.synchronize(src)
- torch.cuda.synchronize(dst)
- a_h = a.cpu()
- fwd_ok = torch.equal(a_h, b.cpu())
- bwd_ok = fwd_ok and torch.equal(a_h, c.cpu())
- return fwd_ok, bwd_ok
+ the host so the verdict never depends on a peer copy itself.
+
+ A probe is diagnostic. Some drivers raise instead of silently corrupting when the
+ peer path is broken; treat any probe failure as "bounce" rather than letting the
+ diagnostic turn the first real cross-device move into a crash.
+ """
+ try:
+ a = torch.rand(64, dtype = torch.float32, device = src)
+ b = a.to(dst)
+ c = b.to(src)
+ torch.cuda.synchronize(src)
+ torch.cuda.synchronize(dst)
+ a_h = a.cpu()
+ fwd_ok = torch.equal(a_h, b.cpu())
+ bwd_ok = fwd_ok and torch.equal(a_h, c.cpu())
+ return fwd_ok, bwd_ok
+ except Exception as exc: # noqa: BLE001 - diagnostic must not crash
+ print(f" !! P2P probe between {src} and {dst} failed "
+ f"({type(exc).__name__}: {exc}); routing through system memory "
+ f"(see EXLLAMA_NO_P2P_COPY)")
+ return False, False
+
+
+def _mark_bounce(src: torch.device, dst: torch.device) -> None:
+ with _lock:
+ _verdicts[(src.index, dst.index)] = True
+ _verdicts[(dst.index, src.index)] = True
def needs_bounce(src: torch.device, dst: torch.device) -> bool:
@@ -82,7 +102,19 @@ def to_device(t: torch.Tensor, device: torch.device | str | int, non_blocking: b
if needs_bounce(t.device, device):
stats["bounced"] += 1
return t.cpu().to(device)
+ try:
+ out = t.to(device, non_blocking = non_blocking)
+ except RuntimeError as exc:
+ # A probe can pass and a later direct copy can still fail on a flaky link.
+ # Cache the safe verdict and retry through host memory.
+ _mark_bounce(t.device, device)
+ stats["bounced"] += 1
+ print(f" !! Direct copy {t.device} -> {device} failed "
+ f"({type(exc).__name__}: {exc}); routing through system memory "
+ f"(see EXLLAMA_NO_P2P_COPY)")
+ return t.cpu().to(device)
stats["direct"] += 1
+ return out
return t.to(device, non_blocking = non_blocking)
diff --git a/tests/test_device_copy_.py b/tests/test_device_copy_.py
index afa2632..84838a5 100644
--- a/tests/test_device_copy_.py
+++ b/tests/test_device_copy_.py
@@ -61,6 +61,40 @@ to_device(y, d0)
assert dc._verdicts == {(1, 0): False, (0, 1): True}, dc._verdicts
dc._probe_direct = orig_probe
+# 2c. a probe that raises is a bounce verdict, not a crash on the first real move
+reset()
+class _RaisingTensor:
+ def to(self, *_args, **_kwargs):
+ raise RuntimeError("hipErrorInvalidValue")
+
+orig_rand = dc.torch.rand
+dc.torch.rand = lambda *_args, **_kwargs: _RaisingTensor()
+try:
+ z = to_device(x, d1)
+finally:
+ dc.torch.rand = orig_rand
+assert torch.equal(x.cpu(), z.cpu())
+assert dc._verdicts == {(0, 1): True, (1, 0): True}, dc._verdicts
+assert dc.stats == {"probes": 1, "bounced": 1, "direct": 0}, dc.stats
+
+# 2d. a direct copy that raises after a passed probe is retried and cached as bounce
+reset()
+dc.FORCED = False
+class _DirectFail:
+ device = torch.device("cuda", 0)
+
+ def to(self, _device, non_blocking = False):
+ raise RuntimeError("hipErrorInvalidValue")
+
+ def cpu(self):
+ return torch.ones(8, dtype = torch.float32)
+
+out = to_device(_DirectFail(), d1)
+assert out.device == d1
+assert dc._verdicts == {(0, 1): True, (1, 0): True}, dc._verdicts
+assert dc.stats == {"probes": 0, "bounced": 1, "direct": 0}, dc.stats
+dc.FORCED = None
+
# 3. the real probe agrees with the verdict recorded in 1
reset()
fwd_ok, bwd_ok = dc._probe_direct(d0, d1)
diff --git a/tests/test_split_plan_cpu.py b/tests/test_split_plan_cpu.py
index 40fc3ea..fe1610e 100644
--- a/tests/test_split_plan_cpu.py
+++ b/tests/test_split_plan_cpu.py
@@ -364,6 +364,23 @@ def test_bridge_installs_per_config_and_is_reused(split_modules, monkeypatch):
assert single.split_active is False
+def test_disabled_bridge_forces_host_bounce(split_modules, monkeypatch):
+ _dm, bridge_mod, _split = split_modules
+ from exllamav3.util import device_copy
+
+ monkeypatch.setenv("KYOJIN_BRIDGE", "0")
+ old = device_copy.FORCED
+ device_copy.FORCED = None
+ try:
+ cfg = SimpleNamespace()
+ assert bridge_mod.install_bridge(
+ cfg, part1 = torch.device("cuda", 0), part0 = torch.device("cuda", 1)) is None
+ assert cfg.split_active is True
+ assert device_copy.FORCED is True
+ finally:
+ device_copy.FORCED = old
+
+
def test_named_preset_is_applied_by_the_plan(split_modules, monkeypatch):
dm, _bridge, split = split_modules
monkeypatch.setenv("EXL3_TEST_PRESET", "0")