Skip to content

Commit 66cffa8

Browse files
committed
resolve merge conflicts
2 parents d905a9e + 504af20 commit 66cffa8

File tree

127 files changed

+8142
-6033
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+8142
-6033
lines changed

CONTRIBUTING.md

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Pull requests (for contributors)
22

33
- Test your changes:
4-
- Execute [the full CI locally on your machine](ci/README.md) before publishing
5-
- Verify that the perplexity and the performance are not affected negatively by your changes (use `llama-perplexity` and `llama-bench`)
6-
- If you modified the `ggml` source, run the `test-backend-ops` tool to check whether different backend implementations of the `ggml` operators produce consistent results (this requires access to at least two different `ggml` backends)
7-
- If you modified a `ggml` operator or added a new one, add the corresponding test cases to `test-backend-ops`
4+
- Execute [the full CI locally on your machine](ci/README.md) before publishing
5+
- Verify that the perplexity and the performance are not affected negatively by your changes (use `llama-perplexity` and `llama-bench`)
6+
- If you modified the `ggml` source, run the `test-backend-ops` tool to check whether different backend implementations of the `ggml` operators produce consistent results (this requires access to at least two different `ggml` backends)
7+
- If you modified a `ggml` operator or added a new one, add the corresponding test cases to `test-backend-ops`
88
- Consider allowing write access to your branch for faster reviews, as reviewers can push commits directly
99
- If your PR becomes stale, don't hesitate to ping the maintainers in the comments
1010

@@ -20,14 +20,104 @@
2020
- Avoid adding third-party dependencies, extra files, extra headers, etc.
2121
- Always consider cross-compatibility with other operating systems and architectures
2222
- Avoid fancy-looking modern STL constructs, use basic `for` loops, avoid templates, keep it simple
23-
- There are no strict rules for the code style, but try to follow the patterns in the code (indentation, spaces, etc.). Vertical alignment makes things more readable and easier to batch edit
23+
- Vertical alignment makes things more readable and easier to batch edit
2424
- Clean-up any trailing whitespaces, use 4 spaces for indentation, brackets on the same line, `void * ptr`, `int & a`
25-
- Naming usually optimizes for common prefix (see https://github.com/ggerganov/ggml/pull/302#discussion_r1243240963)
25+
- Use sized integer types such as `int32_t` in the public API, e.g. `size_t` may also be appropriate for allocation sizes or byte offsets
26+
- Declare structs with `struct foo {}` instead of `typedef struct foo {} foo`
27+
- In C++ code omit optional `struct` and `enum` keyword whenever they are not necessary
28+
```cpp
29+
// OK
30+
llama_context * ctx;
31+
const llama_rope_type rope_type;
32+
33+
// not OK
34+
struct llama_context * ctx;
35+
const enum llama_rope_type rope_type;
36+
```
37+
38+
_(NOTE: this guideline is yet to be applied to the `llama.cpp` codebase. New code should follow this guideline.)_
39+
40+
- Try to follow the existing patterns in the code (indentation, spaces, etc.). In case of doubt use `clang-format` to format the added code
41+
- For anything not covered in the current guidelines, refer to the [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines)
2642
- Tensors store data in row-major order. We refer to dimension 0 as columns, 1 as rows, 2 as matrices
2743
- Matrix multiplication is unconventional: [`C = ggml_mul_mat(ctx, A, B)`](https://github.com/ggerganov/llama.cpp/blob/880e352277fc017df4d5794f0c21c44e1eae2b84/ggml.h#L1058-L1064) means $C^T = A B^T \Leftrightarrow C = B A^T.$
2844
2945
![matmul](media/matmul.png)
3046
47+
# Naming guidelines
48+
49+
- Use `snake_case` for function, variable and type names
50+
- Naming usually optimizes for longest common prefix (see https://github.com/ggerganov/ggml/pull/302#discussion_r1243240963)
51+
52+
```cpp
53+
// not OK
54+
int small_number;
55+
int big_number;
56+
57+
// OK
58+
int number_small;
59+
int number_big;
60+
```
61+
62+
- Enum values are always in upper case and prefixed with the enum name
63+
64+
```cpp
65+
enum llama_vocab_type {
66+
LLAMA_VOCAB_TYPE_NONE = 0,
67+
LLAMA_VOCAB_TYPE_SPM = 1,
68+
LLAMA_VOCAB_TYPE_BPE = 2,
69+
LLAMA_VOCAB_TYPE_WPM = 3,
70+
LLAMA_VOCAB_TYPE_UGM = 4,
71+
LLAMA_VOCAB_TYPE_RWKV = 5,
72+
};
73+
```
74+
75+
- The general naming pattern is `<class>_<method>`, with `<method>` being `<action>_<noun>`
76+
77+
```cpp
78+
llama_model_init(); // class: "llama_model", method: "init"
79+
llama_sampler_chain_remove(); // class: "llama_sampler_chain", method: "remove"
80+
llama_sampler_get_seed(); // class: "llama_sampler", method: "get_seed"
81+
llama_set_embeddings(); // class: "llama_context", method: "set_embeddings"
82+
llama_n_threads(); // class: "llama_context", method: "n_threads"
83+
llama_adapter_lora_free(); // class: "llama_adapter_lora", method: "free"
84+
```
85+
86+
- The `get` `<action>` can be omitted
87+
- The `<noun>` can be omitted if not necessary
88+
- The `_context` suffix of the `<class>` is optional. Use it to disambiguate symbols when needed
89+
- Use `init`/`free` for constructor/destructor `<action>`
90+
91+
- Use the `_t` suffix when a type is supposed to be opaque to the user - it's not relevant to them if it is a struct or anything else
92+
93+
```cpp
94+
typedef struct llama_context * llama_context_t;
95+
96+
enum llama_pooling_type llama_pooling_type(const llama_context_t ctx);
97+
```
98+
99+
_(NOTE: this guideline is yet to be applied to the `llama.cpp` codebase. New code should follow this guideline)_
100+
101+
- C/C++ filenames are all lowercase with dashes. Headers use the `.h` extension. Source files use the `.c` or `.cpp` extension
102+
- Python filenames are all lowercase with underscores
103+
104+
- _(TODO: abbreviations usage)_
105+
106+
# Preprocessor directives
107+
108+
- _(TODO: add guidelines with examples and apply them to the codebase)_
109+
110+
```cpp
111+
#ifdef FOO
112+
#endif // FOO
113+
```
114+
115+
# Documentation
116+
117+
- Documentation is a community effort
118+
- When you need to look into the source code to figure out how to use an API consider adding a short summary to the header file for future reference
119+
- When you notice incorrect or outdated documentation, please update it
120+
31121
# Resources
32122
33123
The Github issues, PRs and discussions contain a lot of information that can be useful to get familiar with the codebase. For convenience, some of the more important information is referenced from Github projects:

README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Instructions for adding support for new models: [HOWTO-add-model.md](docs/develo
6969
- [x] [Qwen models](https://huggingface.co/models?search=Qwen/Qwen)
7070
- [x] [PLaMo-13B](https://github.com/ggerganov/llama.cpp/pull/3557)
7171
- [x] [Phi models](https://huggingface.co/models?search=microsoft/phi)
72+
- [x] [PhiMoE](https://github.com/ggerganov/llama.cpp/pull/11003)
7273
- [x] [GPT-2](https://huggingface.co/gpt2)
7374
- [x] [Orion 14B](https://github.com/ggerganov/llama.cpp/pull/5118)
7475
- [x] [InternLM2](https://huggingface.co/models?search=internlm2)
@@ -98,6 +99,7 @@ Instructions for adding support for new models: [HOWTO-add-model.md](docs/develo
9899
- [x] [Jais](https://huggingface.co/inceptionai/jais-13b-chat)
99100
- [x] [Bielik-11B-v2.3](https://huggingface.co/collections/speakleash/bielik-11b-v23-66ee813238d9b526a072408a)
100101
- [x] [RWKV-6](https://github.com/BlinkDL/RWKV-LM)
102+
- [x] [QRWKV-6](https://huggingface.co/recursal/QRWKV6-32B-Instruct-Preview-v0.1)
101103
- [x] [GigaChat-20B-A3B](https://huggingface.co/ai-sage/GigaChat-20B-A3B-instruct)
102104

103105
#### Multimodal
@@ -243,6 +245,8 @@ The [Hugging Face](https://huggingface.co) platform hosts a [number of LLMs](htt
243245
- [Trending](https://huggingface.co/models?library=gguf&sort=trending)
244246
- [LLaMA](https://huggingface.co/models?sort=trending&search=llama+gguf)
245247

248+
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]`
249+
246250
After downloading a model, use the CLI tools to run it locally - see below.
247251

248252
`llama.cpp` requires the model to be stored in the [GGUF](https://github.com/ggerganov/ggml/blob/master/docs/gguf.md) file format. Models in other data formats can be converted to GGUF using the `convert_*.py` Python scripts in this repo.
@@ -261,21 +265,12 @@ To learn more about model quantization, [read this documentation](examples/quant
261265
#### A CLI tool for accessing and experimenting with most of `llama.cpp`'s functionality.
262266

263267
- <details open>
264-
<summary>Run simple text completion</summary>
265-
266-
```bash
267-
llama-cli -m model.gguf -p "I believe the meaning of life is" -n 128
268-
269-
# I believe the meaning of life is to find your own truth and to live in accordance with it. For me, this means being true to myself and following my passions, even if they don't align with societal expectations. I think that's what I love about yoga – it's not just a physical practice, but a spiritual one too. It's about connecting with yourself, listening to your inner voice, and honoring your own unique journey.
270-
```
271-
272-
</details>
273-
274-
- <details>
275268
<summary>Run in conversation mode</summary>
276269

270+
Models with a built-in chat template will automatically activate conversation mode. If this doesn't occur, you can manually enable it by adding `-cnv` and specifying a suitable chat template with `--chat-template NAME`
271+
277272
```bash
278-
llama-cli -m model.gguf -p "You are a helpful assistant" -cnv
273+
llama-cli -m model.gguf
279274

280275
# > hi, who are you?
281276
# Hi there! I'm your helpful assistant! I'm an AI-powered chatbot designed to assist and provide information to users like you. I'm here to help answer your questions, provide guidance, and offer support on a wide range of topics. I'm a friendly and knowledgeable AI, and I'm always happy to help with anything you need. What's on your mind, and how can I assist you today?
@@ -287,17 +282,28 @@ To learn more about model quantization, [read this documentation](examples/quant
287282
</details>
288283

289284
- <details>
290-
<summary>Run with custom chat template</summary>
285+
<summary>Run in conversation mode with custom chat template</summary>
291286

292287
```bash
293-
# use the "chatml" template
294-
llama-cli -m model.gguf -p "You are a helpful assistant" -cnv --chat-template chatml
288+
# use the "chatml" template (use -h to see the list of supported templates)
289+
llama-cli -m model.gguf -cnv --chat-template chatml
295290
296291
# use a custom template
297-
llama-cli -m model.gguf -p "You are a helpful assistant" -cnv --in-prefix 'User: ' --reverse-prompt 'User:'
292+
llama-cli -m model.gguf -cnv --in-prefix 'User: ' --reverse-prompt 'User:'
298293
```
299294

300-
[Supported templates](https://github.com/ggerganov/llama.cpp/wiki/Templates-supported-by-llama_chat_apply_template)
295+
</details>
296+
297+
- <details>
298+
<summary>Run simple text completion</summary>
299+
300+
To disable conversation mode explicitly, use `-no-cnv`
301+
302+
```bash
303+
llama-cli -m model.gguf -p "I believe the meaning of life is" -n 128 -no-cnv
304+
305+
# I believe the meaning of life is to find your own truth and to live in accordance with it. For me, this means being true to myself and following my passions, even if they don't align with societal expectations. I think that's what I love about yoga – it's not just a physical practice, but a spiritual one too. It's about connecting with yourself, listening to your inner voice, and honoring your own unique journey.
306+
```
301307

302308
</details>
303309

common/arg.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,26 @@ std::string common_arg::to_string() {
130130

131131
static void common_params_handle_model_default(
132132
std::string & model,
133-
std::string & model_url,
133+
const std::string & model_url,
134134
std::string & hf_repo,
135-
std::string & hf_file) {
135+
std::string & hf_file,
136+
const std::string & hf_token) {
136137
if (!hf_repo.empty()) {
137138
// short-hand to avoid specifying --hf-file -> default it to --model
138139
if (hf_file.empty()) {
139140
if (model.empty()) {
140-
throw std::invalid_argument("error: --hf-repo requires either --hf-file or --model\n");
141+
auto auto_detected = common_get_hf_file(hf_repo, hf_token);
142+
if (auto_detected.first.empty() || auto_detected.second.empty()) {
143+
exit(1); // built without CURL, error message already printed
144+
}
145+
hf_repo = auto_detected.first;
146+
hf_file = auto_detected.second;
147+
} else {
148+
hf_file = model;
141149
}
142-
hf_file = model;
143-
} else if (model.empty()) {
150+
}
151+
// make sure model path is present (for caching purposes)
152+
if (model.empty()) {
144153
// this is to avoid different repo having same file name, or same file name in different subdirs
145154
std::string filename = hf_repo + "_" + hf_file;
146155
// to make sure we don't have any slashes in the filename
@@ -290,8 +299,8 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
290299
}
291300

292301
// TODO: refactor model params in a common struct
293-
common_params_handle_model_default(params.model, params.model_url, params.hf_repo, params.hf_file);
294-
common_params_handle_model_default(params.vocoder.model, params.vocoder.model_url, params.vocoder.hf_repo, params.vocoder.hf_file);
302+
common_params_handle_model_default(params.model, params.model_url, params.hf_repo, params.hf_file, params.hf_token);
303+
common_params_handle_model_default(params.vocoder.model, params.vocoder.model_url, params.vocoder.hf_repo, params.vocoder.hf_file, params.hf_token);
295304

296305
if (params.escape) {
297306
string_process_escapes(params.prompt);
@@ -768,15 +777,19 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
768777
).set_examples({LLAMA_EXAMPLE_MAIN, LLAMA_EXAMPLE_SERVER}));
769778
add_opt(common_arg(
770779
{"-cnv", "--conversation"},
771-
string_format(
772-
"run in conversation mode:\n"
773-
"- does not print special tokens and suffix/prefix\n"
774-
"- interactive mode is also enabled\n"
775-
"(default: %s)",
776-
params.conversation ? "true" : "false"
777-
),
780+
"run in conversation mode:\n"
781+
"- does not print special tokens and suffix/prefix\n"
782+
"- interactive mode is also enabled\n"
783+
"(default: auto enabled if chat template is available)",
784+
[](common_params & params) {
785+
params.conversation_mode = COMMON_CONVERSATION_MODE_ENABLED;
786+
}
787+
).set_examples({LLAMA_EXAMPLE_MAIN}));
788+
add_opt(common_arg(
789+
{"-no-cnv", "--no-conversation"},
790+
"force disable conversation mode (default: false)",
778791
[](common_params & params) {
779-
params.conversation = true;
792+
params.conversation_mode = COMMON_CONVERSATION_MODE_DISABLED;
780793
}
781794
).set_examples({LLAMA_EXAMPLE_MAIN}));
782795
add_opt(common_arg(
@@ -1590,21 +1603,23 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
15901603
}
15911604
).set_env("LLAMA_ARG_MODEL_URL"));
15921605
add_opt(common_arg(
1593-
{"-hfr", "--hf-repo"}, "REPO",
1594-
"Hugging Face model repository (default: unused)",
1606+
{"-hf", "-hfr", "--hf-repo"}, "<user>/<model>[:quant]",
1607+
"Hugging Face 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"
1608+
"example: unsloth/phi-4-GGUF:q4_k_m\n"
1609+
"(default: unused)",
15951610
[](common_params & params, const std::string & value) {
15961611
params.hf_repo = value;
15971612
}
15981613
).set_env("LLAMA_ARG_HF_REPO"));
15991614
add_opt(common_arg(
16001615
{"-hff", "--hf-file"}, "FILE",
1601-
"Hugging Face model file (default: unused)",
1616+
"Hugging Face model file. If specified, it will override the quant in --hf-repo (default: unused)",
16021617
[](common_params & params, const std::string & value) {
16031618
params.hf_file = value;
16041619
}
16051620
).set_env("LLAMA_ARG_HF_FILE"));
16061621
add_opt(common_arg(
1607-
{"-hfrv", "--hf-repo-v"}, "REPO",
1622+
{"-hfv", "-hfrv", "--hf-repo-v"}, "<user>/<model>[:quant]",
16081623
"Hugging Face model repository for the vocoder model (default: unused)",
16091624
[](common_params & params, const std::string & value) {
16101625
params.vocoder.hf_repo = value;

0 commit comments

Comments
 (0)