Files
Strata/include/strata/core/spec_proposal.hpp
T
Keyan Hu f4612b1add spec: STRATA_SPEC_PROB=1 keeps the confidence gate in the proposal law, opt-in (#3416)
Problem (KeyanHu-git, refs #2028, #2591): with STRATA_SPEC_PROB=1 the default
--spec-min-p gate tests the token drawn from the draft head's distribution q, but the
verifier still judged the draft against the unfiltered q and treated a failed gate as a
plain sample from p. That shifts the output distribution: with p = (0.5, 0.5),
q = (0.875, 0.125) and threshold 0.5 it emits (0.5625, 0.4375).

Change: the gate is part of the proposal. Keep q's mass on the tokens that pass the gate
without renormalising (mu); a draw that fails the gate becomes NULL. An offered token D
is accepted with min(1, p(D)/mu(D)); a rejection and NULL draw from the normalised
positive part of p - mu; a position with no attempted proposal is a plain sample from p.
Serve and the CLI build the same packet (core/gated_proposal.hpp) and give it to the
verifier explicitly (Verifier::set_spec_proposal, sample_tokens_proposal). The explicit
call has no plain-p fallback: an invalid packet row is reported, not sampled around.
STRATA_SPEC_PROB_GATE=top keeps its normalised-q rule.

Default path (STRATA_SPEC_PROB unset): unchanged. mtp.prob() is false, so gated_proposal
is false, the new draft-result argument is null, GatedDraftLimit does nothing,
Verifier::run takes the old branch (set_spec_q clears the measure flag), and the
sampler kernel's old instance is the old code with the new branches compiled out.
Read hunk by hunk; no hunk runs a new branch or a new allocation per token. The
default-identity model run is written as a gate script and not run yet.

Port notes against the PR as submitted:
- Unsupported combinations (parallel or multi-device decode, --lookup-chain,
  --adapt-swaps, PCIe share, --spec-min-p not finite, STRATA_SPEC_COST, DFlash2,
  STRATA_SPEC_T_CYCLE, --spec-corrupt, Metal) used to be an error that ended the
  request or the server. They now warn once and keep the older rule, so setting the
  variable never stops a run. --adapt-swaps 96 and the PCIe share are on by default, so
  the exact rule needs --lookup-chain 0 --adapt-swaps 0 --pcie-frac 0.
- The lookup source of a probabilistic window is chosen by a copy of the policy fixed at
  the request start, without asking whether the MTP's first guess agrees. Learning from
  committed rounds inside a request is the policy commit of this series.
- The CLI loop on the current tree has the out-of-memory window retry, DFlash2 and
  STRATA_SPEC_T_CYCLE; the gated path does not use the retry (a narrower window would
  change the room the packet was built for). Serve keeps merged first windows
  (STRATA_ADMIT_MERGE_FIRST) at their length.
- docs/DETAILS.md: two short bullets instead of the PR's long paragraph (that paragraph
  no longer exists on this tree). bench/results and the PNGs are not ported.

Tests: gated_proposal_test and spec_admission_test (default, pick, top, exact-top-only)
pass on z440 (CPU, g++ 13) and on the owner PC (MSVC). On the owner PC's RTX 5070
(sm_120, MSVC build, synthetic data, no model) spec_proposal_parity, spec_verify_parity and
sampler_parity (default, one-block and old-kernel configurations) pass. Also built:
CUDA sm_86 on z440 and boston, SYCL on P1006 (engine and host tests). The Metal path is
not built here.
Not covered: a model-level distribution test (chi-square of the first tokens against
plain sampling) and the identity run with the variable unset. Both are written as gate
scripts (3416-spec-prob-dist.sh, 3416-default-identity.sh) and not run.
2026-10-27 01:45:35 +01:00

29 lines
1.4 KiB
C++

#pragma once
#include <cstdint>
namespace strata::core::proposal {
// Per-call proposal row contract; no process-wide mode.
// Never a vocabulary ID or the existing no-draft marker (-1).
inline constexpr int32_t kProposalSamplerErrorToken = -2147483647;
enum class RowKind { MuOffer, PointOffer, NullTail, PlainBonus };
#if defined(__CUDACC__) || defined(__HIPCC__)
#define STRATA_PROPOSAL_HD __host__ __device__
#else
#define STRATA_PROPOSAL_HD
#endif
// Preconditions: 0 <= t < T, 0 <= nq <= T. The proposal API permits nq == T
// for a NULL tail, which has no candidate token even when T == 1.
STRATA_PROPOSAL_HD constexpr RowKind rowkind(int t, int T, int nq) {
return t == T - 1 ? (nq == T ? RowKind::NullTail : RowKind::PlainBonus)
: (t < nq ? RowKind::MuOffer : RowKind::PointOffer);
}
#undef STRATA_PROPOSAL_HD
// qbuf owns immutable masked mu, not a renormalized conditional proposal.
// MuOffer: require mu(D)>0; accept min(1,p(D)/mu(D)), else residual (p-mu)+.
// NullTail: no dtok read and no accept draw; sample residual (p-mu)+ directly.
// PointOffer and PlainBonus retain existing semantics. Target ex is untouched.
// Missing/nonpositive/nonfinite mu(D), or zero NULL residual, emits the unique
// error token above. The verifier rejects it after synchronization.
// This is a packet/interface check, not a replacement for caller law validation.
} // namespace strata::core::proposal