Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gpt4all-backend: Add temperature sampling with repetition penalty #541

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions gpt4all-backend/llamamodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,28 @@ void LLamaModel::prompt(const std::string &prompt,
int32_t totalPredictions = 0;
for (int i = 0; i < promptCtx.n_predict; i++) {
// sample next token
llama_token id = llama_sample_top_p_top_k(d_ptr->ctx,
promptCtx.tokens.data() + promptCtx.n_ctx - promptCtx.repeat_last_n,
promptCtx.repeat_last_n, promptCtx.top_k, promptCtx.top_p, promptCtx.temp,
float* logits = llama_get_logits(d_ptr->ctx);
std::vector<llama_token_data> candidates;
candidates.resize(llama_n_vocab(d_ptr->ctx));

for (llama_token i = 0; i < candidates.size(); i++) {
candidates[i] = llama_token_data{
i, logits[i], 0.0f,
};
}
llama_token_data_array candidates_data = {
candidates.data(), candidates.size(), false,
};

// Temperature sampling with repetition penalty
llama_sample_repetition_penalty(
d_ptr->ctx, &candidates_data,
promptCtx.tokens.data() + promptCtx.n_ctx - promptCtx.repeat_last_n, promptCtx.repeat_last_n,
promptCtx.repeat_penalty);
llama_sample_top_k(d_ptr->ctx, &candidates_data, promptCtx.top_k, 1);
llama_sample_top_p(d_ptr->ctx, &candidates_data, promptCtx.top_p, 1);
llama_sample_temperature(d_ptr->ctx, &candidates_data, promptCtx.temp);
llama_token id = llama_sample_token(d_ptr->ctx, &candidates_data);

// Check if the context has run out...
if (promptCtx.n_past + 1 > promptCtx.n_ctx) {
Expand Down