verify: STRATA_LOGPOS hook for per-position teacher-forced log-probabilities

Verifier::window_logprobs writes one line per row of the last verify window's
head: position, target, its log-probability, the top token and its
log-probability, a hit flag, the log-probability of STRATA_LOGPOS_EXTRA
(default 248046, <|im_end|>) and the target's log-probability renormalized
without that token. read_windows calls it before commit when
STRATA_LOGPOS=<path> is set; otherwise the only cost is one null check.
A layer split's earlier stage forwards to the stage that holds the head,
under OnDevice as commit() does.
This commit is contained in:
Enky authored and Niko1221 committed 2026-09-30 20:35:02 +02:00
1 parent a3037d5435
commit 5396360ef7
3 files changed
+64

No files matched your search

+9
View File
@@ -110,6 +110,15 @@ public:
/// Keep the first `n_keep` (1..T) tokens of the last window; advances `ss.ple_prev` by them.
bool commit(int n_keep, std::string& err);
/// Measurement hook (STRATA_LOGPOS): after run(), write one line per row t of the last window's head -
/// "pos target logprob top top_logprob hit extra_logprob target_logprob_without_extra" - where row t is the
/// distribution at pos0 + t, targets[t] is the token at pos0 + t + 1, extra_logprob is the log-probability of
/// `extra_id` in the same row, and the last column is the target's log-probability in that row renormalized
/// over every token but `extra_id` (both nan when they do not apply). A layer split's earlier stage forwards
/// to the stage that holds the head.
bool window_logprobs(const int32_t* targets, int T, int64_t pos0, int32_t extra_id, std::FILE* out,
std::string& err);
/// Token t's residual after the last layer, (hc, n_embd) on the device, valid until the next `run`.
const float* final_R(int t) const;
const float* final_R_all() const { return next_ ? next_->final_R_all() : R_; }
+41
View File
@@ -1170,6 +1170,47 @@ void Verifier::publish_plan(void* ctx) {
*(volatile uint32_t*) v->h_flagA_ = v->cur_layer_ + 1;
}
bool Verifier::window_logprobs(const int32_t* targets, int T, int64_t pos0, int32_t extra_id, std::FILE* out,
std::string& err) {
if (next_ != nullptr) return next_->window_logprobs(targets, T, pos0, extra_id, out, err);
const OnDevice on_device(device_);
if (head_logits_ == nullptr || n_vocab_ <= 0 || T <= 0 || targets == nullptr || out == nullptr) {
err = "window_logprobs: no head logits for this window";
return false;
}
// run() synchronized cs_ before returning, so the head of this window is complete
std::vector<float> h((size_t) T * (size_t) n_vocab_);
if (cudaMemcpy(h.data(), head_logits_, h.size() * sizeof(float), cudaMemcpyDeviceToHost) != cudaSuccess) {
err = "window_logprobs: the head logits copy failed";
return false;
}
for (int t = 0; t < T; ++t) {
const float* row = h.data() + (size_t) t * (size_t) n_vocab_;
const int32_t tgt = targets[t];
if (tgt < 0 || (int64_t) tgt >= n_vocab_) continue;
int64_t top = 0;
for (int64_t v = 1; v < n_vocab_; ++v)
if (row[v] > row[top]) top = v;
const double maxv = row[top];
const bool has_extra = extra_id >= 0 && (int64_t) extra_id < n_vocab_;
double sum = 0.0, sum_without = 0.0; // the second skips extra_id: no cancellation when it holds ~all mass
for (int64_t v = 0; v < n_vocab_; ++v) {
const double e = std::exp((double) row[v] - maxv);
sum += e;
if (v != (int64_t) extra_id) sum_without += e;
}
const double lse = maxv + std::log(sum);
const double extra = has_extra ? (double) row[extra_id] - lse : NAN;
const double without = has_extra && tgt != extra_id && sum_without > 0.0
? (double) row[tgt] - (maxv + std::log(sum_without)) : NAN;
std::fprintf(out, "%lld\t%d\t%.9f\t%lld\t%.9f\t%d\t%.9f\t%.9f\n", (long long) (pos0 + t), (int) tgt,
(double) row[tgt] - lse, (long long) top, maxv - lse, (int) (top == (int64_t) tgt), extra,
without);
}
std::fflush(out);
return true;
}
bool Verifier::commit(int n_keep, std::string& err) {
const OnDevice on_device(device_);
if (n_keep < 1 || n_keep > last_t_) { err = "verify: commit count out of range"; return false; }
+14
View File
@@ -4497,6 +4497,20 @@ int main(int argc, char** argv) {
if (drive.d.failed && drive.d.fail) e = drive.d.fail;
return false;
}
// STRATA_LOGPOS=<path>: the teacher-forced log-probability of every token read here (every
// token is committed and nxt[t] is the prompt's own next token), appended to <path>; the last
// column is the log-probability of STRATA_LOGPOS_EXTRA (default 248046, <|im_end|>), so the
// end-of-turn mass a chat model puts on raw text can be taken out of the measurement
static std::FILE* logpos = [] {
const char* p = std::getenv("STRATA_LOGPOS");
return p != nullptr ? std::fopen(p, "ab") : nullptr;
}();
static const int32_t logpos_extra = [] {
const char* p = std::getenv("STRATA_LOGPOS_EXTRA");
return p != nullptr ? (int32_t) std::atoi(p) : (int32_t) 248046;
}();
if (logpos != nullptr && !ver.window_logprobs(nxt.data(), T, q, logpos_extra, logpos, e))
return false;
if (!ver.commit(T, e) || !mtp.prefill(ver.final_R_all(), nxt.data(), T, q, e)) return false;
q += T;
}