Skip to content

Commit

Permalink
feat: remove a sampler from a chain (#9445)
Browse files Browse the repository at this point in the history
* feat: remove a sampler from a chain

* fix: return removed sampler

* fix: safer casting
  • Loading branch information
giladgd authored Sep 13, 2024
1 parent 7820364 commit bd35cb0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,9 @@ extern "C" {
LLAMA_API struct llama_sampler * llama_sampler_chain_get(const struct llama_sampler * chain, int32_t i);
LLAMA_API int llama_sampler_chain_n (const struct llama_sampler * chain);

// after removing a sampler, the chain will no longer own it, and it will not be freed when the chain is freed
LLAMA_API struct llama_sampler * llama_sampler_chain_remove( struct llama_sampler * chain, int32_t i);

// available samplers:

LLAMA_API struct llama_sampler * llama_sampler_init_greedy (void);
Expand Down
15 changes: 14 additions & 1 deletion src/llama-sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,26 @@ void llama_sampler_chain_add(struct llama_sampler * chain, struct llama_sampler
struct llama_sampler * llama_sampler_chain_get(const struct llama_sampler * chain, int32_t i) {
const auto * p = (const llama_sampler_chain *) chain->ctx;

if (i < 0 || i >= (int32_t) p->samplers.size()) {
if (i < 0 || (size_t) i >= p->samplers.size()) {
return nullptr;
}

return p->samplers[i];
}

struct llama_sampler * llama_sampler_chain_remove(struct llama_sampler * chain, int32_t i) {
auto * p = (llama_sampler_chain *) chain->ctx;

if (i < 0 || (size_t) i >= p->samplers.size()) {
return nullptr;
}

auto * result = p->samplers[i];
p->samplers.erase(p->samplers.begin() + i);

return result;
}

int llama_sampler_chain_n(const struct llama_sampler * chain) {
const auto * p = (const llama_sampler_chain *) chain->ctx;

Expand Down

0 comments on commit bd35cb0

Please sign in to comment.