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(mcp): scope the device parse promise to the paths that keep it

Aashish4d69a04b678b
diff --git a/laya/mcp/device.py b/laya/mcp/device.py
index 68a86661..9cc1d898 100644
--- a/laya/mcp/device.py
+++ b/laya/mcp/device.py
@@ -95,8 +95,12 @@ def resolve_device(force: str | None = None) -> str:
fallback happens silently at agent build time).
A ``LAYA_DEVICE`` value torch cannot parse raises here too (see
- ``env_device``): there is no path on which this function labels a device
- torch would refuse, so the label and the build cannot disagree.
+ ``env_device``), and the auto-detected labels are device types torch names,
+ so neither the environment nor the availability chain can label a device
+ torch would refuse. ``force`` is the exception, on purpose: it is returned
+ verbatim, exactly like the CLI ``--device`` flag, so a test can ask what a
+ given preference would be reported as -- including a preference torch
+ refuses, which then fails where it is used rather than here.
"""
if force:
return force
@@ -150,8 +154,12 @@ def agent_device(agent: Any) -> str | None:
"""The real device a loaded agent computes on (str), or None if unreadable.
Reads ``Agent.device``, a ``torch.device`` that already reflects the
- silent GPU -> CPU fallback done at build time. Accepts a plain string too,
- so tests can fake an agent without torch.
+ silent GPU -> CPU fallback done at build time. The answer is that device's
+ *type*, which carries no index: a checkpoint built on ``cuda:1`` is
+ reported as ``cuda``. A plain string is accepted too, so tests can fake an
+ agent without torch, and a string is returned as written -- so a fake whose
+ ``device`` is ``"cuda:1"`` reports an index that the real ``torch.device``
+ it stands in for would have dropped.
"""
if agent is None:
return None
diff --git a/tests/test_mcp_device.py b/tests/test_mcp_device.py
index 1b29a7cc..704eca78 100644
--- a/tests/test_mcp_device.py
+++ b/tests/test_mcp_device.py
@@ -9,9 +9,15 @@ survived startup, was reported as the configured device, and the refusal landed
the first request that built a checkpoint. `env_device` now parses the value it returns
and raises a named ValueError where the environment is read.
+The other half of the contract is its scope: `force=` is passed straight through, unchecked, so the
+parse guarantee covers the environment and the auto-detection chain and not that hook -- and
+`agent_device`, the answer for a *loaded* checkpoint, reports a device type without its index while
+the plain-string form it accepts for fakes keeps whatever it was given.
+
No model weights, no network, no GPU: torch is imported for its parser only.
Run: python tests/test_mcp_device.py
"""
+import inspect
import os
import sys
from pathlib import Path
@@ -24,7 +30,7 @@ except ImportError:
print("SKIP: torch not importable, so there is no device parser to check")
sys.exit(0)
-from laya.mcp.device import env_device, resolve_device # noqa: E402
+from laya.mcp.device import agent_device, env_device, resolve_device # noqa: E402
PASS, FAIL = [], []
@@ -91,7 +97,7 @@ def test_unparseable_values_are_rejected_by_name():
"Expected one of" in rejection("gpu"), rejection("gpu"))
-def test_no_reporting_path_labels_a_refused_device():
+def test_the_env_path_raises_and_force_passes_through():
old = os.environ.get("LAYA_DEVICE")
try:
os.environ["LAYA_DEVICE"] = "cuda:"
@@ -111,6 +117,83 @@ def test_no_reporting_path_labels_a_refused_device():
ok("force= hook stays verbatim", resolve_device("cuda:") == "cuda:")
+def parses(value):
+ """Whether torch's own parser accepts `value` -- the judge this module defers to."""
+ try:
+ torch.device(value)
+ except Exception:
+ return False
+ return True
+
+
+def test_force_is_the_only_path_that_can_label_a_refused_device():
+ # The two paths the docstring claims for never hand back a label torch would refuse:
+ # the environment either parses or raises, and the auto-detection chain answers with a
+ # device type torch names. Dropping `env_device`'s parse check turns the refused rows of
+ # the first loop into returned labels, which is what this catches.
+ for raw in ("cpu", "CUDA:0", "cuda:1", "mps", "meta", " xpu:2 ",
+ "cuda:", "gpu", "cuda: 0", "cuda:0:1", "1cuda", "cuda-0"):
+ try:
+ value = env_device_with(raw)
+ except ValueError:
+ ok("env %r raises instead of labelling" % raw, True)
+ continue
+ except Exception as exc: # a wrong exception type is a failed check, not an aborted run
+ ok("env %r raises ValueError or returns a label" % raw, False,
+ "raised %s: %s" % (type(exc).__name__, exc))
+ continue
+ ok("env %r labels a device torch accepts" % raw, parses(value), repr(value))
+ old = os.environ.get("LAYA_DEVICE")
+ try:
+ os.environ.pop("LAYA_DEVICE", None)
+ label = resolve_device()
+ ok("auto-detected label parses", parses(label), repr(label))
+ finally:
+ if old is None:
+ os.environ.pop("LAYA_DEVICE", None)
+ else:
+ os.environ["LAYA_DEVICE"] = old
+ # force= is the one path that can, and it is returned verbatim on purpose. Checked
+ # defensively: a future edit that parses `force` too must fail a named check, not the run.
+ for raw in ("cuda:", "gpu", "cuda: 0"):
+ try:
+ given = resolve_device(raw)
+ except Exception as exc:
+ ok("force=%r comes back as given" % raw, False, "raised %s: %s" % (type(exc).__name__, exc))
+ continue
+ ok("force=%r comes back as given" % raw, given == raw, repr(given))
+ ok("force=%r is a label torch refuses" % raw, not parses(given), repr(given))
+
+ # The claim itself, read off the docstring: whatever states the parse guarantee has to
+ # name `force` as the path it does not cover. A docstring that reverts to "there is no
+ # path on which this function labels a device torch would refuse" fails both halves --
+ # and the second half is what the line above pins as true.
+ tail = inspect.getdoc(resolve_device).split("torch cannot parse", 1)
+ ok("the docstring still states the parse guarantee", len(tail) == 2, inspect.getdoc(resolve_device))
+ claim = tail[1] if len(tail) == 2 else ""
+ ok("the parse claim names force as the exception", "`force`" in claim, claim)
+ ok("the parse claim is scoped, not universal", "no path" not in claim, claim)
+
+
+def test_agent_device_answers_by_input_shape():
+ # A loaded agent's `device` is a torch.device, whose `type` carries no index; the plain
+ # string the suite accepts for fakes has no such split, so the two shapes do not answer
+ # the same question. Both halves are what `agent_device`'s docstring now says.
+ def held(device):
+ return type("Agent", (), {"device": device})()
+
+ for raw in ("cuda:1", "cuda:0", "cpu:0"):
+ ok("torch.device(%r) loses its index" % raw, agent_device(held(torch.device(raw))) == raw.split(":")[0],
+ repr(agent_device(held(torch.device(raw)))))
+ ok("the same string keeps its index", agent_device(held(raw)) == raw, repr(agent_device(held(raw))))
+ ok("a type-only device is unchanged", agent_device(held(torch.device("mps"))) == "mps"
+ and agent_device(held("mps")) == "mps")
+ # The asymmetry above is invisible to a caller who reads only the docstring, so the prose
+ # is pinned to it: whatever describes this function has to say that an index is dropped.
+ doc = inspect.getdoc(agent_device) or ""
+ ok("the docstring states that the index is not reported", "index" in doc, doc)
+
+
def test_reported_equals_handed_to_torch():
old = os.environ.get("LAYA_DEVICE")
try:
@@ -128,7 +211,9 @@ def main():
test_normalisation_still_applies_first,
test_the_parser_is_the_judge_not_an_allowlist,
test_unparseable_values_are_rejected_by_name,
- test_no_reporting_path_labels_a_refused_device,
+ test_the_env_path_raises_and_force_passes_through,
+ test_force_is_the_only_path_that_can_label_a_refused_device,
+ test_agent_device_answers_by_input_shape,
test_reported_equals_handed_to_torch):
fn()
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))