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
feat: add lang_temperatures and fix shadowing bug (#258)
R SAI DHEERAJc115b7b700eb
diff --git a/laya/agent.py b/laya/agent.pyindex 5af79da9..41b205b6 100644--- a/laya/agent.py+++ b/laya/agent.py@@ -213,6 +213,7 @@ class Agent(HookRegistry):subfolder: Optional[str] = None,fast: bool = False,compile: bool = False,+ lang_temperatures: Optional[Dict[str, Dict[str, Any]]] = None,hooks=None,on_predict_start=None,on_predict_end=None,@@ -349,6 +350,18 @@ class Agent(HookRegistry):self.temperature = [clamp_temperature(t) for t in self.temperature_raw]self.temperature_by_options = {k: clamp_temperature(v)for k, v in self.temperature_by_options_raw.items()}++ self.lang_temperatures = {}+ for l, cfg in (lang_temperatures or {}).items():+ norm_l = l.split("-")[0].lower()+ t_raw = cfg.get("temperature", self.temperature_raw)+ if len(t_raw) != 3:+ raise ValueError("Language override %r temperature must be a list of 3 floats" % l)+ tbo_raw = cfg.get("temperature_by_options", {})+ self.lang_temperatures[norm_l] = {+ "temperature": [clamp_temperature(t) for t in t_raw],+ "temperature_by_options": {k: clamp_temperature(v) for k, v in tbo_raw.items()}+ }entries = [(k, v, self.temperature_by_options[k]) for k, v in self.temperature_by_options_raw.items()]entries += [("temperature[%d]" % i, t, self.temperature[i]) for i, t in enumerate(self.temperature_raw)]rejected = []@@ -622,7 +635,7 @@ class Agent(HookRegistry):return logits.float().cpu().numpy(), torch.softmax(act.float(), -1).cpu().numpy()def _decode_answers(self, logits, act, items: List[Dict], ids: List[str],- internal: Dict[str, Dict], offset: int) -> Dict[str, Any]:+ internal: Dict[str, Dict], offset: int, lang: Optional[str] = None) -> Dict[str, Any]:"""Turn one state's logit rows (starting at `offset`) into typed answers."""answers = {}for j, qid in enumerate(ids):@@ -631,6 +644,9 @@ class Agent(HookRegistry):k = len(items[j]["markers"])qt = QTYPES[q["t"]]t_scale = self.temperature_by_options.get(temp_bucket(qt, k), self.temperature[qt])+ if lang and lang.split("-")[0].lower() in self.lang_temperatures:+ l_cfg = self.lang_temperatures[lang.split("-")[0].lower()]+ t_scale = l_cfg["temperature_by_options"].get(temp_bucket(qt, k), l_cfg["temperature"][qt])z = logits[r, :k] / t_scalep = np.exp(z - z.max())p = p / p.sum()@@ -667,7 +683,8 @@ class Agent(HookRegistry):@torch.no_grad()def predict_batch(self, states: List[Union[str, dict, list]], questions: Dict[str, Dict[str, Any]],- batch_size: Optional[int] = None, hooks=None,+ batch_size: Optional[int] = None, lang: Optional[str] = None,+ hooks=None,on_predict_start=None, on_predict_end=None,hooks_raise: Optional[bool] = None,max_len: Optional[int] = None,@@ -766,7 +783,8 @@ class Agent(HookRegistry):for index, items in zip(indices, per_state_items):nrows = len(items)n_tokens = int(att[row:row + nrows].sum())- answers = self._decode_answers(logits, act, items, ids, internal, row)+ answers = self._decode_answers(logits, act, items, ids, internal, row,+ **({"lang": lang} if lang else {}))window_results[index] = {"model": "laya-rl-agent","answers": answers,@@ -798,7 +816,7 @@ class Agent(HookRegistry):return ctx.results@torch.no_grad()- def system_one(self, state: Union[str, dict, list], questions: Dict[str, Dict[str, Any]],+ def system_one(self, state: Union[str, dict, list], questions: Dict[str, Dict[str, Any]], lang: Optional[str] = None,hooks=None, on_predict_start=None, on_predict_end=None,hooks_raise: Optional[bool] = None,max_len: Optional[int] = None,@@ -825,7 +843,7 @@ class Agent(HookRegistry):To score many states at once, see `predict_batch`, which shares forward passes across them."""- return self.predict_batch([state], questions, hooks=hooks,+ return self.predict_batch([state], questions, lang=lang, hooks=hooks,on_predict_start=on_predict_start,on_predict_end=on_predict_end, hooks_raise=hooks_raise,max_len=max_len, head_max_len=head_max_len)[0]@@ -855,6 +873,7 @@ RLAgent = Agentdef load(model_id_or_path: str = "convaiinnovations/laya", device: Optional[str] = None,token: Optional[str] = None, subfolder: Optional[str] = None, fast: bool = False,+ lang_temperatures: Optional[Dict[str, Dict[str, Any]]] = None,hooks=None, on_predict_start=None, on_predict_end=None,hooks_raise: bool = True, hooks_concurrent: bool = True) -> Agent:"""Load a Laya agent.@@ -869,5 +888,6 @@ def load(model_id_or_path: str = "convaiinnovations/laya", device: Optional[str]`laya.hooks`."""return Agent(model_id_or_path, device=device, token=token, subfolder=subfolder, fast=fast,+ lang_temperatures=lang_temperatures,hooks=hooks, on_predict_start=on_predict_start, on_predict_end=on_predict_end,hooks_raise=hooks_raise, hooks_concurrent=hooks_concurrent)diff --git a/laya/router.py b/laya/router.pyindex 10dcb97b..ccbe5980 100644--- a/laya/router.py+++ b/laya/router.py@@ -529,6 +529,10 @@ class Router(HookRegistry):decision = self.route(state, questions, model=model, task=task, lang=lang,lang_guess=lang_guess, hooks=hooks, hooks_raise=hooks_raise)agent = self.load(decision["model"])+ effective_lang = lang+ if effective_lang is None and decision.get("detection") and decision["detection"].get("language"):+ effective_lang = decision["detection"]["language"]+ctx = PredictContext(states=[state], questions=questions, decision=dict(decision),model=decision["model"], agent=agent, router=self,max_len=max_len, head_max_len=head_max_len)@@ -542,7 +546,14 @@ class Router(HookRegistry):overrides["max_len"] = ctx.max_lenif ctx.head_max_len is not None:overrides["head_max_len"] = ctx.head_max_len- result = agent.system_one(ctx.states[0], ctx.questions, **overrides)++ try:+ result = agent.system_one(ctx.states[0], ctx.questions, lang=effective_lang, **overrides)+ except TypeError as e:+ if "unexpected keyword argument 'lang'" in str(e):+ result = agent.system_one(ctx.states[0], ctx.questions, **overrides)+ else:+ raiseresult["routing"] = dict(decision)ctx.results = [result]else:diff --git a/tests/test_system_one_lang.py b/tests/test_system_one_lang.pynew file mode 100644index 00000000..5152036f--- /dev/null+++ b/tests/test_system_one_lang.py@@ -0,0 +1,30 @@+from typing import Any, Dict, List, Union+import pytest+from laya.agent import Agent++class DummyAgent(Agent):+ def __init__(self):+ # Skip the whole HuggingFace initialization for testing the plumbing+ self.device = "cpu"+ self.temperature_by_options = {}+ self.temperature = [1.0, 1.0, 1.0]+ self.lang_temperatures = {}+ self.cfg = {"max_len": 512, "head_max_len": 192}++ def predict_batch(self, states: List[Union[str, dict, list]], questions: Dict[str, Dict[str, Any]],+ batch_size: int = None, lang: str = None, **kwargs) -> List[Dict[str, Any]]:+ # Mock predict_batch to just return the lang it was given so we can test the pass-through+ return [{"lang_passed_down": lang}]+++def test_system_one_lang_parameter_passthrough():+ agent = DummyAgent()+ questions = {"q1": {"type": "noul", "instructions": "test"}}++ # 1. Test without lang+ res_no_lang = agent.system_one("test state", questions)+ assert res_no_lang.get("lang_passed_down") is None++ # 2. Test with lang+ res_with_lang = agent.system_one("test state", questions, lang="zh-CN")+ assert res_with_lang.get("lang_passed_down") == "zh-CN"