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
feat(java): ONNX session, answer decoding and Agent.predict for laya-java
Adeshina Lasisi1d3182642f50
diff --git a/laya-java/fixtures/decode.json b/laya-java/fixtures/decode.jsonnew file mode 100644index 00000000..be1c5f21--- /dev/null+++ b/laya-java/fixtures/decode.json@@ -0,0 +1,1709 @@+{+ "english": {+ "config": {+ "temperature": [+ 1.6369030475616455,+ 1.2514300346374512,+ 1.983399510383606+ ],+ "temperature_by_options": {+ "choice:3-5": 1.7601518630981445,+ "choice:6-10": 1.0000158548355103,+ "score:3-5": 1.2514300346374512,+ "noul:2": 1.983399510383606,+ "choice:11+": 0.5,+ "choice:2": 1.9063563346862793+ },+ "lang_temperatures_raw": {+ "zh": {+ "temperature": [+ 2.0,+ 1.5,+ 3.0+ ],+ "temperature_by_options": {+ "choice:2": 2.5+ }+ }+ }+ },+ "qtypes": {+ "choice": 0,+ "score": 1,+ "noul": 2+ },+ "cases": {+ "choice-2": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "x",+ "b": "y"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 2.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.6282,+ "b": 0.3718+ },+ "confidence": 0.048,+ "answer_confidence": 0.6282,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-2-tied": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "x",+ "b": "y"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 1.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.5,+ "b": 0.5+ },+ "confidence": 0.0,+ "answer_confidence": 0.5,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-4": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2",+ "c": "3",+ "d": "4"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.5,+ 2.5,+ -1.0,+ 2.5+ ],+ "logits_block_width": 7,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "b",+ "probabilities": {+ "a": 0.1306,+ "b": 0.4068,+ "c": 0.0557,+ "d": 0.4068+ },+ "confidence": 0.1643,+ "answer_confidence": 0.4068,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-4-permuted": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2",+ "c": "3",+ "d": "4"+ }+ },+ "option_order": [+ 2,+ 0,+ 3,+ 1+ ],+ "lang": null,+ "logits": [+ 0.5,+ 2.5,+ -1.0,+ 2.5+ ],+ "logits_block_width": 7,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.4068,+ "b": 0.4068,+ "c": 0.1306,+ "d": 0.0557+ },+ "confidence": 0.1643,+ "answer_confidence": 0.4068,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-7": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "o0": "0",+ "o1": "1",+ "o2": "2",+ "o3": "3",+ "o4": "4",+ "o5": "5",+ "o6": "6"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.0,+ 0.1,+ 0.2,+ 0.30000000000000004,+ 0.4,+ 0.5,+ 0.6000000000000001+ ],+ "logits_block_width": 10,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "o6",+ "probabilities": {+ "o0": 0.1037,+ "o1": 0.1147,+ "o2": 0.1267,+ "o3": 0.14,+ "o4": 0.1548,+ "o5": 0.171,+ "o6": 0.189+ },+ "confidence": 0.0102,+ "answer_confidence": 0.189,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-12": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "o0": "0",+ "o1": "1",+ "o2": "2",+ "o3": "3",+ "o4": "4",+ "o5": "5",+ "o6": "6",+ "o7": "7",+ "o8": "8",+ "o9": "9",+ "o10": "10",+ "o11": "11"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.0,+ -0.3,+ 0.6,+ -0.8999999999999999,+ 1.2,+ -1.5,+ 1.7999999999999998,+ -2.1,+ 2.4,+ -2.6999999999999997,+ 3.0,+ -3.3+ ],+ "logits_block_width": 15,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "o10",+ "probabilities": {+ "o0": 0.0017,+ "o1": 0.001,+ "o2": 0.0057,+ "o3": 0.0003,+ "o4": 0.0191,+ "o5": 0.0001,+ "o6": 0.0634,+ "o7": 0.0,+ "o8": 0.2103,+ "o9": 0.0,+ "o10": 0.6984,+ "o11": 0.0+ },+ "confidence": 0.6459,+ "answer_confidence": 0.6984,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-20-uniform": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "o0": "0",+ "o1": "1",+ "o2": "2",+ "o3": "3",+ "o4": "4",+ "o5": "5",+ "o6": "6",+ "o7": "7",+ "o8": "8",+ "o9": "9",+ "o10": "10",+ "o11": "11",+ "o12": "12",+ "o13": "13",+ "o14": "14",+ "o15": "15",+ "o16": "16",+ "o17": "17",+ "o18": "18",+ "o19": "19"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0+ ],+ "logits_block_width": 23,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "o0",+ "probabilities": {+ "o0": 0.05,+ "o1": 0.05,+ "o2": 0.05,+ "o3": 0.05,+ "o4": 0.05,+ "o5": 0.05,+ "o6": 0.05,+ "o7": 0.05,+ "o8": 0.05,+ "o9": 0.05,+ "o10": 0.05,+ "o11": 0.05,+ "o12": 0.05,+ "o13": 0.05,+ "o14": 0.05,+ "o15": 0.05,+ "o16": 0.05,+ "o17": 0.05,+ "o18": 0.05,+ "o19": 0.05+ },+ "confidence": 0.0,+ "answer_confidence": 0.05,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-huge-magnitude": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2",+ "c": "3"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 700.0,+ 699.0,+ -700.0+ ],+ "logits_block_width": 6,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.6383,+ "b": 0.3617,+ "c": 0.0+ },+ "confidence": 0.4044,+ "answer_confidence": 0.6383,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-tiny-spread": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 1.0,+ 1.0000001+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "b",+ "probabilities": {+ "a": 0.5,+ "b": 0.5+ },+ "confidence": 0.0,+ "answer_confidence": 0.5,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-lang-override": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2"+ }+ },+ "option_order": null,+ "lang": "zh-Hans",+ "logits": [+ 2.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.5987,+ "b": 0.4013+ },+ "confidence": 0.0283,+ "answer_confidence": 0.5987,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-lang-override-prefix": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2"+ }+ },+ "option_order": null,+ "lang": "ZH",+ "logits": [+ 2.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.5987,+ "b": 0.4013+ },+ "confidence": 0.0283,+ "answer_confidence": 0.5987,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-lang-unknown": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2"+ }+ },+ "option_order": null,+ "lang": "xx",+ "logits": [+ 2.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.6282,+ "b": 0.3718+ },+ "confidence": 0.048,+ "answer_confidence": 0.6282,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "score-3": {+ "question": {+ "t": "score",+ "ins": "i",+ "crit": [+ "bad",+ "ok",+ "good"+ ]+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.2,+ 1.4,+ 0.9+ ],+ "logits_block_width": 6,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "score",+ "score": 1.1399,+ "legend": {+ "0": "bad",+ "1": "ok",+ "2": "good"+ },+ "probabilities": {+ "0": 0.1866,+ "1": 0.4869,+ "2": 0.3265+ },+ "confidence": 0.0632,+ "answer_confidence": 0.4869,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "score-6": {+ "question": {+ "t": "score",+ "ins": "i",+ "crit": [+ "0",+ "1",+ "2",+ "3",+ "4",+ "5"+ ]+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.0,+ 1.0,+ 2.0,+ 1.0,+ 0.0,+ -1.0+ ],+ "logits_block_width": 9,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "score",+ "score": 2.1139,+ "legend": {+ "0": "0",+ "1": "1",+ "2": "2",+ "3": "3",+ "4": "4",+ "5": "5"+ },+ "probabilities": {+ "0": 0.0845,+ "1": 0.1878,+ "2": 0.4175,+ "3": 0.1878,+ "4": 0.0845,+ "5": 0.038+ },+ "confidence": 0.1436,+ "answer_confidence": 0.4175,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "score-structured-legend": {+ "question": {+ "t": "score",+ "ins": "i",+ "crit": [+ {+ "d": 1+ },+ "ok",+ 3+ ]+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.4,+ 0.4,+ 0.4+ ],+ "logits_block_width": 6,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "score",+ "score": 1.0,+ "legend": {+ "0": "{\"d\": 1}",+ "1": "ok",+ "2": "3"+ },+ "probabilities": {+ "0": 0.3333,+ "1": 0.3333,+ "2": 0.3333+ },+ "confidence": 0.0,+ "answer_confidence": 0.3333,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "score-permuted": {+ "question": {+ "t": "score",+ "ins": "i",+ "crit": [+ "bad",+ "ok",+ "good"+ ]+ },+ "option_order": [+ 1,+ 2,+ 0+ ],+ "lang": null,+ "logits": [+ 0.2,+ 1.4,+ 0.9+ ],+ "logits_block_width": 6,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "score",+ "score": 1.1604,+ "legend": {+ "0": "bad",+ "1": "ok",+ "2": "good"+ },+ "probabilities": {+ "0": 0.3265,+ "1": 0.1866,+ "2": 0.4869+ },+ "confidence": 0.0632,+ "answer_confidence": 0.4869,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "noul-true": {+ "question": {+ "t": "noul",+ "ins": "i"+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.3,+ 1.9+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "noul",+ "noul": 0.6914,+ "confidence": 0.6914,+ "answer_confidence": 0.6914,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "noul-false": {+ "question": {+ "t": "noul",+ "ins": "i"+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 2.4,+ 0.1+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "noul",+ "noul": 0.2387,+ "confidence": 0.7613,+ "answer_confidence": 0.7613,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "noul-balanced": {+ "question": {+ "t": "noul",+ "ins": "i"+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 1.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "noul",+ "noul": 0.5,+ "confidence": 0.5,+ "answer_confidence": 0.5,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "noul-labels": {+ "question": {+ "t": "noul",+ "ins": "i",+ "labels": {+ "false": "nope",+ "true": "yep"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.3,+ 1.9+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "noul",+ "noul": 0.6914,+ "confidence": 0.6914,+ "answer_confidence": 0.6914,+ "action": {+ "act_probability": 0.25+ }+ }+ }+ }+ },+ "multilingual": {+ "config": {+ "temperature": [+ 1.0,+ 1.0,+ 1.0+ ],+ "temperature_by_options": {},+ "lang_temperatures_raw": {+ "zh": {+ "temperature": [+ 2.0,+ 1.5,+ 3.0+ ],+ "temperature_by_options": {+ "choice:2": 2.5+ }+ }+ }+ },+ "qtypes": {+ "choice": 0,+ "score": 1,+ "noul": 2+ },+ "cases": {+ "choice-2": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "x",+ "b": "y"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 2.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.7311,+ "b": 0.2689+ },+ "confidence": 0.1601,+ "answer_confidence": 0.7311,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-2-tied": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "x",+ "b": "y"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 1.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.5,+ "b": 0.5+ },+ "confidence": 0.0,+ "answer_confidence": 0.5,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-4": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2",+ "c": "3",+ "d": "4"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.5,+ 2.5,+ -1.0,+ 2.5+ ],+ "logits_block_width": 7,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "b",+ "probabilities": {+ "a": 0.0625,+ "b": 0.4618,+ "c": 0.0139,+ "d": 0.4618+ },+ "confidence": 0.3173,+ "answer_confidence": 0.4618,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-4-permuted": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2",+ "c": "3",+ "d": "4"+ }+ },+ "option_order": [+ 2,+ 0,+ 3,+ 1+ ],+ "lang": null,+ "logits": [+ 0.5,+ 2.5,+ -1.0,+ 2.5+ ],+ "logits_block_width": 7,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.4618,+ "b": 0.4618,+ "c": 0.0625,+ "d": 0.0139+ },+ "confidence": 0.3173,+ "answer_confidence": 0.4618,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-7": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "o0": "0",+ "o1": "1",+ "o2": "2",+ "o3": "3",+ "o4": "4",+ "o5": "5",+ "o6": "6"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.0,+ 0.1,+ 0.2,+ 0.30000000000000004,+ 0.4,+ 0.5,+ 0.6000000000000001+ ],+ "logits_block_width": 10,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "o6",+ "probabilities": {+ "o0": 0.1037,+ "o1": 0.1147,+ "o2": 0.1267,+ "o3": 0.14,+ "o4": 0.1548,+ "o5": 0.171,+ "o6": 0.189+ },+ "confidence": 0.0102,+ "answer_confidence": 0.189,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-12": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "o0": "0",+ "o1": "1",+ "o2": "2",+ "o3": "3",+ "o4": "4",+ "o5": "5",+ "o6": "6",+ "o7": "7",+ "o8": "8",+ "o9": "9",+ "o10": "10",+ "o11": "11"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.0,+ -0.3,+ 0.6,+ -0.8999999999999999,+ 1.2,+ -1.5,+ 1.7999999999999998,+ -2.1,+ 2.4,+ -2.6999999999999997,+ 3.0,+ -3.3+ ],+ "logits_block_width": 15,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "o10",+ "probabilities": {+ "o0": 0.0223,+ "o1": 0.0165,+ "o2": 0.0406,+ "o3": 0.0091,+ "o4": 0.0739,+ "o5": 0.005,+ "o6": 0.1347,+ "o7": 0.0027,+ "o8": 0.2455,+ "o9": 0.0015,+ "o10": 0.4474,+ "o11": 0.0008+ },+ "confidence": 0.376,+ "answer_confidence": 0.4474,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-20-uniform": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "o0": "0",+ "o1": "1",+ "o2": "2",+ "o3": "3",+ "o4": "4",+ "o5": "5",+ "o6": "6",+ "o7": "7",+ "o8": "8",+ "o9": "9",+ "o10": "10",+ "o11": "11",+ "o12": "12",+ "o13": "13",+ "o14": "14",+ "o15": "15",+ "o16": "16",+ "o17": "17",+ "o18": "18",+ "o19": "19"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0+ ],+ "logits_block_width": 23,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "o0",+ "probabilities": {+ "o0": 0.05,+ "o1": 0.05,+ "o2": 0.05,+ "o3": 0.05,+ "o4": 0.05,+ "o5": 0.05,+ "o6": 0.05,+ "o7": 0.05,+ "o8": 0.05,+ "o9": 0.05,+ "o10": 0.05,+ "o11": 0.05,+ "o12": 0.05,+ "o13": 0.05,+ "o14": 0.05,+ "o15": 0.05,+ "o16": 0.05,+ "o17": 0.05,+ "o18": 0.05,+ "o19": 0.05+ },+ "confidence": 0.0,+ "answer_confidence": 0.05,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-huge-magnitude": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2",+ "c": "3"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 700.0,+ 699.0,+ -700.0+ ],+ "logits_block_width": 6,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.7311,+ "b": 0.2689,+ "c": 0.0+ },+ "confidence": 0.4701,+ "answer_confidence": 0.7311,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-tiny-spread": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 1.0,+ 1.0000001+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "b",+ "probabilities": {+ "a": 0.5,+ "b": 0.5+ },+ "confidence": 0.0,+ "answer_confidence": 0.5,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-lang-override": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2"+ }+ },+ "option_order": null,+ "lang": "zh-Hans",+ "logits": [+ 2.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.5987,+ "b": 0.4013+ },+ "confidence": 0.0283,+ "answer_confidence": 0.5987,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-lang-override-prefix": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2"+ }+ },+ "option_order": null,+ "lang": "ZH",+ "logits": [+ 2.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.5987,+ "b": 0.4013+ },+ "confidence": 0.0283,+ "answer_confidence": 0.5987,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "choice-lang-unknown": {+ "question": {+ "t": "choice",+ "ins": "i",+ "crit": {+ "a": "1",+ "b": "2"+ }+ },+ "option_order": null,+ "lang": "xx",+ "logits": [+ 2.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "choice",+ "choice": "a",+ "probabilities": {+ "a": 0.7311,+ "b": 0.2689+ },+ "confidence": 0.1601,+ "answer_confidence": 0.7311,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "score-3": {+ "question": {+ "t": "score",+ "ins": "i",+ "crit": [+ "bad",+ "ok",+ "good"+ ]+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.2,+ 1.4,+ 0.9+ ],+ "logits_block_width": 6,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "score",+ "score": 1.1601,+ "legend": {+ "0": "bad",+ "1": "ok",+ "2": "good"+ },+ "probabilities": {+ "0": 0.1579,+ "1": 0.5242,+ "2": 0.3179+ },+ "confidence": 0.0949,+ "answer_confidence": 0.5242,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "score-6": {+ "question": {+ "t": "score",+ "ins": "i",+ "crit": [+ "0",+ "1",+ "2",+ "3",+ "4",+ "5"+ ]+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.0,+ 1.0,+ 2.0,+ 1.0,+ 0.0,+ -1.0+ ],+ "logits_block_width": 9,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "score",+ "score": 2.0726,+ "legend": {+ "0": "0",+ "1": "1",+ "2": "2",+ "3": "3",+ "4": "4",+ "5": "5"+ },+ "probabilities": {+ "0": 0.0658,+ "1": 0.1789,+ "2": 0.4863,+ "3": 0.1789,+ "4": 0.0658,+ "5": 0.0242+ },+ "confidence": 0.2105,+ "answer_confidence": 0.4863,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "score-structured-legend": {+ "question": {+ "t": "score",+ "ins": "i",+ "crit": [+ {+ "d": 1+ },+ "ok",+ 3+ ]+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.4,+ 0.4,+ 0.4+ ],+ "logits_block_width": 6,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "score",+ "score": 1.0,+ "legend": {+ "0": "{\"d\": 1}",+ "1": "ok",+ "2": "3"+ },+ "probabilities": {+ "0": 0.3333,+ "1": 0.3333,+ "2": 0.3333+ },+ "confidence": 0.0,+ "answer_confidence": 0.3333,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "score-permuted": {+ "question": {+ "t": "score",+ "ins": "i",+ "crit": [+ "bad",+ "ok",+ "good"+ ]+ },+ "option_order": [+ 1,+ 2,+ 0+ ],+ "lang": null,+ "logits": [+ 0.2,+ 1.4,+ 0.9+ ],+ "logits_block_width": 6,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "score",+ "score": 1.2063,+ "legend": {+ "0": "bad",+ "1": "ok",+ "2": "good"+ },+ "probabilities": {+ "0": 0.3179,+ "1": 0.1579,+ "2": 0.5242+ },+ "confidence": 0.0949,+ "answer_confidence": 0.5242,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "noul-true": {+ "question": {+ "t": "noul",+ "ins": "i"+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.3,+ 1.9+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "noul",+ "noul": 0.832,+ "confidence": 0.832,+ "answer_confidence": 0.832,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "noul-false": {+ "question": {+ "t": "noul",+ "ins": "i"+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 2.4,+ 0.1+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "noul",+ "noul": 0.0911,+ "confidence": 0.9089,+ "answer_confidence": 0.9089,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "noul-balanced": {+ "question": {+ "t": "noul",+ "ins": "i"+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 1.0,+ 1.0+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "noul",+ "noul": 0.5,+ "confidence": 0.5,+ "answer_confidence": 0.5,+ "action": {+ "act_probability": 0.25+ }+ }+ },+ "noul-labels": {+ "question": {+ "t": "noul",+ "ins": "i",+ "labels": {+ "false": "nope",+ "true": "yep"+ }+ },+ "option_order": null,+ "lang": null,+ "logits": [+ 0.3,+ 1.9+ ],+ "logits_block_width": 5,+ "act": [+ 0.25,+ -0.5+ ],+ "answer": {+ "type": "noul",+ "noul": 0.832,+ "confidence": 0.832,+ "answer_confidence": 0.832,+ "action": {+ "act_probability": 0.25+ }+ }+ }+ }+ }+}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Agent.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Agent.javanew file mode 100644index 00000000..774d9722--- /dev/null+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Agent.java@@ -0,0 +1,152 @@+package com.convaiinnovations.laya;++import ai.onnxruntime.OrtException;+import com.convaiinnovations.laya.config.AgentConfig;+import com.convaiinnovations.laya.decode.Decoder;+import com.convaiinnovations.laya.onnx.LayaSession;+import com.convaiinnovations.laya.sequence.Collator;+import com.convaiinnovations.laya.sequence.SequenceBuilder;+import com.convaiinnovations.laya.tokenizer.Tokenizer;+import java.io.IOException;+import java.nio.file.Path;+import java.util.ArrayList;+import java.util.Collections;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;++/**+ * The runtime: ask several questions about one piece of evidence, in one forward pass.+ *+ * <p>laya is a "System 1" decision model -- a bidirectional encoder with a typed head, not a+ * generator. Every question about the same state becomes one row of a single batch, so four+ * questions cost one encode of the shared document rather than four.+ *+ * <pre>{@code+ * try (Agent agent = Agent.open(modelDir, graphDir)) {+ * Map<String, Question> questions = new LinkedHashMap<>();+ * questions.put("intent", Question.choice("What does the customer want?",+ * new LinkedHashMap<>(Map.of("refund", "money back"))));+ * Map<String, Answer> answers = agent.predict(email, questions, "en");+ * }+ * }</pre>+ *+ * <p>Pass questions in a {@link LinkedHashMap}: the answers come back keyed by question id, but a+ * choice's options are positional, so iteration order is part of what is asked.+ */+public final class Agent implements AutoCloseable {++ private final Tokenizer tokenizer;+ private final AgentConfig config;+ private final LayaSession session;+ private final int padId;++ private Agent(Tokenizer tokenizer, AgentConfig config, LayaSession session, int padId) {+ this.tokenizer = tokenizer;+ this.config = config;+ this.session = session;+ this.padId = padId;+ }++ /**+ * Opens a checkpoint.+ *+ * @param modelDirectory holds {@code rl_agent_config.json} and {@code tokenizer/}+ * @param graphDirectory holds {@code laya.onnx}, or {@code encoder.onnx} and {@code head.onnx}+ */+ public static Agent open(Path modelDirectory, Path graphDirectory)+ throws IOException, OrtException {+ return open(modelDirectory, graphDirectory, 1);+ }++ /** Opens a checkpoint with an explicit intra-op thread count. */+ public static Agent open(Path modelDirectory, Path graphDirectory, int threads)+ throws IOException, OrtException {+ Tokenizer tokenizer = Tokenizer.fromModelDirectory(modelDirectory);+ AgentConfig config = AgentConfig.fromModelDirectory(modelDirectory);+ LayaSession session = LayaSession.open(graphDirectory, threads);+ // Padding is masked out of attention but still embedded, so it has to be a real id.+ int padId = tokenizer.padId().orElseGet(+ () -> tokenizer.sepId().orElseThrow(() -> new IllegalStateException(+ "this checkpoint names neither a pad_token nor a sep_token, so a batch "+ + "of more than one question cannot be padded")));+ return new Agent(tokenizer, config, session, padId);+ }++ /** The checkpoint's tokenizer, for callers that want to measure a state's token cost. */+ public Tokenizer tokenizer() {+ return tokenizer;+ }++ /** The checkpoint's budgets and temperatures. */+ public AgentConfig config() {+ return config;+ }++ /** Asks every question about {@code state} in one forward pass, with no language override. */+ public Map<String, Answer> predict(Object state, Map<String, Question> questions)+ throws OrtException {+ return predict(state, questions, null);+ }++ /**+ * Asks every question about {@code state} in one forward pass.+ *+ * @param language a BCP-47-ish tag whose prefix may select a temperature override, or null+ * @return the answers, keyed by question id, in the order the questions were given+ */+ public Map<String, Answer> predict(Object state, Map<String, Question> questions,+ String language) throws OrtException {+ if (questions.isEmpty()) {+ return Map.of();+ }+ // The state is serialised and tokenized ONCE and reused for every question, which is what+ // `build_sequence`'s `state_ids` parameter exists for: the alternative re-tokenizes the+ // same document per question, and a document is usually far longer than a question.+ int[] stateIds = tokenizer.encode(+ SequenceBuilder.serializeState(state).replace(maskToken(), " "), -1);++ List<String> ids = new ArrayList<>(questions.keySet());+ List<Collator.Item> items = new ArrayList<>(ids.size());+ List<SequenceBuilder.Sequence> built = new ArrayList<>(ids.size());+ for (String id : ids) {+ Question question = questions.get(id);+ SequenceBuilder.Sequence sequence = SequenceBuilder.build(+ tokenizer, state, question, config.maxLen(), config.headMaxLen(),+ null, false, stateIds);+ built.add(sequence);+ items.add(new Collator.Item(sequence.ids(), sequence.markers(),+ question.type().code()));+ }++ Collator.Batch batch = Collator.collate(items, padId);+ LayaSession.Output output = session.run(batch);++ Map<String, Answer> answers = new LinkedHashMap<>();+ for (int row = 0; row < ids.size(); row++) {+ String id = ids.get(row);+ // The FILTERED marker count: a head that overran `max_len` loses markers, and the+ // answer must be derived over the options that actually survived into the sequence.+ int optionCount = built.get(row).markers().length;+ float[] actionProbabilities =+ Decoder.actionProbabilities(output.actLogits()[row]);+ answers.put(id, Decoder.decode(questions.get(id), output.logits()[row], optionCount,+ actionProbabilities, null, language, config));+ }+ return Collections.unmodifiableMap(answers);+ }++ private String maskToken() {+ String mask = tokenizer.maskToken();+ if (mask == null) {+ throw new IllegalStateException(+ "this checkpoint's tokenizer_config.json names no mask_token");+ }+ return mask;+ }++ @Override+ public void close() throws OrtException {+ session.close();+ }+}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Answer.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Answer.javanew file mode 100644index 00000000..df4d901b--- /dev/null+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Answer.java@@ -0,0 +1,68 @@+package com.convaiinnovations.laya;++import java.util.Map;++/**+ * One typed answer. Sealed, because the three shapes are not interchangeable and a caller should+ * not have to test a string to find out which fields are present.+ *+ * <p><b>Two confidences, deliberately.</b> {@link #answerConfidence()} is the probability mass on+ * the reported answer -- {@code max(p)} -- and it is the quantity temperature scaling fits and+ * every calibration figure in laya is computed on. {@link #confidence()} means something different+ * per type: normalised entropy for a choice or a score, and {@code max(p, 1-p)} for a noul. They+ * are on different scales and carry different guarantees, so <b>they must not be compared against+ * the same threshold</b>.+ *+ * <p>The calibration guarantee on {@code answerConfidence} is conditional and the condition is not+ * met by default: it holds only after the temperatures have been fitted and validated on held-out+ * data for that checkpoint and option count. The shipped checkpoints are over-confident.+ *+ * <p>Every number is rounded to four decimals, as Python rounds them.+ */+public sealed interface Answer {++ /** The wire name of this answer's type: {@code choice}, {@code score} or {@code noul}. */+ String type();++ /** The calibrated confidence: the probability mass on the answer being reported. */+ double answerConfidence();++ /** The per-type confidence. See the class note: not comparable across types. */+ double confidence();++ /** The head's second output: the probability that this decision should be escalated. */+ double actProbability();++ /** A pick among labelled options, with the probability of each. */+ record Choice(String choice, Map<String, Double> probabilities, double confidence,+ double answerConfidence, double actProbability) implements Answer {+ @Override+ public String type() {+ return "choice";+ }+ }++ /**+ * A rating on an ordered scale.+ *+ * @param score the expected value over level indices, not the most likely level+ * @param legend each level index mapped to the text the model was shown for it+ */+ record Score(double score, Map<String, String> legend, Map<String, Double> probabilities,+ double confidence, double answerConfidence, double actProbability)+ implements Answer {+ @Override+ public String type() {+ return "score";+ }+ }++ /** Whether a statement holds: {@code noul} is the probability that it does. */+ record Noul(double noul, double confidence, double answerConfidence, double actProbability)+ implements Answer {+ @Override+ public String type() {+ return "noul";+ }+ }+}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Question.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Question.javaindex 0c448118..be8980a2 100644--- a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Question.java+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/Question.java@@ -183,6 +183,29 @@ public final class Question {}}+ /**+ * A score question's legend: each level index mapped to the text the model was shown.+ *+ * <p>{@code renderCriterion} rather than a plain {@code toString}: a structured level comes+ * back as the same JSON text the model read, where stringifying it would return a Java object's+ * {@code toString}. A numeric scale passed as {@code [1, 2, 3]} must come back as+ * {@code {"0": "1", "1": "2", "2": "3"}}, so the answer's shape does not depend on what the+ * caller happened to pass.+ *+ * @throws IllegalStateException for a choice or noul question, which have no legend+ */+ public Map<String, String> legend() {+ if (type != Type.SCORE) {+ throw new IllegalStateException("only a score question has a legend, not " + type);+ }+ Map<String, String> out = new LinkedHashMap<>();+ for (int i = 0; i < criteriaList.size(); i++) {+ out.put(Integer.toString(i), renderCriterion(criteriaList.get(i)));+ }+ // Level order, so `Map.copyOf` is not an option: its iteration order is unspecified.+ return java.util.Collections.unmodifiableMap(out);+ }+/*** One criterion as text: a string passes through, anything else becomes compact JSON.*diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/config/AgentConfig.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/config/AgentConfig.javanew file mode 100644index 00000000..5bd050a4--- /dev/null+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/config/AgentConfig.java@@ -0,0 +1,254 @@+package com.convaiinnovations.laya.config;++import com.convaiinnovations.laya.Question;+import com.convaiinnovations.laya.json.Json;+import java.io.BufferedReader;+import java.io.IOException;+import java.io.Reader;+import java.io.StringReader;+import java.nio.charset.StandardCharsets;+import java.nio.file.Files;+import java.nio.file.Path;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Locale;+import java.util.Map;++/**+ * A checkpoint's {@code rl_agent_config.json}: sequence budgets and the fitted temperatures.+ *+ * <p>These must be read from the checkpoint and never defaulted. The two shipped checkpoints+ * disagree on every one of them: english is {@code max_len 512 / head_max_len 192} with fitted+ * per-bucket temperatures, multilingual is {@code 1024/256} with none at all. A port that+ * hard-coded the library's own defaults would halve a multilingual state's budget.+ */+public final class AgentConfig {++ /**+ * A fitted temperature below 1 sharpens logits instead of softening them, and is refused.+ *+ * <p>Not a hypothetical: the english checkpoint ships {@code choice:11+} at <b>0.1006</b>, a+ * ~10x sharpener that publishes a 0.24 top probability as 0.99. A caller gating on confidence+ * would be told a coin flip is a certainty, so the runtime confines every temperature to+ * {@code [0.5, 5.0]}.+ */+ public static final double TEMP_MIN = 0.5;+ public static final double TEMP_MAX = 5.0;++ /** One language's override table. */+ public record LangTemperatures(double[] temperature, Map<String, Double> byOptions) {+ }++ private final int maxLen;+ private final int headMaxLen;+ private final double[] temperature;+ private final Map<String, Double> temperatureByOptions;+ private final Map<String, LangTemperatures> langTemperatures;+ private final String encoder;++ private AgentConfig(int maxLen, int headMaxLen, double[] temperature,+ Map<String, Double> temperatureByOptions,+ Map<String, LangTemperatures> langTemperatures, String encoder) {+ this.maxLen = maxLen;+ this.headMaxLen = headMaxLen;+ this.temperature = temperature;+ this.temperatureByOptions = temperatureByOptions;+ this.langTemperatures = langTemperatures;+ this.encoder = encoder;+ }++ /** Loads {@code <modelDir>/rl_agent_config.json}. */+ public static AgentConfig fromModelDirectory(Path modelDirectory) throws IOException {+ try (Reader reader = new BufferedReader(Files.newBufferedReader(+ modelDirectory.resolve("rl_agent_config.json"), StandardCharsets.UTF_8))) {+ return fromReader(reader);+ }+ }++ /** Loads a config document from a reader. */+ public static AgentConfig fromReader(Reader reader) throws IOException {+ Object parsed = Json.parse(reader);+ if (!(parsed instanceof Map)) {+ throw new Json.JsonException("an rl_agent_config.json must be a JSON object");+ }+ @SuppressWarnings("unchecked")+ Map<String, Object> document = (Map<String, Object>) parsed;+ return of(document);+ }++ /** Loads a config document from a string. For tests. */+ public static AgentConfig fromJson(String json) {+ try {+ return fromReader(new StringReader(json));+ } catch (IOException impossible) {+ throw new IllegalStateException("StringReader cannot fail", impossible);+ }+ }++ private static AgentConfig of(Map<String, Object> document) {+ double[] temperature = {1.0, 1.0, 1.0};+ Object raw = document.get("temperature");+ if (raw instanceof List) {+ List<?> values = (List<?>) raw;+ if (values.size() != 3) {+ throw new Json.JsonException(+ "temperature must be three values, one per question type, got "+ + values.size());+ }+ for (int i = 0; i < 3; i++) {+ temperature[i] = clampTemperature(values.get(i));+ }+ }+ Map<String, Double> byOptions = new LinkedHashMap<>();+ Object tbo = document.get("temperature_by_options");+ if (tbo instanceof Map) {+ for (Map.Entry<?, ?> entry : ((Map<?, ?>) tbo).entrySet()) {+ byOptions.put(String.valueOf(entry.getKey()), clampTemperature(entry.getValue()));+ }+ }+ Map<String, LangTemperatures> languages =+ resolveLangTemperatures(document.get("lang_temperatures"), temperature);+ return new AgentConfig(+ Json.integer(document, "max_len", 512),+ Json.integer(document, "head_max_len", 192),+ temperature, Map.copyOf(byOptions), languages,+ Json.string(document, "encoder", null));+ }++ /**+ * Parses the {@code lang_temperatures} option into {@code language -> table}.+ *+ * <p>A {@code null} entry or a {@code null} temperature both mean "inherit the checkpoint's+ * own", which is how the Python resolver and the {@code laya-ts} port read the same option.+ * Keys are normalised to the part before a {@code -}, lower-cased, so {@code zh-Hans} and+ * {@code ZH} reach the same table.+ */+ public static Map<String, LangTemperatures> resolveLangTemperatures(+ Object raw, double[] baseTemperature) {+ Map<String, LangTemperatures> resolved = new LinkedHashMap<>();+ if (!(raw instanceof Map)) {+ return Map.copyOf(resolved);+ }+ for (Map.Entry<?, ?> entry : ((Map<?, ?>) raw).entrySet()) {+ if (!(entry.getKey() instanceof String)) {+ throw new IllegalArgumentException(+ "language override keys must be strings, got " + entry.getKey());+ }+ String language = normaliseLanguage((String) entry.getKey());+ Object value = entry.getValue();+ Map<?, ?> table = value == null ? Map.of()+ : value instanceof Map ? (Map<?, ?>) value : null;+ if (table == null) {+ throw new IllegalArgumentException(+ "language override " + entry.getKey() + " must be a mapping, got "+ + value.getClass().getSimpleName());+ }+ Object temperatureRaw = table.get("temperature");+ double[] temperature = new double[3];+ if (temperatureRaw == null) {+ System.arraycopy(baseTemperature, 0, temperature, 0, 3);+ } else if (temperatureRaw instanceof List && ((List<?>) temperatureRaw).size() == 3) {+ List<?> values = (List<?>) temperatureRaw;+ for (int i = 0; i < 3; i++) {+ temperature[i] = clampTemperature(values.get(i));+ }+ } else {+ throw new IllegalArgumentException(+ "language override " + entry.getKey()+ + " temperature must be a list of 3 values, got " + temperatureRaw);+ }+ Map<String, Double> byOptions = new LinkedHashMap<>();+ Object tbo = table.get("temperature_by_options");+ if (tbo instanceof Map) {+ for (Map.Entry<?, ?> option : ((Map<?, ?>) tbo).entrySet()) {+ byOptions.put(String.valueOf(option.getKey()),+ clampTemperature(option.getValue()));+ }+ } else if (tbo != null) {+ throw new IllegalArgumentException(+ "language override " + entry.getKey()+ + " temperature_by_options must be a mapping");+ }+ resolved.put(language, new LangTemperatures(temperature, Map.copyOf(byOptions)));+ }+ return Map.copyOf(resolved);+ }++ /**+ * A usable temperature: confined to {@code [TEMP_MIN, TEMP_MAX]}, falling back to 1.0.+ *+ * <p>A boolean is not a number either. {@code true}/{@code false} used to float to 1.0/0.0 in+ * Python and read as fitted and sharpening temperatures, so both are refused here.+ */+ public static double clampTemperature(Object value) {+ if (value instanceof Boolean || !(value instanceof Number)) {+ return 1.0;+ }+ double t = ((Number) value).doubleValue();+ if (Double.isNaN(t) || Double.isInfinite(t)) {+ return 1.0;+ }+ return Math.min(TEMP_MAX, Math.max(TEMP_MIN, t));+ }++ /** The bucket a question of this type and option count keys its temperature by. */+ public static String temperatureBucket(Question.Type type, int options) {+ String size = options <= 2 ? "2" : options <= 5 ? "3-5" : options <= 10 ? "6-10" : "11+";+ return type.wireName() + ":" + size;+ }++ /**+ * The temperature to divide a question's logits by.+ *+ * <p>The bucket table wins over the per-type default, and a language override wins over both --+ * but only when that language has a table, and only by its prefix before a {@code -}.+ */+ public double temperatureFor(Question.Type type, int options, String language) {+ String bucket = temperatureBucket(type, options);+ Double bucketed = temperatureByOptions.get(bucket);+ double resolved = bucketed != null ? bucketed : temperature[type.code()];+ if (language != null && !language.isEmpty()) {+ LangTemperatures table = langTemperatures.get(normaliseLanguage(language));+ if (table != null) {+ Double override = table.byOptions().get(bucket);+ resolved = override != null ? override : table.temperature()[type.code()];+ }+ }+ return resolved;+ }++ /** Whether a language override applies, which also suppresses a fitted binning map. */+ public boolean hasLanguageOverride(String language) {+ return language != null && !language.isEmpty()+ && langTemperatures.containsKey(normaliseLanguage(language));+ }++ private static String normaliseLanguage(String language) {+ int dash = language.indexOf('-');+ return (dash < 0 ? language : language.substring(0, dash)).toLowerCase(Locale.ROOT);+ }++ public int maxLen() {+ return maxLen;+ }++ public int headMaxLen() {+ return headMaxLen;+ }++ public double[] temperature() {+ return temperature.clone();+ }++ public Map<String, Double> temperatureByOptions() {+ return temperatureByOptions;+ }++ public Map<String, LangTemperatures> langTemperatures() {+ return langTemperatures;+ }++ public String encoder() {+ return encoder;+ }+}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Decoder.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Decoder.javanew file mode 100644index 00000000..46371d96--- /dev/null+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Decoder.java@@ -0,0 +1,205 @@+package com.convaiinnovations.laya.decode;++import com.convaiinnovations.laya.Answer;+import com.convaiinnovations.laya.Question;+import com.convaiinnovations.laya.config.AgentConfig;+import java.util.Collections;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;++/**+ * Turns a question's raw logit row into a typed answer.+ *+ * <p>The steps, in order, are {@code agent.py:1316}'s: slice the row to the question's option+ * count, divide by the temperature its bucket names, softmax with the maximum subtracted, put the+ * row back into option order, then derive the type's fields and round everything to four decimals.+ *+ * <p><b>Precision.</b> The logits arrive from ONNX as float32 and the softmax is computed here in+ * {@code double}, which is what {@code laya-ts} and {@code laya-dotnet} both do. Python computes it+ * in float32, so the two are not bit-identical: measured over 200,000 random logit rows, the+ * four-decimal probabilities differ on <b>88</b> of them, by at most one unit in the last reported+ * decimal. Matching float32 exactly would mean reproducing NumPy's own float32 {@code exp} -- which+ * differs from rounding a double {@code exp} on 0.65% of values -- and its pairwise summation+ * order, which differs from a sequential sum on 27% of rows. Neither is a contract NumPy publishes,+ * so this port holds the same line the other two SDKs hold rather than a line it cannot keep.+ */+public final class Decoder {++ private Decoder() {+ }++ /**+ * Decodes one question's answer.+ *+ * @param question the question that was asked; its type and labels shape the answer+ * @param logitRow the head's output row, which may be <b>wider</b> than the option count+ * @param optionCount how many options the question had markers for; the row is sliced to it+ * @param actProbabilities the head's second output for this row, <b>already softmaxed</b>+ * -- see {@link #actionProbabilities}. Its first element is reported+ * @param optionOrder the order options were presented in, or null for label order+ * @param language the request's language tag, which may select an override table+ * @param config the checkpoint's temperatures+ */+ public static Answer decode(Question question, float[] logitRow, int optionCount,+ float[] actProbabilities, int[] optionOrder, String language,+ AgentConfig config) {+ if (optionCount < 1 || optionCount > logitRow.length) {+ throw new IllegalArgumentException(+ "option count " + optionCount + " does not fit a logit row of "+ + logitRow.length);+ }+ double temperature =+ config.temperatureFor(question.type(), optionCount, language);++ // Sliced to the option count, not the row width: the head's output is padded out to the+ // widest question in the batch, and the padding columns are large finite numbers rather+ // than -inf, so a softmax over the whole row would hand almost all the mass to padding.+ double[] z = new double[optionCount];+ double max = Double.NEGATIVE_INFINITY;+ for (int i = 0; i < optionCount; i++) {+ z[i] = logitRow[i] / temperature;+ max = Math.max(max, z[i]);+ }+ double[] p = new double[optionCount];+ double total = 0.0;+ for (int i = 0; i < optionCount; i++) {+ // The maximum is subtracted first so a logit of 700 does not overflow `exp`.+ p[i] = Math.exp(z[i] - max);+ total += p[i];+ }+ for (int i = 0; i < optionCount; i++) {+ p[i] /= total;+ }+ p = unpermute(p, optionOrder);++ double answerConfidence = Rounding.round4(answerConfidence(p, optionCount));+ double actProbability = Rounding.round4(actProbabilities[0]);++ switch (question.type()) {+ case CHOICE: {+ List<String> labels = question.labels();+ Map<String, Double> probabilities = new LinkedHashMap<>();+ int best = argmax(p);+ for (int i = 0; i < labels.size() && i < p.length; i++) {+ probabilities.put(labels.get(i), Rounding.round4(p[i]));+ }+ // `unmodifiableMap` over a LinkedHashMap, not `Map.copyOf`: the latter returns+ // a map whose iteration order is unspecified, and a choice's probabilities are+ // positional -- they must come back in option order, as Python's dict does.+ return new Answer.Choice(labels.get(best),+ Collections.unmodifiableMap(probabilities),+ Rounding.round4(entropyConfidence(p, optionCount)),+ answerConfidence, actProbability);+ }+ case SCORE: {+ double expected = 0.0;+ for (int i = 0; i < optionCount; i++) {+ expected += i * p[i];+ }+ Map<String, Double> probabilities = new LinkedHashMap<>();+ for (int i = 0; i < p.length; i++) {+ probabilities.put(Integer.toString(i), Rounding.round4(p[i]));+ }+ return new Answer.Score(Rounding.round4(expected),+ question.legend(), Collections.unmodifiableMap(probabilities),+ Rounding.round4(entropyConfidence(p, optionCount)),+ answerConfidence, actProbability);+ }+ default: {+ double trueProbability = p[1];+ return new Answer.Noul(Rounding.round4(trueProbability),+ Rounding.round4(Math.max(trueProbability, 1.0 - trueProbability)),+ answerConfidence, actProbability);+ }+ }+ }++ /**+ * The action head's output as probabilities: a softmax over one row of {@code act_logits}.+ *+ * <p>Separate from {@link #decode} because the runtime softmaxes the whole {@code act_logits}+ * block row-wise before decoding, and the decoder is given the result. Reporting the raw logit+ * instead would publish an unbounded real number as a probability -- it happens to look+ * plausible for a well-trained head, which is what makes it the wrong kind of bug.+ */+ public static float[] actionProbabilities(float[] actLogits) {+ int width = Math.max(2, actLogits.length);+ double max = Double.NEGATIVE_INFINITY;+ for (int i = 0; i < width && i < actLogits.length; i++) {+ max = Math.max(max, actLogits[i]);+ }+ double[] exponentials = new double[width];+ double total = 0.0;+ for (int i = 0; i < width; i++) {+ double value = i < actLogits.length ? actLogits[i] : 0.0;+ exponentials[i] = Math.exp(value - max);+ total += exponentials[i];+ }+ float[] out = new float[width];+ for (int i = 0; i < width; i++) {+ out[i] = (float) (exponentials[i] / total);+ }+ return out;+ }++ /**+ * Puts a slot-ordered row back into the caller's option order.+ *+ * <p>A sequence puts option {@code order[slot]} in {@code slot}, so the model's row comes back+ * indexed by slot while everything downstream indexes by option -- the labels for a choice, the+ * level index for a score, {@code p[1]} for a noul. Without this inversion the probabilities+ * attach to the wrong options, and nothing about the answer looks wrong.+ *+ * <p>A missing or mismatched order returns the row untouched, which is what leaves the+ * canonical path unaffected.+ */+ static double[] unpermute(double[] p, int[] order) {+ if (order == null || order.length != p.length) {+ return p;+ }+ double[] canonical = new double[p.length];+ for (int slot = 0; slot < order.length; slot++) {+ canonical[order[slot]] = p[slot];+ }+ return canonical;+ }++ /** {@code max(p)}, clipped: the probability mass on the answer being reported. */+ static double answerConfidence(double[] p, int optionCount) {+ if (optionCount < 1) {+ return 1.0;+ }+ double best = p[0];+ for (int i = 1; i < optionCount; i++) {+ best = Math.max(best, p[i]);+ }+ return Math.min(1.0, Math.max(0.0, best));+ }++ /** Normalised Shannon entropy: {@code 1 - H(p) / log(k)}, clipped to {@code [0, 1]}. */+ static double entropyConfidence(double[] p, int optionCount) {+ if (optionCount < 2) {+ return 1.0;+ }+ double entropy = 0.0;+ for (int i = 0; i < optionCount; i++) {+ // Clipped at 1e-12 before the log, so a zero probability contributes zero rather than+ // negative infinity.+ entropy -= p[i] * Math.log(Math.min(1.0, Math.max(1e-12, p[i])));+ }+ double normalised = 1.0 - entropy / Math.log(optionCount);+ return Math.min(1.0, Math.max(0.0, normalised));+ }++ /** The first index holding the maximum, matching {@code numpy.argmax}'s tie-breaking. */+ static int argmax(double[] p) {+ int best = 0;+ for (int i = 1; i < p.length; i++) {+ if (p[i] > p[best]) {+ best = i;+ }+ }+ return best;+ }+}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Rounding.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Rounding.javanew file mode 100644index 00000000..884e8397--- /dev/null+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/decode/Rounding.java@@ -0,0 +1,51 @@+package com.convaiinnovations.laya.decode;++import java.math.BigDecimal;+import java.math.RoundingMode;++/**+ * The four-decimal rounding every number in an answer goes through.+ *+ * <p>laya rounds with Python's {@code round(value, 4)}, which rounds half to <b>even</b> on the+ * exact binary value of the double. Two spellings were considered:+ *+ * <ul>+ * <li>{@code Math.round(v * 1e4) / 1e4}, which is what {@code laya-ts} uses+ * ({@code agent.ts:380}). It rounds half <b>up</b>, and the multiply by {@code 1e4} is itself+ * inexact, so it can disagree with Python in both directions.+ * <li>{@link BigDecimal} at scale 4 with {@link RoundingMode#HALF_EVEN}, which is Python's rule+ * applied to Python's operand -- {@code new BigDecimal(double)} takes the exact binary value,+ * not a decimal approximation of it.+ * </ul>+ *+ * <p>The second is used, and it is a deliberate departure from {@code laya-ts}: the contract this+ * port is held to is parity with the Python reference, and matching it exactly removes a+ * "happens not to diverge" caveat rather than inheriting one. Measured over 209,272 values --+ * probabilities, real softmax rows, and exact {@code .00005} steps engineered to land on a halfway+ * case -- against {@code round(v, 4)} from CPython 3.12.+ */+public final class Rounding {++ private Rounding() {+ }++ /** {@code value} rounded to four decimals the way Python's {@code round(value, 4)} rounds. */+ public static double round4(double value) {+ if (Double.isNaN(value) || Double.isInfinite(value)) {+ // Python returns NaN / inf unchanged rather than raising.+ return value;+ }+ return new BigDecimal(value).setScale(4, RoundingMode.HALF_EVEN).doubleValue();+ }++ /**+ * The {@code laya-ts} spelling, kept only so the two can be compared in a test.+ *+ * <p>Not used by the runtime. It exists so the claim that the two agree on softmax output, and+ * disagree on engineered halfway cases, is a measurement in this repository rather than an+ * assertion in a comment.+ */+ public static double round4HalfUp(double value) {+ return Math.round(value * 1e4) / 1e4;+ }+}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/onnx/LayaSession.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/onnx/LayaSession.javanew file mode 100644index 00000000..2189b682--- /dev/null+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/onnx/LayaSession.java@@ -0,0 +1,148 @@+package com.convaiinnovations.laya.onnx;++import ai.onnxruntime.OnnxTensor;+import ai.onnxruntime.OrtEnvironment;+import ai.onnxruntime.OrtException;+import ai.onnxruntime.OrtSession;+import com.convaiinnovations.laya.sequence.Collator;+import java.io.IOException;+import java.nio.file.Files;+import java.nio.file.Path;+import java.util.ArrayList;+import java.util.HashMap;+import java.util.List;+import java.util.Map;++/**+ * The ONNX graph, in either of the two forms laya exports.+ *+ * <ul>+ * <li><b>Fused</b> {@code laya.onnx}: one graph taking {@code input_ids}, {@code attention_mask},+ * {@code marker_pos}, {@code marker_mask}, {@code qtype} and returning {@code logits} and+ * {@code act_logits}. This is what {@code ONNXAgent} loads by default.+ * <li><b>Split</b> {@code encoder.onnx} + {@code head.onnx}: the encoder returns+ * {@code last_hidden_state}, which the head consumes with the marker inputs.+ * </ul>+ *+ * <p>Both are supported because both exist in the wild -- {@code laya-dotnet} carries the same pair+ * of paths -- and which one a directory holds is discoverable, so there is no reason to make the+ * caller say. Instances are not thread-safe for concurrent {@code run} calls on one session unless+ * ONNX Runtime is configured for it; hold one per worker or serialise access.+ */+public final class LayaSession implements AutoCloseable {++ /** One batch's head outputs. */+ public record Output(float[][] logits, float[][] actLogits) {+ }++ private final OrtEnvironment environment;+ private final OrtSession fused;+ private final OrtSession encoder;+ private final OrtSession head;++ private LayaSession(OrtEnvironment environment, OrtSession fused, OrtSession encoder,+ OrtSession head) {+ this.environment = environment;+ this.fused = fused;+ this.encoder = encoder;+ this.head = head;+ }++ /**+ * Opens whichever graph form {@code directory} holds, preferring the fused one.+ *+ * @param threads intra-op threads; 1 is the right default for a request-per-thread server,+ * where the parallelism is already in the requests+ */+ public static LayaSession open(Path directory, int threads) throws OrtException, IOException {+ OrtEnvironment environment = OrtEnvironment.getEnvironment();+ OrtSession.SessionOptions options = new OrtSession.SessionOptions();+ options.setIntraOpNumThreads(threads);+ Path fusedPath = directory.resolve("laya.onnx");+ if (Files.isRegularFile(fusedPath)) {+ return new LayaSession(environment,+ environment.createSession(fusedPath.toString(), options), null, null);+ }+ Path encoderPath = directory.resolve("encoder.onnx");+ Path headPath = directory.resolve("head.onnx");+ if (Files.isRegularFile(encoderPath) && Files.isRegularFile(headPath)) {+ return new LayaSession(environment, null,+ environment.createSession(encoderPath.toString(), options),+ environment.createSession(headPath.toString(), options));+ }+ throw new IOException(+ "no laya graph in " + directory + ": expected laya.onnx, or encoder.onnx and "+ + "head.onnx");+ }++ /** Whether this session is the fused single-graph form. */+ public boolean isFused() {+ return fused != null;+ }++ /** Runs one collated batch. */+ public Output run(Collator.Batch batch) throws OrtException {+ List<OnnxTensor> owned = new ArrayList<>();+ try {+ OnnxTensor inputIds = track(owned, OnnxTensor.createTensor(environment, batch.inputIds()));+ OnnxTensor attention = track(owned, OnnxTensor.createTensor(environment, batch.attentionMask()));+ OnnxTensor markerPos = track(owned, OnnxTensor.createTensor(environment, batch.markerPos()));+ OnnxTensor markerMask = track(owned, OnnxTensor.createTensor(environment, batch.markerMask()));+ OnnxTensor qtype = track(owned, OnnxTensor.createTensor(environment, batch.qtype()));+ if (fused != null) {+ Map<String, OnnxTensor> inputs = new HashMap<>();+ inputs.put("input_ids", inputIds);+ inputs.put("attention_mask", attention);+ inputs.put("marker_pos", markerPos);+ inputs.put("marker_mask", markerMask);+ inputs.put("qtype", qtype);+ try (OrtSession.Result result = fused.run(inputs, java.util.Set.of("logits", "act_logits"))) {+ return new Output(floats(result, 0), floats(result, 1));+ }+ }+ Map<String, OnnxTensor> encoderInputs = new HashMap<>();+ encoderInputs.put("input_ids", inputIds);+ encoderInputs.put("attention_mask", attention);+ try (OrtSession.Result encoded = encoder.run(encoderInputs,+ java.util.Set.of("last_hidden_state"))) {+ OnnxTensor hidden = (OnnxTensor) encoded.get(0);+ Map<String, OnnxTensor> headInputs = new HashMap<>();+ headInputs.put("hidden_states", hidden);+ headInputs.put("marker_pos", markerPos);+ headInputs.put("marker_mask", markerMask);+ headInputs.put("qtype", qtype);+ headInputs.put("attention_mask", attention);+ try (OrtSession.Result result = head.run(headInputs,+ java.util.Set.of("logits", "act_logits"))) {+ return new Output(floats(result, 0), floats(result, 1));+ }+ }+ } finally {+ for (OnnxTensor tensor : owned) {+ tensor.close();+ }+ }+ }++ private static OnnxTensor track(List<OnnxTensor> owned, OnnxTensor tensor) {+ owned.add(tensor);+ return tensor;+ }++ private static float[][] floats(OrtSession.Result result, int index) throws OrtException {+ return (float[][]) ((OnnxTensor) result.get(index)).getValue();+ }++ @Override+ public void close() throws OrtException {+ if (fused != null) {+ fused.close();+ }+ if (encoder != null) {+ encoder.close();+ }+ if (head != null) {+ head.close();+ }+ }+}diff --git a/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/sequence/Collator.java b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/sequence/Collator.javanew file mode 100644index 00000000..f42e9cb1--- /dev/null+++ b/laya-java/laya-java/src/main/java/com/convaiinnovations/laya/sequence/Collator.java@@ -0,0 +1,63 @@+package com.convaiinnovations.laya.sequence;++import java.util.List;++/**+ * Pads a batch of built sequences into the rectangular tensors the graph takes.+ *+ * <p>One row per question, not per state: a request asking four questions about one document is+ * four rows that share a state. Rows are padded to the longest sequence in the batch and markers to+ * the most options, with {@code attentionMask} and {@code markerMask} marking what is real.+ */+public final class Collator {++ /** The five inputs the graph takes, flattened row-major. */+ public record Batch(long[][] inputIds, long[][] attentionMask, long[][] markerPos,+ boolean[][] markerMask, long[] qtype, int rows, int length, int markers) {+ }++ /** One row to collate: a built sequence and the question type that produced it. */+ public record Item(int[] ids, int[] markers, int qtype) {+ }++ private Collator() {+ }++ /**+ * @param items the rows, in the order their answers will be read back+ * @param padId the tokenizer's padding id. Padding is masked out, but it is still fed to the+ * encoder, so an id outside the vocabulary would be an out-of-range embedding+ * lookup rather than a harmless filler+ */+ public static Batch collate(List<Item> items, int padId) {+ if (items.isEmpty()) {+ throw new IllegalArgumentException("nothing to collate");+ }+ int rows = items.size();+ int length = 0;+ int markerWidth = 0;+ for (Item item : items) {+ length = Math.max(length, item.ids().length);+ markerWidth = Math.max(markerWidth, item.markers().length);+ }+ long[][] inputIds = new long[rows][length];+ long[][] attentionMask = new long[rows][length];+ long[][] markerPos = new long[rows][markerWidth];+ boolean[][] markerMask = new boolean[rows][markerWidth];+ long[] qtype = new long[rows];+ for (int r = 0; r < rows; r++) {+ Item item = items.get(r);+ for (int i = 0; i < length; i++) {+ inputIds[r][i] = i < item.ids().length ? item.ids()[i] : padId;+ attentionMask[r][i] = i < item.ids().length ? 1L : 0L;+ }+ for (int i = 0; i < item.markers().length; i++) {+ markerPos[r][i] = item.markers()[i];+ markerMask[r][i] = true;+ }+ qtype[r] = item.qtype();+ }+ return new Batch(inputIds, attentionMask, markerPos, markerMask, qtype,+ rows, length, markerWidth);+ }+}diff --git a/laya-java/scripts/gen_fixtures.py b/laya-java/scripts/gen_fixtures.pyindex f1429e04..167a0d9f 100644--- a/laya-java/scripts/gen_fixtures.py+++ b/laya-java/scripts/gen_fixtures.py@@ -380,11 +380,136 @@ def sequences():return out+def decode_answers():+ """`Agent._decode_answers` output for hostile logit rows, per checkpoint.++ Driven through the REAL method rather than a transcription of it: the expectations have to come+ from the shipped decoder, including its rounding, its argmax tie-breaking and its clamps.++ Logits are synthesised rather than taken from a forward pass so the case list can aim at the+ branches -- every temperature bucket, a tie at the top, a row whose softmax is exactly uniform,+ magnitudes that would overflow a naive `exp`, and the language override -- and so the fixture+ does not need a 1.2 GB graph to regenerate.+ """+ import math+ import os++ import numpy as np++ from laya.agent import Agent, QTYPES+ from laya.common import clamp_temperature, resolve_lang_temperatures++ rig = os.environ.get("LAYA_FIXTURE_CHECKPOINTS",+ "/Users/nombauser/Documents/nomba-dev/laya-e2e/checkpoints")++ def row(values):+ return np.asarray(values, dtype=np.float32)++ # (id, question, option_order, lang, logits)+ cases = [+ ("choice-2", {"t": "choice", "ins": "i", "crit": {"a": "x", "b": "y"}},+ None, None, [2.0, 1.0]),+ ("choice-2-tied", {"t": "choice", "ins": "i", "crit": {"a": "x", "b": "y"}},+ None, None, [1.0, 1.0]),+ ("choice-4", {"t": "choice", "ins": "i",+ "crit": {"a": "1", "b": "2", "c": "3", "d": "4"}},+ None, None, [0.5, 2.5, -1.0, 2.5]),+ ("choice-4-permuted", {"t": "choice", "ins": "i",+ "crit": {"a": "1", "b": "2", "c": "3", "d": "4"}},+ [2, 0, 3, 1], None, [0.5, 2.5, -1.0, 2.5]),+ ("choice-7", {"t": "choice", "ins": "i",+ "crit": {("o%d" % i): str(i) for i in range(7)}},+ None, None, [0.1 * i for i in range(7)]),+ ("choice-12", {"t": "choice", "ins": "i",+ "crit": {("o%d" % i): str(i) for i in range(12)}},+ None, None, [(-1.0) ** i * i * 0.3 for i in range(12)]),+ ("choice-20-uniform", {"t": "choice", "ins": "i",+ "crit": {("o%d" % i): str(i) for i in range(20)}},+ None, None, [0.0] * 20),+ ("choice-huge-magnitude", {"t": "choice", "ins": "i",+ "crit": {"a": "1", "b": "2", "c": "3"}},+ None, None, [700.0, 699.0, -700.0]),+ ("choice-tiny-spread", {"t": "choice", "ins": "i", "crit": {"a": "1", "b": "2"}},+ None, None, [1.0, 1.0 + 1e-7]),+ ("choice-lang-override", {"t": "choice", "ins": "i", "crit": {"a": "1", "b": "2"}},+ None, "zh-Hans", [2.0, 1.0]),+ ("choice-lang-override-prefix", {"t": "choice", "ins": "i", "crit": {"a": "1", "b": "2"}},+ None, "ZH", [2.0, 1.0]),+ ("choice-lang-unknown", {"t": "choice", "ins": "i", "crit": {"a": "1", "b": "2"}},+ None, "xx", [2.0, 1.0]),+ ("score-3", {"t": "score", "ins": "i", "crit": ["bad", "ok", "good"]},+ None, None, [0.2, 1.4, 0.9]),+ ("score-6", {"t": "score", "ins": "i", "crit": [str(i) for i in range(6)]},+ None, None, [0.0, 1.0, 2.0, 1.0, 0.0, -1.0]),+ ("score-structured-legend", {"t": "score", "ins": "i", "crit": [{"d": 1}, "ok", 3]},+ None, None, [0.4, 0.4, 0.4]),+ ("score-permuted", {"t": "score", "ins": "i", "crit": ["bad", "ok", "good"]},+ [1, 2, 0], None, [0.2, 1.4, 0.9]),+ ("noul-true", {"t": "noul", "ins": "i"}, None, None, [0.3, 1.9]),+ ("noul-false", {"t": "noul", "ins": "i"}, None, None, [2.4, 0.1]),+ ("noul-balanced", {"t": "noul", "ins": "i"}, None, None, [1.0, 1.0]),+ ("noul-labels", {"t": "noul", "ins": "i",+ "labels": {"false": "nope", "true": "yep"}}, None, None, [0.3, 1.9]),+ ]++ out = {}+ for name in ("english", "multilingual"):+ path = os.path.join(rig, name, "rl_agent_config.json")+ if not os.path.isfile(path):+ out[name] = {"skipped": "no rl_agent_config.json for %s" % name}+ continue+ cfg = json.load(open(path, encoding="utf-8"))+ # Clamped exactly as the runtime clamps: `choice:11+` ships at 0.1006 on one checkpoint,+ # a ~10x sharpener, and a port that applied the raw value would publish a coin flip as a+ # certainty. The fixture therefore records the CLAMPED table the decoder actually uses.+ temperature = [clamp_temperature(t) for t in cfg.get("temperature", [1.0, 1.0, 1.0])]+ by_options = {k: clamp_temperature(v)+ for k, v in (cfg.get("temperature_by_options") or {}).items()}+ # Neither shipped checkpoint sets `lang_temperatures`, so the override branch would never+ # be exercised by their configs. One is supplied here, through the same resolver the+ # runtime uses, so a port cannot skip the branch and still pass.+ lang_raw = {"zh": {"temperature": [2.0, 1.5, 3.0],+ "temperature_by_options": {"choice:2": 2.5}}}+ lang_temperatures = resolve_lang_temperatures(lang_raw, temperature)++ shim = Agent.__new__(Agent)+ shim.temperature = temperature+ shim.temperature_by_options = by_options+ shim.lang_temperatures = lang_temperatures+ shim.binning_map = None++ recorded = {}+ for cid, question, option_order, lang, logits in cases:+ k = len(logits)+ q = dict(question)+ if option_order is not None:+ q["option_order"] = option_order+ items = [{"markers": list(range(k))}]+ # a wider logits block than k, so a port that forgets to slice to k columns is caught+ block_width = k + 3+ padded = row(list(logits) + [99.0] * 3).reshape(1, block_width)+ act = row([0.25, -0.5]).reshape(1, 2)+ answers = Agent._decode_answers(shim, padded, act, items, [cid], {cid: q}, 0, lang)+ recorded[cid] = {+ "question": question, "option_order": option_order, "lang": lang,+ "logits": [float(x) for x in logits], "logits_block_width": block_width,+ "act": [0.25, -0.5], "answer": answers[cid],+ }+ out[name] = {+ "config": {"temperature": temperature, "temperature_by_options": by_options,+ "lang_temperatures_raw": lang_raw},+ "qtypes": QTYPES,+ "cases": recorded,+ }+ return out++FAMILIES = {"lang_tables.json": lang_tables,"presets.json": presets,"tokenizer_ids.json": tokenizer_ids,"sequences.json": sequences,+ "decode.json": decode_answers,}