diff --git a/tests/decision_test.cpp b/tests/decision_test.cpp index 787634dbb..1a7743467 100644 --- a/tests/decision_test.cpp +++ b/tests/decision_test.cpp @@ -25,6 +25,7 @@ #include #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace fs = std::filesystem; using onebit::npu::DecisionHead; @@ -74,7 +75,7 @@ nlohmann::json base_config() { bool close(float a, float b, float eps = 1e-5f) { return std::fabs(a - b) <= eps; } void test_load_and_logits() { - const fs::path dir = fs::temp_directory_path() / "onebit-decision-test"; + const fs::path dir = ::onebit::test::tmp_root() / "onebit-decision-test"; fs::remove_all(dir); fs::create_directories(dir); // W = [[1,0,0],[0,1,0],[0,0,2]] rows A,B,C over a 3-wide hidden state. @@ -113,7 +114,7 @@ void test_load_and_logits() { } void test_temperature_and_shape_errors() { - const fs::path dir = fs::temp_directory_path() / "onebit-decision-test2"; + const fs::path dir = ::onebit::test::tmp_root() / "onebit-decision-test2"; fs::remove_all(dir); fs::create_directories(dir); const std::vector w = {2, 0, 0, 2}; // two options over a 2-wide state @@ -149,7 +150,7 @@ void test_temperature_and_shape_errors() { } void test_missing_pieces() { - const fs::path dir = fs::temp_directory_path() / "onebit-decision-test3"; + const fs::path dir = ::onebit::test::tmp_root() / "onebit-decision-test3"; fs::remove_all(dir); fs::create_directories(dir); DecisionHead head; diff --git a/tests/gguf_meta_test.cpp b/tests/gguf_meta_test.cpp index c9772590f..206b10bf3 100755 --- a/tests/gguf_meta_test.cpp +++ b/tests/gguf_meta_test.cpp @@ -23,6 +23,7 @@ #include #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace { @@ -66,7 +67,7 @@ void expect(const std::string& what, const std::string& got, const std::string& } // namespace int main() { - const auto dir = std::filesystem::temp_directory_path() / "onebit_gguf_meta_test"; + const auto dir = ::onebit::test::tmp_root() / "onebit_gguf_meta_test"; std::filesystem::create_directories(dir); Gguf first; diff --git a/tests/laya_bounds_test.cpp b/tests/laya_bounds_test.cpp index 30400bd05..2604e2249 100644 --- a/tests/laya_bounds_test.cpp +++ b/tests/laya_bounds_test.cpp @@ -33,6 +33,7 @@ #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace fs = std::filesystem; using json = nlohmann::json; @@ -69,7 +70,7 @@ void write_st(const fs::path& path, const std::vector out; diff --git a/tests/mem_admit_test.cpp b/tests/mem_admit_test.cpp index 17b163af1..696195d13 100644 --- a/tests/mem_admit_test.cpp +++ b/tests/mem_admit_test.cpp @@ -28,6 +28,7 @@ #include #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace fs = std::filesystem; using namespace onebit; @@ -94,7 +95,7 @@ void put(const fs::path& p, const std::string& text) { } // namespace int main() { - const fs::path dir = fs::temp_directory_path() / ("mem_admit_test_" + std::to_string(::getpid())); + const fs::path dir = ::onebit::test::tmp_root() / ("mem_admit_test_" + std::to_string(::getpid())); fs::create_directories(dir); const std::string d = dir.string(); diff --git a/tests/moe_gguf_index_test.cpp b/tests/moe_gguf_index_test.cpp index 5ee343198..d7577bc5e 100644 --- a/tests/moe_gguf_index_test.cpp +++ b/tests/moe_gguf_index_test.cpp @@ -30,6 +30,7 @@ #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace fs = std::filesystem; using onebit::moe::GgufIndex; @@ -77,7 +78,7 @@ std::string open_error(const fs::path& path, Gguf g, size_t data = 64) { } // namespace int main() { - const fs::path root = fs::temp_directory_path() / ("moe_gguf_index_test." + std::to_string(::getpid())); + const fs::path root = ::onebit::test::tmp_root() / ("moe_gguf_index_test." + std::to_string(::getpid())); fs::remove_all(root); fs::create_directories(root); diff --git a/tests/npu_flm_head_import_test.cpp b/tests/npu_flm_head_import_test.cpp index 65a48713c..a0807c88d 100644 --- a/tests/npu_flm_head_import_test.cpp +++ b/tests/npu_flm_head_import_test.cpp @@ -34,6 +34,7 @@ #include #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace { @@ -220,7 +221,7 @@ void test_refusal(const std::string& root) { int main(int argc, char** argv) { const std::string golden = argc > 1 ? argv[1] : ""; - const std::string root = (fs::temp_directory_path() / "npu_flm_head_import_test").string(); + const std::string root = (::onebit::test::tmp_root() / "npu_flm_head_import_test").string(); try { if (!golden.empty()) test_golden(golden); else std::printf("skip golden: no directory given\n"); diff --git a/tests/npu_model_bounds_test.cpp b/tests/npu_model_bounds_test.cpp index cd4ff1921..b97c63fd9 100644 --- a/tests/npu_model_bounds_test.cpp +++ b/tests/npu_model_bounds_test.cpp @@ -36,6 +36,7 @@ #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs using namespace onebit::npu; namespace fs = std::filesystem; @@ -120,7 +121,7 @@ std::vector one_layer(std::vector q_shape = {1, 5120}, int64_t g } // namespace int main() { - const fs::path root = fs::temp_directory_path() / ("npu_model_bounds_test." + std::to_string(::getpid())); + const fs::path root = ::onebit::test::tmp_root() / ("npu_model_bounds_test." + std::to_string(::getpid())); fs::remove_all(root); { // well-formed: loads and packs diff --git a/tests/npu_moe_test.cpp b/tests/npu_moe_test.cpp index 3f197fbc9..8bdfad36e 100644 --- a/tests/npu_moe_test.cpp +++ b/tests/npu_moe_test.cpp @@ -44,6 +44,7 @@ #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs using namespace onebit::npu; namespace fs = std::filesystem; @@ -80,7 +81,7 @@ struct Crafted { }; fs::path write_dir(const Crafted& m, const std::string& tag) { - fs::path dir = fs::temp_directory_path() / ("npu_moe_test_" + std::to_string(::getpid()) + tag); + fs::path dir = ::onebit::test::tmp_root() / ("npu_moe_test_" + std::to_string(::getpid()) + tag); fs::remove_all(dir); fs::create_directories(dir); const uint64_t chunk = uint64_t(m.tiles) * kTileBytes; diff --git a/tests/npu_pack_import_test.cpp b/tests/npu_pack_import_test.cpp index a2656f574..8647fece7 100644 --- a/tests/npu_pack_import_test.cpp +++ b/tests/npu_pack_import_test.cpp @@ -53,6 +53,7 @@ #include #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace { @@ -799,7 +800,7 @@ void test_external_import_success(const std::string& root) { // alone, so what can go wrong is the test being too willing -- a truncated copy from an interrupted run, a leftover // directory, a copy from a bigger pack. Those are the arms. void test_canonical_copy_reusable() { - const fs::path dir = fs::temp_directory_path() / ("1bit-reuse-" + std::to_string(::getpid())); + const fs::path dir = ::onebit::test::tmp_root() / ("1bit-reuse-" + std::to_string(::getpid())); std::error_code ec; fs::remove_all(dir, ec); fs::create_directories(dir); @@ -836,7 +837,7 @@ void test_canonical_copy_reusable() { // changes, so 699 files / 8.7 GiB of tmpfs (RAM) had built up on one box. The eviction rule is "same pack and geometry, // different source identity", and what can go wrong is deleting one of the neighbours that must survive, so each is an arm. void test_evict_stale_canonical_copies() { - const fs::path dir = fs::temp_directory_path() / ("1bit-evict-" + std::to_string(::getpid())); + const fs::path dir = ::onebit::test::tmp_root() / ("1bit-evict-" + std::to_string(::getpid())); std::error_code ec; fs::remove_all(dir, ec); fs::create_directories(dir); @@ -930,7 +931,7 @@ void test_evict_stale_canonical_copies() { // again, and 17.0 GiB of copies left there by older binaries stayed in tmpfs (RAM). Every candidate is swept now; one that does // not exist is not created. void test_sweep_fconst_scratch_dirs() { - const fs::path root = fs::temp_directory_path() / ("1bit-sweep-" + std::to_string(::getpid())); + const fs::path root = ::onebit::test::tmp_root() / ("1bit-sweep-" + std::to_string(::getpid())); std::error_code ec; fs::remove_all(root, ec); const fs::path written = root / "cache", other = root / "tmp", absent = root / "never-made"; @@ -996,8 +997,8 @@ void test_sweep_fconst_scratch_dirs() { // override set only that directory is swept, so the second directory's copy must still be there. void test_sweep_is_wired(const std::string& root) { const fs::path dir = fs::path(root) / "sweep-wired"; - const fs::path scratch = fs::temp_directory_path() / ("1bit-sweep-wired-" + std::to_string(::getpid())); - const fs::path cache_home = fs::temp_directory_path() / ("1bit-sweep-wired-xdg-" + std::to_string(::getpid())); + const fs::path scratch = ::onebit::test::tmp_root() / ("1bit-sweep-wired-" + std::to_string(::getpid())); + const fs::path cache_home = ::onebit::test::tmp_root() / ("1bit-sweep-wired-xdg-" + std::to_string(::getpid())); std::error_code ec; fs::remove_all(dir, ec); fs::remove_all(scratch, ec); @@ -1047,7 +1048,7 @@ void test_sweep_is_wired(const std::string& root) { } setenv("ONEBIT_FCONST_SCRATCH", scratch.c_str(), 1); setenv("XDG_CACHE_HOME", cache_home.c_str(), 1); - const std::string out = (fs::temp_directory_path() / ("oga_pack_import_sweepwired." + std::to_string(::getpid()))).string(); + const std::string out = (::onebit::test::tmp_root() / ("oga_pack_import_sweepwired." + std::to_string(::getpid()))).string(); fs::remove_all(out, ec); pi::Options allow; allow.allow_incomplete = true; @@ -1794,7 +1795,7 @@ void test_tied_block32_head(const std::string& root) { allow.allow_incomplete = true; { // arm 1: declared tied -> synthesised from the embedding, and the note says so const fs::path dir = build(true, "tied-block32"); - const std::string out = (fs::temp_directory_path() / + const std::string out = (::onebit::test::tmp_root() / ("oga_pack_import_tied32." + std::to_string(::getpid()))).string(); fs::remove_all(out); bool threw = false; @@ -1869,7 +1870,7 @@ void test_tied_block32_head(const std::string& root) { } { // arm 2: the same pack without the declaration -> refused by name, nothing written const fs::path dir = build(false, "untied-block32"); - const std::string out = (fs::temp_directory_path() / + const std::string out = (::onebit::test::tmp_root() / ("oga_pack_import_untied32." + std::to_string(::getpid()))).string(); fs::remove_all(out); bool threw = false; @@ -2017,7 +2018,7 @@ void test_block32_tail_head(const std::string& root) { const auto run = [&](const fs::path& dir, const char* tag) { Run x; const std::string out = - (fs::temp_directory_path() / + (::onebit::test::tmp_root() / (std::string("oga_pack_import_b32tail_") + tag + "." + std::to_string(::getpid()))).string(); fs::remove_all(out); pi::Options allow; @@ -2204,7 +2205,7 @@ void test_final_norm_envelope(const std::string& root) { (geom.why.empty() ? "" : ", why: " + geom.why) + ")"); pi::Options allow; allow.allow_incomplete = true; - const std::string out = (fs::temp_directory_path() / + const std::string out = (::onebit::test::tmp_root() / ("oga_pack_import_norm." + std::to_string(::getpid()))).string(); fs::remove_all(out); const pi::Report r = pi::import_pack(dir.string(), out, allow); @@ -2355,7 +2356,7 @@ void test_pack(const std::string& pack, const std::string& golden_dir) { // a fully representable pack imports without --allow-incomplete; a partial one writes nothing. // Per-process scratch dir: two concurrent runs of this binary (or two ctest shards) otherwise write // into the same directory and one dies with "cannot write .../model.q4nx" (measured 2026-11-03). - const std::string out = (fs::temp_directory_path() / + const std::string out = (::onebit::test::tmp_root() / ("oga_pack_import_test." + std::to_string(::getpid()))).string(); fs::remove_all(out); const pi::Report r0 = pi::import_pack(pack, out, pi::Options{}); @@ -2745,7 +2746,7 @@ void test_import_fp32_params(const std::string& root) { {pb::matmulnbits("lm_head", {"x", "lm_head_qweight", "lm_head_scales", "lm_head_zeros"}, H, V, 4, block)})); - const std::string out = (fs::temp_directory_path() / + const std::string out = (::onebit::test::tmp_root() / ("oga_pack_import_fp32." + std::to_string(::getpid()))).string(); fs::remove_all(out); const pi::Report r = pi::import_pack(dir.string(), out, pi::Options{}); @@ -2852,7 +2853,7 @@ void test_import_fp32_params(const std::string& root) { // than misread it (npu/forward/src/model.c). Q4nxNpuForward::init_impl converts first through // npu/q4nx_device.cpp; this is that conversion, and the proof it changes only the parameter // width: the device tiles are byte-identical to the old bf16 container's for the same weights. - const std::string cache = (fs::temp_directory_path() / + const std::string cache = (::onebit::test::tmp_root() / ("oga_q4nx_device_form." + std::to_string(::getpid()))).string(); fs::remove_all(cache); { @@ -3005,7 +3006,7 @@ void test_hybrid_pack_import(const std::string& root) { int64_t(H) * H * 4)}, {})); put_file((dir / "dd_metastate_Llm_Token_Token_crafted.fconst").string(), fc); - const std::string out = (fs::temp_directory_path() / + const std::string out = (::onebit::test::tmp_root() / ("oga_pack_import_hybrid." + std::to_string(::getpid()))).string(); fs::remove_all(out); pi::Options allow; @@ -3192,7 +3193,7 @@ void test_genre_a_projection_gap(const std::string& root) { R"("rope_theta":10000.0,"tie_word_embeddings":false,"rms_norm_eps":1e-06})"); (void)groups; }; - const std::string out = (fs::temp_directory_path() / ("oga_pack_genre_a." + std::to_string(::getpid()))).string(); + const std::string out = (::onebit::test::tmp_root() / ("oga_pack_genre_a." + std::to_string(::getpid()))).string(); auto names_down = [](const pi::Report& r) { for (const auto& s : r.refused) if (s.find("mlp.down_proj.weight") != std::string::npos && s.find("no quantised projection the decoder can pair") != std::string::npos) @@ -3678,7 +3679,7 @@ void test_post_attn_norm_header(const std::string& root) { pi::Options allow; allow.allow_incomplete = true; // the crafted block has no tiles, so the payload legs are refused - const std::string out = (fs::temp_directory_path() / + const std::string out = (::onebit::test::tmp_root() / ("oga_pack_import_pa." + std::to_string(::getpid()))).string(); fs::remove_all(out); try { @@ -3749,7 +3750,7 @@ void test_kernel_set_refusal() { // missing tokenizer -- the misleading report #1751 was written to remove. A file the pack has and cannot copy is refused // BY NAME; a file the pack does not ship is not a failure. void test_copy_text_artifacts() { - const fs::path base = fs::temp_directory_path() / ("1bit-textcopy-" + std::to_string(::getpid())); + const fs::path base = ::onebit::test::tmp_root() / ("1bit-textcopy-" + std::to_string(::getpid())); std::error_code ec; fs::remove_all(base, ec); const fs::path pack = base / "pack", out = base / "out"; @@ -4498,7 +4499,7 @@ void test_per_layer_import(const std::string& root) { put_file((dir / "genai_config.json").string(), R"({"model":{"embedding":{"filename":"embedding/model.onnx"}}})"); const std::string out = - (fs::temp_directory_path() / ("oga_pack_import_perlayer." + std::to_string(::getpid()))).string(); + (::onebit::test::tmp_root() / ("oga_pack_import_perlayer." + std::to_string(::getpid()))).string(); fs::remove_all(out); pi::Options allow; allow.allow_incomplete = true; @@ -4591,7 +4592,7 @@ void test_fconst_variable_stride(const std::string& root) { put_file((dir / "model.onnx.data").string(), std::string(4096, '\x11')); pi::Options allow; allow.allow_incomplete = true; - const std::string out = (fs::temp_directory_path() / ("varstride_out." + std::to_string(::getpid()))).string(); + const std::string out = (::onebit::test::tmp_root() / ("varstride_out." + std::to_string(::getpid()))).string(); fs::remove_all(out); bool refused = false; std::string what; @@ -4813,7 +4814,7 @@ void test_header_witness_refusal() { int main(int argc, char** argv) { // engine#1907: every crafted 1.9 import writes a canonical copy into the scratch cache. Without this the suite left 0-byte // copies in the user's real ~/.cache/1bit/fconst19 and in /tmp on every run. Give the run its own directory, removed at exit. - const fs::path run_scratch = fs::temp_directory_path() / ("1bit-pack-test-scratch-" + std::to_string(::getpid())); + const fs::path run_scratch = ::onebit::test::tmp_root() / ("1bit-pack-test-scratch-" + std::to_string(::getpid())); if (!std::getenv("ONEBIT_FCONST_SCRATCH")) setenv("ONEBIT_FCONST_SCRATCH", run_scratch.c_str(), 1); struct ScratchCleanup { fs::path p; diff --git a/tests/npu_plan_test.cpp b/tests/npu_plan_test.cpp index 50001ed94..c9518f318 100644 --- a/tests/npu_plan_test.cpp +++ b/tests/npu_plan_test.cpp @@ -27,6 +27,7 @@ #include #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace fs = std::filesystem; using onebit::npu::plan::Plan; @@ -42,7 +43,7 @@ void check(bool ok, const std::string& what) { } std::string write_plan(const std::string& name, const nlohmann::json& body) { - const fs::path p = fs::temp_directory_path() / ("npu_plan_test_" + name + ".json"); + const fs::path p = ::onebit::test::tmp_root() / ("npu_plan_test_" + name + ".json"); std::ofstream f(p); f << body.dump(2) << "\n"; f.close(); @@ -501,7 +502,7 @@ void test_artifacts() { return p; }; const Plan p = Plan::load(write_plan("artifacts", with_designs())); - const fs::path root = fs::temp_directory_path() / "npu_plan_test_artifacts"; + const fs::path root = ::onebit::test::tmp_root() / "npu_plan_test_artifacts"; const fs::path kdir = root / "kernels"; std::error_code ec; fs::remove_all(root, ec); diff --git a/tests/npu_private_route_test.cpp b/tests/npu_private_route_test.cpp index c3675a278..e2a35dfd8 100644 --- a/tests/npu_private_route_test.cpp +++ b/tests/npu_private_route_test.cpp @@ -27,6 +27,7 @@ #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace { @@ -70,7 +71,7 @@ void no_addon_refuses_the_option() { int main() { no_addon_refuses_the_option(); namespace fs = std::filesystem; - const fs::path dir = fs::temp_directory_path() / ("npu_private_route_test." + std::to_string(::getpid())); + const fs::path dir = ::onebit::test::tmp_root() / ("npu_private_route_test." + std::to_string(::getpid())); fs::create_directories(dir); std::ofstream(dir / "config.json") << R"({"model_type": "qwen3_5_moe", "num_hidden_layers": 40})"; check("model_type_of reads config.json", model_type_of(dir.string()) == "qwen3_5_moe"); diff --git a/tests/npu_target_caps_test.cpp b/tests/npu_target_caps_test.cpp index 1c34f85f3..2cbb5bef5 100644 --- a/tests/npu_target_caps_test.cpp +++ b/tests/npu_target_caps_test.cpp @@ -33,6 +33,7 @@ #include #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace fs = std::filesystem; using onebit::npu::plan::Plan; @@ -68,7 +69,7 @@ nlohmann::json base_plan() { } std::string write_plan(const std::string& name, const nlohmann::json& body) { - const fs::path p = fs::temp_directory_path() / ("npu_target_caps_test_" + name + ".json"); + const fs::path p = ::onebit::test::tmp_root() / ("npu_target_caps_test_" + name + ".json"); std::ofstream f(p); f << body.dump(2) << "\n"; f.close(); diff --git a/tests/q4nx_cache_test.cpp b/tests/q4nx_cache_test.cpp index 32cd463a0..87558cf39 100644 --- a/tests/q4nx_cache_test.cpp +++ b/tests/q4nx_cache_test.cpp @@ -25,6 +25,7 @@ #include #include #include +#include "tmp_root.h" // engine#2029: scratch goes on disk, not in tmpfs namespace fs = std::filesystem; static int failures = 0; @@ -44,7 +45,7 @@ static void make_entry(const fs::path& root, const std::string& name, int mb, in int main() { std::error_code ec; - const fs::path root = fs::temp_directory_path() / ("q4nx-cache-test-" + std::to_string(::getpid())); + const fs::path root = ::onebit::test::tmp_root() / ("q4nx-cache-test-" + std::to_string(::getpid())); fs::remove_all(root, ec); // (a) under the bound: nothing is removed diff --git a/tests/tmp_root.h b/tests/tmp_root.h new file mode 100644 index 000000000..d3000188e --- /dev/null +++ b/tests/tmp_root.h @@ -0,0 +1,44 @@ +// Copyright 2026 bong-water-water-bong +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// tmp_root.h -- where a test puts its scratch. +// +// /tmp on this box is **tmpfs**, i.e. RAM, and swap has been full for hours (owner directive, 2026-11-10 21:15Z). A test +// that writes containers there -- `npu_pack_import_test` builds multi-hundred-MB packs, and every crafted 1.9 import +// writes a canonical `.fconst` copy -- spends memory that a GPU/NPU lane needs, and a leaked directory stays spent until +// something deletes it. +// +// So scratch goes on disk under `~/.cache/scratch-tests/tmp` unless `ONEBIT_TEST_TMP` names somewhere else; `temp_directory_path()` +// is the last resort (no HOME, unwritable cache), never the default. +#pragma once + +#include +#include + +namespace onebit::test { + +inline std::filesystem::path tmp_root() { + if (const char* e = std::getenv("ONEBIT_TEST_TMP"); e && *e) return std::filesystem::path(e); + if (const char* h = std::getenv("HOME"); h && *h) { + std::error_code ec; + const std::filesystem::path p = std::filesystem::path(h) / ".cache" / "scratch-tests" / "tmp"; + std::filesystem::create_directories(p, ec); + // An existing directory is what we want; a create that failed because it is already there is fine too. + if (std::filesystem::is_directory(p, ec) && !ec) return p; + } + return std::filesystem::temp_directory_path(); +} + +} // namespace onebit::test