{"abstract":"Top-k probability mass is measured before candidate restriction.","category":"Token sampling filters","checks":6,"contract":"Select top k by descending score and lexical ties, normalize softmax over this restricted set, and retain its shortest prefix reaching p; p<=0 or k<=0 returns empty.","contract_signature":"scores, k, p","evaluation_group":"w3-tsf-top-k-before-top-p","failed_approach":"Using unnormalized exponentials as cumulative mass makes the threshold cease to represent a probability fraction.","family":"w3-token-sampling-filters-top-k-before-top-p","id":"FA-98426","implementations":{"attempt":{"sha256":"35d644e5786b605522468077b405210052e2ecd36410350ef5ecd99cfc844624","source":"\"\"\"Failure Map reference implementation. Python standard library only.\"\"\"\nimport json\nimport math\nN = 1\nobservations = []\ndef solve(scores, k, p):\n    import math\n    ranked=sorted(scores,key=lambda t:(-scores[t],t))[:max(0,k)]; ex=[math.exp(scores[t]) for t in ranked]; out=[]; total=0\n    for t,v in zip(ranked,ex):\n        total+=v; out.append(t)\n        if total>=p:break\n    return out\ndef check(label, actual, expected):\n    observations.append({\"check\": label, \"actual\": actual, \"expected\": expected, \"passed\": actual == expected})\ncheck('restricted mass',solve({'a':2.0,'b':1.0,'c':0.0},2,.68),['a'])\ncheck('full retained mass',solve({'a':2.0,'b':1.0,'c':0.0},2,.8),['a','b'])\ncheck('one candidate top k',solve({'a':N,'b':0.0},1,.5),['a'])\ncheck('zero k',solve({'a':N},0,.5),[])\ncheck('empty',solve({},2,.5),[])\ncheck('zero target',solve({'a':N,'b':0.0},2,0),[])\nprint(json.dumps({\"observations\": observations, \"passed\": all(x[\"passed\"] for x in observations)}, ensure_ascii=False))\nraise SystemExit(0 if all(x[\"passed\"] for x in observations) else 1)\n"},"broken":{"sha256":"ea343be9f1fb68d3a051275845d6fa9eabdb157b845e03dc108c5fc953d7e696","source":"\"\"\"Failure Map reference implementation. Python standard library only.\"\"\"\nimport json\nimport math\nN = 1\nobservations = []\ndef solve(scores, k, p):\n    import math\n    ranked=sorted(scores,key=lambda t:(-scores[t],t)); m=max(scores.values(),default=0)\n    ex={t:math.exp(v-m) for t,v in scores.items()}; z=sum(ex.values()); out=[]; total=0\n    for t in ranked:\n        total+=ex[t]/z; out.append(t)\n        if total>=p:break\n    return out[:max(0,k)]\ndef check(label, actual, expected):\n    observations.append({\"check\": label, \"actual\": actual, \"expected\": expected, \"passed\": actual == expected})\ncheck('restricted mass',solve({'a':2.0,'b':1.0,'c':0.0},2,.68),['a'])\ncheck('full retained mass',solve({'a':2.0,'b':1.0,'c':0.0},2,.8),['a','b'])\ncheck('one candidate top k',solve({'a':N,'b':0.0},1,.5),['a'])\ncheck('zero k',solve({'a':N},0,.5),[])\ncheck('empty',solve({},2,.5),[])\ncheck('zero target',solve({'a':N,'b':0.0},2,0),[])\nprint(json.dumps({\"observations\": observations, \"passed\": all(x[\"passed\"] for x in observations)}, ensure_ascii=False))\nraise SystemExit(0 if all(x[\"passed\"] for x in observations) else 1)\n"}},"limitations":"These are toy finite-vocabulary contracts with supplied scores, histories, and draws; they do not claim production model quality or compatibility. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.","method":"Deterministic executable model with adversarial boundary fixtures.","provenance":{"created_by":"Failure Map","dependencies":"Python standard library","family":"w3-token-sampling-filters-top-k-before-top-p","generated_at":"2026-09-30T16:48:12.803371+00:00","license":"CC0-1.0","python":"3.12.14","seed":1,"split":"open-access"},"relevance":"A finite, deterministic token-selection contract isolates one decoding operation from model inference and runtime behavior.","root_cause":"The nucleus cumulative mass is calculated over the full vocabulary before the top-k restriction.","sha256":"96cdd7562cf2a434147aee668f1ae5a0ecb2eecb5fd29be899a04364241a85ee","title":"Token sampling: Top-k probability mass is measured before candidate restriction · case 01","variant":1,"variant_policy":"The fixture code references N, supplied as 1 through 5. Fixed boundary checks may repeat. These records share one mechanism and must remain together in evaluation splits.","verified":true,"visibility":"public","verification":{"attempt":{"elapsed_ms":28.125,"exit_code":1,"observations":[{"actual":["a"],"check":"restricted mass","expected":["a"],"passed":true},{"actual":["a"],"check":"full retained mass","expected":["a","b"],"passed":false},{"actual":["a"],"check":"one candidate top k","expected":["a"],"passed":true},{"actual":[],"check":"zero k","expected":[],"passed":true},{"actual":[],"check":"empty","expected":[],"passed":true},{"actual":["a"],"check":"zero target","expected":[],"passed":false}],"passed":false,"stderr":"","stdout":"{\"observations\": [{\"check\": \"restricted mass\", \"actual\": [\"a\"], \"expected\": [\"a\"], \"passed\": true}, {\"check\": \"full retained mass\", \"actual\": [\"a\"], \"expected\": [\"a\", \"b\"], \"passed\": false}, {\"check\": \"one candidate top k\", \"actual\": [\"a\"], \"expected\": [\"a\"], \"passed\": true}, {\"check\": \"zero k\", \"actual\": [], \"expected\": [], \"passed\": true}, {\"check\": \"empty\", \"actual\": [], \"expected\": [], \"passed\": true}, {\"check\": \"zero target\", \"actual\": [\"a\"], \"expected\": [], \"passed\": false}], \"passed\": false}\n"},"broken":{"elapsed_ms":26.111,"exit_code":1,"observations":[{"actual":["a","b"],"check":"restricted mass","expected":["a"],"passed":false},{"actual":["a","b"],"check":"full retained mass","expected":["a","b"],"passed":true},{"actual":["a"],"check":"one candidate top k","expected":["a"],"passed":true},{"actual":[],"check":"zero k","expected":[],"passed":true},{"actual":[],"check":"empty","expected":[],"passed":true},{"actual":["a"],"check":"zero target","expected":[],"passed":false}],"passed":false,"stderr":"","stdout":"{\"observations\": [{\"check\": \"restricted mass\", \"actual\": [\"a\", \"b\"], \"expected\": [\"a\"], \"passed\": false}, {\"check\": \"full retained mass\", \"actual\": [\"a\", \"b\"], \"expected\": [\"a\", \"b\"], \"passed\": true}, {\"check\": \"one candidate top k\", \"actual\": [\"a\"], \"expected\": [\"a\"], \"passed\": true}, {\"check\": \"zero k\", \"actual\": [], \"expected\": [], \"passed\": true}, {\"check\": \"empty\", \"actual\": [], \"expected\": [], \"passed\": true}, {\"check\": \"zero target\", \"actual\": [\"a\"], \"expected\": [], \"passed\": false}], \"passed\": false}\n"}},"member_only":{"stages":["fixed"],"fields":["implementations.fixed","verification.fixed","harness","repair"],"note":"The verified repair, its recorded checks, the repair description, and the scoring harness are available to members."}}