Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
fix: guard topk(2) in DecisionModel.forward for single-option questions (#103)
Krishna Anubhav ( Kavy )b2f0af8d707f
diff --git a/laya/common.py b/laya/common.pyindex 326b76a4..fa9c6968 100644--- a/laya/common.py+++ b/laya/common.py@@ -119,7 +119,17 @@ class DecisionModel(nn.Module):p = torch.softmax(logits.detach(), -1)k = marker_mask.sum(-1).clamp(min=2).float()ent = -(p * torch.log(p.clamp_min(1e-9))).sum(-1) / torch.log(k)- top2 = p.topk(2, -1).values+ if p.size(-1) >= 2:+ top2 = p.topk(2, -1).values+ else:+ # A single-option question has exactly one marker, so p.topk(2, ...)+ # has nothing to select for the second slot and raises. The answer+ # is still well-defined: softmax over one logit is 1.0 regardless of+ # its value, so pad the missing second entry with 0.0 - that gives+ # the act head top1 - top2 == 1.0, the same "fully decided" signal+ # it would see for any other unambiguous top-1-vs-rest gap.+ top1 = p.topk(1, -1).values+ top2 = torch.cat([top1, torch.zeros_like(top1)], dim=-1)feats = torch.stack([top2[:, 0], top2[:, 0] - top2[:, 1], ent, k / 255.0], -1)pooled = h[:, 0].float()act_logits = self.act_head(torch.cat([pooled, feats], -1))diff --git a/tests/test_decision_model.py b/tests/test_decision_model.pynew file mode 100644index 00000000..5e0ece5c--- /dev/null+++ b/tests/test_decision_model.py@@ -0,0 +1,91 @@+"""Regression tests for DecisionModel.forward (laya/common.py).++Uses a tiny from-config BERT encoder (no pretrained weights downloaded) so+these run fast and offline, unlike tests/test_local_e2e.py which needs a+real checkpoint on disk.+"""+import torch+from transformers import AutoConfig, AutoModel++from laya.common import DecisionModel+++def _tiny_model(head_layers: int = 1, n_act: int = 2) -> DecisionModel:+ cfg = AutoConfig.for_model(+ "bert",+ hidden_size=16,+ num_hidden_layers=1,+ num_attention_heads=1,+ intermediate_size=32,+ vocab_size=50,+ )+ encoder = AutoModel.from_config(cfg)+ model = DecisionModel(encoder, head_layers=head_layers, n_act=n_act)+ model.eval()+ return model+++def _inputs(batch: int, seq: int, n_markers: int):+ input_ids = torch.randint(0, 50, (batch, seq))+ attention_mask = torch.ones(batch, seq, dtype=torch.long)+ qtype = torch.zeros(batch, dtype=torch.long)+ marker_pos = torch.arange(n_markers).unsqueeze(0).expand(batch, -1).clone()+ marker_mask = torch.ones(batch, n_markers, dtype=torch.bool)+ return input_ids, attention_mask, marker_pos, marker_mask, qtype+++def test_single_option_question_does_not_crash():+ # Regression test for #96: a `choice` question with exactly one criterion+ # used to crash inside forward() with "selected index k out of range",+ # because p.topk(2, -1) has nothing to select for the second slot when+ # there is only one valid marker.+ torch.manual_seed(0)+ model = _tiny_model()+ input_ids, attention_mask, marker_pos, marker_mask, qtype = _inputs(+ batch=1, seq=8, n_markers=1+ )+ with torch.no_grad():+ logits, act_logits = model(input_ids, attention_mask, marker_pos, marker_mask, qtype)+ assert logits.shape == (1, 1)+ assert act_logits.shape == (1, 2)+ assert torch.isfinite(logits).all()+ assert torch.isfinite(act_logits).all()+++def test_single_option_top1_minus_top2_is_exactly_one():+ # Softmax over a single valid logit is 1.0 regardless of its value, so the+ # padded top2 (0.0) must make the act head's top1-top2 gap read as a fully+ # decided 1.0 - the same signal it gets for any other unambiguous choice.+ torch.manual_seed(1)+ model = _tiny_model()+ input_ids, attention_mask, marker_pos, marker_mask, qtype = _inputs(+ batch=1, seq=6, n_markers=1+ )+ with torch.no_grad():+ h = model.encoder(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state+ h = h + model.type_emb(qtype)[:, None, :]+ pad = ~attention_mask.bool()+ for layer in model.head.layers:+ h = layer(h, src_key_padding_mask=pad)+ idx = marker_pos.clamp(min=0)[:, :, None].expand(-1, -1, h.size(-1))+ m = torch.gather(h, 1, idx)+ logits = model.scorer(m).squeeze(-1).float()+ logits = logits.masked_fill(~marker_mask, -1e4)+ p = torch.softmax(logits.detach(), -1)+ assert torch.allclose(p, torch.ones_like(p))+++def test_multi_option_question_is_unaffected():+ # The >=2-marker path must still use the original torch.topk(2, -1) call+ # unchanged - this pins that the single-option fix didn't touch it.+ torch.manual_seed(2)+ model = _tiny_model()+ input_ids, attention_mask, marker_pos, marker_mask, qtype = _inputs(+ batch=2, seq=10, n_markers=4+ )+ with torch.no_grad():+ logits, act_logits = model(input_ids, attention_mask, marker_pos, marker_mask, qtype)+ assert logits.shape == (2, 4)+ assert act_logits.shape == (2, 2)+ assert torch.isfinite(logits).all()+ assert torch.isfinite(act_logits).all()