Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ int llama_download(int argc, char ** argv) {
return 1;
}

const bool has_source = !params.model.hf_repo.empty() || !params.model.url.empty() ||
!params.model.path.empty() || !params.model.docker_repo.empty();
const bool has_source = !params.model.hf_repo.empty() || !params.model.ms_repo.empty() ||
!params.model.url.empty() || !params.model.path.empty() ||
!params.model.docker_repo.empty();
if (!has_source) {
fprintf(stderr, "error: no model source specified (use --hf-repo, --model-url, --model or --docker-repo)\n");
fprintf(stderr, "error: no model source specified (use --hf-repo, --ms-repo, --model-url, --model or --docker-repo)\n");
return 1;
}

Expand Down
2 changes: 2 additions & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ add_library(${TARGET}
fit.h
hf-cache.cpp
hf-cache.h
ms-cache.cpp
ms-cache.h
http.h
imatrix-loader.cpp
imatrix-loader.h
Expand Down
98 changes: 96 additions & 2 deletions common/arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ static bool spec_types_is_default(const common_params & params) {
common_models_handler common_models_handler_init(const common_params & params, llama_example curr_ex) {
common_download_hf_plan plan;
common_download_hf_plan plan_spec;
common_download_ms_plan plan_ms;
common_download_opts opts;

const bool spec_type_draft_mtp = std::find(params.speculative.types.begin(),
Expand Down Expand Up @@ -413,7 +414,11 @@ common_models_handler common_models_handler_init(const common_params & params, l
plan_spec = common_download_get_hf_plan(params.speculative.draft.mparams, opts_spec);
}

return common_models_handler{plan, plan_spec, opts};
if (!params.model.ms_repo.empty()) {
plan_ms = common_download_get_ms_plan(params.model, opts);
}

return common_models_handler{plan, plan_spec, plan_ms, opts};
}

bool common_models_handler_is_preset_repo(const common_models_handler & handler) {
Expand Down Expand Up @@ -463,6 +468,7 @@ void common_models_handler_apply(common_models_handler & handler, common_params

auto & plan = handler.plan;
auto & plan_spec = handler.plan_spec;
auto & plan_ms = handler.plan_ms;

auto opts = handler.opts; // copy
opts.callback = callback;
Expand Down Expand Up @@ -683,6 +689,77 @@ void common_models_handler_apply(common_models_handler & handler, common_params
});
}


// handle ms_plan tasks
// MS authenticates via a cookie, so attach it only to MS tasks (keep HF tasks on bearer)
common_download_opts ms_opts = opts;
if (!params.model.ms_repo.empty() && !opts.bearer_token.empty()) {
ms_opts.headers.emplace_back("Cookie", "m_session_id=" + opts.bearer_token);
}
auto add_ms_tasks = [&ms_opts, &tasks](const ms_cache::ms_files & model_files,
const ms_cache::ms_file & primary,
common_params_model & model) {
for (size_t i = 0; i < model_files.size(); ++i) {
auto & model_file = model_files[i];
bool is_primary = (model_file.path == primary.path);
tasks.emplace_back(model_file, ms_opts, [&model_file, &is_primary, &model]() {
if (is_primary) {
// the primary file is the first split (00001-of), use it as model path
model.path = ms_cache::finalize_file(model_file);
} else {
ms_cache::finalize_file(model_file);
}
});
}
};
if (!plan_ms.model_files.empty()) {
add_ms_tasks(plan_ms.model_files, plan_ms.primary, params.model);
}
if (!plan_ms.mmproj.local_path.empty()) {
tasks.emplace_back(plan_ms.mmproj, ms_opts, [&]() {
params.mmproj.path = ms_cache::finalize_file(plan_ms.mmproj);
});
}
if (!plan_ms.mtp.local_path.empty() && !had_spec_url) {
tasks.emplace_back(plan_ms.mtp, ms_opts, [&]() {
// only fall back to the discovered MTP head when no draft was explicitly provided
if (params.speculative.draft.mparams.empty()) {
params.speculative.draft.mparams.path = ms_cache::finalize_file(plan_ms.mtp);
} else {
ms_cache::finalize_file(plan_ms.mtp);
}
});
}
if (!plan_ms.dflash.local_path.empty() && !had_spec_url) {
tasks.emplace_back(plan_ms.dflash, ms_opts, [&]() {
// only fall back to the discovered DFlash sidecar when no draft was explicitly provided
if (params.speculative.draft.mparams.empty()) {
params.speculative.draft.mparams.path = ms_cache::finalize_file(plan_ms.dflash);
} else {
ms_cache::finalize_file(plan_ms.dflash);
}
});
}
if (!plan_ms.eagle3.local_path.empty() && !had_spec_url) {
tasks.emplace_back(plan_ms.eagle3, ms_opts, [&]() {
// only fall back to the discovered Eagle3 sidecar when no draft was explicitly provided
if (params.speculative.draft.mparams.empty()) {
params.speculative.draft.mparams.path = ms_cache::finalize_file(plan_ms.eagle3);
} else {
ms_cache::finalize_file(plan_ms.eagle3);
}
});
}
if (!plan_ms.dspark.local_path.empty() && !had_spec_url) {
tasks.emplace_back(plan_ms.dspark, ms_opts, [&]() {
// only fall back to the discovered DSpark sidecar when no draft was explicitly provided
if (params.speculative.draft.mparams.empty()) {
params.speculative.draft.mparams.path = ms_cache::finalize_file(plan_ms.dspark);
} else {
ms_cache::finalize_file(plan_ms.dspark);
}
});
}
// run all tasks in parallel
if (!params.offline) {
// if duplicated files are found, only download once (but still call on_done for each task)
Expand Down Expand Up @@ -803,6 +880,13 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
}
}

// ModelScope authentication token (passed as a cookie, reusing hf_token as the carrier)
if (params.hf_token.empty()) {
if (const char * ms_token = std::getenv("MS_TOKEN")) {
params.hf_token = ms_token;
}
}

// handle command line arguments
auto check_arg = [&](int i) {
if (i+1 >= argc) {
Expand Down Expand Up @@ -1459,7 +1543,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
auto models = common_list_cached_models();
printf("number of models in cache: %zu\n", models.size());
for (size_t i = 0; i < models.size(); i++) {
printf("%4zu. %s\n", i + 1, models[i].to_string().c_str());
printf("%4zu. [%s] %s\n", i + 1, models[i].source.c_str(), models[i].to_string().c_str());
}
exit(0);
}
Expand Down Expand Up @@ -3047,6 +3131,16 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
params.model.hf_file = value;
}
).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_DOWNLOAD, LLAMA_EXAMPLE_TOKENIZE}).set_env("LLAMA_ARG_HF_FILE"));
add_opt(common_arg(
{"-ms", "-msr", "--ms-repo"}, "<user>/<model>[:quant]",
"ModelScope model repository; quant is optional, case-insensitive, default to Q4_K_M, or falls back to the first file in the repo if Q4_K_M doesn't exist.\n"
"mmproj is also downloaded automatically if available. to disable, add --no-mmproj\n"
"example: Qwen/Qwen3-0.6B-GGUF:Q4_K_M\n"
"(default: unused)",
[](common_params & params, const std::string & value) {
params.model.ms_repo = value;
}
).set_examples({LLAMA_EXAMPLE_COMMON, LLAMA_EXAMPLE_DOWNLOAD}).set_env("LLAMA_ARG_MS_REPO"));
add_opt(common_arg(
{"-hft", "--hf-token"}, "TOKEN",
"Hugging Face access token (default: value from HF_TOKEN environment variable)",
Expand Down
1 change: 1 addition & 0 deletions common/arg.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void common_params_add_preset_options(std::vector<common_arg> & args);
struct common_models_handler {
common_download_hf_plan plan;
common_download_hf_plan plan_spec;
common_download_ms_plan plan_ms;
common_download_opts opts;
};

Expand Down
4 changes: 4 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,15 @@ struct common_params_model {
std::string hf_repo = ""; // HF repo
std::string hf_file = ""; // HF file
std::string docker_repo = ""; // Docker repo
std::string ms_repo = ""; // ModelScope repo

std::string get_name() const {
if (!hf_repo.empty()) {
return hf_repo;
}
if (!ms_repo.empty()) {
return ms_repo;
}
if (!docker_repo.empty()) {
return docker_repo;
}
Expand Down
Loading