Skip to content

llama : refactor src/llama.cpp - #10902

Merged
ggerganov merged 25 commits into
masterfrom
gg/llama-refactor-0
Jan 3, 2025
Merged

ggerganov merged 25 commits into
masterfrom
gg/llama-refactor-0

Conversation

@ggerganov

@ggerganov ggerganov commented Dec 19, 2024 •

Copy link
Copy Markdown
Member

Attempting to split the src/llama.cpp into a few separate modules. Very work-in-progress, mainly opening this PR for people to keep track and suggest improvements as we move along. This part does not involve functional changes, just code reorganization and decoupling to make it easier to work with the codebase. The batch and KV cache abstractions and reimplementations will be done in follow-up PRs.

graph TD;
chat;
model_loader;
model   --> arch[<b>arch </b>];
model   --> hparams[<b>hparams </b>];
model   ----> mmap[<b>mmap </b> <br><br> llama_file <br> llama_mmap <br> llama_mlock];
model   -.-> model_loader;
model   --> vocab;
vocab   --> unicode;
adapter -.-> model;
kv_cache -.-> batch;
kv_cache -.-> cparams;
kv_cache -.-> model;
context --> adapter[<b>adapter</b> <br><br> llama_adapter_cvec <br> llama_adapter_lora];
context -.-> batch;
context --> cparams;
context --> kv_cache;
context --> model;

style adapter fill:green
style arch fill:green
style batch fill:green
style chat fill:green
style cparams fill:green
style hparams fill:green
style kv_cache fill:green
style mmap fill:green
style model fill:green
style model_loader fill:green
style unicode fill:green
style vocab fill:green
Loading

TODO

  • move the llama_mmaps and llama_mlocks from llama_model to llama_context? (no)
  • change _internal suffix to _impl (next PR)
  • add llama_tensor_loader ?
  • model loading
  • quantization

Conflicts

@ngxson

ngxson commented Dec 19, 2024 •

Copy link
Copy Markdown
Collaborator

I think control_vector and lora related stuff should be re-grouped into a module, maybe called adapters (if someone has a better naming, feel free to comment). That's because they work kinda the same way, by "adding things" on top of the original cgraph.

@ggerganov
ggerganov force-pushed the gg/llama-refactor-0 branch 8 times, most recently from 524886b to 7ab08d5 Compare December 22, 2024 16:24
@github-actions github-actions Bot added examples devops improvements to build systems and github actions labels Dec 22, 2024
@ggerganov
ggerganov force-pushed the gg/llama-refactor-0 branch 2 times, most recently from be8f568 to dcbfda1 Compare December 22, 2024 20:30
@ggerganov
ggerganov force-pushed the gg/llama-refactor-0 branch 7 times, most recently from ba48e37 to 0ccae21 Compare December 23, 2024 17:22
@ggerganov
ggerganov force-pushed the gg/llama-refactor-0 branch from 1e7e338 to 597ae05 Compare January 2, 2025 10:39
@MaggotHATE

Copy link
Copy Markdown
Contributor

After this PR, common_tokenize fails to tokenize any text, exits without console errors on llama_tokenize. Windows 10, static cpu build. Model and ctx are loaded at that moment (main-based application).

What change caused this? I don't see changes to llama_tokenize_internal function, for example. Are models required to be reconverted after this PR?

@ggerganov

Copy link
Copy Markdown
Member Author

There should be no functional changes. Try to clean your build folder.

@MaggotHATE

Copy link
Copy Markdown
Contributor

There should be no functional changes.

There's definitely a change, I see llama_n_ctx returning random numbers instead of the actual context size. At the same time, the context size is reported correctly on loading:

llama_new_context_with_model: n_ctx_per_seq (16384) < n_ctx_train (131072) -- the full capacity of the model will not be utilized
llama_kv_cache_init: kv_size = 16384, offload = 0, type_k = 'f16', type_v = 'f16', n_layer = 56, can_shift = 1

but later:

load: llama_n_ctx = 458134640

@ngxson

ngxson commented Jan 3, 2025 •

Copy link
Copy Markdown
Collaborator

There is a slight change in common_init_result, both model and ctx are now unique pointer. I'm not sure if it's the root problem. (i.e. you need to modify your code to use model.get() instead of just model)

Please note that we don't provide stable interface for common library, so undocumented breaking changes are expected.

@MaggotHATE

Copy link
Copy Markdown
Contributor

you need to modify your code to use model.get() instead of just model

That's already done according to the changes in main this PR introduces. The problem is that everything before llama_n_ctx seems to work fine, but it looks like ctx either doesn't load properly, or is unloaded (which didn't happen before and probably shouldn't happen).

undocumented breaking changes

That's exactly what I'm trying to find.

@ngxson

ngxson commented Jan 3, 2025

Copy link
Copy Markdown
Collaborator

Be careful that with unique ptr, the ctx maybe deallocated when it goes out of scope. You should make sure that common_init_result always valid in the scope, otherwise .release() the ptr from unique ptr and use it as raw ptr.

@MaggotHATE

Copy link
Copy Markdown
Contributor

ctx maybe deallocated when it goes out of scope

Yes, looks like that's what happens here. I see that llama_free(ctx) and llama_free_model(model) are no longer used, so deallocation and unloading happen automatically. It there a way to lock model and ctx manually now?

Although, it's a bit weird that this wasn't needed previously.

@ngxson

ngxson commented Jan 3, 2025 •

Copy link
Copy Markdown
Collaborator

llama_free is no longer needed because common_init_result is in scope of main(), and deleter will be called automatically when we exit from main() (go out of scope)


Another way is: (but remember to free it manually afterwards)

common_init_result llama_init = common_init_from_params(params);
llama_model * model = llama_init.model.release();
llama_context * ctx = llama_init.context.release();

llama_free(ctx);
llama_free_model(model);

@MaggotHATE

MaggotHATE commented Jan 3, 2025 •

Copy link
Copy Markdown
Contributor
llama_model * model = llama_init.model.release();
llama_context * ctx = llama_init.context.release();

Yes, thank you, that's exactly what I needed!

So, release() allows to match previous behavior (for example, reloading works exactly as it used to), while .get() relies on common_init_result (I tested it, and keeping common_init_result as a part of the object helps, but regen doesn't work - looks like it should be cleared/reset manually too).

@ngxson

ngxson commented Jan 3, 2025 •

Copy link
Copy Markdown
Collaborator

In cpp, you have notion of "ownership". For example, std::string owns a char * buffer under the hood that you can access it using str.data(), and buffer is freed once std::string goes out of scope.

Same for unique_ptr, llama_model * model is owned by unique_ptr and get() let you access it. You can also release() it from being owned.

unique_ptr cannot be reassigned, you can only reset() it or std::move it. Edit: it can be reassigned, which will be equivalent to reset()

@MaggotHATE

Copy link
Copy Markdown
Contributor

Same for unique_ptr, llama_model * model is owned by unique_ptr and get() let you access it. You can also release() it from being owned.

I understand that, but the problem is that release() isn't used anywhere for model and ctx, so there are no examples to learn that from. Plus, in practical sense, functionality has changed in the examples (they are examples to learn from!), because it used to rely on free functions, while now they rely on simply going out of scope.

@ggerganov ggerganov mentioned this pull request Jan 7, 2025
Nexesenex pushed a commit to Nexesenex/croco.cpp that referenced this pull request Feb 25, 2025
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
arthw pushed a commit to arthw/llama.cpp that referenced this pull request Feb 26, 2025
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
* llama : scatter llama.cpp into multiple modules (wip)

* llama : control-vector -> adapter

* llama : arch

* llama : mmap

ggml-ci

* ci : remove BUILD_SHARED_LIBS=OFF

ggml-ci

* llama : arch (cont)

ggml-ci

* llama : chat

ggml-ci

* llama : model

ggml-ci

* llama : hparams

ggml-ci

* llama : adapter

ggml-ci

* examples : fix

ggml-ci

* rebase

ggml-ci

* minor

* llama : kv cache

ggml-ci

* llama : impl

ggml-ci

* llama : batch

ggml-ci

* cont

ggml-ci

* llama : context

ggml-ci

* minor

* llama : context (cont)

ggml-ci

* llama : model loader

ggml-ci

* common : update lora

ggml-ci

* llama : quant

ggml-ci

* llama : quant (cont)

ggml-ci

* minor [no ci]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

devops improvements to build systems and github actions examples server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants