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.

confidence: keep the entropy sum vectorised after #1299

Nandakishor6df985c750f5
diff --git a/laya/common.py b/laya/common.py
index 435e7b3c..31df5707 100644
--- a/laya/common.py
+++ b/laya/common.py
@@ -831,14 +831,15 @@ def confidence_from_probs(p: np.ndarray, k: int) -> float:
"""
if k < 2:
return 1.0
+ # The scalar `np.clip` on the score is the part worth replacing (a native comparison is ~4x
+ # the dispatcher); the entropy sum itself stays vectorised. A Python loop over `pk` is faster
+ # below roughly k=40 and slower above it -- 0.9 us vs 2.4 us at k=11, but 14.6 us vs 3.0 us at
+ # k=256 -- and summing in a different order moves the last bits (1.7e-15 over 136k measured
+ # distributions). Both callers `round(..., 4)` this, so that drift is invisible today; it is
+ # the SDK goldens in laya-rust, laya-dotnet and laya-ts that have to keep reproducing these
+ # numbers, and there is no reason to spend bit-exactness on a microsecond.
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)
+ ent = -float((pk * np.log(np.clip(pk, 1e-12, 1.0))).sum())
score = 1.0 - ent / math.log(k)
return 0.0 if score < 0.0 else (1.0 if score > 1.0 else score)