From 504579da127afa37a24022956b11c32b8429c1aa Mon Sep 17 00:00:00 2001 From: Constantin Date: Fri, 2 Oct 2026 00:59:31 -0400 Subject: [PATCH] fix: --dump-logits must not promise rows it will never write A native pack leaves the per-token loop at `if (native_pack) { spec_pos = pos; break; }`, before the site that dumps a row per position. The header was written at open anyway, so the file claimed 64 or 66 rows and contained none - and a reader that trusted it reported "0 differing positions" from a file with no positions in it, which is the one answer that looks like a pass. The header is now written with the FIRST row. A run that dumps nothing leaves an empty file, which is what actually happened, and a reader can treat size zero as "this mode never dumps" instead of as agreement. Verified on the native pack: the file is now 0 bytes where it was 8. The non-native path is unchanged apart from WHEN the header lands; it was not exercised here because no non-native model is set up on this machine. (cherry picked from commit b87d7dcfe9a0754c0a2b8b9776b7f2ec8980b6a1) --- src/program/generate.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index a02b3e3d..11d473e8 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -3353,6 +3353,10 @@ int main(int argc, char** argv) { } std::FILE* dump = nullptr; + // The logits header is written WITH THE FIRST ROW, not at open: a native pack never reaches the + // per-token dump site, and a header promising rows that were never written is worse than no file. + int32_t hdr[2] = {0, 0}; + bool hdr_written = false; const int64_t dump_positions = (int64_t) o.tokens.size() - 1 + o.max_new; if (!o.dump_logits.empty()) { if (dump_positions > INT32_MAX || n_vocab > INT32_MAX) { @@ -3372,12 +3376,7 @@ int main(int argc, char** argv) { // of the size gets a wrong answer that looks authoritative. `tools/logits_identical.py` caught it by // parsing the header and refusing the file. const int32_t n_rows = (int32_t) strata::program::logits_selection::row_count(dump_positions, o.logits_stride); - const int32_t hdr[2] = {(int32_t) n_vocab, n_rows}; - if (std::fwrite(hdr, sizeof hdr, 1, dump) != 1) { - std::fprintf(stderr, "strata generate: cannot write logits header\n"); - std::fclose(dump); - return 1; - } + hdr[0] = n_vocab; hdr[1] = n_rows; } // ---- THE C1 ORACLE: ONE RESIDUAL SNAPSHOT PER LAYER PER POSITION, so the engine can be bisected against @@ -6073,6 +6072,17 @@ int main(int argc, char** argv) { (long long) n_vocab, (long long) pos); return 1; } + // THE HEADER IS WRITTEN WITH THE FIRST ROW, NOT AT OPEN. A native pack leaves the per-token + // loop at `if (native_pack) { spec_pos = pos; break; }` and never reaches this site, so a header + // written at open promised 64 or 66 rows for a file that would contain none - and a reader that + // trusted it reported "0 differing positions" from a file with no positions in it. Written here, + // a run that dumps nothing leaves an empty file, which is what actually happened. + if (emit_logits && !hdr_written && std::fwrite(hdr, sizeof hdr, 1, dump) != 1) { + std::fprintf(stderr, "strata generate: cannot write logits header\n"); + std::fclose(dump); + return 1; + } + if (emit_logits) hdr_written = true; if (emit_logits && std::fwrite(logits.data(), sizeof(float), (size_t) n_vocab, dump) != (size_t) n_vocab) { std::fprintf(stderr, "strata generate: cannot write logits at position %lld\n", (long long) pos); std::fclose(dump);