Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(agent): validate CUDA ordinals, accept auto, survive capability-probe failures

sahii5b26138a80d3
diff --git a/laya/agent.py b/laya/agent.py
index 83b18da4..0f9e91cd 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -593,12 +593,28 @@ class Agent(HookRegistry):
)
# 1. Device resolution with automatic fallback
+ if isinstance(device, str) and device.strip().lower() == "auto":
+ device = None
if device is not None:
target_device = torch.device(device)
if target_device.type == "cuda" and not torch.cuda.is_available():
warnings.warn("Warning: CUDA requested but not available. Falling back to CPU.",
RuntimeWarning)
self.device = torch.device("cpu")
+ elif target_device.type == "cuda" and target_device.index is not None:
+ # `is_available` only says a GPU exists; a bad ordinal sails through it
+ # and dies later in `.to()` with a bare CUDA error. Check up front.
+ try:
+ count = torch.cuda.device_count()
+ except (RuntimeError, AttributeError):
+ count = 0
+ if target_device.index < 0 or target_device.index >= max(count, 1):
+ warnings.warn("Warning: CUDA device %s not found (%d visible). Falling back to CPU."
+ % (target_device, count),
+ RuntimeWarning)
+ self.device = torch.device("cpu")
+ else:
+ self.device = target_device
elif target_device.type == "mps" and not (hasattr(torch.backends, "mps") and torch.backends.mps.is_available()):
warnings.warn("Warning: MPS requested but not available. Falling back to CPU.",
RuntimeWarning)
@@ -703,10 +719,20 @@ class Agent(HookRegistry):
self.mps_amp_min_rows = _mps_amp_min_rows()
if self.device.type == "cuda":
self.amp_enabled = True
- if torch.cuda.get_device_capability(self.device)[0] < 8:
+ try:
+ major = torch.cuda.get_device_capability(self.device)[0]
+ except (RuntimeError, AttributeError):
+ # A driver that reports a GPU but cannot answer the capability query
+ # cannot be trusted with bf16 either; fp16 is the safe default and
+ # placement below still decides whether the device is usable at all.
+ warnings.warn("Warning: could not query CUDA capability; assuming fp16 defaults.",
+ RuntimeWarning)
self.dtype = torch.float16
else:
- self.dtype = _cuda_amp_dtype(self.cfg.get("amp_dtype", "fp16"))
+ if major < 8:
+ self.dtype = torch.float16
+ else:
+ self.dtype = _cuda_amp_dtype(self.cfg.get("amp_dtype", "fp16"))
elif self.device.type == "mps":
self.amp_enabled = True
self.dtype = torch.float16
@@ -750,8 +776,9 @@ class Agent(HookRegistry):
" Reason: %s\n"
" Inference will be roughly 10-15x slower (~200-500 ms rather than ~35 ms).\n"
" If this is a newer NVIDIA GPU (Blackwell / RTX 50-series), your PyTorch build\n"
- " may not support its CUDA architecture:\n"
- " pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cu128\n"
+ " may predate stable Blackwell support (stable since torch 2.6, CUDA 12.8+).\n"
+ " Upgrade to a current stable build with a CUDA 13.x wheel, e.g.:\n"
+ " pip install --upgrade torch --index-url https://download.pytorch.org/whl/cu130\n"
" See https://pytorch.org/get-started/locally/\n"
% (fell_back_from, fell_back_why), RuntimeWarning)
diff --git a/tests/test_criteria.py b/tests/test_criteria.py
index 3ff59b25..21ddab5e 100644
--- a/tests/test_criteria.py
+++ b/tests/test_criteria.py
@@ -205,7 +205,7 @@ _src = inspect.getsource(_agent.Agent.__init__)
check_true("fallback/flag is initialised", "fell_back_from = fell_back_why = None" in _src)
check_true("fallback/warns only on a real fallback", "if fell_back_from is not None:" in _src)
check_true("fallback/reports the underlying reason", "Reason: %s" in _src)
-check_true("fallback/keeps the actionable advice", "download.pytorch.org/whl/nightly" in _src)
+check_true("fallback/keeps the actionable advice", "download.pytorch.org/whl/cu130" in _src)
check_true("fallback/no bare cuda probe for the warning",
"torch.cuda.is_available() or getattr(torch.version" not in _src)
diff --git a/tests/test_load_errors.py b/tests/test_load_errors.py
index bbb44ebb..76e73d6c 100644
--- a/tests/test_load_errors.py
+++ b/tests/test_load_errors.py
@@ -452,6 +452,44 @@ check_true("device fallback/warns instead",
del _fallback
+# ------------------------------------------- 9. unusable ordinals and "auto"
+# `is_available` only says a GPU exists: on a one-GPU box `cuda:99` sails through it
+# and dies later in `.to()` with a bare CUDA error. The ordinal is validated up front
+# instead, with the same warn-and-CPU shape as section 8. "auto" (any case or
+# surrounding whitespace) means "decide yourself", the way the serve layer already
+# treats LAYA_DEVICE. A driver that reports CUDA but cannot answer the capability
+# query gets safe fp16 defaults rather than a crash in the amp policy.
+with warnings.catch_warnings(record=True) as caught, \
+ patch("torch.cuda.is_available", return_value=True), \
+ patch("torch.cuda.device_count", return_value=1):
+ warnings.simplefilter("always")
+ _bad_ordinal = load(str(REPO), device="cuda:99")
+check("bad ordinal/lands on CPU", _bad_ordinal.device.type, "cpu")
+check_true("bad ordinal/names the ordinal",
+ any("cuda:99" in str(w.message) for w in caught),
+ str([str(w.message) for w in caught]))
+del _bad_ordinal
+
+_auto = load(str(REPO), device="AUTO")
+check_true("auto/resolves without crashing",
+ _auto.device.type in ("cpu", "cuda", "mps", "xpu"), str(_auto.device))
+del _auto
+
+with warnings.catch_warnings(record=True) as caught, \
+ patch("torch.cuda.is_available", return_value=True), \
+ patch("torch.cuda.device_count", return_value=1), \
+ patch("torch.cuda.get_device_capability", side_effect=RuntimeError("no driver")), \
+ patch.object(torch.nn.Module, "to", lambda self, *a, **k: self):
+ warnings.simplefilter("always")
+ _no_probe = load(str(REPO), device="cuda:0")
+check("unprobed GPU/stays on the requested device", _no_probe.device.type, "cuda")
+check("unprobed GPU/falls back to safe fp16", _no_probe.dtype, torch.float16)
+check_true("unprobed GPU/warns about the probe",
+ any("capability" in str(w.message) for w in caught),
+ str([str(w.message) for w in caught]))
+del _no_probe
+
+
TMP.cleanup()
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))