function insertTop(ids, values, id, value, k) { const worst = values[values.length - 1] ?? -Infinity; if (ids.length === k && value <= worst) return; let lo = 0; let hi = ids.length; while (lo < hi) { const mid = (lo + hi) >> 1; if (values[mid] >= value) lo = mid + 1; else hi = mid; } ids.splice(lo, 0, id); values.splice(lo, 0, value); if (ids.length > k) { ids.pop(); values.pop(); } } // One reusable ranker per decode batch. The common path computes raw argmax, // masked argmax and top-16 in a single vocabulary pass. Top-256/full scans are // materialized only when the name/repeat guard actually escalates. export function createGuardedLogitRanker(vocab, neg) { const marks = new Uint32Array(vocab); let stamp = 0; const validFloor = neg / 2; const prepareMask = (request) => { const mode = request.mode === 'allow' || request.mode === 'ban' ? request.mode : 'none'; if (mode === 'none') return mode; stamp = (stamp + 1) >>> 0; if (stamp === 0) { marks.fill(0); stamp = 1; } for (const id of request.ids) { if (id >= 0 && id < vocab) marks[id] = stamp; } return mode; }; const maskedValue = (raw, id, mode) => { const base = Math.fround(raw); const marked = marks[id] === stamp; const blocked = mode === 'allow' ? !marked : mode === 'ban' && marked; return blocked ? Math.fround(base + neg) : base; }; return { rank(view, base, bias, request) { const mode = prepareMask(request); let rawTop = 0; let rawBest = -Infinity; let maskedTop = 0; let maskedBest = -Infinity; const top16Ids = []; const top16Values = []; for (let id = 0; id < vocab; id += 1) { const raw = view[base + id] + bias[id]; if (raw > rawBest) { rawBest = raw; rawTop = id; } const value = maskedValue(raw, id, mode); if (value > maskedBest) { maskedBest = value; maskedTop = id; } if (value > validFloor) insertTop(top16Ids, top16Values, id, value, 16); } const tiers = [{ ids: top16Ids, validCount: top16Ids.length }]; const selectTop = (k) => { const ids = []; const values = []; for (let id = 0; id < vocab; id += 1) { const raw = view[base + id] + bias[id]; const value = maskedValue(raw, id, mode); if (value > validFloor) insertTop(ids, values, id, value, k); } return { ids, validCount: ids.length }; }; const selectAll = () => { const ids = []; const values = new Float32Array(vocab); for (let id = 0; id < vocab; id += 1) { const raw = view[base + id] + bias[id]; const value = maskedValue(raw, id, mode); values[id] = value; if (value > validFloor) ids.push(id); } ids.sort((a, b) => values[b] - values[a] || a - b); return { ids, validCount: ids.length }; }; return { rawTop, maskedTop, getTier(tierIndex) { if (!tiers[tierIndex]) { tiers[tierIndex] = tierIndex === 1 ? selectTop(Math.min(256, vocab)) : selectAll(); } return tiers[tierIndex]; }, }; }, }; }