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(agent): add opt-in reduce-overhead compilation

cklxx354d9289a798
diff --git a/README.md b/README.md
index ba575e05..55a48f46 100644
--- a/README.md
+++ b/README.md
@@ -884,6 +884,12 @@ persistent directory to keep them across restarts. Alternatively, load with
`~/.cache/laya/torchinductor`). This opt-in sets the process-wide environment variable only if
absent; an existing value always wins. It does nothing on eager or TileLang loads.
+`compile_mode="reduce-overhead"` is a separate opt-in for CUDA graphs. With `compile=True`, it
+can reduce launch overhead on repeated shapes, but each new shape needs recording and GPU memory
+can grow. Laya copies outputs out of reusable graph buffers and serializes these CUDA forwards.
+The default mode remains unchanged. See [the engineering notes](docs/compile-and-fast-path.md)
+for measurements and limitations.
+
---
## Automated Confidence Gating
diff --git a/benchmarks/bench_compile_defaults.py b/benchmarks/bench_compile_defaults.py
index 91a79a82..946e7f6c 100644
--- a/benchmarks/bench_compile_defaults.py
+++ b/benchmarks/bench_compile_defaults.py
@@ -53,6 +53,7 @@ def main():
times = []
# Repeat the sequence to expose CUDA graph recording/replay and retained-output issues.
answers = []
+ pass_memory = []
for i, (n, k, words) in enumerate(CALLS * 3):
state = " ".join(WORDS[j % len(WORDS)] for j in range(words))
t = time.perf_counter()
@@ -65,7 +66,16 @@ def main():
else:
assert result["answers"] == answers[i % len(CALLS)], i
print("request", i + 1, "seconds", elapsed, "graphs", counters["stats"]["unique_graphs"], flush=True)
+ if (i + 1) % len(CALLS) == 0:
+ pass_memory.append({"allocated_mib": torch.cuda.memory_allocated() / 2**20,
+ "reserved_mib": torch.cuda.memory_reserved() / 2**20})
+ from torch._inductor.cudagraph_trees import get_manager
+ manager = get_manager(agent.device.index or 0, create_if_none_exists=False)
+ def count_nodes(node):
+ return 1 + sum(count_nodes(child) for children in node.children.values() for child in children)
+ graph_nodes = sum(count_nodes(node) for node in manager.get_roots()) if manager else 0
print("RESULT", json.dumps({"load_s": load_s, "requests_s": times,
+ "answers": answers, "cuda_graph_nodes": graph_nodes, "pass_memory": pass_memory,
"allocated_mib": torch.cuda.memory_allocated() / 2**20,
"reserved_mib": torch.cuda.memory_reserved() / 2**20,
"peak_allocated_mib": torch.cuda.max_memory_allocated() / 2**20,
diff --git a/benchmarks/compare_compile_defaults.py b/benchmarks/compare_compile_defaults.py
new file mode 100644
index 00000000..ae3184fd
--- /dev/null
+++ b/benchmarks/compare_compile_defaults.py
@@ -0,0 +1,33 @@
+"""Assert decoded parity across benchmark logs that include answers.
+
+python benchmarks/compare_compile_defaults.py reference.log reduced.log restart.log
+"""
+import json
+import sys
+from pathlib import Path
+
+
+def read(path):
+ return json.loads(next(line[7:] for line in Path(path).read_text().splitlines()
+ if line.startswith("RESULT ")))
+
+
+def compare(left, right):
+ if isinstance(left, dict):
+ assert left.keys() == right.keys()
+ return max((compare(left[key], right[key]) for key in left), default=0.0)
+ if isinstance(left, list):
+ assert len(left) == len(right)
+ return max((compare(a, b) for a, b in zip(left, right)), default=0.0)
+ if isinstance(left, float):
+ return abs(left - right)
+ assert left == right, (left, right)
+ return 0.0
+
+
+if __name__ == "__main__":
+ reference = read(sys.argv[1])["answers"]
+ for path in sys.argv[2:]:
+ delta = compare(reference, read(path)["answers"])
+ assert delta <= 1e-5, delta
+ print("%s: categorical parity passed; max numerical delta %.9g" % (path, delta))
diff --git a/benchmarks/results/compile-defaults/README.md b/benchmarks/results/compile-defaults/README.md
index 6e13b44b..683eed50 100644
--- a/benchmarks/results/compile-defaults/README.md
+++ b/benchmarks/results/compile-defaults/README.md
@@ -81,3 +81,108 @@ The second process hit both FX graphs, reducing load from 111.64 s to 51.29 s.
The cache location adds no meaningful GPU memory requirement; both peaks match the earlier
warm-up experiments. These final runs had no other compute jobs in their GPU snapshots.
Reboot/container persistence was not exercised; that depends on retaining the directory.
+
+## Item 3: opt-in reduce-overhead mode
+
+`compile_mode="reduce-overhead"` forwards that mode to `torch.compile`. The default remains
+`"default"`; the active compiled path rejects other values. Laya serializes its CUDA graph
+forwards, marks a new step, and clones both outputs before unlocking. Eager, CPU, and
+TileLang forwards do not enter that CUDA context. Builds lacking the step-marker API
+fail explicitly when this CUDA mode is invoked.
+
+The benchmark now reports CUDA graph node count and allocator memory after each of three
+passes, and stores decoded answers so they can be compared across modes. The default-mode
+reference reuses the verified cache. Reduced-overhead uses a separate fresh XDG directory
+for its cold run and reuses it for its restart. GPU snapshots accompany all runs.
+
+The weight-free CUDA check actually records a graph, changes input values across replays,
+retains all returned tensors, and verifies two-thread calls against eager results:
+
+```
+TORCHINDUCTOR_CACHE_DIR=/tmp/laya-compile-defaults-cuda-test-20261003 /home/ckl/projects/S/laya/.venv/bin/python tests/test_compile_cuda.py
+```
+
+Output: `CUDA graph capture, replay, retained outputs, and two-thread parity passed`.
+`mode-cuda-tests.log` records the initial check; the final check is in `mode-cuda-final.log`.
+`mode-final-validation.log` records all required lint/compile gates, the compile regression,
+API contract, router/criteria/hooks suites, and strict docs build. `mode-regressions.log`
+records exact commands and output for batch, predict_batch, runtime_fixes, load_errors,
+doc_tables, and portability checks. All exit codes are recorded.
+
+Cross-mode decoded parity is checked with:
+
+```
+/home/ckl/projects/S/laya/.venv/bin/python benchmarks/compare_compile_defaults.py benchmarks/results/compile-defaults/mode-default-reference.log benchmarks/results/compile-defaults/mode-cold.log benchmarks/results/compile-defaults/mode-restart.log
+```
+
+See `mode-parity.log` for output. Categorical outputs must match, and numerical differences
+must be at most 1e-5. The benchmark also asserts exact answer equality on repeated requests
+inside each process.
+
+| Run | Load s | Request 1 ms | Request 8 ms | Pass 2 median ms | Pass 3 median ms | Peak alloc/reserved MiB | CUDA graphs | FX hits/misses |
+|---|---:|---:|---:|---:|---:|---:|---:|---:|
+| mode-default-reference | 46.659 | 22.27 | 8.67 | 12.64 | 12.65 | 1671.0 / 1796.0 | 0 | 2 / 0 |
+| mode-cold | 99.089 | 25.73 | 14.69 | 26.94 | 12.41 | 1687.5 / 1892.0 | 10 | 0 / 2 |
+| mode-restart | 48.100 | 25.00 | 13.86 | 28.72 | 13.83 | 1669.5 / 1818.0 | 10 | 2 / 0 |
+
+Both reduced-overhead runs exactly match the default-mode decoded answers (max delta 0).
+They each record ten CUDA graphs despite having only two Dynamo graphs. The second pass pays
+recording costs; graph recordings are recreated after restart even when both FX graphs hit disk.
+On the cold run, median replay improves only from 12.65 to 12.41 ms, while recording costs
+26.94 ms median. This is not a general latency win, which is why the mode stays opt-in.
+Peak reserved memory is 1,892 MiB on the reduced cold run versus 1,820 MiB for the default
+cold run; end-of-run reserved memory is 1,842 versus 1,796 MiB. Memory depends on recording
+history; the reduced restart peaks at 1,818 MiB. Longer shape sweeps can retain more pools.
+
+## Verification limits
+
+- One English checkpoint, one GPU, torch 2.11.0+cu130, and ten concrete request shapes.
+ No multilingual checkpoint, other GPU/OS/torch versions, long-batch OOM stress, or unbounded
+ shape sweep was measured. CPU logic is covered by weight-free tests, not a CPU performance run.
+- The initial warm-up/cache attempts overlapped other GPU jobs and are identified above.
+ Final verified cache and mode GPU snapshots contain only this benchmark plus desktop processes.
+ CPU regression suites also ran during some compilation intervals. These are single trials,
+ not confidence intervals or an isolated machine throughput study.
+- Persistence was checked across fresh processes, not a reboot or container recreation.
+ Filesystem persistence remains the deployment's responsibility; PyTorch may invalidate graphs.
+- The threaded retained-output regression uses a tiny CUDA model. The real checkpoint is
+ checked serially for decoded parity. Application-owned compiled graphs/custom streams are
+ outside Laya's lock; callers must coordinate them. OOM fallback under graph-pool pressure was
+ not forced.
+- No pushes or pull requests were performed. All three changes are local conventional commits.
+
+## Changed source files versus fa9a2a7
+
+Generated benchmark/test output is kept separately in this directory so the implementation
+remains reviewable. The nine source, test, benchmark, and documentation files total +420/-25:
+
+| File | Added | Removed |
+|---|---:|---:|
+| `laya/agent.py` | 37 | 10 |
+| `laya/_compile.py` | 26 | 2 |
+| `tests/test_compile.py` | 111 | 0 |
+| `tests/test_compile_cuda.py` | 62 | 0 |
+| `tests/test_hooks_api.py` | 5 | 0 |
+| `benchmarks/bench_compile_defaults.py` | 88 | 0 |
+| `benchmarks/compare_compile_defaults.py` | 33 | 0 |
+| `README.md` | 17 | 11 |
+| `docs/compile-and-fast-path.md` | 41 | 2 |
+
+Final required validation (full output in `mode-final-validation.log`):
+
+```
+PATH=/home/ckl/projects/S/laya/.venv/bin:$PATH
+ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
+python -m compileall -q laya/ tests/
+python tests/test_compile.py
+python tests/test_hooks_api.py
+python tests/test_router.py
+python tests/test_criteria.py
+python tests/test_hooks.py
+zensical build --strict --clean
+```
+
+Outputs: `All checks passed!`; compileall silent, exit 0; all 9 compile tests pass;
+API 403 passed / 0 failed; router 703 / 0; criteria 198 / 0; hooks 240 / 0;
+docs `No issues found`, with no `griffe:` lines. Every command exits 0.
+Raw GPU logs preserve `nvidia-smi` trailing spaces; source diffs pass whitespace checks.
diff --git a/benchmarks/results/compile-defaults/mode-cold.command b/benchmarks/results/compile-defaults/mode-cold.command
new file mode 100644
index 00000000..a0277bff
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-cold.command
@@ -0,0 +1 @@
+env -u TORCHINDUCTOR_CACHE_DIR XDG_CACHE_HOME=/home/ckl/.cache/laya-compile-defaults-reduce-20261003 HF_HUB_OFFLINE=1 /home/ckl/projects/S/laya/.venv/bin/python benchmarks/bench_compile_defaults.py --model /home/ckl/.cache/huggingface/hub/models--convaiinnovations--laya/snapshots/55cf4c4ebb4ebe31b2550e8bdf3bd21b99753851 --cache --mode reduce-overhead
diff --git a/benchmarks/results/compile-defaults/mode-cold.log b/benchmarks/results/compile-defaults/mode-cold.log
new file mode 100644
index 00000000..88b38b85
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-cold.log
@@ -0,0 +1,108 @@
+Sat Oct 3 18:57:49 2026
++-----------------------------------------------------------------------------------------+
+| NVIDIA-SMI 615.71.09 KMD Version: 615.71.09 CUDA UMD Version: 13.4 |
++-----------------------------------------+------------------------+----------------------+
+| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
+| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
+| | | MIG M. |
+|=========================================+========================+======================|
+| 0 NVIDIA GeForce RTX 4070 ... On | 00000000:26:00.0 Off | N/A |
+| 0% 36C P3 21W / 295W | 728MiB / 16376MiB | 0% Default |
+| | | N/A |
++-----------------------------------------+------------------------+----------------------+
+
++-----------------------------------------------------------------------------------------+
+| Processes: |
+| GPU GI CI PID Type Process name GPU Memory |
+| ID ID Usage |
+|=========================================================================================|
+| 0 N/A N/A 892 G Hyprland 146MiB |
+| 0 N/A N/A 952 G quickshell 38MiB |
+| 0 N/A N/A 995 G Xwayland 4MiB |
+| 0 N/A N/A 1168 G monocloud 3MiB |
+| 0 N/A N/A 3324 C+G /usr/bin/sunshine 215MiB |
+| 0 N/A N/A 12883 G /usr/bin/ghostty 100MiB |
+| 0 N/A N/A 3906709 C+G ...rack-uuid=3190708988185955192 36MiB |
++-----------------------------------------------------------------------------------------+
+
+torch 2.11.0+cu130 model /home/ckl/.cache/huggingface/hub/models--convaiinnovations--laya/snapshots/55cf4c4ebb4ebe31b2550e8bdf3bd21b99753851
+/home/ckl/projects/S/laya-wt/compile-defaults/benchmarks/bench_compile_defaults.py:41: RuntimeWarning: laya: this checkpoint ships invalid temperatures or values outside [0.5, 5]; using choice:11+=0.10058280825614929 -> 0.5. Treat confidence from the affected entries as uncalibrated.
+ agent = Agent(args.model, device="cuda", compile=True, **kw)
+/home/ckl/projects/S/laya/.venv/lib/python3.12/site-packages/torch/_inductor/compile_fx.py:322: UserWarning: TensorFloat32 tensor cores for float32 matrix multiplication available but not enabled. Consider setting `torch.set_float32_matmul_precision('high')` for better performance.
+ warnings.warn(
+W1003 18:58:23.013000 4085988 torch/_inductor/utils.py:1731] [0/0_1] Not enough SMs to use max_autotune_gemm mode
+/home/ckl/projects/S/laya/.venv/lib/python3.12/site-packages/torch/_inductor/lowering.py:7836: UserWarning:
+Online softmax is disabled on the fly since Inductor decides to
+split the reduction. Cut an issue to PyTorch if this is an
+important use case and you want to speed it up with online
+softmax.
+
+ warnings.warn(
+/home/ckl/projects/S/laya/.venv/lib/python3.12/site-packages/torch/_inductor/lowering.py:7836: UserWarning:
+Online softmax is disabled on the fly since Inductor decides to
+split the reduction. Cut an issue to PyTorch if this is an
+important use case and you want to speed it up with online
+softmax.
+
+ warnings.warn(
+load_s 99.0893518710509 cache /home/ckl/.cache/laya-compile-defaults-reduce-20261003/laya/torchinductor
+request 1 seconds 0.025734404916875064 graphs 2
+request 2 seconds 0.01453058805782348 graphs 2
+request 3 seconds 0.012982798973098397 graphs 2
+request 4 seconds 0.014625189010985196 graphs 2
+request 5 seconds 0.01274273602757603 graphs 2
+request 6 seconds 0.024385808035731316 graphs 2
+request 7 seconds 0.01703020802233368 graphs 2
+request 8 seconds 0.014693628996610641 graphs 2
+W1003 18:59:28.734000 4085988 torch/_inductor/cudagraph_utils.py:343] [__cudagraphs] CUDAGraph supports dynamic shapes by recording a new graph for each distinct input size. Recording too many CUDAGraphs may lead to extra overhead. We have observed 9 distinct sizes. Please consider the following options for better performance: a) padding inputs to a few fixed number of shapes; or b) set torch._inductor.config.triton.cudagraph_skip_dynamic_graphs=True. Set torch._inductor.config.triton.cudagraph_dynamic_shape_warn_limit=None to silence this warning.
+request 9 seconds 0.030573343974538147 graphs 2
+request 10 seconds 0.015227455995045602 graphs 2
+request 11 seconds 0.023516298038884997 graphs 2
+request 12 seconds 0.025996888987720013 graphs 2
+request 13 seconds 0.023551008896902204 graphs 2
+request 14 seconds 0.0278804519912228 graphs 2
+request 15 seconds 0.022287482977844775 graphs 2
+request 16 seconds 0.03713312407489866 graphs 2
+request 17 seconds 0.029326139017939568 graphs 2
+request 18 seconds 0.020676783053204417 graphs 2
+request 19 seconds 0.0418188109761104 graphs 2
+request 20 seconds 0.028128245030529797 graphs 2
+request 21 seconds 0.011183045920915902 graphs 2
+request 22 seconds 0.011743274051696062 graphs 2
+request 23 seconds 0.00940060499124229 graphs 2
+request 24 seconds 0.013070899993181229 graphs 2
+request 25 seconds 0.008301391964778304 graphs 2
+request 26 seconds 0.022707357071340084 graphs 2
+request 27 seconds 0.015341027989052236 graphs 2
+request 28 seconds 0.006724223028868437 graphs 2
+request 29 seconds 0.027819311013445258 graphs 2
+request 30 seconds 0.013455564039759338 graphs 2
+RESULT {"load_s": 99.0893518710509, "requests_s": [0.025734404916875064, 0.01453058805782348, 0.012982798973098397, 0.014625189010985196, 0.01274273602757603, 0.024385808035731316, 0.01703020802233368, 0.014693628996610641, 0.030573343974538147, 0.015227455995045602, 0.023516298038884997, 0.025996888987720013, 0.023551008896902204, 0.0278804519912228, 0.022287482977844775, 0.03713312407489866, 0.029326139017939568, 0.020676783053204417, 0.0418188109761104, 0.028128245030529797, 0.011183045920915902, 0.011743274051696062, 0.00940060499124229, 0.013070899993181229, 0.008301391964778304, 0.022707357071340084, 0.015341027989052236, 0.006724223028868437, 0.027819311013445258, 0.013455564039759338], "answers": [{"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7064, "team1": 0.202, "team2": 0.0586, "team3": 0.033}, "confidence": 0.3887, "answer_confidence": 0.7064, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team1", "probabilities": {"team0": 0.3572, "team1": 0.5618, "team2": 0.0525, "team3": 0.0285}, "confidence": 0.3164, "answer_confidence": 0.5618, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.3673, "team1": 0.3346, "team2": 0.2558, "team3": 0.0423}, "confidence": 0.1223, "answer_confidence": 0.3673, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team3", "probabilities": {"team0": 0.2634, "team1": 0.2405, "team2": 0.1972, "team3": 0.2989}, "confidence": 0.008, "answer_confidence": 0.2989, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7116, "team1": 0.1982, "team2": 0.0593, "team3": 0.031}, "confidence": 0.3955, "answer_confidence": 0.7116, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5216, "team1": 0.3789, "team2": 0.068, "team3": 0.0316}, "confidence": 0.2793, "answer_confidence": 0.5216, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.4358, "team1": 0.3617, "team2": 0.1669, "team3": 0.0357}, "confidence": 0.1723, "answer_confidence": 0.4358, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team3", "probabilities": {"team0": 0.2222, "team1": 0.2336, "team2": 0.1975, "team3": 0.3467}, "confidence": 0.0179, "answer_confidence": 0.3467, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.706, "team1": 0.2044, "team2": 0.0578, "team3": 0.0319}, "confidence": 0.3906, "answer_confidence": 0.706, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team1", "probabilities": {"team0": 0.4021, "team1": 0.5087, "team2": 0.0583, "team3": 0.0309}, "confidence": 0.2907, "answer_confidence": 0.5087, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.3836, "team1": 0.3542, "team2": 0.221, "team3": 0.0411}, "confidence": 0.1343, "answer_confidence": 0.3836, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.8163, "team1": 0.1837}, "confidence": 0.312, "answer_confidence": 0.8163, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team1", "probabilities": {"team0": 0.3627, "team1": 0.6373}, "confidence": 0.0551, "answer_confidence": 0.6373, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6074, "team1": 0.3926}, "confidence": 0.0336, "answer_confidence": 0.6074, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5892, "team1": 0.4108}, "confidence": 0.0231, "answer_confidence": 0.5892, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6157, "team1": 0.3843}, "confidence": 0.039, "answer_confidence": 0.6157, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.8281, "team1": 0.1261, "team2": 0.0458}, "confidence": 0.4915, "answer_confidence": 0.8281, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.61, "team1": 0.3255, "team2": 0.0646}, "confidence": 0.232, "answer_confidence": 0.61, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6964, "team1": 0.156, "team2": 0.0818, "team3": 0.0382, "team4": 0.0275}, "confidence": 0.3972, "answer_confidence": 0.6964, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5545, "team1": 0.2619, "team2": 0.1073, "team3": 0.045, "team4": 0.0312}, "confidence": 0.276, "answer_confidence": 0.5545, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.4954, "team1": 0.2435, "team2": 0.1825, "team3": 0.0476, "team4": 0.0311}, "confidence": 0.2202, "answer_confidence": 0.4954, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5082, "team1": 0.2196, "team2": 0.1485, "team3": 0.0892, "team4": 0.0345}, "confidence": 0.1973, "answer_confidence": 0.5082, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team4", "probabilities": {"team0": 0.1385, "team1": 0.1616, "team2": 0.1441, "team3": 0.1157, "team4": 0.4401}, "confidence": 0.0939, "answer_confidence": 0.4401, "action": {"act_probability": 1.0}}, "q5": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5591, "team1": 0.2101, "team2": 0.124, "team3": 0.0641, "team4": 0.0426}, "confidence": 0.2405, "answer_confidence": 0.5591, "action": {"act_probability": 1.0}}, "q6": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5558, "team1": 0.2121, "team2": 0.1255, "team3": 0.0623, "team4": 0.0443}, "confidence": 0.2377, "answer_confidence": 0.5558, "action": {"act_probability": 1.0}}, "q7": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5773, "team1": 0.2057, "team2": 0.1185, "team3": 0.059, "team4": 0.0395}, "confidence": 0.2607, "answer_confidence": 0.5773, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.8306, "team1": 0.1225, "team2": 0.035, "team3": 0.0068, "team4": 0.0031, "team5": 0.002}, "confidence": 0.6689, "answer_confidence": 0.8306, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5518, "team1": 0.3974, "team2": 0.0394, "team3": 0.0063, "team4": 0.003, "team5": 0.002}, "confidence": 0.5065, "answer_confidence": 0.5518, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.4753, "team1": 0.2883, "team2": 0.221, "team3": 0.0093, "team4": 0.0037, "team5": 0.0023}, "confidence": 0.3725, "answer_confidence": 0.4753, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5962, "team1": 0.2447, "team2": 0.1065, "team3": 0.0452, "team4": 0.0048, "team5": 0.0026}, "confidence": 0.4013, "answer_confidence": 0.5962, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team4", "probabilities": {"team0": 0.1874, "team1": 0.1468, "team2": 0.103, "team3": 0.0381, "team4": 0.5195, "team5": 0.0052}, "confidence": 0.2624, "answer_confidence": 0.5195, "action": {"act_probability": 1.0}}, "q5": {"type": "choice", "choice": "team5", "probabilities": {"team0": 0.018, "team1": 0.0253, "team2": 0.0233, "team3": 0.0073, "team4": 0.0046, "team5": 0.9215}, "confidence": 0.7829, "answer_confidence": 0.9215, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7733, "team1": 0.1752, "team2": 0.0516}, "confidence": 0.4021, "answer_confidence": 0.7733, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7494, "team1": 0.1764, "team2": 0.0742}, "confidence": 0.3489, "answer_confidence": 0.7494, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5272, "team1": 0.368, "team2": 0.1048}, "confidence": 0.1428, "answer_confidence": 0.5272, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.439, "team1": 0.3469, "team2": 0.2141}, "confidence": 0.0363, "answer_confidence": 0.439, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.586, "team1": 0.2731, "team2": 0.141}, "confidence": 0.1409, "answer_confidence": 0.586, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5872, "team1": 0.2668, "team2": 0.146}, "confidence": 0.1388, "answer_confidence": 0.5872, "action": {"act_probability": 1.0}}, "q5": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6022, "team1": 0.2635, "team2": 0.1343}, "confidence": 0.1567, "answer_confidence": 0.6022, "action": {"act_probability": 1.0}}, "q6": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.613, "team1": 0.2585, "team2": 0.1285}, "confidence": 0.1686, "answer_confidence": 0.613, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6977, "team1": 0.1946, "team2": 0.067, "team3": 0.0407}, "confidence": 0.3644, "answer_confidence": 0.6977, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5732, "team1": 0.2913, "team2": 0.0869, "team3": 0.0486}, "confidence": 0.2516, "answer_confidence": 0.5732, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5202, "team1": 0.2908, "team2": 0.1395, "team3": 0.0495}, "confidence": 0.1901, "answer_confidence": 0.5202, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.2906, "team1": 0.2515, "team2": 0.1768, "team3": 0.2811}, "confidence": 0.0122, "answer_confidence": 0.2906, "action": {"act_probability": 1.0}}}], "cuda_graph_nodes": 10, "pass_memory": [{"allocated_mib": 1607.35888671875, "reserved_mib": 1892.0}, {"allocated_mib": 1607.35888671875, "reserved_mib": 1842.0}, {"allocated_mib": 1607.35888671875, "reserved_mib": 1842.0}], "allocated_mib": 1607.35888671875, "reserved_mib": 1842.0, "peak_allocated_mib": 1687.5029296875, "peak_reserved_mib": 1892.0, "counters": {"stats": {"calls_captured": 6700, "unique_graphs": 2}, "inductor": {"pattern_matcher_count": 786, "pattern_matcher_nodes": 910, "benchmarking.InductorBenchmarker.benchmark_gpu": 112, "pad_mm_bench": 4, "fxgraph_cache_miss": 2, "cudagraph_partitions": 4, "extern_calls": 386, "async_compile_cache_miss": 166, "async_compile_cache_hit": 83, "triton_bundler_save_kernel": 1104, "triton_bundler_save_static_autotuner": 2, "benchmarking.InductorBenchmarker.benchmark": 104, "cudagraph_recorded_non_static_inputs": 89}}}
+Sat Oct 3 18:59:29 2026
++-----------------------------------------------------------------------------------------+
+| NVIDIA-SMI 615.71.09 KMD Version: 615.71.09 CUDA UMD Version: 13.4 |
++-----------------------------------------+------------------------+----------------------+
+| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
+| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
+| | | MIG M. |
+|=========================================+========================+======================|
+| 0 NVIDIA GeForce RTX 4070 ... On | 00000000:26:00.0 Off | N/A |
+| 0% 48C P2 146W / 295W | 2897MiB / 16376MiB | 82% Default |
+| | | N/A |
++-----------------------------------------+------------------------+----------------------+
+
++-----------------------------------------------------------------------------------------+
+| Processes: |
+| GPU GI CI PID Type Process name GPU Memory |
+| ID ID Usage |
+|=========================================================================================|
+| 0 N/A N/A 892 G Hyprland 146MiB |
+| 0 N/A N/A 952 G quickshell 38MiB |
+| 0 N/A N/A 995 G Xwayland 4MiB |
+| 0 N/A N/A 1168 G monocloud 3MiB |
+| 0 N/A N/A 3324 C+G /usr/bin/sunshine 215MiB |
+| 0 N/A N/A 12883 G /usr/bin/ghostty 100MiB |
+| 0 N/A N/A 3906709 C+G ...rack-uuid=3190708988185955192 36MiB |
+| 0 N/A N/A 4085988 C ...jects/S/laya/.venv/bin/python 2164MiB |
++-----------------------------------------------------------------------------------------+
+
diff --git a/benchmarks/results/compile-defaults/mode-cuda-final.log b/benchmarks/results/compile-defaults/mode-cuda-final.log
new file mode 100644
index 00000000..b5740d42
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-cuda-final.log
@@ -0,0 +1 @@
+CUDA graph capture, replay, retained outputs, and two-thread parity passed
diff --git a/benchmarks/results/compile-defaults/mode-cuda-tests.log b/benchmarks/results/compile-defaults/mode-cuda-tests.log
new file mode 100644
index 00000000..b5740d42
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-cuda-tests.log
@@ -0,0 +1 @@
+CUDA graph capture, replay, retained outputs, and two-thread parity passed
diff --git a/benchmarks/results/compile-defaults/mode-default-reference.command b/benchmarks/results/compile-defaults/mode-default-reference.command
new file mode 100644
index 00000000..2d578752
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-default-reference.command
@@ -0,0 +1 @@
+env -u TORCHINDUCTOR_CACHE_DIR XDG_CACHE_HOME=/home/ckl/.cache/laya-compile-defaults-verified-20261003 HF_HUB_OFFLINE=1 /home/ckl/projects/S/laya/.venv/bin/python benchmarks/bench_compile_defaults.py --model /home/ckl/.cache/huggingface/hub/models--convaiinnovations--laya/snapshots/55cf4c4ebb4ebe31b2550e8bdf3bd21b99753851 --cache
diff --git a/benchmarks/results/compile-defaults/mode-default-reference.log b/benchmarks/results/compile-defaults/mode-default-reference.log
new file mode 100644
index 00000000..ea15b83d
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-default-reference.log
@@ -0,0 +1,90 @@
+Sat Oct 3 18:56:59 2026
++-----------------------------------------------------------------------------------------+
+| NVIDIA-SMI 615.71.09 KMD Version: 615.71.09 CUDA UMD Version: 13.4 |
++-----------------------------------------+------------------------+----------------------+
+| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
+| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
+| | | MIG M. |
+|=========================================+========================+======================|
+| 0 NVIDIA GeForce RTX 4070 ... On | 00000000:26:00.0 Off | N/A |
+| 0% 36C P8 11W / 295W | 734MiB / 16376MiB | 28% Default |
+| | | N/A |
++-----------------------------------------+------------------------+----------------------+
+
++-----------------------------------------------------------------------------------------+
+| Processes: |
+| GPU GI CI PID Type Process name GPU Memory |
+| ID ID Usage |
+|=========================================================================================|
+| 0 N/A N/A 892 G Hyprland 156MiB |
+| 0 N/A N/A 952 G quickshell 38MiB |
+| 0 N/A N/A 995 G Xwayland 4MiB |
+| 0 N/A N/A 1168 G monocloud 3MiB |
+| 0 N/A N/A 3324 C+G /usr/bin/sunshine 215MiB |
+| 0 N/A N/A 12883 G /usr/bin/ghostty 100MiB |
+| 0 N/A N/A 3906709 C+G ...rack-uuid=3190708988185955192 31MiB |
++-----------------------------------------------------------------------------------------+
+
+torch 2.11.0+cu130 model /home/ckl/.cache/huggingface/hub/models--convaiinnovations--laya/snapshots/55cf4c4ebb4ebe31b2550e8bdf3bd21b99753851
+/home/ckl/projects/S/laya-wt/compile-defaults/benchmarks/bench_compile_defaults.py:41: RuntimeWarning: laya: this checkpoint ships invalid temperatures or values outside [0.5, 5]; using choice:11+=0.10058280825614929 -> 0.5. Treat confidence from the affected entries as uncalibrated.
+ agent = Agent(args.model, device="cuda", compile=True, **kw)
+load_s 46.659154976019636 cache /home/ckl/.cache/laya-compile-defaults-verified-20261003/laya/torchinductor
+request 1 seconds 0.02227416110690683 graphs 2
+request 2 seconds 0.013271530973725021 graphs 2
+request 3 seconds 0.009847079985775054 graphs 2
+request 4 seconds 0.01395110902376473 graphs 2
+request 5 seconds 0.009092980995774269 graphs 2
+request 6 seconds 0.024529938003979623 graphs 2
+request 7 seconds 0.016685683047398925 graphs 2
+request 8 seconds 0.008671556017361581 graphs 2
+request 9 seconds 0.029906653915531933 graphs 2
+request 10 seconds 0.014891040977090597 graphs 2
+request 11 seconds 0.009502526023425162 graphs 2
+request 12 seconds 0.01175652293022722 graphs 2
+request 13 seconds 0.00937346403952688 graphs 2
+request 14 seconds 0.013521144981496036 graphs 2
+request 15 seconds 0.008561403956264257 graphs 2
+request 16 seconds 0.022972889011725783 graphs 2
+request 17 seconds 0.01577568193897605 graphs 2
+request 18 seconds 0.008254111045971513 graphs 2
+request 19 seconds 0.028295972966589034 graphs 2
+request 20 seconds 0.014113781973719597 graphs 2
+request 21 seconds 0.00948852498549968 graphs 2
+request 22 seconds 0.011776432977057993 graphs 2
+request 23 seconds 0.009372873930260539 graphs 2
+request 24 seconds 0.013515894999727607 graphs 2
+request 25 seconds 0.008511213934980333 graphs 2
+request 26 seconds 0.02297718997579068 graphs 2
+request 27 seconds 0.015807762974873185 graphs 2
+request 28 seconds 0.008288851007819176 graphs 2
+request 29 seconds 0.02831886406056583 graphs 2
+request 30 seconds 0.01407532102894038 graphs 2
+RESULT {"load_s": 46.659154976019636, "requests_s": [0.02227416110690683, 0.013271530973725021, 0.009847079985775054, 0.01395110902376473, 0.009092980995774269, 0.024529938003979623, 0.016685683047398925, 0.008671556017361581, 0.029906653915531933, 0.014891040977090597, 0.009502526023425162, 0.01175652293022722, 0.00937346403952688, 0.013521144981496036, 0.008561403956264257, 0.022972889011725783, 0.01577568193897605, 0.008254111045971513, 0.028295972966589034, 0.014113781973719597, 0.00948852498549968, 0.011776432977057993, 0.009372873930260539, 0.013515894999727607, 0.008511213934980333, 0.02297718997579068, 0.015807762974873185, 0.008288851007819176, 0.02831886406056583, 0.01407532102894038], "answers": [{"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7064, "team1": 0.202, "team2": 0.0586, "team3": 0.033}, "confidence": 0.3887, "answer_confidence": 0.7064, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team1", "probabilities": {"team0": 0.3572, "team1": 0.5618, "team2": 0.0525, "team3": 0.0285}, "confidence": 0.3164, "answer_confidence": 0.5618, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.3673, "team1": 0.3346, "team2": 0.2558, "team3": 0.0423}, "confidence": 0.1223, "answer_confidence": 0.3673, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team3", "probabilities": {"team0": 0.2634, "team1": 0.2405, "team2": 0.1972, "team3": 0.2989}, "confidence": 0.008, "answer_confidence": 0.2989, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7116, "team1": 0.1982, "team2": 0.0593, "team3": 0.031}, "confidence": 0.3955, "answer_confidence": 0.7116, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5216, "team1": 0.3789, "team2": 0.068, "team3": 0.0316}, "confidence": 0.2793, "answer_confidence": 0.5216, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.4358, "team1": 0.3617, "team2": 0.1669, "team3": 0.0357}, "confidence": 0.1723, "answer_confidence": 0.4358, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team3", "probabilities": {"team0": 0.2222, "team1": 0.2336, "team2": 0.1975, "team3": 0.3467}, "confidence": 0.0179, "answer_confidence": 0.3467, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.706, "team1": 0.2044, "team2": 0.0578, "team3": 0.0319}, "confidence": 0.3906, "answer_confidence": 0.706, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team1", "probabilities": {"team0": 0.4021, "team1": 0.5087, "team2": 0.0583, "team3": 0.0309}, "confidence": 0.2907, "answer_confidence": 0.5087, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.3836, "team1": 0.3542, "team2": 0.221, "team3": 0.0411}, "confidence": 0.1343, "answer_confidence": 0.3836, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.8163, "team1": 0.1837}, "confidence": 0.312, "answer_confidence": 0.8163, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team1", "probabilities": {"team0": 0.3627, "team1": 0.6373}, "confidence": 0.0551, "answer_confidence": 0.6373, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6074, "team1": 0.3926}, "confidence": 0.0336, "answer_confidence": 0.6074, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5892, "team1": 0.4108}, "confidence": 0.0231, "answer_confidence": 0.5892, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6157, "team1": 0.3843}, "confidence": 0.039, "answer_confidence": 0.6157, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.8281, "team1": 0.1261, "team2": 0.0458}, "confidence": 0.4915, "answer_confidence": 0.8281, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.61, "team1": 0.3255, "team2": 0.0646}, "confidence": 0.232, "answer_confidence": 0.61, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6964, "team1": 0.156, "team2": 0.0818, "team3": 0.0382, "team4": 0.0275}, "confidence": 0.3972, "answer_confidence": 0.6964, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5545, "team1": 0.2619, "team2": 0.1073, "team3": 0.045, "team4": 0.0312}, "confidence": 0.276, "answer_confidence": 0.5545, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.4954, "team1": 0.2435, "team2": 0.1825, "team3": 0.0476, "team4": 0.0311}, "confidence": 0.2202, "answer_confidence": 0.4954, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5082, "team1": 0.2196, "team2": 0.1485, "team3": 0.0892, "team4": 0.0345}, "confidence": 0.1973, "answer_confidence": 0.5082, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team4", "probabilities": {"team0": 0.1385, "team1": 0.1616, "team2": 0.1441, "team3": 0.1157, "team4": 0.4401}, "confidence": 0.0939, "answer_confidence": 0.4401, "action": {"act_probability": 1.0}}, "q5": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5591, "team1": 0.2101, "team2": 0.124, "team3": 0.0641, "team4": 0.0426}, "confidence": 0.2405, "answer_confidence": 0.5591, "action": {"act_probability": 1.0}}, "q6": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5558, "team1": 0.2121, "team2": 0.1255, "team3": 0.0623, "team4": 0.0443}, "confidence": 0.2377, "answer_confidence": 0.5558, "action": {"act_probability": 1.0}}, "q7": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5773, "team1": 0.2057, "team2": 0.1185, "team3": 0.059, "team4": 0.0395}, "confidence": 0.2607, "answer_confidence": 0.5773, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.8306, "team1": 0.1225, "team2": 0.035, "team3": 0.0068, "team4": 0.0031, "team5": 0.002}, "confidence": 0.6689, "answer_confidence": 0.8306, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5518, "team1": 0.3974, "team2": 0.0394, "team3": 0.0063, "team4": 0.003, "team5": 0.002}, "confidence": 0.5065, "answer_confidence": 0.5518, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.4753, "team1": 0.2883, "team2": 0.221, "team3": 0.0093, "team4": 0.0037, "team5": 0.0023}, "confidence": 0.3725, "answer_confidence": 0.4753, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5962, "team1": 0.2447, "team2": 0.1065, "team3": 0.0452, "team4": 0.0048, "team5": 0.0026}, "confidence": 0.4013, "answer_confidence": 0.5962, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team4", "probabilities": {"team0": 0.1874, "team1": 0.1468, "team2": 0.103, "team3": 0.0381, "team4": 0.5195, "team5": 0.0052}, "confidence": 0.2624, "answer_confidence": 0.5195, "action": {"act_probability": 1.0}}, "q5": {"type": "choice", "choice": "team5", "probabilities": {"team0": 0.018, "team1": 0.0253, "team2": 0.0233, "team3": 0.0073, "team4": 0.0046, "team5": 0.9215}, "confidence": 0.7829, "answer_confidence": 0.9215, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7733, "team1": 0.1752, "team2": 0.0516}, "confidence": 0.4021, "answer_confidence": 0.7733, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7494, "team1": 0.1764, "team2": 0.0742}, "confidence": 0.3489, "answer_confidence": 0.7494, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5272, "team1": 0.368, "team2": 0.1048}, "confidence": 0.1428, "answer_confidence": 0.5272, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.439, "team1": 0.3469, "team2": 0.2141}, "confidence": 0.0363, "answer_confidence": 0.439, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.586, "team1": 0.2731, "team2": 0.141}, "confidence": 0.1409, "answer_confidence": 0.586, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5872, "team1": 0.2668, "team2": 0.146}, "confidence": 0.1388, "answer_confidence": 0.5872, "action": {"act_probability": 1.0}}, "q5": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6022, "team1": 0.2635, "team2": 0.1343}, "confidence": 0.1567, "answer_confidence": 0.6022, "action": {"act_probability": 1.0}}, "q6": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.613, "team1": 0.2585, "team2": 0.1285}, "confidence": 0.1686, "answer_confidence": 0.613, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6977, "team1": 0.1946, "team2": 0.067, "team3": 0.0407}, "confidence": 0.3644, "answer_confidence": 0.6977, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5732, "team1": 0.2913, "team2": 0.0869, "team3": 0.0486}, "confidence": 0.2516, "answer_confidence": 0.5732, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5202, "team1": 0.2908, "team2": 0.1395, "team3": 0.0495}, "confidence": 0.1901, "answer_confidence": 0.5202, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.2906, "team1": 0.2515, "team2": 0.1768, "team3": 0.2811}, "confidence": 0.0122, "answer_confidence": 0.2906, "action": {"act_probability": 1.0}}}], "cuda_graph_nodes": 0, "pass_memory": [{"allocated_mib": 1616.48291015625, "reserved_mib": 1796.0}, {"allocated_mib": 1616.48291015625, "reserved_mib": 1796.0}, {"allocated_mib": 1616.48291015625, "reserved_mib": 1796.0}], "allocated_mib": 1616.48291015625, "reserved_mib": 1796.0, "peak_allocated_mib": 1671.017578125, "peak_reserved_mib": 1796.0, "counters": {"stats": {"calls_captured": 6700, "unique_graphs": 2}, "inductor": {"triton_bundler_load_static_autotuner": 68, "async_compile_cache_miss": 243, "async_compile_cache_hit": 149, "pattern_matcher_count": 20, "pattern_matcher_nodes": 26, "extern_calls": 384, "fxgraph_cache_hit": 2}}}
+Sat Oct 3 18:57:46 2026
++-----------------------------------------------------------------------------------------+
+| NVIDIA-SMI 615.71.09 KMD Version: 615.71.09 CUDA UMD Version: 13.4 |
++-----------------------------------------+------------------------+----------------------+
+| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
+| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
+| | | MIG M. |
+|=========================================+========================+======================|
+| 0 NVIDIA GeForce RTX 4070 ... On | 00000000:26:00.0 Off | N/A |
+| 0% 46C P2 79W / 295W | 2789MiB / 16376MiB | 82% Default |
+| | | N/A |
++-----------------------------------------+------------------------+----------------------+
+
++-----------------------------------------------------------------------------------------+
+| Processes: |
+| GPU GI CI PID Type Process name GPU Memory |
+| ID ID Usage |
+|=========================================================================================|
+| 0 N/A N/A 892 G Hyprland 146MiB |
+| 0 N/A N/A 952 G quickshell 38MiB |
+| 0 N/A N/A 995 G Xwayland 4MiB |
+| 0 N/A N/A 1168 G monocloud 3MiB |
+| 0 N/A N/A 3324 C+G /usr/bin/sunshine 215MiB |
+| 0 N/A N/A 12883 G /usr/bin/ghostty 100MiB |
+| 0 N/A N/A 3906709 C+G ...rack-uuid=3190708988185955192 36MiB |
+| 0 N/A N/A 4085393 C ...jects/S/laya/.venv/bin/python 2056MiB |
++-----------------------------------------------------------------------------------------+
+
diff --git a/benchmarks/results/compile-defaults/mode-final-validation.log b/benchmarks/results/compile-defaults/mode-final-validation.log
new file mode 100644
index 00000000..ce298bbf
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-final-validation.log
@@ -0,0 +1,58 @@
+PATH=/home/ckl/projects/S/laya/.venv/bin:$PATH
+
+$ ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
+All checks passed!
+
+exit=0
+
+$ python -m compileall -q laya/ tests/
+
+exit=0
+
+$ python tests/test_compile.py
+ok test_cuda_compile_padding_masks_only_the_new_tail
+ok test_one_graph_across_shapes_and_same_outputs
+ok test_warmup_builds_every_graph_before_the_first_request
+ok test_duck_shape_restored_after_errors_and_nesting
+ok test_duck_shape_restored_across_threads
+ok test_eager_agent_leaves_the_setting_alone
+ok test_constructor_warms_only_the_active_compiled_path
+ok test_persistent_cache_is_opt_in_and_respects_the_environment
+ok test_cuda_graph_step_marks_each_forward_and_releases_after_failure
+all compile tests passed
+
+exit=0
+
+$ python tests/test_hooks_api.py
+
+403 passed, 0 failed
+all hook API tests passed
+
+exit=0
+
+$ python tests/test_router.py
+
+703 passed, 0 failed
+all routing tests passed
+
+exit=0
+
+$ python tests/test_criteria.py
+
+198 passed, 0 failed
+
+exit=0
+
+$ python tests/test_hooks.py
+
+240 passed, 0 failed
+all hook tests passed
+
+exit=0
+
+$ zensical build --strict --clean
+Build started
+No issues found
+Build finished in 2.01s
+
+exit=0
diff --git a/benchmarks/results/compile-defaults/mode-parity.log b/benchmarks/results/compile-defaults/mode-parity.log
new file mode 100644
index 00000000..2c51f19c
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-parity.log
@@ -0,0 +1,2 @@
+benchmarks/results/compile-defaults/mode-cold.log: categorical parity passed; max numerical delta 0
+benchmarks/results/compile-defaults/mode-restart.log: categorical parity passed; max numerical delta 0
diff --git a/benchmarks/results/compile-defaults/mode-regressions.log b/benchmarks/results/compile-defaults/mode-regressions.log
new file mode 100644
index 00000000..193b043e
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-regressions.log
@@ -0,0 +1,56 @@
+
+$ /home/ckl/projects/S/laya/.venv/bin/python tests/test_batch.py
+
+78 passed, 0 failed
+
+exit=0
+
+$ /home/ckl/projects/S/laya/.venv/bin/python tests/test_predict_batch.py
+.......
+----------------------------------------------------------------------
+Ran 7 tests in 1.995s
+
+OK
+
+exit=0
+
+$ /home/ckl/projects/S/laya/.venv/bin/python tests/test_runtime_fixes.py
+/home/ckl/projects/S/laya-wt/compile-defaults/laya/agent.py:1198: RuntimeWarning: Warning: autocast failed 3 times in a row. Disabling mixed precision.
+ warnings.warn("Warning: autocast failed %d times in a row. "
+/home/ckl/projects/S/laya-wt/compile-defaults/laya/agent.py:1153: RuntimeWarning: Warning: GPU memory exceeded during inference. Retrying this request on CPU...
+ warnings.warn("Warning: GPU memory exceeded during inference. "
+
+86 passed, 0 failed
+all runtime-fix tests passed
+
+exit=0
+
+$ /home/ckl/projects/S/laya/.venv/bin/python tests/test_load_errors.py
+
+52 passed, 0 failed
+
+exit=0
+
+$ /home/ckl/projects/S/laya/.venv/bin/python tests/test_doc_tables.py
+
+114 passed, 0 failed
+every cell of the parity table matches its committed JSON
+not checkable from this repository (not asserted either way):
+ - README.md typed-decisions rows: no committed artifact for the fine-tuned checkpoint
+ - README.md / BENCHMARKS.md 103-332 q/s throughput: hand-picked from a range spanning 55-553
+ - BENCHMARKS.md fast-path latency table: the committed fast_*.json have no 'eval' key
+ - README.md CPU/T4 reload latencies: research/latency_benchmark_results.json is absent
+
+exit=0
+
+$ /home/ckl/projects/S/laya/.venv/bin/python tests/test_portability.py
+/home/ckl/projects/S/laya-wt/compile-defaults/laya/agent.py:1153: RuntimeWarning: Warning: GPU memory exceeded during inference. Retrying this request on CPU...
+ warnings.warn("Warning: GPU memory exceeded during inference. "
+/home/ckl/projects/S/laya-wt/compile-defaults/laya/agent.py:867: RuntimeWarning: Warning: could not move the model back to mps after the CPU retry (CUDA out of memory: model no longer fits after the retry); staying on CPU.
+ warnings.warn("Warning: could not move the model back to %s after the CPU retry (%s); "
+/home/ckl/projects/S/laya-wt/compile-defaults/laya/agent.py:1198: RuntimeWarning: Warning: autocast failed 3 times in a row. Disabling mixed precision.
+ warnings.warn("Warning: autocast failed %d times in a row. "
+
+35 passed, 0 failed
+
+exit=0
diff --git a/benchmarks/results/compile-defaults/mode-restart.command b/benchmarks/results/compile-defaults/mode-restart.command
new file mode 100644
index 00000000..a0277bff
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-restart.command
@@ -0,0 +1 @@
+env -u TORCHINDUCTOR_CACHE_DIR XDG_CACHE_HOME=/home/ckl/.cache/laya-compile-defaults-reduce-20261003 HF_HUB_OFFLINE=1 /home/ckl/projects/S/laya/.venv/bin/python benchmarks/bench_compile_defaults.py --model /home/ckl/.cache/huggingface/hub/models--convaiinnovations--laya/snapshots/55cf4c4ebb4ebe31b2550e8bdf3bd21b99753851 --cache --mode reduce-overhead
diff --git a/benchmarks/results/compile-defaults/mode-restart.log b/benchmarks/results/compile-defaults/mode-restart.log
new file mode 100644
index 00000000..5b0a9771
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-restart.log
@@ -0,0 +1,91 @@
+Sat Oct 3 18:59:33 2026
++-----------------------------------------------------------------------------------------+
+| NVIDIA-SMI 615.71.09 KMD Version: 615.71.09 CUDA UMD Version: 13.4 |
++-----------------------------------------+------------------------+----------------------+
+| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
+| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
+| | | MIG M. |
+|=========================================+========================+======================|
+| 0 NVIDIA GeForce RTX 4070 ... On | 00000000:26:00.0 Off | N/A |
+| 0% 36C P3 20W / 295W | 756MiB / 16376MiB | 11% Default |
+| | | N/A |
++-----------------------------------------+------------------------+----------------------+
+
++-----------------------------------------------------------------------------------------+
+| Processes: |
+| GPU GI CI PID Type Process name GPU Memory |
+| ID ID Usage |
+|=========================================================================================|
+| 0 N/A N/A 892 G Hyprland 156MiB |
+| 0 N/A N/A 952 G quickshell 38MiB |
+| 0 N/A N/A 995 G Xwayland 4MiB |
+| 0 N/A N/A 1168 G monocloud 3MiB |
+| 0 N/A N/A 3324 C+G /usr/bin/sunshine 215MiB |
+| 0 N/A N/A 12883 G /usr/bin/ghostty 100MiB |
+| 0 N/A N/A 3906709 C+G ...rack-uuid=3190708988185955192 53MiB |
++-----------------------------------------------------------------------------------------+
+
+torch 2.11.0+cu130 model /home/ckl/.cache/huggingface/hub/models--convaiinnovations--laya/snapshots/55cf4c4ebb4ebe31b2550e8bdf3bd21b99753851
+/home/ckl/projects/S/laya-wt/compile-defaults/benchmarks/bench_compile_defaults.py:41: RuntimeWarning: laya: this checkpoint ships invalid temperatures or values outside [0.5, 5]; using choice:11+=0.10058280825614929 -> 0.5. Treat confidence from the affected entries as uncalibrated.
+ agent = Agent(args.model, device="cuda", compile=True, **kw)
+load_s 48.09955651999917 cache /home/ckl/.cache/laya-compile-defaults-reduce-20261003/laya/torchinductor
+request 1 seconds 0.024998015956953168 graphs 2
+request 2 seconds 0.015785163966938853 graphs 2
+request 3 seconds 0.013096580049023032 graphs 2
+request 4 seconds 0.017309981980361044 graphs 2
+request 5 seconds 0.013071800000034273 graphs 2
+request 6 seconds 0.02874909294769168 graphs 2
+request 7 seconds 0.01985401299316436 graphs 2
+request 8 seconds 0.013861170038580894 graphs 2
+W1003 19:00:21.654000 4087163 torch/_inductor/cudagraph_utils.py:343] [__cudagraphs] CUDAGraph supports dynamic shapes by recording a new graph for each distinct input size. Recording too many CUDAGraphs may lead to extra overhead. We have observed 9 distinct sizes. Please consider the following options for better performance: a) padding inputs to a few fixed number of shapes; or b) set torch._inductor.config.triton.cudagraph_skip_dynamic_graphs=True. Set torch._inductor.config.triton.cudagraph_dynamic_shape_warn_limit=None to silence this warning.
+request 9 seconds 0.03384523501154035 graphs 2
+request 10 seconds 0.01667958404868841 graphs 2
+request 11 seconds 0.02393182402011007 graphs 2
+request 12 seconds 0.02793458302039653 graphs 2
+request 13 seconds 0.023818362038582563 graphs 2
+request 14 seconds 0.030347452964633703 graphs 2
+request 15 seconds 0.022572787012904882 graphs 2
+request 16 seconds 0.04100456403102726 graphs 2
+request 17 seconds 0.030690836952999234 graphs 2
+request 18 seconds 0.021115539013408124 graphs 2
+request 19 seconds 0.04513622401282191 graphs 2
+request 20 seconds 0.029510962078347802 graphs 2
+request 21 seconds 0.011164887109771371 graphs 2
+request 22 seconds 0.013740508002229035 graphs 2
+request 23 seconds 0.010554309003055096 graphs 2
+request 24 seconds 0.013917091069743037 graphs 2
+request 25 seconds 0.00853716500569135 graphs 2
+request 26 seconds 0.02545068203471601 graphs 2
+request 27 seconds 0.016663685091771185 graphs 2
+request 28 seconds 0.006403109058737755 graphs 2
+request 29 seconds 0.03161943797022104 graphs 2
+request 30 seconds 0.015088704996742308 graphs 2
+RESULT {"load_s": 48.09955651999917, "requests_s": [0.024998015956953168, 0.015785163966938853, 0.013096580049023032, 0.017309981980361044, 0.013071800000034273, 0.02874909294769168, 0.01985401299316436, 0.013861170038580894, 0.03384523501154035, 0.01667958404868841, 0.02393182402011007, 0.02793458302039653, 0.023818362038582563, 0.030347452964633703, 0.022572787012904882, 0.04100456403102726, 0.030690836952999234, 0.021115539013408124, 0.04513622401282191, 0.029510962078347802, 0.011164887109771371, 0.013740508002229035, 0.010554309003055096, 0.013917091069743037, 0.00853716500569135, 0.02545068203471601, 0.016663685091771185, 0.006403109058737755, 0.03161943797022104, 0.015088704996742308], "answers": [{"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7064, "team1": 0.202, "team2": 0.0586, "team3": 0.033}, "confidence": 0.3887, "answer_confidence": 0.7064, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team1", "probabilities": {"team0": 0.3572, "team1": 0.5618, "team2": 0.0525, "team3": 0.0285}, "confidence": 0.3164, "answer_confidence": 0.5618, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.3673, "team1": 0.3346, "team2": 0.2558, "team3": 0.0423}, "confidence": 0.1223, "answer_confidence": 0.3673, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team3", "probabilities": {"team0": 0.2634, "team1": 0.2405, "team2": 0.1972, "team3": 0.2989}, "confidence": 0.008, "answer_confidence": 0.2989, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7116, "team1": 0.1982, "team2": 0.0593, "team3": 0.031}, "confidence": 0.3955, "answer_confidence": 0.7116, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5216, "team1": 0.3789, "team2": 0.068, "team3": 0.0316}, "confidence": 0.2793, "answer_confidence": 0.5216, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.4358, "team1": 0.3617, "team2": 0.1669, "team3": 0.0357}, "confidence": 0.1723, "answer_confidence": 0.4358, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team3", "probabilities": {"team0": 0.2222, "team1": 0.2336, "team2": 0.1975, "team3": 0.3467}, "confidence": 0.0179, "answer_confidence": 0.3467, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.706, "team1": 0.2044, "team2": 0.0578, "team3": 0.0319}, "confidence": 0.3906, "answer_confidence": 0.706, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team1", "probabilities": {"team0": 0.4021, "team1": 0.5087, "team2": 0.0583, "team3": 0.0309}, "confidence": 0.2907, "answer_confidence": 0.5087, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.3836, "team1": 0.3542, "team2": 0.221, "team3": 0.0411}, "confidence": 0.1343, "answer_confidence": 0.3836, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.8163, "team1": 0.1837}, "confidence": 0.312, "answer_confidence": 0.8163, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team1", "probabilities": {"team0": 0.3627, "team1": 0.6373}, "confidence": 0.0551, "answer_confidence": 0.6373, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6074, "team1": 0.3926}, "confidence": 0.0336, "answer_confidence": 0.6074, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5892, "team1": 0.4108}, "confidence": 0.0231, "answer_confidence": 0.5892, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6157, "team1": 0.3843}, "confidence": 0.039, "answer_confidence": 0.6157, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.8281, "team1": 0.1261, "team2": 0.0458}, "confidence": 0.4915, "answer_confidence": 0.8281, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.61, "team1": 0.3255, "team2": 0.0646}, "confidence": 0.232, "answer_confidence": 0.61, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6964, "team1": 0.156, "team2": 0.0818, "team3": 0.0382, "team4": 0.0275}, "confidence": 0.3972, "answer_confidence": 0.6964, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5545, "team1": 0.2619, "team2": 0.1073, "team3": 0.045, "team4": 0.0312}, "confidence": 0.276, "answer_confidence": 0.5545, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.4954, "team1": 0.2435, "team2": 0.1825, "team3": 0.0476, "team4": 0.0311}, "confidence": 0.2202, "answer_confidence": 0.4954, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5082, "team1": 0.2196, "team2": 0.1485, "team3": 0.0892, "team4": 0.0345}, "confidence": 0.1973, "answer_confidence": 0.5082, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team4", "probabilities": {"team0": 0.1385, "team1": 0.1616, "team2": 0.1441, "team3": 0.1157, "team4": 0.4401}, "confidence": 0.0939, "answer_confidence": 0.4401, "action": {"act_probability": 1.0}}, "q5": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5591, "team1": 0.2101, "team2": 0.124, "team3": 0.0641, "team4": 0.0426}, "confidence": 0.2405, "answer_confidence": 0.5591, "action": {"act_probability": 1.0}}, "q6": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5558, "team1": 0.2121, "team2": 0.1255, "team3": 0.0623, "team4": 0.0443}, "confidence": 0.2377, "answer_confidence": 0.5558, "action": {"act_probability": 1.0}}, "q7": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5773, "team1": 0.2057, "team2": 0.1185, "team3": 0.059, "team4": 0.0395}, "confidence": 0.2607, "answer_confidence": 0.5773, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.8306, "team1": 0.1225, "team2": 0.035, "team3": 0.0068, "team4": 0.0031, "team5": 0.002}, "confidence": 0.6689, "answer_confidence": 0.8306, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5518, "team1": 0.3974, "team2": 0.0394, "team3": 0.0063, "team4": 0.003, "team5": 0.002}, "confidence": 0.5065, "answer_confidence": 0.5518, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.4753, "team1": 0.2883, "team2": 0.221, "team3": 0.0093, "team4": 0.0037, "team5": 0.0023}, "confidence": 0.3725, "answer_confidence": 0.4753, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5962, "team1": 0.2447, "team2": 0.1065, "team3": 0.0452, "team4": 0.0048, "team5": 0.0026}, "confidence": 0.4013, "answer_confidence": 0.5962, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team4", "probabilities": {"team0": 0.1874, "team1": 0.1468, "team2": 0.103, "team3": 0.0381, "team4": 0.5195, "team5": 0.0052}, "confidence": 0.2624, "answer_confidence": 0.5195, "action": {"act_probability": 1.0}}, "q5": {"type": "choice", "choice": "team5", "probabilities": {"team0": 0.018, "team1": 0.0253, "team2": 0.0233, "team3": 0.0073, "team4": 0.0046, "team5": 0.9215}, "confidence": 0.7829, "answer_confidence": 0.9215, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7733, "team1": 0.1752, "team2": 0.0516}, "confidence": 0.4021, "answer_confidence": 0.7733, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.7494, "team1": 0.1764, "team2": 0.0742}, "confidence": 0.3489, "answer_confidence": 0.7494, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5272, "team1": 0.368, "team2": 0.1048}, "confidence": 0.1428, "answer_confidence": 0.5272, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.439, "team1": 0.3469, "team2": 0.2141}, "confidence": 0.0363, "answer_confidence": 0.439, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.586, "team1": 0.2731, "team2": 0.141}, "confidence": 0.1409, "answer_confidence": 0.586, "action": {"act_probability": 1.0}}, "q4": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5872, "team1": 0.2668, "team2": 0.146}, "confidence": 0.1388, "answer_confidence": 0.5872, "action": {"act_probability": 1.0}}, "q5": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6022, "team1": 0.2635, "team2": 0.1343}, "confidence": 0.1567, "answer_confidence": 0.6022, "action": {"act_probability": 1.0}}, "q6": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.613, "team1": 0.2585, "team2": 0.1285}, "confidence": 0.1686, "answer_confidence": 0.613, "action": {"act_probability": 1.0}}}, {"q0": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.6977, "team1": 0.1946, "team2": 0.067, "team3": 0.0407}, "confidence": 0.3644, "answer_confidence": 0.6977, "action": {"act_probability": 1.0}}, "q1": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5732, "team1": 0.2913, "team2": 0.0869, "team3": 0.0486}, "confidence": 0.2516, "answer_confidence": 0.5732, "action": {"act_probability": 1.0}}, "q2": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.5202, "team1": 0.2908, "team2": 0.1395, "team3": 0.0495}, "confidence": 0.1901, "answer_confidence": 0.5202, "action": {"act_probability": 1.0}}, "q3": {"type": "choice", "choice": "team0", "probabilities": {"team0": 0.2906, "team1": 0.2515, "team2": 0.1768, "team3": 0.2811}, "confidence": 0.0122, "answer_confidence": 0.2906, "action": {"act_probability": 1.0}}}], "cuda_graph_nodes": 10, "pass_memory": [{"allocated_mib": 1607.35888671875, "reserved_mib": 1818.0}, {"allocated_mib": 1607.35888671875, "reserved_mib": 1818.0}, {"allocated_mib": 1607.35888671875, "reserved_mib": 1818.0}], "allocated_mib": 1607.35888671875, "reserved_mib": 1818.0, "peak_allocated_mib": 1669.47119140625, "peak_reserved_mib": 1818.0, "counters": {"stats": {"calls_captured": 6700, "unique_graphs": 2}, "inductor": {"triton_bundler_load_static_autotuner": 68, "async_compile_cache_miss": 249, "async_compile_cache_hit": 151, "pattern_matcher_count": 20, "pattern_matcher_nodes": 26, "cudagraph_partitions": 4, "extern_calls": 386, "fxgraph_cache_hit": 2, "cudagraph_recorded_non_static_inputs": 89}}}
+Sat Oct 3 19:00:22 2026
++-----------------------------------------------------------------------------------------+
+| NVIDIA-SMI 615.71.09 KMD Version: 615.71.09 CUDA UMD Version: 13.4 |
++-----------------------------------------+------------------------+----------------------+
+| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
+| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
+| | | MIG M. |
+|=========================================+========================+======================|
+| 0 NVIDIA GeForce RTX 4070 ... On | 00000000:26:00.0 Off | N/A |
+| 0% 48C P2 89W / 295W | 2881MiB / 16376MiB | 77% Default |
+| | | N/A |
++-----------------------------------------+------------------------+----------------------+
+
++-----------------------------------------------------------------------------------------+
+| Processes: |
+| GPU GI CI PID Type Process name GPU Memory |
+| ID ID Usage |
+|=========================================================================================|
+| 0 N/A N/A 892 G Hyprland 156MiB |
+| 0 N/A N/A 952 G quickshell 38MiB |
+| 0 N/A N/A 995 G Xwayland 4MiB |
+| 0 N/A N/A 1168 G monocloud 3MiB |
+| 0 N/A N/A 3324 C+G /usr/bin/sunshine 215MiB |
+| 0 N/A N/A 12883 G /usr/bin/ghostty 100MiB |
+| 0 N/A N/A 3906709 C+G ...rack-uuid=3190708988185955192 35MiB |
+| 0 N/A N/A 4087163 C ...jects/S/laya/.venv/bin/python 2138MiB |
++-----------------------------------------------------------------------------------------+
+
diff --git a/benchmarks/results/compile-defaults/mode-validation.log b/benchmarks/results/compile-defaults/mode-validation.log
new file mode 100644
index 00000000..2e0eb6b8
--- /dev/null
+++ b/benchmarks/results/compile-defaults/mode-validation.log
@@ -0,0 +1,58 @@
+PATH=/home/ckl/projects/S/laya/.venv/bin:$PATH
+
+$ ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
+All checks passed!
+
+exit=0
+
+$ python -m compileall -q laya/ tests/
+
+exit=0
+
+$ python tests/test_compile.py
+ok test_cuda_compile_padding_masks_only_the_new_tail
+ok test_one_graph_across_shapes_and_same_outputs
+ok test_warmup_builds_every_graph_before_the_first_request
+ok test_duck_shape_restored_after_errors_and_nesting
+ok test_duck_shape_restored_across_threads
+ok test_eager_agent_leaves_the_setting_alone
+ok test_constructor_warms_only_the_active_compiled_path
+ok test_persistent_cache_is_opt_in_and_respects_the_environment
+ok test_cuda_graph_step_marks_each_forward_and_releases_after_failure
+all compile tests passed
+
+exit=0
+
+$ python tests/test_hooks_api.py
+
+403 passed, 0 failed
+all hook API tests passed
+
+exit=0
+
+$ python tests/test_router.py
+
+703 passed, 0 failed
+all routing tests passed
+
+exit=0
+
+$ python tests/test_criteria.py
+
+198 passed, 0 failed
+
+exit=0
+
+$ python tests/test_hooks.py
+
+240 passed, 0 failed
+all hook tests passed
+
+exit=0
+
+$ zensical build --strict --clean
+Build started
+No issues found
+Build finished in 2.06s
+
+exit=0
diff --git a/docs/compile-and-fast-path.md b/docs/compile-and-fast-path.md
index 0076af49..d491652f 100644
--- a/docs/compile-and-fast-path.md
+++ b/docs/compile-and-fast-path.md
@@ -40,6 +40,35 @@ by sequence length, so it has no such buffer.
home/volume. Cache compatibility and invalidation are managed by PyTorch; a GPU, torch,
compiler, model, or input guard change can require compilation again.
+## Opt-in CUDA graphs
+
+```python
+agent = laya.load("convaiinnovations/laya", compile=True,
+ compile_cache=True, compile_mode="reduce-overhead")
+```
+
+`compile_mode` defaults to `"default"`; only `"default"` and `"reduce-overhead"` are accepted
+on the active compiled path. Eager and TileLang loads ignore the compile options. CPU compilation
+still works, but CUDA graph recording only applies on CUDA. The CUDA mode requires PyTorch's
+`torch.compiler.cudagraph_mark_step_begin` API; older builds without it raise an explicit error.
+
+Dynamic Dynamo graphs do not imply shape-independent CUDA graphs: new concrete shapes may
+require warm-up and recording again, without a new Dynamo graph. The two default synthetic
+warm-up shapes do not pre-record every request shape. Repeated shapes can benefit, but varying
+shapes can pay extra latency and retain graph pools. PyTorch may skip CUDA graphs for unsupported
+operations or configurations; setting this mode is not a guarantee of capture.
+
+Laya marks each compiled CUDA forward as a new step, serializes these forwards across its agents,
+and clones both output tensors outside the compiled graph before releasing the lock. This keeps
+retained outputs valid across replays, at the cost of two copies and serialized forward execution.
+The lock does not coordinate unrelated application-owned compiled models; callers sharing CUDA
+graph iterations or using custom streams must manage their own coordination. Disk caches reuse
+compiled code, not live CUDA graph recordings or their device memory, across processes.
+
+Reproduce cold/restart timings, memory, and cache counters with
+`benchmarks/bench_compile_defaults.py`; see
+[the recorded measurements](https://github.com/NandhaKishorM/laya/blob/main/benchmarks/results/compile-defaults/README.md).
+
## AOTInductor: not yet
Shipping a precompiled artifact per checkpoint and GPU architecture
diff --git a/laya/_compile.py b/laya/_compile.py
index 852a8442..ab170930 100644
--- a/laya/_compile.py
+++ b/laya/_compile.py
@@ -22,8 +22,9 @@ last call returns (refcounted, so concurrent calls on several threads do not res
torch (2.14) keeps config overrides per thread (a `ContextVar` per entry), so each call sets and
restores its own thread's value with the config's `patch`. Nothing is left changed between calls.
-Mode and device are unchanged from what `compile=True` has always done: the default inductor mode,
-on whatever device the agent runs on, CPU included.
+The default Inductor mode is unchanged. `compile_mode="reduce-overhead"` opts into CUDA graphs;
+Laya serializes those forwards and copies their outputs before a later replay can overwrite them.
+Compilation still runs on whatever device the agent uses, CPU included.
"""
import threading
import os
@@ -35,6 +36,7 @@ import torch
_lock = threading.Lock()
_depth = 0
_saved = None
+_cudagraph_lock = threading.Lock()
def compile_model(model, **kwargs):
@@ -52,6 +54,17 @@ def configure_cache():
return directory
+@contextmanager
+def cuda_graph_step():
+ """Serialize Laya CUDA graph forwards until their outputs have been copied."""
+ mark_step = getattr(getattr(torch, "compiler", None), "cudagraph_mark_step_begin", None)
+ if mark_step is None:
+ raise RuntimeError("reduce-overhead on CUDA requires torch.compiler.cudagraph_mark_step_begin")
+ with _cudagraph_lock:
+ mark_step()
+ yield
+
+
def _fx_config():
try:
from torch.fx.experimental import _config
diff --git a/laya/agent.py b/laya/agent.py
index 354b99c4..751a5a5d 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -13,7 +13,7 @@ from typing import Any, Dict, List, Optional, Union
import numpy as np
import torch
-from ._compile import compile_model, independent_dims, configure_cache
+from ._compile import compile_model, independent_dims, configure_cache, cuda_graph_step
from .calibrate import (
_install_temperatures,
apply_calibration_payload,
@@ -472,6 +472,7 @@ class Agent(HookRegistry):
last_fallback_reason = None
# Set when `compile=True` wrapped the model in torch.compile.
_compiled = False
+ _reduce_overhead = False
_gate = _InferenceGate()
def __init__(
@@ -494,6 +495,7 @@ class Agent(HookRegistry):
calibration: Optional[str] = None,
compile_warmup: bool = True,
compile_cache: bool = False,
+ compile_mode: str = "default",
):
"""Load a Laya checkpoint.
@@ -516,6 +518,9 @@ class Agent(HookRegistry):
work to requests or a manual `warmup()` call. Eager and fast agents are unchanged.
`compile_cache=True` opts into a persistent Laya Inductor directory (process-wide),
respecting any existing `TORCHINDUCTOR_CACHE_DIR`; see the compile engineering notes.
+ `compile_mode="reduce-overhead"` opts into CUDA graphs. It can retain more GPU memory
+ and records each new shape separately. CUDA outputs are copied before the next replay;
+ compiled CUDA graph forwards are serialized. The default mode remains "default".
`subfolder` selects one checkpoint from a repo that bundles several, e.g.
`Agent("convaiinnovations/laya", subfolder="multilingual")`. Only that subfolder is
@@ -654,7 +659,11 @@ class Agent(HookRegistry):
# dynamic=True plus independent dimensions (laya/_compile.py) so a new request shape
# does not recompile.
if compile and not fast:
- self.model = compile_model(self.model)
+ if compile_mode not in ("default", "reduce-overhead"):
+ raise ValueError("compile_mode must be 'default' or 'reduce-overhead'")
+ self._reduce_overhead = compile_mode == "reduce-overhead"
+ compile_kwargs = {"mode": compile_mode} if self._reduce_overhead else {}
+ self.model = compile_model(self.model, **compile_kwargs)
self._compiled = True
# Keep what the checkpoint shipped for inspection, but only ever apply clamped values:
@@ -1119,14 +1128,18 @@ class Agent(HookRegistry):
if enabled is None:
enabled = self._amp_enabled_for(b["input_ids"].shape[0])
dims = independent_dims() if self._compiled else nullcontext()
- with _amp_context(self.device, self.dtype, enabled), dims:
- return self.model(
+ graph_outputs = self._reduce_overhead and self._fast is None and self.device.type == "cuda"
+ step = cuda_graph_step() if graph_outputs else nullcontext()
+ with _amp_context(self.device, self.dtype, enabled), dims, step:
+ out = self.model(
b["input_ids"].to(self.device),
b["attention_mask"].to(self.device),
b["marker_pos"].to(self.device),
b["marker_mask"].to(self.device),
b["qtype"].to(self.device),
)
+ # CUDA graph outputs belong to a reusable pool; callers can retain our copies.
+ return tuple(t.clone() for t in out) if graph_outputs else out
try:
with self._gate.read_lock():
@@ -1920,7 +1933,7 @@ def load(model_id_or_path: str = "convaiinnovations/laya", device: Optional[str]
hooks_raise: bool = True, hooks_concurrent: bool = True,
hooks_timeout: Optional[float] = None,
calibration: Optional[str] = None, compile_warmup: bool = True,
- compile_cache: bool = False) -> Agent:
+ compile_cache: bool = False, compile_mode: str = "default") -> Agent:
"""Load a Laya agent.
`subfolder` picks one checkpoint out of a repo that bundles several:
@@ -1957,6 +1970,7 @@ def load(model_id_or_path: str = "convaiinnovations/laya", device: Optional[str]
model_id_or_path, subfolder = spec
return Agent(model_id_or_path, device=device, token=token, subfolder=subfolder, fast=fast,
compile=compile, compile_warmup=compile_warmup, compile_cache=compile_cache,
+ compile_mode=compile_mode,
revision=revision, expected_sha256=expected_sha256,
lang_temperatures=lang_temperatures,
hooks=hooks, on_predict_start=on_predict_start, on_predict_end=on_predict_end,
diff --git a/tests/test_compile.py b/tests/test_compile.py
index f3426aac..0fa0a8d5 100644
--- a/tests/test_compile.py
+++ b/tests/test_compile.py
@@ -215,6 +215,20 @@ def test_constructor_warms_only_the_active_compiled_path():
load(directory, device="cpu", compile=False, compile_cache=True)
load(directory, device="cpu", compile=True, fast=True, compile_cache=True)
compile_spy.assert_not_called()
+ reduced = load(directory, device="cpu", compile=True, compile_warmup=False,
+ compile_mode="reduce-overhead")
+ assert reduced._reduce_overhead
+ assert compile_spy.call_args.kwargs == {"mode": "reduce-overhead"}
+ compile_spy.reset_mock()
+ load(directory, device="cpu", compile=False, compile_mode="reduce-overhead")
+ load(directory, device="cpu", compile=True, fast=True, compile_mode="reduce-overhead")
+ compile_spy.assert_not_called()
+ try:
+ load(directory, device="cpu", compile=True, compile_mode="typo")
+ except ValueError as error:
+ assert "compile_mode" in str(error)
+ else:
+ raise AssertionError("invalid mode accepted")
cache_spy.assert_not_called()
@@ -247,6 +261,29 @@ def test_persistent_cache_is_opt_in_and_respects_the_environment():
assert configure_cache() == directory + "/.cache/laya/torchinductor"
+def test_cuda_graph_step_marks_each_forward_and_releases_after_failure():
+ from unittest.mock import patch
+ from laya._compile import cuda_graph_step
+
+ with patch("torch.compiler.cudagraph_mark_step_begin") as mark:
+ try:
+ with cuda_graph_step():
+ raise RuntimeError("forward failed")
+ except RuntimeError:
+ pass
+ with cuda_graph_step():
+ pass
+ assert mark.call_count == 2
+ with patch.object(torch, "compiler", None):
+ try:
+ with cuda_graph_step():
+ pass
+ except RuntimeError as error:
+ assert "requires torch.compiler.cudagraph_mark_step_begin" in str(error)
+ else:
+ raise AssertionError("unsupported CUDA graph step API accepted")
+
+
if __name__ == "__main__":
for name, fn in list(globals().items()):
if name.startswith("test_"):
diff --git a/tests/test_compile_cuda.py b/tests/test_compile_cuda.py
new file mode 100644
index 00000000..330ec8aa
--- /dev/null
+++ b/tests/test_compile_cuda.py
@@ -0,0 +1,62 @@
+"""GPU output ownership and threaded replay check; no checkpoint required.
+
+Run: python tests/test_compile_cuda.py (skips without CUDA).
+"""
+import os
+import sys
+from concurrent.futures import ThreadPoolExecutor
+from types import SimpleNamespace
+
+import torch
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+from laya.agent import Agent
+from laya._compile import compile_model
+
+
+def test_reduce_overhead_retains_outputs_across_replay_and_threads():
+ if not torch.cuda.is_available():
+ print("SKIP: CUDA is unavailable")
+ return
+
+ class Model(torch.nn.Module):
+ def forward(self, ids, mask, pos, marker_mask, qtype):
+ values = ids.float().sum(1, keepdim=True)
+ return values.sin(), values.cos()
+
+ agent = Agent.__new__(Agent)
+ agent.device = torch.device("cuda")
+ agent.dtype = torch.float32
+ agent.amp_enabled = False
+ agent.tok = SimpleNamespace(pad_token_id=0)
+ agent._compiled = True
+ agent._reduce_overhead = True
+ agent.model = compile_model(Model().cuda().eval(), mode="reduce-overhead")
+
+ @torch.no_grad()
+ def call(value):
+ b = {"input_ids": torch.full((2, 16), value, dtype=torch.long),
+ "attention_mask": torch.ones(2, 16, dtype=torch.long),
+ "marker_pos": torch.ones(2, 3, dtype=torch.long),
+ "marker_mask": torch.ones(2, 3, dtype=torch.bool),
+ "qtype": torch.zeros(2, dtype=torch.long)}
+ out = agent._infer(b)
+ ref = Model()(*b.values())
+ return out, ref
+
+ # First call warms, second records, later calls replay with changing tensor contents.
+ held = [call(value) for value in range(1, 6)]
+ from torch._inductor.cudagraph_trees import get_manager
+ manager = get_manager(0, create_if_none_exists=False)
+ assert manager is not None and list(manager.get_roots()), "CUDA graph was not recorded"
+ with ThreadPoolExecutor(max_workers=2) as pool:
+ held.extend(pool.map(call, range(6, 12)))
+ torch.cuda.synchronize()
+ for out, ref in held:
+ for actual, expected in zip(out, ref):
+ torch.testing.assert_close(actual.cpu(), expected, atol=1e-5, rtol=1e-5)
+ print("CUDA graph capture, replay, retained outputs, and two-thread parity passed")
+
+
+if __name__ == "__main__":
+ test_reduce_overhead_retains_outputs_across_replay_and_threads()
diff --git a/tests/test_hooks_api.py b/tests/test_hooks_api.py
index 528527a3..8223d4c6 100644
--- a/tests/test_hooks_api.py
+++ b/tests/test_hooks_api.py
@@ -78,6 +78,7 @@ for label, fn in (("Agent.__init__", Agent.__init__), ("load", load),
for label, fn in (("Agent.__init__", Agent.__init__), ("load", load)):
check_param(label, fn, "compile_warmup", True)
check_param(label, fn, "compile_cache", False)
+ check_param(label, fn, "compile_mode", "default")
# Router keeps lang_guess and explicit per-model revisions too
check_param("Router.__init__", Router.__init__, "lang_guess", None)