sampler : add llama_sampler_copy()

Copy the state of src into dst, implemented generically on top of llama_sampler_clone. Both samplers must be of the same type.

Assisted-by: llama.cpp:DeepSeek-v4-Flash-0731
This commit is contained in:
Georgi Gerganov
2026-08-05 14:27:01 +03:00
parent 6ea215d171
commit eeed56f725
2 changed files with 23 additions and 0 deletions
+2
View File
@@ -1310,6 +1310,8 @@ extern "C" {
LLAMA_API void llama_sampler_apply ( struct llama_sampler * smpl, llama_token_data_array * cur_p);
LLAMA_API void llama_sampler_reset ( struct llama_sampler * smpl);
LLAMA_API struct llama_sampler * llama_sampler_clone (const struct llama_sampler * smpl);
// copy the state of src into dst; both must be samplers of the same type
LLAMA_API void llama_sampler_copy ( struct llama_sampler * dst, const struct llama_sampler * src);
// important: do not free if the sampler has been added to a llama_sampler_chain (via llama_sampler_chain_add)
LLAMA_API void llama_sampler_free ( struct llama_sampler * smpl);
+21
View File
@@ -417,6 +417,27 @@ struct llama_sampler * llama_sampler_clone(const struct llama_sampler * smpl) {
GGML_ABORT("the sampler does not support cloning");
}
void llama_sampler_copy(struct llama_sampler * dst, const struct llama_sampler * src) {
if (!dst || !src) {
return;
}
GGML_ASSERT(dst->iface == src->iface && "llama_sampler_copy: cannot copy between different sampler types");
// build a temporary sampler carrying src's current state
llama_sampler * tmp = llama_sampler_clone(src);
// free dst's old state (frees dst->ctx, including children for a chain)
if (dst->iface->free) {
dst->iface->free(dst);
}
// transplant tmp's state into dst, then destroy the (now empty) temp shell
dst->ctx = tmp->ctx;
tmp->ctx = nullptr;
delete tmp;
}
void llama_sampler_free(struct llama_sampler * smpl) {
if (smpl == nullptr) {
return;