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.

Merge pull request #19 from mvanhorn/cursor/fit-persist-bucket-temperatures-b1f1

Nandakishord8f47e1dc476
diff --cc .github/workflows/ci.yml
index d4f38072,5822a576..9e168a30
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@@ -301,19 -234,13 +302,19 @@@ jobs
# pass -- a change to a container script, so it belongs in its own PR, not this one.
- name: Routing and language-detection tests
run: |
- for t in test_router test_criteria test_download test_shortlist \
- test_decision_model test_packaging test_lang_guess \
- test_identifier_complexity \
- test_calibration_persistence test_calibrate test_context_manager \
+ for t in test_router test_criteria test_batch test_predict_long \
+ test_structured test_structured_api test_onnx_lang_parity \
+ test_download test_shortlist test_decision_model \
+ test_head_checkpointing test_packaging test_doc_tables \
+ test_tokenizer_cache test_tokenizer_concurrency test_lazy_import \
+ test_runtime_fixes test_lang_guess test_identifier_complexity \
- test_lang_stats test_calibration_persistence test_context_manager \
++ test_lang_stats test_calibration_persistence test_calibrate test_context_manager \
test_criteria_normalization test_docker_entrypoint \
test_empty_questions test_router_memory test_shortlist_cosine \
- test_temperature_loading; do
+ test_temperature_loading test_confidence test_cli test_mcp \
+ test_langchain test_portability test_training \
+ test_example_server_limits test_blank_lang_routing \
+ test_export_onnx_safety test_load_errors test_revision_pinning; do
if [ "$t" = "test_docker_entrypoint" ]; then
echo "skipping $t: docker/entrypoint.py is Linux-container only"
continue
diff --cc docs/reference/helpers.md
index 972c520b,dcedd204..4cd6f348
--- a/docs/reference/helpers.md
+++ b/docs/reference/helpers.md
@@@ -50,6 -42,6 +50,12 @@@
::: laya.common.ece_score
++::: laya.calibrate.fit_temperatures
++
++::: laya.calibrate.fit_one_temperature
++
++::: laya.calibrate.fit_temperature_map
++
::: laya.common.render_options
::: laya.common.proper_reward
diff --cc laya/agent.py
index 5ca3861c,49f96ddd..b0977831
--- a/laya/agent.py
+++ b/laya/agent.py
@@@ -11,7 -11,12 +11,13 @@@ from typing import Any, Dict, List, Opt
import numpy as np
import torch
+from ._compile import compile_model, independent_dims
+ from .calibrate import (
+ _install_temperatures,
+ apply_calibration_payload,
+ calibration_payload,
+ fit_temperature_map,
+ )
from .common import (
QTYPES,
TEMP_MAX,
@@@ -221,38 -222,13 +227,45 @@@ def _cuda_amp_dtype(checkpoint_default
return amp_dtype(checkpoint_default)
+def _start_evidence():
+ """A recorder for `predict_long`: what the start-hook chain left for inference to run on.
+
+ Why a recorder at all: `ctx.skip()` only assigns `ctx.results` (see `laya.hooks.PredictContext`),
+ so outside `predict_batch` the only account of what the hooks did is the context they were
+ handed. The result count cannot tell a hook that answered the document from one that replaced
+ the window list -- both return fewer results than there were windows, and the two need opposite
+ readings: the first scored no window, the second scored the ones it left behind and booked
+ tokens for them.
+
+ The probe belongs last in the chain because `compose_hooks` orders defaults, then installed,
+ then per-call hooks, and `dispatch` calls them in order, so a probe appended after the caller's
+ own start hooks observes exactly what `predict_batch` is about to act on.
+
+ Returns the probe and the dict it fills: `answered` is whether a hook replaced the call before
+ inference, `states` is a snapshot of the states that reached it (`None` if the probe never ran,
+ which means `predict_batch` was replaced and no hook chain was dispatched).
+ """
+ evidence = {"answered": False, "states": None}
+
+ def probe(ctx):
+ evidence["answered"] = ctx.results is not None
+ evidence["states"] = list(ctx.states)
+
+ return probe, evidence
+
+
+def _with_start_probe(hook_kwargs, probe):
+ """`hook_kwargs` with `probe` appended after the caller's own start hooks."""
+ kwargs = dict(hook_kwargs)
+ kwargs["on_predict_start"] = list(_as_sequence(hook_kwargs.get("on_predict_start"))) + [probe]
+ return kwargs
+ def _option_logits(logits, items, offset):
+ """Raw per-option logits, the rows `_decode_answers` divides by temperature.
+
+ Calibration record collection slices with this same helper, so a fitted map sees the
+ option width the decoder scales and not a second tokenization of the state.
+ """
+ return [logits[offset + j, : len(item["markers"])] for j, item in enumerate(items)]
class Agent(HookRegistry):
@@@ -1399,10 -1212,9 +1462,10 @@@ def load(model_id_or_path: str = "conva
`revision`/`expected_sha256` pin and verify the downloaded artifacts; see `Agent`.
`hooks` / `on_predict_start` / `on_predict_end` observe or shape every prediction; see
- `laya.hooks`.
+ `laya.hooks`. `calibration` is the same optional JSON path accepted by `Agent`.
"""
return Agent(model_id_or_path, device=device, token=token, subfolder=subfolder, fast=fast,
+ compile=compile,
revision=revision, expected_sha256=expected_sha256,
lang_temperatures=lang_temperatures,
hooks=hooks, on_predict_start=on_predict_start, on_predict_end=on_predict_end,