diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 351be94..d34c915 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -1912,6 +1912,12 @@ int main(int argc, char** argv) { "engine to) and is not a --serve mode\n"); return 2; } + if (o.teacher_force && !o.mtp.empty()) { + // the window's rows after the first would be the drafter's guesses, and teacher forcing commits every row: + // the state then holds tokens the oracle never had (seen as 70 of 400 top-1 on a run that is 394 without) + std::fprintf(stderr, "strata generate: --teacher-force takes its window rows from --spec-oracle; drop --mtp\n"); + return 2; + } if (o.no_ple && !o.ple_gguf.empty()) { std::fprintf(stderr, "strata generate: --no-ple and --ple-gguf are mutually exclusive\n"); return 2; @@ -7509,6 +7515,52 @@ int main(int argc, char** argv) { int a = 0; while (a < T - 1 && window[(size_t) a + 1] == outv[(size_t) a]) ++a; if (from_sfx) { ++sfx_windows; sfx_drafts += T - 1; sfx_ok += a; } + // Diagnostics, STRATA_SERVE_LOGIT_TRACE=FILE: one line per emitted token with the head's five + // largest logits, the window's shape and the experts it missed, so two runs of one request can + // be compared where they part (docs/qwen36-port/step5-serve.md, "The same greedy request"). + // With STRATA_SERVE_LOGIT_DUMP=PREFIX too, every emitted token's whole row goes to + // PREFIX..f32 (raw F32, tools/qwen36_compare_logits.py --no-engine-header): 1 MB a token. + // The window's result is not touched. + static FILE* const logit_trace = [] { + const char* f = std::getenv("STRATA_SERVE_LOGIT_TRACE"); + return f != nullptr && *f != '\0' ? std::fopen(f, "a") : nullptr; + }(); + if (logit_trace != nullptr) { + static std::vector row; + static int64_t tr_miss = 0, tr_pcie = 0, tr_hits = 0, tr_req = -1; + if (first_window) ++tr_req; + static FILE* dumpf = nullptr; + row.resize((size_t) ver.vocab()); + if (first_window) { + if (dumpf != nullptr) std::fclose(dumpf); + const char* pre = std::getenv("STRATA_SERVE_LOGIT_DUMP"); + dumpf = pre != nullptr && *pre != '\0' + ? std::fopen((std::string(pre) + "." + std::to_string(tr_req) + ".f32").c_str(), "wb") : nullptr; + } + for (int i = 0; i <= a && produced_n + i < max_new; ++i) { + if (!ver.copy_logits(i, row.data())) break; + if (dumpf != nullptr) std::fwrite(row.data(), sizeof(float), row.size(), dumpf); + int top[5]; + for (int k = 0; k < 5; ++k) { + int best = -1; + for (int v = 0; v < (int) row.size(); ++v) { + bool taken = false; + for (int q = 0; q < k; ++q) taken = taken || top[q] == v; + if (!taken && (best < 0 || row[(size_t) v] > row[(size_t) best])) best = v; + } + top[k] = best; + } + std::fprintf(logit_trace, "L %lld %lld %d row %d of T %d%s miss %lld pcie %lld hits %lld top", + (long long) tr_req, (long long) (produced_n + i), (int) outv[(size_t) i], i, T, + from_sfx ? " sfx" : "", (long long) (drive.d.multi_misses - tr_miss), + (long long) (drive.d.pcie_experts - tr_pcie), (long long) (drive.d.cache_hits - tr_hits)); + for (int k = 0; k < 5; ++k) std::fprintf(logit_trace, " %d:%.6f", top[k], (double) row[(size_t) top[k]]); + std::fputc('\n', logit_trace); + } + tr_miss = drive.d.multi_misses; tr_pcie = drive.d.pcie_experts; tr_hits = drive.d.cache_hits; + std::fflush(logit_trace); + if (dumpf != nullptr) std::fflush(dumpf); + } const Clock::time_point tw1 = Clock::now(); std::thread adapt_thr; // the adaptive tier beside the commit and the draft (as in generate) bool adapt_ok = true; diff --git a/tools/qwen36_teacher_llama.cpp b/tools/qwen36_teacher_llama.cpp index e914ee0..027c1cd 100644 --- a/tools/qwen36_teacher_llama.cpp +++ b/tools/qwen36_teacher_llama.cpp @@ -9,6 +9,9 @@ // // qwen36-teacher MODEL.gguf OUTDIR --prompt-file FILE [--n-gen 64] [--ngl 99] [--ctx 8192] [--probe 0,3,20,39] // qwen36-teacher MODEL.gguf --detok FILE the ids in FILE (commas or whitespace) as text, to stdout +// --prompt-ids FILE the prompt as token ids (commas or whitespace) instead of --prompt-file's text +// --force-ids FILE teacher forcing: row j's logits are written, then FILE's j-th token is decoded instead of the +// argmax (continuation.txt still holds the argmax per row); --n-gen is capped at FILE's length // // Writes OUTDIR/prompt_ids.txt (the prompt's token ids, parse_special on: the ChatML tags of the regina prompts are // tokens), OUTDIR/continuation.txt (the N greedy tokens), OUTDIR/logits.f32 (N rows of n_vocab F32: row j is the @@ -76,13 +79,15 @@ int main(int argc, char** argv) { return 1; } const std::string model_path = argv[1]; - std::string outdir, prompt_file, detok; + std::string outdir, prompt_file, prompt_ids, force_ids, detok; int n_gen = 64, ngl = 99, n_ctx = 8192; std::set probe = {0, 3, 20, 39}; for (int i = 2; i < argc; ++i) { const std::string a = argv[i]; auto next = [&]() -> const char* { if (i + 1 >= argc) { std::fprintf(stderr, "%s needs a value\n", a.c_str()); std::exit(1); } return argv[++i]; }; if (a == "--prompt-file") prompt_file = next(); + else if (a == "--prompt-ids") prompt_ids = next(); + else if (a == "--force-ids") force_ids = next(); else if (a == "--n-gen") n_gen = std::atoi(next()); else if (a == "--ngl") ngl = std::atoi(next()); else if (a == "--ctx") n_ctx = std::atoi(next()); @@ -109,16 +114,31 @@ int main(int argc, char** argv) { llama_backend_free(); return 0; } - if (outdir.empty() || prompt_file.empty()) { std::fprintf(stderr, "OUTDIR and --prompt-file are required\n"); return 1; } + if (outdir.empty() || (prompt_file.empty() && prompt_ids.empty())) { std::fprintf(stderr, "OUTDIR and --prompt-file or --prompt-ids are required\n"); return 1; } mkdir(outdir.c_str(), 0755); - std::ifstream pf(prompt_file); - std::string prompt((std::istreambuf_iterator(pf)), std::istreambuf_iterator()); - while (!prompt.empty() && prompt.back() == '\n') prompt.pop_back(); // the files end in one newline the prompt does not - std::vector toks(prompt.size() + 16); - int n = llama_tokenize(vocab, prompt.c_str(), (int32_t) prompt.size(), toks.data(), (int32_t) toks.size(), false, true); - if (n < 0) { toks.resize((size_t) -n); n = llama_tokenize(vocab, prompt.c_str(), (int32_t) prompt.size(), toks.data(), (int32_t) toks.size(), false, true); } - if (n <= 0) { std::fprintf(stderr, "tokenize failed\n"); return 1; } - toks.resize((size_t) n); + std::vector toks; + int n = 0; + if (!prompt_ids.empty()) { + toks = read_ids(prompt_ids); + n = (int) toks.size(); + if (n <= 0) { std::fprintf(stderr, "no ids in %s\n", prompt_ids.c_str()); return 1; } + prompt_file = prompt_ids; + } else { + std::ifstream pf(prompt_file); + std::string prompt((std::istreambuf_iterator(pf)), std::istreambuf_iterator()); + while (!prompt.empty() && prompt.back() == '\n') prompt.pop_back(); // the files end in one newline the prompt does not + toks.resize(prompt.size() + 16); + n = llama_tokenize(vocab, prompt.c_str(), (int32_t) prompt.size(), toks.data(), (int32_t) toks.size(), false, true); + if (n < 0) { toks.resize((size_t) -n); n = llama_tokenize(vocab, prompt.c_str(), (int32_t) prompt.size(), toks.data(), (int32_t) toks.size(), false, true); } + if (n <= 0) { std::fprintf(stderr, "tokenize failed\n"); return 1; } + toks.resize((size_t) n); + } + std::vector forced; + if (!force_ids.empty()) { + forced = read_ids(force_ids); + if (forced.empty()) { std::fprintf(stderr, "no ids in %s\n", force_ids.c_str()); return 1; } + n_gen = std::min(n_gen, (int) forced.size()); + } const int n_vocab = llama_vocab_n_tokens(vocab); Routes routes; @@ -152,7 +172,7 @@ int main(int argc, char** argv) { text += piece(vocab, t); if (j + 1 == n_gen) break; routes.row = j + 1; // the next decode's logits are row j + 1 - llama_token one = t; + llama_token one = forced.empty() ? t : forced[(size_t) j]; if (llama_decode(ctx, llama_batch_get_one(&one, 1))) { std::fprintf(stderr, "decode failed at row %d\n", j + 1); return 1; } } std::fclose(lf);