From aeabfff7eeff227c6311c01ad428b593449393ea Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Thu, 12 Nov 2026 06:44:07 -0400 Subject: [PATCH 1/2] q4nx cache: the sweep's note prints sizes in MiB below 1 GiB, like the CLI (refs #2263) Review of #2271: the library's note printed "%.1f GiB" for every size, so a small cache or bound read "0.0 GiB" from the library and "2.0 MiB" from the CLI. Since #2271 the library is linked by three binaries and its note is the only output a conversion-only box gets. q4nx_cache_size_text() (GiB from 1 GiB up, MiB below) lives in npu/q4nx_cache_sweep.cpp and is used by the note and by every size the `1bit q4nx-cache` command prints, including the "sweep is off" line. Four q4nx_cache_test arms pin it (6 MiB, 0, just under 1 GiB, 60 GiB). Co-Authored-By: Claude Opus 5.5 --- app/q4nx_cache.cpp | 7 ++----- npu/q4nx_cache_sweep.cpp | 13 ++++++++++--- npu/q4nx_cache_sweep.h | 5 +++++ tests/q4nx_cache_test.cpp | 11 +++++++++-- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/app/q4nx_cache.cpp b/app/q4nx_cache.cpp index ca5d89c0a..5d2eb8d27 100644 --- a/app/q4nx_cache.cpp +++ b/app/q4nx_cache.cpp @@ -70,13 +70,10 @@ int run_q4nx_cache(int argc, char** argv) { : q4nx_cache_bound_bytes(std::getenv("ONEBIT_Q4NX_CACHE_MAX_GIB")); const int64_t before = q4nx_dir_bytes(root); if (max_bytes <= 0) { - std::printf("q4nx-cache: %s is %.1f GiB, the sweep is off (bound 0)\n", root.c_str(), double(before) / (1 << 30)); + std::printf("q4nx-cache: %s is %s, the sweep is off (bound 0)\n", root.c_str(), q4nx_cache_size_text(before).c_str()); return 0; } - const auto size = [](int64_t b, char* out, size_t n) { - if (b >= (int64_t(1) << 30)) std::snprintf(out, n, "%.1f GiB", double(b) / (1 << 30)); - else std::snprintf(out, n, "%.1f MiB", double(b) / (1 << 20)); - }; + const auto size = [](int64_t b, char* out, size_t n) { std::snprintf(out, n, "%s", q4nx_cache_size_text(b).c_str()); }; if (dry) { const std::vector would = q4nx_cache_candidates(root, keep, max_bytes, min_age); int64_t would_free = 0; diff --git a/npu/q4nx_cache_sweep.cpp b/npu/q4nx_cache_sweep.cpp index ce035113a..8054e5930 100644 --- a/npu/q4nx_cache_sweep.cpp +++ b/npu/q4nx_cache_sweep.cpp @@ -24,6 +24,13 @@ namespace fs = std::filesystem; +std::string q4nx_cache_size_text(int64_t bytes) { + char out[32]; + if (bytes >= (int64_t(1) << 30)) std::snprintf(out, sizeof out, "%.1f GiB", double(bytes) / (1 << 30)); + else std::snprintf(out, sizeof out, "%.1f MiB", double(bytes) / (1 << 20)); + return out; +} + int64_t q4nx_dir_bytes(const std::string& path) { std::error_code ec; if (!fs::is_directory(path, ec)) return 0; @@ -90,10 +97,10 @@ Q4nxCacheSweep q4nx_cache_evict(const std::string& root, const std::string& keep if (r.removed > 0) { r.reported = true; std::fprintf(stderr, - "note: q4nx cache: removed %d entr%s, %.1f GiB -> %.1f GiB (bound %.1f GiB, %s); entries younger than " + "note: q4nx cache: removed %d entr%s, %s -> %s (bound %s, %s); entries younger than " "%lld s and the one just written are kept (engine#1956)\n", - r.removed, r.removed == 1 ? "y" : "ies", double(r.bytes_before) / (1 << 30), - double(r.bytes_after) / (1 << 30), double(max_bytes) / (1 << 30), root.c_str(), + r.removed, r.removed == 1 ? "y" : "ies", q4nx_cache_size_text(r.bytes_before).c_str(), + q4nx_cache_size_text(r.bytes_after).c_str(), q4nx_cache_size_text(max_bytes).c_str(), root.c_str(), (long long)min_age_seconds); } return r; diff --git a/npu/q4nx_cache_sweep.h b/npu/q4nx_cache_sweep.h index 8e76c6f32..fce248c6e 100644 --- a/npu/q4nx_cache_sweep.h +++ b/npu/q4nx_cache_sweep.h @@ -57,6 +57,11 @@ struct Q4nxCacheCandidate { std::vector q4nx_cache_candidates(const std::string& root, const std::string& keep, int64_t max_bytes, int64_t min_age_seconds = 600); +// A size as the cache's messages print it: GiB from 1 GiB up, MiB below. A small cache or bound printed as "0.0 GiB" reads +// like "the sweep is off", which is a different state. The one formatter for the sweep's note (shared by every binary that +// links this, and the only output a conversion-only box gets) and for `1bit q4nx-cache`, so the two cannot disagree. +std::string q4nx_cache_size_text(int64_t bytes); + // Directory bytes under `path` (0 when it does not exist). int64_t q4nx_dir_bytes(const std::string& path); diff --git a/tests/q4nx_cache_test.cpp b/tests/q4nx_cache_test.cpp index f72a03b6c..642896b3b 100644 --- a/tests/q4nx_cache_test.cpp +++ b/tests/q4nx_cache_test.cpp @@ -182,8 +182,15 @@ int main() { "q4nx-cache candidates: the age window protects everything inside it"); fs::remove_all(cr, ec); } + // engine#2263 review: the sweep's note and the CLI print sizes through one formatter, MiB below 1 GiB, so a small + // cache never reads "0.0 GiB" (which looks like the sweep being off). + check(q4nx_cache_size_text(int64_t(6) << 20) == "6.0 MiB", "size text: 6 MiB is MiB, not 0.0 GiB"); + check(q4nx_cache_size_text(0) == "0.0 MiB", "size text: an empty cache is 0.0 MiB"); + check(q4nx_cache_size_text((int64_t(1) << 30) - 1) == "1024.0 MiB", "size text: just under 1 GiB stays MiB"); + check(q4nx_cache_size_text(int64_t(60) << 30) == "60.0 GiB", "size text: 60 GiB is GiB"); fs::remove_all(root, ec); - std::printf("q4nx cache: %d arm(s) over the sweep (bound, LRU order, keep, age window, disabled, touch on a hit, bound text)\n", - failures ? 0 : 32); + std::printf("q4nx cache: %d arm(s) over the sweep (bound, LRU order, keep, age window, disabled, touch on a hit, bound text, " + "size text)\n", + failures ? 0 : 36); return failures ? 1 : 0; } From d84842aa0bcecfc7ad20bd26f698ef161e5bcc69 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Thu, 12 Nov 2026 06:52:16 -0400 Subject: [PATCH 2/2] q4nx cache: a size within half a MiB of a GiB reads as GiB, not "1024.0 MiB" (engine#2263) --- npu/q4nx_cache_sweep.cpp | 10 ++++++++-- tests/q4nx_cache_test.cpp | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/npu/q4nx_cache_sweep.cpp b/npu/q4nx_cache_sweep.cpp index 8054e5930..58ab2cb4e 100644 --- a/npu/q4nx_cache_sweep.cpp +++ b/npu/q4nx_cache_sweep.cpp @@ -26,8 +26,14 @@ namespace fs = std::filesystem; std::string q4nx_cache_size_text(int64_t bytes) { char out[32]; - if (bytes >= (int64_t(1) << 30)) std::snprintf(out, sizeof out, "%.1f GiB", double(bytes) / (1 << 30)); - else std::snprintf(out, sizeof out, "%.1f MiB", double(bytes) / (1 << 20)); + if (bytes >= (int64_t(1) << 30)) { + std::snprintf(out, sizeof out, "%.1f GiB", double(bytes) / (1 << 30)); + return out; + } + std::snprintf(out, sizeof out, "%.1f MiB", double(bytes) / (1 << 20)); + // Within half a MiB of a GiB the MiB reading rounds to "1024.0", which is a value in the unit it has just left -- the same + // kind of misleading exchange this formatter exists to stop. Print the GiB it is about to become. + if (std::string(out) == "1024.0 MiB") std::snprintf(out, sizeof out, "%.1f GiB", double(bytes) / (1 << 30)); return out; } diff --git a/tests/q4nx_cache_test.cpp b/tests/q4nx_cache_test.cpp index 642896b3b..98e1461f0 100644 --- a/tests/q4nx_cache_test.cpp +++ b/tests/q4nx_cache_test.cpp @@ -186,7 +186,8 @@ int main() { // cache never reads "0.0 GiB" (which looks like the sweep being off). check(q4nx_cache_size_text(int64_t(6) << 20) == "6.0 MiB", "size text: 6 MiB is MiB, not 0.0 GiB"); check(q4nx_cache_size_text(0) == "0.0 MiB", "size text: an empty cache is 0.0 MiB"); - check(q4nx_cache_size_text((int64_t(1) << 30) - 1) == "1024.0 MiB", "size text: just under 1 GiB stays MiB"); + check(q4nx_cache_size_text((int64_t(1) << 30) - 1) == "1.0 GiB", "size text: just under 1 GiB is 1.0 GiB, not \"1024.0 MiB\""); + check(q4nx_cache_size_text(int64_t(1023) << 20) == "1023.0 MiB", "size text: a real 1023 MiB still reads MiB"); check(q4nx_cache_size_text(int64_t(60) << 30) == "60.0 GiB", "size text: 60 GiB is GiB"); fs::remove_all(root, ec); std::printf("q4nx cache: %d arm(s) over the sweep (bound, LRU order, keep, age window, disabled, touch on a hit, bound text, "