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.
This commit is contained in:
1 parent
a8c1d4b7f8
commit
f4612b1add
18 files changed
+963
-57
No files matched your search
+39
-8
@@ -12,6 +12,7 @@
|
||||
#include "strata/core/remote_expert_opt.hpp"
|
||||
#include "strata/core/dma_batch.hpp"
|
||||
#include "strata/core/spec_prob.hpp"
|
||||
#include "strata/core/spec_proposal.hpp"
|
||||
#if defined(_WIN32)
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
@@ -2865,7 +2866,22 @@ bool Verifier::prelaunch(int T, const int32_t* tokens, int64_t pos0, std::string
|
||||
|
||||
bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool, void* user, int32_t* out,
|
||||
std::string& err) {
|
||||
const bool measure = spec_measure_;
|
||||
// Clear this borrow and propagated stage borrows on every exit.
|
||||
struct ProposalBorrow {
|
||||
Verifier* verifier;
|
||||
bool active;
|
||||
~ProposalBorrow() { if (active) verifier->set_spec_q(nullptr, 0); }
|
||||
} proposal_borrow{this, measure};
|
||||
if (measure && (T < 1 || T > strata::kernels::kVerifyMaxT ||
|
||||
spec_nq_ < 0 || spec_nq_ > T || (spec_nq_ > 0 && spec_q_ == nullptr))) {
|
||||
err = "verify: proposal row count/pointer is invalid"; return false;
|
||||
}
|
||||
if (measure && (!head_sampling_ || sampling_.greedy || sampling_.temperature <= 0.0f)) {
|
||||
err = "verify: proposal requires sampled head"; return false;
|
||||
}
|
||||
#if defined(STRATA_GPU_METAL)
|
||||
if (measure) { err = "verify: proposal unsupported on Metal"; return false; }
|
||||
(void) pool;
|
||||
(void) user;
|
||||
return strata::metal_engine::window_run(metal_, T, tokens, pos0, head_sampling_, out, err);
|
||||
@@ -3169,7 +3185,7 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
sp.counter = (uint64_t) pos0;
|
||||
// STRATA_SPEC_PROB: the drafter's q lists for this window, judged by rejection sampling (spec_prob.hpp)
|
||||
bool judged = false;
|
||||
if (sampled && spec_q_ != nullptr && spec_nq_ > 0 && T > 1 && T <= kVerifyMaxT) {
|
||||
if (sampled && (measure || (spec_q_ != nullptr && spec_nq_ > 0 && T > 1)) && T <= kVerifyMaxT) {
|
||||
if (d_spec_ == nullptr &&
|
||||
cudaMalloc((void**) &d_spec_, (size_t) kVerifyMaxT * (1 + strata::core::kSpecQStride) * sizeof(int32_t)) !=
|
||||
cudaSuccess) {
|
||||
@@ -3177,18 +3193,26 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
d_spec_ = nullptr;
|
||||
}
|
||||
if (d_spec_ != nullptr) {
|
||||
const int nq = std::min(spec_nq_, T - 1);
|
||||
const int nq = measure ? spec_nq_ : std::min(spec_nq_, T - 1);
|
||||
int32_t* d_q = d_spec_ + kVerifyMaxT;
|
||||
if (cudaMemcpyAsync(d_spec_, tokens + 1, (size_t) (T - 1) * sizeof(int32_t), cudaMemcpyHostToDevice, cs_) ==
|
||||
cudaSuccess &&
|
||||
cudaMemcpyAsync(d_q, spec_q_, (size_t) nq * strata::core::kSpecQStride * sizeof(int32_t),
|
||||
cudaMemcpyHostToDevice, cs_) == cudaSuccess)
|
||||
judged = sample_tokens_spec(head_logits_, T, (int) n_vocab_, hist_d_, hist_len_, sp, d_spec_, d_q, nq,
|
||||
m_out_, cs_);
|
||||
if ((T == 1 || cudaMemcpyAsync(d_spec_, tokens + 1, (size_t) (T - 1) * sizeof(int32_t), cudaMemcpyHostToDevice, cs_) ==
|
||||
cudaSuccess) &&
|
||||
(nq == 0 || cudaMemcpyAsync(d_q, spec_q_, (size_t) nq * strata::core::kSpecQStride * sizeof(int32_t),
|
||||
cudaMemcpyHostToDevice, cs_) == cudaSuccess))
|
||||
judged = measure
|
||||
? sample_tokens_proposal(head_logits_, T, (int) n_vocab_, hist_d_, hist_len_, sp,
|
||||
d_spec_, d_q, nq, m_out_, cs_)
|
||||
: sample_tokens_spec(head_logits_, T, (int) n_vocab_, hist_d_, hist_len_, sp,
|
||||
d_spec_, d_q, nq, m_out_, cs_);
|
||||
}
|
||||
}
|
||||
spec_q_ = nullptr;
|
||||
spec_nq_ = 0;
|
||||
if (measure && !judged) {
|
||||
(void) cudaStreamSynchronize(cs_);
|
||||
err = "verify: proposal sampler unavailable or allocation/copy/launch failed";
|
||||
return false;
|
||||
}
|
||||
if (judged) ++spec_windows;
|
||||
else sample_tokens(head_logits_, T, (int) n_vocab_, hist_d_, hist_len_, sp, m_out_, cs_);
|
||||
if (cudaStreamSynchronize(cs_) != cudaSuccess) { // m_out_ is the mapped h_out_: synced, it is readable
|
||||
@@ -3196,6 +3220,13 @@ bool Verifier::run(int T, const int32_t* tokens, int64_t pos0, PoolMultiFn pool,
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (measure) {
|
||||
for (int t = 0; t < T; ++t)
|
||||
if (((volatile int32_t*) h_out_)[t] == strata::core::proposal::kProposalSamplerErrorToken) {
|
||||
err = "verify: invalid proposal candidate or zero NULL residual";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (int t = 0; t < T; ++t) out[t] = ((volatile int32_t*) h_out_)[t];
|
||||
if (tap_file_ != nullptr && !batch_rec_) tap_write(T, tokens, pos0);
|
||||
note_nonfinite(T, nullptr);
|
||||
|
||||
Reference in new issue
Block a user