diff --git a/tests/convert_test.cpp b/tests/convert_test.cpp index 8e2de6a8..0af15b8c 100644 --- a/tests/convert_test.cpp +++ b/tests/convert_test.cpp @@ -615,6 +615,34 @@ TEST(lora_map_refusals_name_the_target_the_sums_or_the_flag) { CHECK(!exists(d / "c.rad")); } +/* A checkpoint tensor no declared weight reads is not written (a tower or head the plugin does not + * know), and the run names it; a tensor beside one that is read (its scale) is not named, nor is + * anything when every tensor is read. */ +TEST(a_checkpoint_tensor_no_weight_reads_is_named) { + World& w = world(); + REQUIRE(w.ok); + CHECK(w.log.find("read by no declared weight") == std::string::npos); + tiny::TempDir d("convert_u"); + const std::string ck = d / "llama"; + REQUIRE(mkdir(ck.c_str(), 0755) == 0); + for (const char* f : { "config.json", "tokenizer.json" }) + REQUIRE(copy_file(w.ckpt + "/" + f, ck + "/" + f)); + std::vector ts = tiny::read_safetensors(w.ckpt + "/model.safetensors"); + const size_t n = ts.size(); + ts.push_back({ "model.vision_embedder.patch_dense.weight", "BF16", { 4, 4 }, + std::vector(32, 0) }); + ts.push_back({ "model.layers.0.mlp.down_proj.weight_scale", "BF16", { 1 }, + std::vector(2, 0) }); + REQUIRE(tiny::write_safetensors(ck + "/model.safetensors", ts)); + std::string log; + REQUIRE_EQ(convert(ck, { "-o", d / "u.rad", "--recipe", w.recipe }, &log), 0); + const std::string want = "1 of " + std::to_string(n + 2) + " checkpoint tensors are read by no " + "declared weight and are not written: model.vision_embedder.patch_dense.weight"; + if (log.find(want) == std::string::npos) fprintf(stderr, " log:\n%s\n", log.c_str()); + CHECK(log.find(want) != std::string::npos); + CHECK(log.find("down_proj.weight_scale") == std::string::npos); +} + /* ================================================================== --in-place */ /* Extending a container with nothing new keeps every weight at its offset, and the .pre-append diff --git a/tools/rad_convert.cpp b/tools/rad_convert.cpp index 76c57de1..d56c3e19 100644 --- a/tools/rad_convert.cpp +++ b/tools/rad_convert.cpp @@ -1005,6 +1005,30 @@ int main(int argc, char** argv) { if (L.declared && L.status == RAD_OK) plan.push_back(&L); std::sort(plan.begin(), plan.end(), plan_less); + /* The checkpoint tensors no planned weight reads. Only declared weights are written, so a + * tower or head the plugin does not recognise is left out without an error; this says so. A + * tensor beside a read one (`X.weight_scale` beside `X.weight`) counts as read. */ + { + auto stem = [](const std::string& n) { return n.substr(0, n.rfind('.')); }; + std::unordered_set read, stems; + for (const RadNameMap& nm : prog.name_map) { + const auto it = nm.declared ? logical.find(nm.declared) : logical.end(); + if (it == logical.end() || !it->second.declared || it->second.status != RAD_OK) continue; + for (int k = 0; k < nm.n_src; ++k) + if (nm.src[k]) { read.insert(nm.src[k]); stems.insert(stem(nm.src[k])); } + } + std::vector unread; + for (const SrcTensor& t : ck.tensors()) + if (!read.count(t.name) && !stems.count(stem(t.name))) unread.push_back(t.name); + if (!unread.empty()) { + std::string list; + for (size_t i = 0; i < unread.size() && i < 8; ++i) list += (i ? ", " : "") + unread[i]; + RAD_WARN("%zu of %zu checkpoint tensors are read by no declared weight and are not " + "written: %s%s", unread.size(), ck.tensors().size(), list.c_str(), + unread.size() > 8 ? ", ..." : ""); + } + } + { int64_t n_q = 0, n_kept = 0, n_imp = 0; for (const Logical* L : plan) (L->rule ? n_q : L->src.imported ? n_imp : n_kept)++;