rad-convert: name the checkpoint tensors no declared weight reads

They are not written. A tower or head the plugin does not recognise was
left out without a word. A tensor beside one that is read (its scale)
counts as read.
This commit is contained in:
keptn42 committed 2026-11-04 07:20:29 +01:00
1 parent 326443ca89
commit 9782a9afb5
2 files changed
+52

No files matched your search

+28
View File
@@ -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<tiny::Tensor> 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<uint8_t>(32, 0) });
ts.push_back({ "model.layers.0.mlp.down_proj.weight_scale", "BF16", { 1 },
std::vector<uint8_t>(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
+24
View File
@@ -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<std::string> 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<std::string> 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)++;