Skip to content
Closed
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
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.cpp
ms.h
http.h
json-partial.cpp
json-partial.h
Expand Down
40 changes: 40 additions & 0 deletions common/arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "chat.h"
#include "common.h"
#include "download.h"
#include "ms.h"
#include "json-schema-to-grammar.h"
#include "log.h"
#include "sampling.h"
Expand Down Expand Up @@ -377,6 +378,27 @@ static handle_model_result common_params_handle_model(struct common_params_model
result.found_mtp = true;
result.mtp.path = download_result.mtp_path;
}
} else if (!model.ms_repo.empty()) {
// Handle ModelScope repository
// Split the repo ID to extract clean repo and quantization tag
auto [ms_repo, ms_tag] = common_download_split_repo_tag(model.ms_repo);

// Use the complete original value for model name
model.name = model.ms_repo;

auto download_result = ms::download_model(ms_repo, model.hf_file, offline, ms_tag, bearer_token);

if (download_result.model_path.empty()) {
throw std::runtime_error("failed to download model from ModelScope");
}

model.path = download_result.model_path;

// Set mmproj path if available
if (!download_result.mmproj_path.empty()) {
result.found_mmproj = true;
result.mmproj.path = download_result.mmproj_path;
}
} else if (!model.url.empty()) {
if (model.path.empty()) {
auto f = string_split<std::string>(model.url, '#').front();
Expand Down Expand Up @@ -515,6 +537,14 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
}
}

// Handle MS_TOKEN environment variable for ModelScope authentication
if (params.hf_token.empty()) {
const char * ms_token = std::getenv("MS_TOKEN");
if (ms_token) {
params.hf_token = ms_token;
}
}

// handle command line arguments
auto check_arg = [&](int i) {
if (i+1 >= argc) {
Expand Down Expand Up @@ -2660,6 +2690,16 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
params.hf_token = value;
}
).set_env("HF_TOKEN"));

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"
"example: Qwen/Qwen3-0.6B-GGUF:Q8_0\n"
"(default: unused)",
[](common_params & params, const std::string & value) {
params.model.ms_repo = value;
}
).set_env("LLAMA_ARG_MS_REPO"));
add_opt(common_arg(
{"--context-file"}, "FNAME",
"file to load context from (use comma-separated values to specify multiple files)",
Expand Down
1 change: 1 addition & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ struct common_params_model {
std::string url = ""; // model url to download // NOLINT
std::string hf_repo = ""; // HF repo // NOLINT
std::string hf_file = ""; // HF file // NOLINT
std::string ms_repo = ""; // ModelScope repo // NOLINT
std::string docker_repo = ""; // Docker repo // NOLINT
std::string name = ""; // in format <user>/<model>[:<tag>] (tag is optional) // NOLINT
};
Expand Down
11 changes: 2 additions & 9 deletions common/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,7 @@ int common_download_file_single(const std::string & url,
return 304; // Not Modified - fake cached response
}

struct gguf_split_info {
std::string prefix; // tag included
std::string tag;
int index;
int count;
};

static gguf_split_info get_gguf_split_info(const std::string & path) {
gguf_split_info get_gguf_split_info(const std::string & path) {
static const std::regex re_split("^(.+)-([0-9]{5})-of-([0-9]{5})$", std::regex::icase);
static const std::regex re_tag("[-.]([A-Z0-9_]+)$", std::regex::icase);
std::smatch m;
Expand Down Expand Up @@ -619,7 +612,7 @@ static hf_cache::hf_file find_best_mtp(const hf_cache::hf_files & files,
return find_best_sibling(files, model, "mtp-");
}

static bool gguf_filename_is_model(const std::string & filepath) {
bool gguf_filename_is_model(const std::string & filepath) {
if (!string_ends_with(filepath, ".gguf")) {
return false;
}
Expand Down
12 changes: 12 additions & 0 deletions common/download.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,15 @@ int common_download_file_single(const std::string & url,
// resolve and download model from Docker registry
// return local path to downloaded model file
std::string common_docker_resolve_model(const std::string & docker);

// shared by HF (download.cpp) and ModelScope (ms.cpp)
bool gguf_filename_is_model(const std::string & filepath);

struct gguf_split_info {
std::string prefix; // tag included
std::string tag;
int index;
int count;
};

gguf_split_info get_gguf_split_info(const std::string & path);
Loading