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
test(runtime): hold example 35's precision prose to the branch that sets it
Aashish5b4cbdcc6fd9
diff --git a/tests/test_runtime_fixes.py b/tests/test_runtime_fixes.pyindex 4f06a87f..24a92daa 100644--- a/tests/test_runtime_fixes.py+++ b/tests/test_runtime_fixes.py@@ -382,6 +382,148 @@ check("oom-fallback/plain CPU infer stays 0", cpu_disabled.cpu_fallback_count, 0check("oom-fallback/plain CPU reason stays None", cpu_disabled.last_fallback_reason, None)+# ------------------------------------------------------------------ example 35 must teach this policy+# examples/35 is the page a reader opens to ask "does my machine get mixed precision?", and on main+# it answered no twice: "CPU and MPS run **fp32**; `torch.autocast` has no MPS backend here, so Laya+# only wraps the forward pass on CUDA" in the banner, then, printed under the timings, "both legs+# used fp32 (device.type in ('cpu','mps') -> torch.float32); mixed precision would only be enabled+# on CUDA." The MPS branch of `Agent.__init__` sets `amp_enabled = True` at `torch.float16`, and+# `_amp_enabled_for` decides per call by row count. The second sentence was half true, though -- that+# 3-question call really did run fp32 -- so the gate does not ban the words. It reads the policy out+# of `Agent.__init__` by AST, so a rename or a reformat moves the gate and not the test, and it asks+# any sentence that pairs MPS with fp32 to name the key that decided it (`mps_amp_min_rows`, or the+# `dtype_for(rows)` that consults it). The page also had no more right to the number 5 than to the+# rule, so that is pinned to `MPS_AMP_MIN_ROWS_DEFAULT` rather than repeated here. This is the same+# shape as the example gates in tests/test_truncation.py: prose policed per sentence, contract read+# from the source.+import ast # noqa: E402+import inspect # noqa: E402+import re # noqa: E402+import textwrap # noqa: E402++import laya.agent as laya_agent # noqa: E402++ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+EX35 = "examples/35_device_selection_and_latency.py"+DEVICES = ("cuda", "mps", "xpu", "cpu")+BRANCH_TEST = re.compile(r"self\.device\.type == ['\"]([a-z]+)['\"]")+++def _branch(device):+ """`Agent.__init__`'s `if/elif self.device.type == "<device>"` branch, statement by statement.++ Unparsed from the AST, which normalizes indentation, spacing and line breaks: a branch that is+ only reformatted still reads the same, so this cannot fail for a reason that is not a change of+ policy. That matters here because the branch is exactly the kind of line a reformat touches.+ """+ tree = ast.parse(textwrap.dedent(inspect.getsource(Agent.__init__)))+ for node in ast.walk(tree):+ if isinstance(node, ast.If):+ match = BRANCH_TEST.search(ast.unparse(node.test))+ if match and match.group(1) == device:+ return "\n".join(ast.unparse(stmt) for stmt in node.body)+ return ""+++def _strings(rel):+ """Every string the example carries: the module docstring, the banner, each printed line."""+ with open(os.path.join(ROOT, rel)) as fh:+ tree = ast.parse(fh.read(), filename=rel)+ nodes = [n for n in ast.walk(tree) if isinstance(n, ast.Constant) and isinstance(n.value, str)]+ nodes.sort(key=lambda n: (n.lineno, n.col_offset))+ return [n.value for n in nodes]+++# What the runtime promises, read rather than asserted.+BODIES = {device: _branch(device) for device in DEVICES}+check("amp-policy/every device branch is found",+ [d for d, body in BODIES.items() if not body], [])+ON = tuple(d for d in DEVICES if "self.amp_enabled = True" in BODIES[d])+check("amp-policy/mixed precision is not confined to CUDA", ON, DEVICES)+check("amp-policy/mps autocasts at float16", "self.dtype = torch.float16" in BODIES["mps"], True)+check("amp-policy/xpu autocasts at bfloat16", "self.dtype = torch.bfloat16" in BODIES["xpu"], True)+GATE_SRC = ast.unparse(ast.parse(textwrap.dedent(inspect.getsource(Agent._amp_enabled_for))))+check("amp-policy/mps is gated per call by row count",+ "rows < self.mps_amp_min_rows" in GATE_SRC, True)+READS = set(re.findall(r'"(LAYA_[A-Z0-9_]+)"', inspect.getsource(laya_agent)))+check("amp-policy/the runtime reads an env var per AMP knob",+ {"LAYA_MPS_AMP_MIN_ROWS", "LAYA_CUDA_AMP", "LAYA_CPU_AMP"} <= READS, True)++strings = _strings(EX35)+document = " ".join(strings)+sentences = [s for text in strings for s in re.split(r"(?<=[.!?])\s+", text) if s.strip()]++MPS = re.compile(r"\bMPS\b", re.I)+FP32 = re.compile(r"\bfp32\b|\bfloat32\b", re.I)+GATE_KEY = re.compile(r"mps_amp_min_rows|dtype_for")+OTHER_DEVICE = re.compile(r"\b(MPS|XPU|CPU)\b", re.I)+ONLY_CUDA = re.compile(r"\bonly\b[^.]{0,60}\bcuda\b|\bcuda\b[^.]{0,40}\bonly\b", re.I)+NO_BACKEND = re.compile(r"(?:torch\.autocast|autocast) has no|has no (?:MPS|Apple)[^.]{0,24}backend"+ r"|no MPS backend", re.I)+++def _lie(sentence):+ """Why this sentence would mislead a reader about the autocast policy, or None if it does not."""+ if NO_BACKEND.search(sentence):+ return "asserts the runtime has no MPS autocast backend"+ if MPS.search(sentence) and FP32.search(sentence) and not GATE_KEY.search(sentence):+ return "says an MPS call runs fp32 without naming the key that decides it"+ if ONLY_CUDA.search(sentence) and not OTHER_DEVICE.search(sentence):+ return "confines mixed precision to CUDA, naming no other device that enables it"+ return None+++flagged = ["%s (%s)" % (_lie(s), s[:80]) for s in sentences if _lie(s)]+check("35/carries no sentence that misstates the autocast policy", flagged, [])++# The two sentences the gate exists for, kept verbatim so the rules above are shown to bite: a rule+# that matches nothing would let the page go back to being wrong without anyone noticing.+for historical in (+ "Precision follows the device -- mixed precision (bf16/fp16) is a CUDA win, while CPU and MPS"+ " run **fp32**; `torch.autocast` has no MPS backend here, so Laya only wraps the forward pass"+ " on CUDA.",+ "both legs used fp32 (device.type in ('cpu','mps') -> torch.float32); mixed precision would"+ " only be enabled on CUDA."):+ check("35/the rule still catches %r" % historical[:44], _lie(historical) is not None, True)++# The mechanism has to be taught by name, and the number by source rather than by hand.+for name in ("amp_enabled", "dtype_for", "`mps_amp_min_rows`", "LAYA_MPS_AMP_MIN_ROWS"):+ check("35/names %s so a reader can grep for it" % name, name in document, True)+MPS_TARGET = re.search(r"self\.dtype = (?:torch\.(\w+)|(\w+\(\)))", BODIES["mps"])+MPS_TARGET = MPS_TARGET.group(1) or MPS_TARGET.group(2) if MPS_TARGET else ""+# The window stops at the next device's name: a page may satisfy "MPS ... <dtype>" by going on to+# describe XPU, and that is not the same claim.+MPS_CLAIM = r"\bMPS\b(?:(?!\bXPU\b|\bCPU\b|\bCUDA\b)[^.]){0,200}\b%s\b" % re.escape(MPS_TARGET)+check("35/states MPS's target as the branch sets it (%s)" % MPS_TARGET,+ re.search(MPS_CLAIM, document, re.I) is not None, True)+check("35/the MPS threshold it states is the module default",+ [int(n) for n in re.findall(r"(\d+)\s+rows", document)], [MPS_AMP_MIN_ROWS_DEFAULT])++# Read from the agent, and observed in a real forward: the page may not hand-copy the threshold, nor+# relabel `dtype` as though it were the precision a call ran in. That relabeling is how the old+# sentence got printed with a straight face -- `dtype` said float16 while the call ran fp32.+with open(os.path.join(ROOT, EX35)) as fh:+ example = ast.parse(fh.read(), filename=EX35)+attrs = {n.attr for n in ast.walk(example) if isinstance(n, ast.Attribute)}+calls = {n.func.attr for n in ast.walk(example)+ if isinstance(n, ast.Call) and isinstance(n.func, ast.Attribute)}+# Read twice, not once: the page has to report the agent's threshold in the per-leg line and drive+# the arms from it. Assigning to the attribute would not count, which is why this asks for `Load`.+threshold_reads = [n for n in ast.walk(example) if isinstance(n, ast.Attribute)+ and n.attr == "mps_amp_min_rows" and isinstance(n.ctx, ast.Load)]+check("35/reads the agent's threshold rather than hand-copying it", len(threshold_reads) >= 2, True)+check("35/asks the agent `dtype_for(rows)`", "dtype_for" in calls, True)+check("35/observes the precision inside a real forward",+ sorted({"register_forward_hook", "is_autocast_enabled"} - calls), [])++# Every LAYA_* the page presents as a knob has to be one the runtime reads. `LAYA_DEVICE` is the+# exception the page itself flags, so it is allowed only in a sentence saying Laya does not read it.+invented = sorted(name for name in set(re.findall(r"\bLAYA_[A-Z0-9_]+\b", document))+ if name not in READS and not any(name in s and re.search(r"does not read|not read", s)+ for s in sentences))+check("35/names no env var the runtime does not read, or says so", invented, [])++# ------------------------------------------------------------------ reportprint("\n%d passed, %d failed" % (len(PASS), len(FAIL)))for f in FAIL: