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.

feat(mcp): add in-process tournament prediction with existing controls

Rishit Sharmabba201104f6e
diff --git a/laya/mcp/tools.py b/laya/mcp/tools.py
index 5c7c9988..6da68225 100644
--- a/laya/mcp/tools.py
+++ b/laya/mcp/tools.py
@@ -851,6 +851,101 @@ def laya_shortlist(
return out
+def laya_tournament(
+ state: Any,
+ questions: Any,
+ model: Any = "auto",
+ group_size: Any = 16,
+ *,
+ task: Any = None,
+ lang: Any = None,
+ lang_guess: Any = None,
+ max_len: Any = None,
+ head_max_len: Any = None,
+ min_confidence: Any = None,
+ router: Any = None,
+ agent: Any = None,
+) -> dict:
+ """Answer large choices with core's balanced tournament in-process.
+
+ Select the checkpoint from the original request, then pin it for every round.
+ Each round uses ``laya_predict``'s validation, answer shape and device reporting.
+ Probabilities and confidence describe the final call's finalists; ``tournament``
+ adds their ``labels``, the original option count ``n`` and elimination ``rounds``.
+ Small choices report zero rounds; non-choice questions reach the final call.
+ ``latency_ms`` covers routing and all rounds. A narrowed question's discarded
+ ``option_order`` is reported with its original and kept option counts.
+
+ Remote callers use ``laya_predict`` with the HTTP server's tournament enabled;
+ its probability and usage contract differs from this core helper's contract.
+ """
+ if router is not None and agent is None and hasattr(router, "base_url") and hasattr(router, "health"):
+ raise ToolError(
+ "unsupported_remote",
+ "laya_tournament runs core tournament rounds in-process; it is not available when "
+ "LAYA_BASE_URL points at laya-serve. Use laya_predict with LAYA_TOURNAMENT_GROUP "
+ "enabled on that server, or run the MCP server without LAYA_BASE_URL.",
+ )
+ state_d = validate_state(state)
+ questions_d = validate_questions(questions)
+ model_name = validate_model(model)
+ if isinstance(group_size, bool) or not isinstance(group_size, int) or group_size < 2:
+ raise ToolError("invalid_group_size", f"group_size must be an integer of at least 2, got {group_size!r}")
+ routing_overrides = _overrides(validate_task(task), validate_lang(lang), None, None)
+ hint = validate_lang_guess(lang_guess)
+ if hint is not None:
+ routing_overrides["lang_guess"] = hint
+ forward = _overrides(None, None, validate_budget(max_len, "max_len"),
+ validate_budget(head_max_len, "head_max_len"))
+ min_conf = validate_min_confidence(min_confidence)
+ if min_conf is not None:
+ forward["min_confidence"] = min_conf
+ for name in ("task", "lang_guess"):
+ if model_name != AUTO and name in routing_overrides:
+ raise ToolError(f"invalid_{name}",
+ f"{name} routes between checkpoints; with a pinned model there is nothing to route")
+ if "lang" in routing_overrides:
+ forward["lang"] = routing_overrides["lang"]
+
+ from laya.shortlist import predict_tournament
+
+ started = time.perf_counter()
+ routing = None
+ predict_agent = agent
+ if model_name == AUTO and router is not None:
+ if not hasattr(router, "route"):
+ raise ToolError("internal_error", "router has no route() method")
+ decision = router.route(state_d, questions_d, **routing_overrides)
+ routing = _decision_to_dict(decision)
+ model_name = routing["model"]
+ if not isinstance(model_name, str) or not model_name:
+ raise ToolError("internal_error", "router.route returned no model")
+ detection = (decision.get("detection") if isinstance(decision, dict)
+ else getattr(decision, "detection", None))
+ detected = _detected_language(detection)
+ if detected is not None and "lang" not in forward:
+ forward["lang"] = detected
+ predict_agent = None
+
+ class Predictor:
+ def predict(self, round_state, round_questions):
+ return laya_predict(round_state, round_questions, model=model_name,
+ router=router, agent=predict_agent, **forward)
+
+ out = predict_tournament(Predictor(), state_d, questions_d, group_size=group_size)
+ if routing is not None:
+ out["routing"] = routing
+ out["latency_ms"] = round((time.perf_counter() - started) * 1000.0, 3)
+ dropped_orders = {
+ name: {"n": entry["n"], "kept": len(entry["labels"])}
+ for name, entry in out["tournament"].items()
+ if entry["rounds"] and "option_order" in questions_d[name]
+ }
+ if dropped_orders:
+ out["option_order_dropped"] = dropped_orders
+ return out
+
+
def laya_preset(
preset: Any,
state: Any,
diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.py
index f1914b7e..9c7a7714 100644
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@ -107,6 +107,15 @@ if _mcp_batch_tool is not None:
check_param("laya_predict_batch_tool", _mcp_batch_tool, "min_confidence", None)
check_param("laya_predict_batch_tool", _mcp_batch_tool, "sort_by_length", False)
+from laya.mcp.tools import laya_tournament # noqa: E402
+
+check_param("laya_tournament", laya_tournament, "group_size", 16)
+check_param("laya_tournament", laya_tournament, "model", "auto")
+for param in ("state", "questions"):
+ check_param("laya_tournament", laya_tournament, param, inspect.Parameter.empty)
+for param in ("task", "lang", "lang_guess", "max_len", "head_max_len", "min_confidence", "router", "agent"):
+ check_param("laya_tournament", laya_tournament, param, None, inspect.Parameter.KEYWORD_ONLY)
+
# What the constructor does NOT raise over is part of its contract too: a shared
# `agent_kwargs["expected_sha256"]` overlapping a per-checkpoint `sha256_digests` entry on one file
# name is the ordinary shape, not a contradiction -- `model.safetensors` is the one name every
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index 4e56dc85..9d834773 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -46,6 +46,7 @@ from laya.mcp.tools import ( # noqa: E402
laya_route_batch,
laya_shortlist,
laya_status,
+ laya_tournament,
validate_batch_requests,
validate_budget,
validate_lang,
@@ -994,6 +995,163 @@ BATCH_REQUESTS = [
]
+class TournamentAgent:
+ device = "cpu"
+
+ def __init__(self):
+ self.calls = []
+
+ def predict(self, state, questions, **kwargs):
+ self.calls.append((state, questions, kwargs))
+ answers = {}
+ for name, spec in questions.items():
+ if spec["type"] == "choice":
+ labels = list(spec["criteria"])
+ winner = max(labels, key=lambda label: int(label[3:]))
+ answers[name] = {"type": "choice", "choice": winner, "confidence": 0.9,
+ "probabilities": {label: float(label == winner) for label in labels}}
+ if kwargs.get("min_confidence", 0) > 0.9:
+ answers[name]["low_confidence"] = True
+ elif spec["type"] == "score":
+ answers[name] = {"type": "score", "score": 1.0, "confidence": 0.9}
+ else:
+ answers[name] = {"type": "noul", "noul": 0.9, "confidence": 0.9}
+ return {"answers": answers, "usage": {"call": len(self.calls)}}
+
+
+class TournamentRouter:
+ def __init__(self, decision=None):
+ self.decision = decision or {"model": "english", "repo": "fake/repo", "reason": "original request"}
+ self.routes = []
+ self.agent = TournamentAgent()
+ self._agents = {self.decision["model"]: self.agent}
+
+ def route(self, state, questions, **kwargs):
+ self.routes.append((state, questions, kwargs))
+ return self.decision
+
+ def predict(self, state, questions, **kwargs):
+ result = self.agent.predict(state, questions, **kwargs)
+ result["routing"] = {"model": kwargs["model"], "repo": "fake/repo", "reason": "explicit model"}
+ return result
+
+
+def tournament_questions(n):
+ return {"intent": {"type": "choice", "instructions": "Which intent does `body` express?",
+ "criteria": {"opt%d" % i: "intent %d" % i for i in range(n)}}}
+
+
+def test_tournament():
+ import copy
+
+ for n, rounds, calls, sizes in ((40, 1, 2, [13, 13, 14]), (257, 2, 3, [15] * 8 + [16] + [15] * 7 + [16])):
+ questions = tournament_questions(n)
+ questions["rubric"] = {"type": "score", "instructions": "Rate urgency", "criteria": ["low", "high"]}
+ questions["flag"] = {"type": "noul", "instructions": "Is this urgent?"}
+ original = copy.deepcopy(questions)
+ agent = TournamentAgent()
+ out = laya_tournament(STATE, questions, model="english", agent=agent)
+ ok("tournament/%d_balanced" % n,
+ [len(q["criteria"]) for q in agent.calls[0][1].values()] == sizes)
+ ok("tournament/%d_rounds_and_calls" % n,
+ out["tournament"]["intent"]["rounds"] == rounds and len(agent.calls) == calls)
+ ok("tournament/%d_best_survives" % n, out["answers"]["intent"]["choice"] == "opt%d" % (n - 1))
+ ok("tournament/%d_final_carries_all_questions" % n,
+ set(agent.calls[-1][1]) == set(questions) and set(out["answers"]) == set(questions))
+ ok("tournament/%d_only_choices_have_metadata" % n, set(out["tournament"]) == {"intent"})
+ meta = out["tournament"]["intent"]
+ ok("tournament/%d_original_count" % n, meta["n"] == n)
+ ok("tournament/%d_finalist_probabilities" % n,
+ list(out["answers"]["intent"]["probabilities"]) == meta["labels"])
+ ok("tournament/%d_input_preserved" % n, questions == original)
+ ok("tournament/%d_shape" % n,
+ set(out) == {"answers", "routing", "latency_ms", "device", "tournament"}
+ and out["device"] == "cpu" and out["latency_ms"] >= 0)
+
+ questions = tournament_questions(40)
+ questions["small"] = tournament_questions(2)["intent"]
+ questions["intent"]["option_order"] = list(reversed(range(40)))
+ questions["small"]["option_order"] = [1, 0]
+ original = copy.deepcopy(questions)
+ agent = TournamentAgent()
+ import warnings
+
+ with warnings.catch_warnings(record=True) as seen:
+ warnings.simplefilter("always")
+ out = laya_tournament(STATE, questions, model="en", agent=agent, group_size=4,
+ lang="de", max_len=1024, head_max_len=384, min_confidence=0.95)
+ ok("tournament/order_drop_once", len(seen) == 1 and "option_order" in str(seen[0].message))
+ ok("tournament/order_drop_report", out["option_order_dropped"] == {"intent": {"n": 40, "kept": 3}})
+ ok("tournament/orders_input_preserved", questions == original)
+ ok("tournament/small_order_kept", agent.calls[-1][1]["small"]["option_order"] == [1, 0])
+ ok("tournament/narrowed_order_removed",
+ all("option_order" not in q for _s, qs, _k in agent.calls[:-1] for q in qs.values()))
+ ok("tournament/controls_every_round",
+ all(kwargs == {"lang": "de", "max_len": 1024, "head_max_len": 384, "min_confidence": 0.95}
+ for _s, _qs, kwargs in agent.calls))
+ ok("tournament/low_confidence_final", out["answers"]["intent"]["low_confidence"] is True)
+ ok("tournament/custom_size_rounds", len(agent.calls) == 3 and out["tournament"]["intent"]["rounds"] == 2)
+
+ for model in ("english", None, "auto"):
+ agent = TournamentAgent()
+ small = tournament_questions(2)
+ out = laya_tournament(STATE, small, model=model, agent=agent)
+ ok("tournament/%r_passthrough" % model, len(agent.calls) == 1 and agent.calls[0][1] == small)
+ ok("tournament/%r_zero_round_metadata" % model,
+ out["tournament"] == {"intent": {"labels": ["opt0", "opt1"], "n": 2, "rounds": 0}})
+ ok("tournament/%r_direct_routing" % model, out["routing"]["model"] == ("english" if model == "english" else None))
+ ok("tournament/%r_no_order_drop" % model, "option_order_dropped" not in out)
+ agent = TournamentAgent()
+ out = laya_tournament(STATE, {"flag": {"type": "noul", "instructions": "Is this urgent?"}},
+ model="english", agent=agent)
+ ok("tournament/non_choice_one_call", len(agent.calls) == 1 and out["tournament"] == {})
+
+
+def test_tournament_routing_and_validation():
+ questions = tournament_questions(40)
+ for detection in ({"language": "de"}, SimpleNamespace(language="de")):
+ router = TournamentRouter({"model": "multilingual", "repo": "fake/ml", "reason": "German",
+ "detection": detection})
+ unused = TournamentAgent()
+ out = laya_tournament(STATE, questions, router=router, agent=unused,
+ task="typed_decisions", lang_guess="de", max_len=1024)
+ ok("tournament/auto_original_request", router.routes == [(STATE, questions, {"task": "typed_decisions", "lang_guess": "de"})])
+ ok("tournament/auto_pinned_every_round",
+ all(kwargs == {"model": "multilingual", "lang": "de", "max_len": 1024}
+ for _s, _qs, kwargs in router.agent.calls))
+ ok("tournament/auto_preserves_route", out["routing"] == {"model": "multilingual", "repo": "fake/ml", "reason": "German"})
+ ok("tournament/auto_uses_routed_agent", unused.calls == [] and out["device"] == "cpu")
+ router = TournamentRouter({"model": "multilingual", "reason": "German", "detection": detection})
+ laya_tournament(STATE, questions, router=router, lang="fr")
+ ok("tournament/explicit_lang_wins", all(k["lang"] == "fr" for _s, _qs, k in router.agent.calls))
+
+ router = TournamentRouter()
+ out = laya_tournament(STATE, questions, model="EN", router=router)
+ ok("tournament/pin_skips_selection", router.routes == [] and out["routing"]["model"] == "english")
+ ok("tournament/pin_no_unset_controls", all(k == {"model": "english"} for _s, _qs, k in router.agent.calls))
+ for args, code in (({"state": []}, "invalid_state"), ({"questions": {}}, "invalid_questions"),
+ ({"model": "gpt4"}, "invalid_model"), ({"task": "bad"}, "invalid_task"),
+ ({"lang": 3}, "invalid_lang"), ({"lang_guess": 3}, "invalid_lang_guess"),
+ ({"max_len": 0}, "invalid_max_len"), ({"head_max_len": "384"}, "invalid_head_max_len"),
+ ({"min_confidence": 2}, "invalid_min_confidence"),
+ ({"model": "english", "task": "typed"}, "invalid_task"),
+ ({"model": "english", "lang_guess": "de"}, "invalid_lang_guess")):
+ router = TournamentRouter()
+ params = {"state": STATE, "questions": questions, "router": router, **args}
+ expect_tool_error("tournament/invalid_%r" % args, lambda: laya_tournament(**params), code)
+ ok("tournament/invalid_before_inference_%r" % args, not router.routes and not router.agent.calls)
+ for size in (True, False, None, 1, 0, -1, 2.5, "16"):
+ expect_tool_error("tournament/invalid_group_%r" % size,
+ lambda: laya_tournament(STATE, questions, group_size=size), "invalid_group_size")
+ expect_tool_error("tournament/not_ready", lambda: laya_tournament(STATE, questions), "models_not_ready")
+ expect_tool_error("tournament/pinned_not_ready",
+ lambda: laya_tournament(STATE, questions, model="english"), "models_not_ready")
+ bad = tournament_questions(40)
+ bad["intent"]["option_order"] = [0]
+ expect_tool_error("tournament/bad_order_before_rounds",
+ lambda: laya_tournament(STATE, bad), "invalid_questions")
+
+
def test_batch_validation():
for bad in (None, {}, "x", {"state": STATE, "questions": QUESTIONS}):
expect_tool_error("batch/requests_bad_%r" % (type(bad).__name__,),
@@ -2912,6 +3070,8 @@ test_shape()
test_question_forwarding()
test_option_order_forwarding()
test_shortlist()
+test_tournament()
+test_tournament_routing_and_validation()
test_batch_validation()
test_batch_predict()
test_batch_tool_control_forwarding()