Add llama_sampler_init
for safe usage of llama_sampler_free
#11727
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds a new
llama_sampler_init
function to the public API so that libllama does both the allocation and deallocation of customllama_sampler
objects. The caller deals withctx
and nothing else. This seems to be in-line with the intent of the current API.The C API in llama.h claims users can implement
llama_sampler_i
to create customllama_sampler
. The sampler chain takes ownership and callsllama_sampler_free
on them. However,llama_sampler_free
is hard-coded to usedelete
. This is undefined behavior if the object wasn't also allocated vianew
from libllama's C++ runtime. Callers in C and C-compatible languages do not use C++'snew
operator. C++ callers may not be sharing the same heap as libllama.(I kept the redundant
struct
keyword to match the style of the rest of the file despite the contributing guidelines.)$ zig run test.zig -Iinclude -Iggml/include -Lbuild/bin -lc -lllama free(): invalid pointer zsh: IOT instruction (core dumped)
Alternative
The caller could be made responsible of both allocation and deallocation of custom sampler types.
That approach would allow flattening sampler objects (removing the
ctx
member and its second dynamic allocation).It would be a breaking change:
llama_sampler_i::free
becomes a mandatory fieldllama_sampler_free
doesn't assume how to deallocatellama_sampler_clone
becomes unable to clone samplers whose interface don't specifyclone
Pseudo-code: