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_initfunction to the public API so that libllama does both the allocation and deallocation of customllama_samplerobjects. The caller deals withctxand 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_ito create customllama_sampler. The sampler chain takes ownership and callsllama_sampler_freeon them. However,llama_sampler_freeis hard-coded to usedelete. This is undefined behavior if the object wasn't also allocated vianewfrom libllama's C++ runtime. Callers in C and C-compatible languages do not use C++'snewoperator. C++ callers may not be sharing the same heap as libllama.(I kept the redundant
structkeyword 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
ctxmember and its second dynamic allocation).It would be a breaking change:
llama_sampler_i::freebecomes a mandatory fieldllama_sampler_freedoesn't assume how to deallocatellama_sampler_clonebecomes unable to clone samplers whose interface don't specifyclonePseudo-code: