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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
fix(agent): warn on fallbacks instead of printing to stdout
thread3da1ba2e66d4dc
diff --git a/laya/agent.py b/laya/agent.pyindex 82342ade..66df0b60 100644--- a/laya/agent.py+++ b/laya/agent.py@@ -595,13 +595,16 @@ class Agent(HookRegistry):if device is not None:target_device = torch.device(device)if target_device.type == "cuda" and not torch.cuda.is_available():- print("Warning: CUDA requested but not available. Falling back to CPU.")+ warnings.warn("Warning: CUDA requested but not available. Falling back to CPU.",+ RuntimeWarning)self.device = torch.device("cpu")elif target_device.type == "mps" and not (hasattr(torch.backends, "mps") and torch.backends.mps.is_available()):- print("Warning: MPS requested but not available. Falling back to CPU.")+ warnings.warn("Warning: MPS requested but not available. Falling back to CPU.",+ RuntimeWarning)self.device = torch.device("cpu")elif target_device.type == "xpu" and not (hasattr(torch, "xpu") and torch.xpu.is_available()):- print("Warning: XPU requested but not available. Falling back to CPU.")+ warnings.warn("Warning: XPU requested but not available. Falling back to CPU.",+ RuntimeWarning)self.device = torch.device("cpu")else:self.device = target_device@@ -741,7 +744,7 @@ class Agent(HookRegistry):self.accelerate()if fell_back_from is not None:- print(+ warnings.warn("\n[laya] Warning: could not place the model on %s, so it is running on CPU.\n"" Reason: %s\n"" Inference will be roughly 10-15x slower (~200-500 ms rather than ~35 ms).\n"@@ -749,7 +752,7 @@ class Agent(HookRegistry):" may not support its CUDA architecture:\n"" pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cu128\n"" See https://pytorch.org/get-started/locally/\n"- % (fell_back_from, fell_back_why), flush=True)+ % (fell_back_from, fell_back_why), RuntimeWarning)def accelerate(self, use_graphs: bool = True, strict: bool = False):"""Replace the model forward with the TileLang fast path (fused GEMM/GEGLU/LayerNorm/RoPE kernels,@@ -779,7 +782,8 @@ class Agent(HookRegistry):if self._fast is None:if strict:raise last- print("Warning: laya fast path unavailable (%s); using the stock forward." % last)+ warnings.warn("Warning: laya fast path unavailable (%s); using the stock forward." % last,+ RuntimeWarning)return Falseself._stock_forward = self.model.forwardself.model.forward = self._fast.forward@@ -838,8 +842,8 @@ class Agent(HookRegistry):self.model.to(torch.device("cpu"))if torch.cuda.is_available():torch.cuda.empty_cache()- print("Warning: could not move the model back to %s after the CPU retry (%s); "- "staying on CPU." % (device, e))+ warnings.warn("Warning: could not move the model back to %s after the CPU retry (%s); "+ "staying on CPU." % (device, e), RuntimeWarning)returnself.device, self.dtype, self.amp_enabled = device, dtype, amp_enabledif had_fast:@@ -1118,7 +1122,8 @@ class Agent(HookRegistry):# "cuda" substring used to be accepted too, so any CUDA-shaped RuntimeError (a# shape, assert or kernel error) silently and permanently demoted the agent.if self.device.type != "cpu" and (isinstance(e, torch.cuda.OutOfMemoryError) or "memory" in low):- print("Warning: GPU memory exceeded during inference. Retrying this request on CPU...")+ warnings.warn("Warning: GPU memory exceeded during inference. "+ "Retrying this request on CPU...", RuntimeWarning)# Scoped, not permanent: the demotion used to rewrite device/dtype/amp and move# the model for the life of the process, so one oversized request left every# later call ~10-15x slower on CPU. Demote under the write lock (#649) so any@@ -1162,8 +1167,9 @@ class Agent(HookRegistry):finally:self._amp_failures += 1if self._amp_failures >= _AMP_FAIL_LIMIT:- print("Warning: autocast failed %d times in a row. Disabling mixed precision."- % self._amp_failures)+ warnings.warn("Warning: autocast failed %d times in a row. "+ "Disabling mixed precision." % self._amp_failures,+ RuntimeWarning)self.amp_enabled = Falseself.dtype = torch.float32raisediff --git a/tests/test_load_errors.py b/tests/test_load_errors.pyindex d885dfdc..37e5c0e0 100644--- a/tests/test_load_errors.py+++ b/tests/test_load_errors.py@@ -10,12 +10,16 @@ No network: the checkpoint is a tiny local one built here, the same shapeRun: python tests/test_load_errors.py"""+import ioimport jsonimport osimport shutilimport sysimport tempfile+import warnings+from contextlib import redirect_stdoutfrom pathlib import Path+from unittest.mock import patchos.environ.setdefault("USE_TF", "0")os.environ.setdefault("USE_TORCH", "1")@@ -220,6 +224,26 @@ check_true("short temperature/names the field", "temperature" in str(err), str(echeck_true("short temperature/says the shape", "list of 3" in str(err), str(err))+# ------------------------------------------- 7. a device fallback warns instead of printing+# Asking for a device this machine does not have falls back to CPU with a note. That note used+# to be a bare `print`, so it landed on stdout -- where `laya --json` and `laya --batch --json`+# write their records and where the MCP server speaks JSON-RPC. A warning goes to stderr and a+# caller can filter it; a print breaks the stream and cannot be silenced.+_stdout = io.StringIO()+with warnings.catch_warnings(record=True) as caught, \+ patch("torch.cuda.is_available", return_value=False), \+ redirect_stdout(_stdout):+ warnings.simplefilter("always")+ _fallback = load(str(REPO), device="cuda")+check("device fallback/lands on CPU", _fallback.device.type, "cpu")+check("device fallback/writes nothing to stdout", _stdout.getvalue(), "")+check_true("device fallback/warns instead",+ any(issubclass(w.category, RuntimeWarning) and "CUDA requested" in str(w.message)+ for w in caught),+ str([str(w.message) for w in caught]))+del _fallback++TMP.cleanup()print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))