diff --git a/core/hf_model.cpp b/core/hf_model.cpp index 5f3d5f0a..f84047ed 100644 --- a/core/hf_model.cpp +++ b/core/hf_model.cpp @@ -97,18 +97,30 @@ std::string hf_default_download_dir() { std::string hf_served_path(const std::string& dir, std::string* why) { std::vector rad; + int64_t other_fmt = 0; std::error_code ec; for (const fs::directory_entry& e : fs::directory_iterator(dir, ec)) { std::error_code fe; - if (e.is_regular_file(fe) && e.path().filename().string().ends_with(".rad")) - rad.push_back(e.path().filename().string()); + if (!e.is_regular_file(fe)) continue; + const std::string name = e.path().filename().string(); + if (name.ends_with(".rad")) rad.push_back(name); + /* A repo fetched with hfget holds its source weights beside anything the operator or a + * conversion added later; a container sitting among them is not self-appointing. */ + else if (name.ends_with(".safetensors") || name.ends_with(".gguf")) ++other_fmt; } if (ec) { *why = dir + ": " + ec.message(); return std::string(); } if (rad.empty()) return dir; - if (rad.size() == 1) return dir + "/" + rad[0]; + if (rad.size() == 1) { + if (other_fmt > 0) { + *why = dir + " holds the container " + rad[0] + " beside safetensors or gguf weights; " + + "pass the one to serve as --model " + dir + "/"; + return std::string(); + } + return dir + "/" + rad[0]; + } std::string names; for (size_t i = 0; i < rad.size(); ++i) names += (i ? ", " : "") + rad[i]; *why = dir + " holds " + std::to_string(rad.size()) + " containers (" + names + diff --git a/scripts/hfget.sh b/scripts/hfget.sh index 8c65e493..56205a7c 100755 --- a/scripts/hfget.sh +++ b/scripts/hfget.sh @@ -147,7 +147,7 @@ bad=$(printf '%s' "$listing" | jq -r '[.[] | select(.type == "file") [ -n "$bad" ] && { echo "hfget: the listing names '$bad', which does not land inside the destination" >&2; exit 1; } have_st=$(printf '%s' "$listing" | jq '[.[] | select(.path | endswith(".safetensors"))] | length') sel='.[] | select(.type == "file")' -[ "$have_st" -gt 0 ] && sel="$sel"' | select((.path | test("\\.(bin|msgpack|h5|onnx|onnx_data|gguf)$") or test("(^|/)(pytorch_model|consolidated)[^/]*\\.pth$")) | not)' +[ "$have_st" -gt 0 ] && sel="$sel"' | select((.path | test("\\.(bin|msgpack|h5|onnx|onnx_data|gguf|rad)$") or test("(^|/)(pytorch_model|consolidated)[^/]*\\.pth$")) | not)' list=$(mktemp "${TMPDIR:-/tmp}/hfget.XXXXXX") trap 'rm -f "$list" "$dest/.hfget-manifest.tmp"' EXIT INT TERM diff --git a/tests/hf_model_test.cpp b/tests/hf_model_test.cpp index 930011be..3a25c37e 100644 --- a/tests/hf_model_test.cpp +++ b/tests/hf_model_test.cpp @@ -108,6 +108,30 @@ TEST(two_containers_are_refused_by_name) { CHECK(why.find("--model") != std::string::npos); } +/* A repo fetched with hfget holds its source weights in the directory; a lone .rad among them + * -- whoever put it there -- must not be served in their place without one word said. The + * engine names the ambiguity instead of resolving it by rank. */ +TEST(a_container_beside_other_weights_is_not_served_silently) { + TempDir t; + t.touch("model-00001-of-00002.safetensors"); + t.touch("model-00002-of-00002.safetensors"); + t.touch("model.safetensors.index.json"); + t.touch("decoy.rad"); + std::string why; + CHECK_EQ(hf_served_path(t.path, &why), std::string()); + CHECK(why.find("decoy.rad") != std::string::npos); + CHECK(why.find("--model") != std::string::npos); + /* gguf competes too, and without any competitor the lone container is still the answer. */ + TempDir g; + g.touch("model.gguf"); + g.touch("converted.rad"); + CHECK_EQ(hf_served_path(g.path, &why), std::string()); + TempDir r; + r.touch("qwen.rad"); + r.touch("config.json"); + CHECK_EQ(hf_served_path(r.path, &why), r.path + "/qwen.rad"); +} + TEST(the_download_dir_defaults_to_the_xdg_cache) { const char* x = std::getenv("XDG_CACHE_HOME"); const std::string saved = x ? x : "";