Files
strata/src/core/spec_admission_test.cpp
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

102 lines
5.4 KiB
C++

// Enumerate proposal and acceptance branches exactly; no Monte Carlo or GPU.
#include "strata/core/spec_prob.hpp"
#include "strata/core/gated_proposal.hpp"
#include "strata/spec/draft_policy.hpp"
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace strata::core;
using Dist = std::array<double, 2>;
int failures = 0;
void check(bool ok, const char* message) {
if (!ok) { ++failures; std::fprintf(stderr, "FAIL: %s\n", message); }
}
Dist judged(const Dist& p, const std::array<float,2>& q, int d) {
const int32_t ids[2] = {0,1};
const SpecP P{ids,p.data(),2}; const SpecQ Q{ids,q.data(),2};
const double a=std::min(1.0,p[d]/q[d]); Dist result{};
if (a>0) result[spec_verify_row(P,d,Q,(float)(a/2),0.5f).token]+=a;
if (a<1) result[spec_verify_row(P,d,Q,(float)((1+a)/2),0.5f).token]+=1-a;
return result;
}
Dist legacy_raw_q_plain_p(const Dist& p, const std::array<float,2>& q, bool own_draw, float threshold) {
Dist result{};
for (int d=0;d<2;++d) {
const float gate=own_draw?q[d]:std::max(q[0],q[1]);
const auto row=gate>=threshold?judged(p,q,d):p;
for (int i=0;i<2;++i) result[i]+=q[d]*row[i];
}
return result;
}
bool equal(const Dist& a,const Dist& b) {
return std::abs(a[0]-b[0])<1e-12 && std::abs(a[1]-b[1])<1e-12;
}
// Enumerate the repaired first-row mixture using the ACTUAL packet builder.
// No candidate-conditioned renormalization: every draw receives the same root mu.
Dist proposal_contract(const Dist& p,const std::array<float,2>& q,float threshold,
strata::core::gated::RootChoice root={}) {
std::array<int32_t,strata::core::gated::kStride> raw;
raw.fill(-1);raw[0]=0;raw[1]=1;
std::memcpy(raw.data()+strata::core::gated::kEntries,q.data(),sizeof(q));
Dist result{};
for (int d=0;d<2;++d) {
const int32_t draft=d;const float score=q[d];strata::core::gated::Packet packet;
const bool ok=strata::core::gated::make_packet(packet,raw.data(),&draft,&score,1,1,2,2,threshold,false,root);
check(ok,"valid gated packet");if(!ok)continue;
Dist mu{strata::core::gated::mass_at(packet.mu.data(),0),strata::core::gated::mass_at(packet.mu.data(),1)};
double acceptance=0;
if (!packet.null_tail) {
check(mu[d]>0,"offer has positive mu");
acceptance=std::min(1.0,p[d]/mu[d]);
result[d]+=q[d]*acceptance;
}
if (acceptance<1) {
Dist residual{std::max(0.0,p[0]-mu[0]),std::max(0.0,p[1]-mu[1])};
const double sum=residual[0]+residual[1];check(sum>0,"reachable failure has residual");
if(sum>0)for(int y=0;y<2;++y)result[y]+=q[d]*(1-acceptance)*residual[y]/sum;
}
}
return result;
}
int main(int argc,char** argv) {
const bool expect_pick=!(argc>1 && std::strcmp(argv[1],"top")==0);
check(spec_gate_pick()==expect_pick,"pick is default; only explicit lowercase top selects Top compatibility");
const Dist p{0.5,0.5};const std::array<float,2> q{0.875f,0.125f};
check(equal(legacy_raw_q_plain_p(p,q,true,0.5f),Dist{0.5625,0.4375}),
"LEGACY negative control: own-draw gate with full-q verifier/plain-p failure is biased");
check(equal(proposal_contract(p,q,0.5f),p),"new pick mu/NULL mixture preserves p");
check(equal(spec_gate_pick()?proposal_contract(p,q,0.5f):legacy_raw_q_plain_p(p,q,false,0.5f),p),
"configured repaired pick or Top compatibility preserves p");
int top_cases=0,proposal_cases=0;
for(int a=1;a<16;++a)for(int b=1;b<16;++b) {
const Dist P{a/16.0,1-a/16.0};const std::array<float,2> Q{b/16.0f,1-b/16.0f};
for(float threshold:{0.0f,0.5f,0.9f,1.1f}) {
check(equal(legacy_raw_q_plain_p(P,Q,false,threshold),P),"Top compatibility distribution law");++top_cases;
check(equal(proposal_contract(P,Q,threshold),P),"H-only proposal mixture");++proposal_cases;
for(int rescue:{0,1}) {
check(equal(proposal_contract(P,Q,threshold,{true,rescue,2}),P),"fixed root UNION on every source branch");++proposal_cases;
}
}
}
// A planner must not learn from draws it may select, including discarded position-keyed lookahead.
strata::spec::DraftPolicy live(6);
for(int i=0;i<5;++i) { live.observe(false,4,3,24,1.0);live.observe(false,6,5,24,1.5); }
const auto request_plan=live;
auto short_chain=live,long_chain=live;
short_chain.observe(false,4,3,24,0.2);long_chain.observe(false,4,3,24,8.0);
const bool short_lookup=short_chain.choose(4,5,24).lookup,long_lookup=long_chain.choose(4,5,24).lookup;
check(!short_lookup && long_lookup,"draw-dependent chain time can change the actual policy choice");
Dist conditional{},planned{};
for(int d=0;d<2;++d) {
const auto selected=(d==0?long_lookup:short_lookup)?p:judged(p,q,d);
const auto fixed=request_plan.choose(4,5,24).lookup?p:judged(p,q,d);
for(int i=0;i<2;++i) { conditional[i]+=q[d]*selected[i];planned[i]+=q[d]*fixed[i]; }
}
check(equal(conditional,Dist{0.4375,0.5625}),"LEGACY negative: D-conditioned source without common first measure changes proposal law");
check(equal(planned,p),"request plan preserves p despite subsequent timing observations");
std::printf("spec_admission_test: %d Top compatibility cases, %d actual mu-packet cases, legacy negatives; %d failures\n",top_cases,proposal_cases,failures);
return failures?1:0;
}