Skip to content

fix(mcp): refuse a LAYA_DEVICE index torch rewrites instead of building another GPU - #1242

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-device-index-rewrite
Oct 17, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-device-index-rewrite

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

laya/mcp/device.py::_check_torch_device refuses a LAYA_DEVICE value torch cannot parse. It
now also refuses one torch rewrites: a value whose parsed device is not the device the string
names raises a ValueError naming LAYA_DEVICE, quoting both the operator's spelling and the
device torch would actually build.

Four files, +120/−8:

file change
laya/mcp/device.py the guard, plus the module / env_device / resolve_device docstrings that state its scope
laya/mcp/server.py the LAYA_DEVICE line of the server docstring, which listed only the parse refusals
tests/test_mcp_device.py 44 new arms (51 -> 95 passed)
docs/cli-mcp.md the LAYA_DEVICE row, which promised the refusal covers only unparseable strings

force= (the tests hook, mirroring the CLI --device flag) is unchanged: still returned
verbatim, still unchecked. That asymmetry is the module's documented contract, and a new arm
pins it.

Why

The module's headline claim is that "the device a client is told about and the device torch is
asked for cannot drift apart". A device index breaks it while the parse check passes, because
torch reads back an index it cannot keep as a different one — on 2.6.0 only the low 8 bits
survive. Measured on the base commit (4ac226d5, release 0.4.8), clean tree, torch 2.6.0:

torch.device('cuda:0')   -> cuda:0      torch.device('cuda:256') -> cuda:0
torch.device('cuda:7')   -> cuda:7      torch.device('cuda:258') -> cuda:2
torch.device('cuda:127') -> cuda:127    torch.device('cuda:300') -> cuda:44
torch.device('cuda:128') -> cuda:-128   torch.device('cuda:511') -> cuda
                                       torch.device('mps:70000') -> mps:112

None of those raise. So LAYA_DEVICE=cuda:258 starts a healthy server, and every surface that
reports the device repeats the operator's typo back to them while the process asks torch for a
different GPU. Base-vs-branch probe, same command, laya.__file__ printed to prove which copy
answered:

CONTROL (base 4ac226d5, clean)              BRANCH (this change)
torch: 'cuda:2'                             torch: 'cuda:2'
env_device    -> 'cuda:258'                 env_device    -> ValueError
resolve_device-> 'cuda:258'                 resolve_device-> ValueError
device_report -> {'device': 'cuda:258',     device_report -> ValueError
                  'torch_cuda': False}
  laya_from .../laya-base-4ac2/laya/mcp/device.py
  laya_from .../laya-device-index/laya/mcp/device.py

The branch message:

LAYA_DEVICE='cuda:258' is not the device torch asks for: torch reads it as 'cuda:2' -- use a
device such as 'cpu', 'cuda', 'cuda:0' or 'mps'

The concrete use case is a multi-GPU box where the operator means cuda:2 and types an index
that wraps, or reaches past the device count: cuda:258 is the same class of typo as cuda:,
and today only one of the two is attributed to the environment. laya.serve reads the value
through the same env_device (laya/serve.py:1115), so the HTTP /health device field has
the same drift. force= and the CLI --device flag are deliberately left as the unchecked
path, exactly as the existing docstring says.

The new test arms are written as a biconditional against torch's own parser — refused iff
this torch would not build the string verbatim — so on a torch that stops truncating the
refusal arms stop asserting and the suite still passes, instead of asserting a falsehood. The
message-shape arms derive their inputs at runtime for the same reason, and print a NOTE line
when no candidate rewrites, so a vacuous pass is visible rather than silent.

How it was verified

PY=/Users/aashish/.subproto/laya-venv/bin/python   # 3.11.15, torch 2.6.0

# the suite this change extends: 95 passed / 0 failed on the branch
PYTHONPATH="$PWD" $PY -B tests/test_mcp_device.py
# control tree, same base, clean worktree: 51 passed / 0 failed -> the +44 arms are this change

# the repo's own gate list (what CI and the release gate share), branch and control
LAYA_TREE="$PWD" $PY -B "/Users/aashish/github contribution /GIT/laya-bench/ci_gate.py"
# -> 78 invocations on each tree; per-invocation diff after normalising paths/tmpdirs/timings:
#    identical: 77   differing: 1  (tests/test_mcp_device.py)
# -> pytest lane on both trees: 3 failed, 473 passed, 3 skipped
#    the 3 failures are torch.compile/inductor cases that need a CUDA device, and the
#    env-red lane failure is tests/test_langchain_real_mode.py: langchain-core is not
#    installed here. Both are identical on the control tree, so neither is this change.

$PY -m ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120        # All checks passed
$PY -m ruff check tests/test_mcp_device.py --select=E9,F63,F7,F82,F401,F811 --line-length=120
$PY -m compileall -q laya/ tests/                                                  # rc 0
$PY -B laya-java/scripts/gen_fixtures.py --check   # rc 1 on both trees, identical message set
                       # (lang_detect.json / python_json.json already stale at base; untouched here)
git diff > /tmp/device_diff.txt && wc -l /tmp/device_diff.txt      # 232 lines, 4 modified files, no new files
grep -icE "api[_-]?key|secret|token|password|credential|private key|AKIA|ghp_|hf_[A-Za-z0-9]{30,}|sk-[A-Za-z0-9]{30,}" /tmp/device_diff.txt
grep -oE "[A-Za-z0-9+/]{40,}={0,2}" /tmp/device_diff.txt           # both: no matches (grep rc=1)

Docs (docs/cli-mcp.md changed) build strict and clean from the repo root — rc 0, No issues found, Build finished in 1.68s — and the rendered row was read back out of
site/cli-mcp/index.html, so the sentence that reaches a user is the one asserted above.

The guard was mutation-checked, six faithful mutants of laya/mcp/device.py against
tests/test_mcp_device.py, each reverted before the next and the file restored byte-for-byte
(restored: True (sha256 84781a7cc173 == 84781a7cc173)). Predictions were written before the
run; all six matched, 0 mismatched:

mutant predicted result what caught it
delete the rewrite branch KILL KILL, 65 passed / 30 failed the six rewrite rows stop raising
invert it (==) KILL KILL, suite aborts at tests/test_mcp_device.py:56 cpu itself stops being accepted
compare only parsed.type KILL KILL, 65 passed / 30 failed an index rewrite slips through the guard
generic message, no LAYA_DEVICE KILL KILL, 77 passed / 18 failed the attribution arms
parse the raw value before normalising KILL KILL, suite aborts at :56 CUDA:0 stops being accepted
validate force= too KILL KILL, 86 passed / 5 failed the force pass-through arm

The two aborts count as detections: an uncaught ValueError out of env_device_with is the
suite dying on a refusal it did not expect, and the harness reports the frame it died at rather
than calling the run inconclusive.

Cross-port check (before touching a parameter any fixture generator pins, since the port
generators record Python's edge values as their oracle): no port reads LAYA_DEVICE — a grep
over the tree finds it only in Python, its tests, the docs, the compose files, Dockerfile,
nix/ and .github/ — and no fixture carries a device string: laya-java (20 JSON),
laya-go (1), laya-dotnet (71) and sdk (5) contain zero matches for cuda. There is no
laya-rust in this tree. So this is a Python-only change and the port oracles are unaffected.

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main (4ac226d5 release: 0.4.8 — HEAD^ is that commit; verified
    with git merge-base --is-ancestor refs/remotes/upstream/main HEAD, since a stale local
    branch named upstream/main makes the short ref ambiguous in this checkout)
  • Tests pass locally (tests/test_mcp_device.py 95/0; the one gate invocation that differs
    from the control tree is that suite; the two env-red lanes are identical on both trees)
  • Docs or examples updated when the public API changed (docs/cli-mcp.md row, plus the
    laya/mcp/server.py and laya/mcp/device.py docstrings that described the narrower guard)

Fixes #

…ng another GPU

env_device asks torch to parse the value before returning it, which is how
`gpu` and `cuda:` became a named environment error rather than a torch
internal one. Parsing is only half of "the device a client is told about and
the device torch is asked for cannot drift apart": an index torch cannot keep
is read back as a different one -- on 2.6.0 it survives only in its low 8
bits, so `LAYA_DEVICE=cuda:258` parses happily, is reported verbatim by
laya_status and laya.serve's /health, and builds GPU 2.

The guard is now "what torch builds is what the string names": a value whose
parsed device is not the device it names raises a ValueError naming
LAYA_DEVICE, quoting both the operator's spelling and torch's answer, with
its own sentence so the two refusals stay tellable apart. `force=` keeps
passing through verbatim -- that asymmetry is the documented tests hook, and
the suite pins it.

Tests: 44 new arms in tests/test_mcp_device.py (51 -> 95), written as a
biconditional against torch's own parser so a torch that stops truncating
leaves the refusals unasserted rather than asserting a falsehood.
@NandhaKishorM
NandhaKishorM merged commit 496d8e7 into NandhaKishorM:main Oct 17, 2026
45 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged in 0.4.9.

I measured this before merging because the whole case rests on it, and it is exactly as reported:

cuda:258   -> 'cuda:2'      same=False
cuda:128   -> 'cuda:-128'   same=False
cuda:0     -> 'cuda:0'      same=True
cpu/mps/meta/cuda          same=True

So torch keeps a CUDA index in its low 8 bits, reads cuda:258 back as GPU 2 and cuda:128 as a negative index. Parsing successfully was never the contract this module wanted: the string the client is told and the device asked of torch have to be the same device, and str(parsed) != value is the honest way to ask that without a list of index limits that would go stale.

Using torch.device as the parser rather than a lookalike was already the right call in this module, and the round-trip test keeps that property: every ordinary spelling satisfies it, so the guard costs nothing in reach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants