mixle.enumeration.rescore module¶
Speculative enumeration: build the index with a low-cost DRAFT model, score results with the TARGET.
Speculative decoding’s economics applied to enumeration. Building any autoregressive index costs one
forward per live prefix – prohibitive when the model is a large transformer. But the ordering work
(which sequences are near a rank/threshold) tolerates approximation, while the scores must be the real
model’s. So: let a low-cost draft (an n-gram, a distilled student, a quantized twin) pay for the tree or
envelope build, and touch the target only for the sequences a query actually returns – one batched
teacher-forcing forward for all of them (AutoregressiveEnumerable.score_sequences()).
Contract: every returned log_prob is the target’s exact score. The order is
draft-approximate, repaired locally by window reranking: top_k(k) / slice pull
k + rerank_window draft-ordered candidates, rescore them all with the target in one batch, and sort by
target score. That is exact whenever no unpulled sequence out-scores the returned ones.
Soundness (the certified verdict on RescoreResult): the draft index’s rank order is only
guaranteed up to its own quantized fine-bucket width (SeekIndex
and AREnvelopeIndex are both built over a
Quantizer) – so the last PULLED candidate’s own exact
draft score does NOT bound every unpulled one: an unpulled candidate can share the edge item’s quantized
bucket, or even score higher, while still being ranked later (MXR-080-0233). The sound bound instead
comes from the draft index’s OWN quantizer: every candidate ranked at or after the pulled window has a
fine bucket at least as deep as the edge item’s, so that bucket’s own best-case (least negative) log-prob
edge – strictly looser than the edge item’s exact score, never tighter – is a certified upper bound on
any unpulled candidate’s draft score. certified is True only when the worst target score in the
returned set provably clears bound + assumed_gap; False when certification was requested but
could not be established (no sound bound available – e.g. the draft index exposes no quantizer – or
the bound did not clear the gap); None when no assumed_gap was supplied at all, so certification
was never attempted. The observed gap is always reported as a diagnostic, certified or not.
- class RescoredIndex(draft_index, target, *, rerank_window=64, assumed_gap=None)[source]
Bases:
objectDraft-ordered, target-scored enumeration with window reranking.
- Parameters:
draft_index (Any) – any index with
unrank(i) -> (sequence, draft_log_prob)– aSeekIndexover a low-costAutoregressiveEnumerable, anAREnvelopeIndex, or anything equivalent. Sound certification additionally requires a.quantizerattribute exposing theQuantizerthe index was built with – both accepted index types have one; without it, queries still work but never certify.target (Any) – the expensive model – an
AutoregressiveEnumerable(itsscore_sequences()batch scorer is used) or a bare callable[seqs] -> log_probs.rerank_window (int) – extra draft candidates pulled around a query and reranked by target score. Larger = more robust to draft/target disagreement, one batched forward either way. Must be a non-negative integer – a negative window used to silently shrink or empty the pulled set while still reporting a falsely certified result (MXR-080-0233).
assumed_gap (float | None) – optional global bound on
|target_lp - draft_lp|(nats). Must be finite and non-negative. When supplied, results carry a genuinecertifiedverdict (seeRescoreResult); otherwisecertifiedisNoneand the observedgapis reported as a diagnostic only.
- top_k(k)[source]
The
kbest sequences by TARGET score among thek + rerank_windowdraft head.Returns a
RescoreResult– target-exact scores, draft+window-approximate completeness (see the class/module docstrings for exactly whatcertifiedproves).- Parameters:
k (int)
- Return type:
RescoreResult
- slice(start, k)[source]
Target-reranked
[start, start + k)slice of the pulledstart + k + rerank_windowhead.Same semantics as
top_k(): order within the pulled set is target-exact; the certificate covers whether an unpulled sequence could belong in (or before) the slice.
- class RescoreResult(items, certified, bound, gap)[source]
Bases:
objectOutcome of a
RescoredIndexquery: target-exact items plus an explicit certificate.top_k/sliceare speculative: the returned items are always target-exact and target-ordered, but completeness (whether some unpulled candidate could outscore them) is only provable under anassumed_gapAND a draft index that can supply a sound bound – a caller that needs the proof, not just a plausible answer, must checkcertified is Truerather than trustingitemsalone (MXR-080-0233: a bare boolean bolted onto an unqualified value cannot make that distinction).- Parameters:
- items
(sequence, target_log_prob)pairs, target-exact and sorted by target score.
- certified
Nonewhen noassumed_gapwas supplied to the index – certification was never attempted anditemsis a plausible, uncertified answer.Trueonly whenboundwas established and the returned set’s worst target score provably clears every unpulled candidate (bound + assumed_gap).Falsewhen certification was requested but could not be proven – either the draft index offered no sound bound, or the bound did not clear the gap;itemsis then still the best-effort draft/window answer, never a proof.- Type:
bool | None
- bound
the certified upper bound on any unpulled candidate’s draft log-probability that the certificate was checked against, or
Nonewhen no bound applies – nothing was left unpulled (the draft support was exhausted), noassumed_gapwas supplied, or the draft index could not supply a sound bound.- Type:
float | None
- gap
running max observed
|target_lp - draft_lp|over everything rescored on this index so far – a diagnostic of what the gap has actually been, never a certificate.- Type: