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.

perf(agent): optimize logit decoding and confidence calculation in _decode_answers

Hamidreza0a899ea3be19
diff --git a/laya/agent.py b/laya/agent.py
index df2cae50..aef82cd8 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -1345,7 +1345,7 @@ class Agent(HookRegistry):
t_scale = l_cfg["temperature_by_options"].get(temp_bucket(qt, k), l_cfg["temperature"][qt])
z = raw_rows[j] / t_scale
p = np.exp(z - z.max())
- p = p / p.sum()
+ p /= p.sum()
# The row comes back in slot order; everything below indexes by option.
p = unpermute_probs(p, q.get("option_order"))
diff --git a/laya/common.py b/laya/common.py
index 156002fb..435e7b3c 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -808,7 +808,8 @@ def answer_confidence(p: np.ndarray, k: int) -> float:
"""
if k < 1:
return 1.0
- return float(np.clip(np.max(p[:k]), 0.0, 1.0))
+ val = float(p[:k].max())
+ return 0.0 if val < 0.0 else (1.0 if val > 1.0 else val)
def confidence_from_probs(p: np.ndarray, k: int) -> float:
@@ -830,9 +831,16 @@ def confidence_from_probs(p: np.ndarray, k: int) -> float:
"""
if k < 2:
return 1.0
- p = p[:k]
- ent = -(p * np.log(np.clip(p, 1e-12, 1.0))).sum()
- return float(np.clip(1.0 - ent / math.log(k), 0.0, 1.0))
+ pk = p[:k]
+ ent = 0.0
+ for val in pk:
+ v = float(val)
+ if v <= 0.0:
+ continue
+ c = 1e-12 if v < 1e-12 else (1.0 if v > 1.0 else v)
+ ent -= v * math.log(c)
+ score = 1.0 - ent / math.log(k)
+ return 0.0 if score < 0.0 else (1.0 if score > 1.0 else score)
def temp_bucket(qtype: int, k: int) -> str:
diff --git a/laya/onnx_agent.py b/laya/onnx_agent.py
index 2b3a1b8c..dfa983b1 100644
--- a/laya/onnx_agent.py
+++ b/laya/onnx_agent.py
@@ -730,7 +730,7 @@ class ONNXAgent(HookRegistry):
t_scale = l_cfg["temperature_by_options"].get(temp_bucket(qt, k), l_cfg["temperature"][qt])
z = logits[offset + r, :k] / t_scale
p = np.exp(z - z.max())
- p = p / p.sum()
+ p /= p.sum()
# The row comes back in slot order; everything below indexes by option.
p = unpermute_probs(p, q.get("option_order"))