Skip to content

Commit a996e2f

Browse files
committed
add arguments
1 parent 95a5274 commit a996e2f

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,24 @@ The [Hugging Face](https://huggingface.co) platform hosts a [number of LLMs](htt
265265

266266
You can either manually download the GGUF file or directly use any `llama.cpp`-compatible models from Hugging Face by using this CLI argument: `-hf <user>/<model>[:quant]`
267267

268-
llama.cpp also supports downloading and running models from [ModelScope](https://www.modelscope.cn/home), just add an env variable: `LLAMACPP_USE_MODELSCOPE=True` to your command with the same arguments(like `-hf <user>/<model>[:quant]`).
268+
llama.cpp also supports downloading and running models from [ModelScope](https://www.modelscope.cn/home), there are two ways to use models in ModelScope:
269+
270+
1. Add an env variable: `LLAMACPP_USE_MODELSCOPE=True` to your command with the same arguments of Hugging Face(like `-hf <user>/<model>[:quant]`).
269271

270272
```shell
271273
LLAMACPP_USE_MODELSCOPE=True llama-cli -hf Qwen/QwQ-32B-GGUF
272274
```
273275

274-
Pay attention to change the model repo to the **existing repo** of ModelScope. If you want to use a private repo, please make sure you have the rights of the repo and run with the `--hf_token` argument:
276+
2. Use modelscope arguments instead of the ones of Hugging Face: `-ms <user>/<model>[:quant] -msf xxx.gguf -mst xxx_token`
277+
278+
```shell
279+
llama-cli -ms Qwen/QwQ-32B-GGUF
280+
```
281+
282+
Pay attention to change the model repo to the **existing repo** of ModelScope. If you want to use a private repo, please make sure you have the rights of the repo and run with the `--ms_token` argument:
275283

276284
```shell
277-
LLAMACPP_USE_MODELSCOPE=True llama-cli -hf Qwen/QwQ-32B-GGUF --hf_token xxx
285+
llama-cli -ms Qwen/QwQ-32B-GGUF --ms_token xxx
278286
```
279287

280288
> You can change the endpoint of ModelScope by using `MODELSCOPE_DOMAIN=xxx`(like MODELSCOPE_DOMAIN=www.modelscope.ai).

common/arg.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ common_arg & common_arg::set_env(const char * env) {
3434
return *this;
3535
}
3636

37+
static void set_ms_env() {
38+
#ifdef _WIN32
39+
SetEnvironmentVariableA("LLAMACPP_USE_MODELSCOPE", "True")
40+
#else
41+
setenv("LLAMACPP_USE_MODELSCOPE", "True", 1);
42+
#endif
43+
}
44+
3745
common_arg & common_arg::set_sparam() {
3846
is_sparam = true;
3947
return *this;
@@ -211,6 +219,10 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
211219
std::string arg;
212220
const std::string arg_prefix = "--";
213221
common_params & params = ctx_arg.params;
222+
std::vector<std::string> ms_params = {
223+
"-ms", "-msr", "--ms-repo",
224+
"-msv", "-msrv", "--ms-repo-v"
225+
};
214226

215227
std::unordered_map<std::string, common_arg *> arg_to_options;
216228
for (auto & opt : ctx_arg.options) {
@@ -224,6 +236,18 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
224236
std::string value;
225237
if (opt.get_value_from_env(value)) {
226238
try {
239+
for (auto msp: ms_params) {
240+
bool ms_arg = false;
241+
for (auto _arg: opt.args) {
242+
if (std::string(_arg) == msp) {
243+
ms_arg = true;
244+
}
245+
}
246+
if (ms_arg && !value.empty()) {
247+
set_ms_env();
248+
break;
249+
}
250+
}
227251
if (opt.handler_void && (value == "1" || value == "true")) {
228252
opt.handler_void(params);
229253
}
@@ -271,6 +295,15 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
271295
// arg with single value
272296
check_arg(i);
273297
std::string val = argv[++i];
298+
299+
for (auto msp: ms_params) {
300+
// Check whether is modelscope params
301+
if (msp == arg && !val.empty()) {
302+
set_ms_env();
303+
break;
304+
}
305+
}
306+
274307
if (opt.handler_int) {
275308
opt.handler_int(params, std::stoi(val));
276309
continue;
@@ -1849,6 +1882,43 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
18491882
params.hf_token = value;
18501883
}
18511884
).set_env("HF_TOKEN"));
1885+
add_opt(common_arg(
1886+
{"-ms", "-msr", "--ms-repo"}, "<user>/<model>[:quant]",
1887+
"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"
1888+
"example: unsloth/phi-4-GGUF:q4_k_m\n"
1889+
"(default: unused)",
1890+
[](common_params & params, const std::string & value) {
1891+
params.hf_repo = value;
1892+
}
1893+
).set_env("LLAMA_ARG_MS_REPO"));
1894+
add_opt(common_arg(
1895+
{"-msf", "--ms-file"}, "FILE",
1896+
"ModelScope model file. If specified, it will override the quant in --ms-repo (default: unused)",
1897+
[](common_params & params, const std::string & value) {
1898+
params.hf_file = value;
1899+
}
1900+
).set_env("LLAMA_ARG_MS_FILE"));
1901+
add_opt(common_arg(
1902+
{"-msv", "-msrv", "--ms-repo-v"}, "<user>/<model>[:quant]",
1903+
"ModelScope model repository for the vocoder model (default: unused)",
1904+
[](common_params & params, const std::string & value) {
1905+
params.vocoder.hf_repo = value;
1906+
}
1907+
).set_env("LLAMA_ARG_MS_REPO_V"));
1908+
add_opt(common_arg(
1909+
{"-msfv", "--ms-file-v"}, "FILE",
1910+
"ModelScope model file for the vocoder model (default: unused)",
1911+
[](common_params & params, const std::string & value) {
1912+
params.vocoder.hf_file = value;
1913+
}
1914+
).set_env("LLAMA_ARG_MS_FILE_V"));
1915+
add_opt(common_arg(
1916+
{"-mst", "--ms-token"}, "TOKEN",
1917+
"ModelScope access token (default: value from MS_TOKEN environment variable)",
1918+
[](common_params & params, const std::string & value) {
1919+
params.hf_token = value;
1920+
}
1921+
).set_env("MS_TOKEN"));
18521922
add_opt(common_arg(
18531923
{"--context-file"}, "FNAME",
18541924
"file to load context from (repeat to specify multiple files)",

common/common.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,8 +1229,6 @@ static bool common_download_file(const std::string & url, const std::string & pa
12291229
return false;
12301230
}
12311231

1232-
bool force_download = false;
1233-
12341232
// Set the URL, allow to follow http redirection
12351233
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
12361234
curl_easy_setopt(curl.get(), CURLOPT_FOLLOWLOCATION, 1L);
@@ -1329,6 +1327,7 @@ static bool common_download_file(const std::string & url, const std::string & pa
13291327
curl_easy_setopt(curl.get(), CURLOPT_HEADERFUNCTION, static_cast<CURLOPT_HEADERFUNCTION_PTR>(header_callback));
13301328
curl_easy_setopt(curl.get(), CURLOPT_HEADERDATA, &headers);
13311329
if (!LLAMACPP_USE_MODELSCOPE_DEFINITION) {
1330+
bool force_download = false;
13321331
bool was_perform_successful = curl_perform_with_retry(url, curl.get(), CURL_MAX_RETRY, CURL_RETRY_DELAY_SECONDS);
13331332
if (!was_perform_successful) {
13341333
return false;

0 commit comments

Comments
 (0)