Skip to content

Commit 5a9753b

Browse files
committed
llama : remove sampling from llama_context
ggml-ci
1 parent 97ab664 commit 5a9753b

File tree

25 files changed

+75
-137
lines changed

25 files changed

+75
-137
lines changed

common/common.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
264264
params.kv_overrides.back().key[0] = 0;
265265
}
266266

267+
if (params.sparams.seed == LLAMA_DEFAULT_SEED) {
268+
params.sparams.seed = time(NULL);
269+
}
270+
267271
return true;
268272
}
269273

@@ -294,8 +298,6 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
294298

295299
if (arg == "-s" || arg == "--seed") {
296300
CHECK_ARG
297-
// TODO: this is temporary, in the future the sampling state will be moved fully to llama_sampling_context.
298-
params.seed = std::stoul(argv[i]);
299301
sparams.seed = std::stoul(argv[i]);
300302
return true;
301303
}
@@ -1414,7 +1416,6 @@ void gpt_params_print_usage(int /*argc*/, char ** argv, const gpt_params & param
14141416
options.push_back({ "*", " --verbose-prompt", "print a verbose prompt before generation (default: %s)", params.verbose_prompt ? "true" : "false" });
14151417
options.push_back({ "*", " --no-display-prompt", "don't print prompt at generation (default: %s)", !params.display_prompt ? "true" : "false" });
14161418
options.push_back({ "*", "-co, --color", "colorise output to distinguish prompt and user input from generations (default: %s)", params.use_color ? "true" : "false" });
1417-
options.push_back({ "*", "-s, --seed SEED", "RNG seed (default: %d, use random seed for < 0)", params.seed });
14181419
options.push_back({ "*", "-t, --threads N", "number of threads to use during generation (default: %d)", params.n_threads });
14191420
options.push_back({ "*", "-tb, --threads-batch N", "number of threads to use during batch and prompt processing (default: same as --threads)" });
14201421
options.push_back({ "speculative", "-td, --threads-draft N", "number of threads to use during generation (default: same as --threads)" });
@@ -1465,6 +1466,7 @@ void gpt_params_print_usage(int /*argc*/, char ** argv, const gpt_params & param
14651466
" --spm-infill", "use Suffix/Prefix/Middle pattern for infill (instead of Prefix/Suffix/Middle) as some models prefer this. (default: %s)", params.spm_infill ? "enabled" : "disabled" });
14661467

14671468
options.push_back({ "sampling" });
1469+
options.push_back({ "*", "-s, --seed SEED", "RNG seed (default: %d, use random seed for < 0)", sparams.seed });
14681470
options.push_back({ "*", " --samplers SAMPLERS", "samplers that will be used for generation in the order, separated by \';\'\n"
14691471
"(default: %s)", sampler_type_names.c_str() });
14701472
options.push_back({ "*", " --sampling-seq SEQUENCE",
@@ -2237,7 +2239,6 @@ struct llama_context_params llama_context_params_from_gpt_params(const gpt_param
22372239
cparams.n_ubatch = params.n_ubatch;
22382240
cparams.n_threads = params.n_threads;
22392241
cparams.n_threads_batch = params.n_threads_batch == -1 ? params.n_threads : params.n_threads_batch;
2240-
cparams.seed = params.seed;
22412242
cparams.logits_all = params.logits_all;
22422243
cparams.embeddings = params.embedding;
22432244
cparams.rope_scaling_type = params.rope_scaling_type;
@@ -3247,7 +3248,6 @@ void yaml_dump_non_result_info(FILE * stream, const gpt_params & params, const l
32473248

32483249
fprintf(stream, "rope_freq_base: %f # default: 10000.0\n", params.rope_freq_base);
32493250
fprintf(stream, "rope_freq_scale: %f # default: 1.0\n", params.rope_freq_scale);
3250-
fprintf(stream, "seed: %u # default: -1 (random seed)\n", params.seed);
32513251
fprintf(stream, "simple_io: %s # default: false\n", params.simple_io ? "true" : "false");
32523252
fprintf(stream, "cont_batching: %s # default: false\n", params.cont_batching ? "true" : "false");
32533253
fprintf(stream, "flash_attn: %s # default: false\n", params.flash_attn ? "true" : "false");

common/common.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ enum dimre_method {
6868
};
6969

7070
struct gpt_params {
71-
uint32_t seed = LLAMA_DEFAULT_SEED; // RNG seed
72-
7371
int32_t n_threads = cpu_get_num_math();
7472
int32_t n_threads_draft = -1;
7573
int32_t n_threads_batch = -1; // number of threads to use for batch processing (-1 = use n_threads)

common/sampling.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,10 @@
33
#include <random>
44

55
struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_params & params, const struct llama_model * model) {
6-
auto result = llama_sampling_init(params, llama_sampling_init(model, params.grammar.c_str(), "root"));
7-
8-
result->owned = true;
9-
10-
return result;
11-
}
12-
13-
struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_params & params, struct llama_sampling * smpl) {
146
struct llama_sampling_context * result = new llama_sampling_context();
157

168
result->params = params;
17-
result->owned = false;
18-
result->smpl = smpl;
9+
result->smpl = llama_sampling_init(model, params.grammar.c_str(), "root");
1910

2011
result->prev.resize(params.n_prev);
2112

@@ -27,9 +18,7 @@ struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_
2718
}
2819

2920
void llama_sampling_free(struct llama_sampling_context * ctx) {
30-
if (ctx->owned) {
31-
llama_sampling_free(ctx->smpl);
32-
}
21+
llama_sampling_free(ctx->smpl);
3322

3423
delete ctx;
3524
}

common/sampling.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ struct llama_sampling_context {
7171
// mirostat sampler state
7272
float mirostat_mu;
7373

74-
bool owned;
75-
7674
llama_sampling * smpl;
7775

7876
// TODO: replace with ring-buffer
@@ -86,7 +84,6 @@ struct llama_sampling_context {
8684

8785
// Create a new sampling context instance.
8886
struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_params & params, const struct llama_model * model);
89-
struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_params & params, struct llama_sampling * smpl);
9087

9188
void llama_sampling_free(struct llama_sampling_context * ctx);
9289

examples/batched.swift/Sources/main.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ guard let model = llama_load_model_from_file(modelPath.cString(using: .utf8), mo
2727
print("Failed to load model")
2828
exit(1)
2929
}
30-
3130
defer {
3231
llama_free_model(model)
3332
}
@@ -37,24 +36,29 @@ var tokens = tokenize(text: prompt, add_bos: true)
3736
let n_kv_req = UInt32(tokens.count) + UInt32((n_len - Int(tokens.count)) * n_parallel)
3837

3938
var context_params = llama_context_default_params()
40-
context_params.seed = 1234
4139
context_params.n_ctx = n_kv_req
4240
context_params.n_batch = UInt32(max(n_len, n_parallel))
4341
context_params.n_threads = 8
4442
context_params.n_threads_batch = 8
4543

4644
let context = llama_new_context_with_model(model, context_params)
47-
let smpl = llama_get_sampling(context)
48-
4945
guard context != nil else {
5046
print("Failed to initialize context")
5147
exit(1)
5248
}
53-
5449
defer {
5550
llama_free(context)
5651
}
5752

53+
let smpl = llama_sampling_init(model, nil, nil)
54+
guard smpl != nil else {
55+
print("Failed to initialize sampling")
56+
exit(1)
57+
}
58+
defer {
59+
llama_sampling_free(smpl)
60+
}
61+
5862
let n_ctx = llama_n_ctx(context)
5963

6064
print("\nn_len = \(n_len), n_ctx = \(n_ctx), n_batch = \(context_params.n_batch), n_parallel = \(n_parallel), n_kv_req = \(n_kv_req)\n")

examples/batched/batched.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int main(int argc, char ** argv) {
6464
ctx_params.n_batch = std::max(n_predict, n_parallel);
6565

6666
llama_context * ctx = llama_new_context_with_model(model, ctx_params);
67-
llama_sampling * smpl = llama_get_sampling(ctx);
67+
llama_sampling * smpl = llama_sampling_init(model, nullptr, nullptr);
6868

6969
if (ctx == NULL) {
7070
fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);

examples/embedding/embedding.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,7 @@ int main(int argc, char ** argv) {
6868

6969
print_build_info();
7070

71-
if (params.seed == LLAMA_DEFAULT_SEED) {
72-
params.seed = time(NULL);
73-
}
74-
75-
fprintf(stderr, "%s: seed = %u\n", __func__, params.seed);
76-
77-
std::mt19937 rng(params.seed);
71+
LOG_TEE("%s: seed = %u\n", __func__, params.sparams.seed);
7872

7973
llama_backend_init();
8074
llama_numa_init(params.numa);

examples/eval-callback/eval-callback.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ int main(int argc, char ** argv) {
151151

152152
print_build_info();
153153

154-
std::mt19937 rng(params.seed);
155-
156154
llama_backend_init();
157155
llama_numa_init(params.numa);
158156

examples/gritlm/gritlm.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,10 @@ static std::vector<std::vector<float>> encode(llama_context * ctx, const std::ve
9292
return result;
9393
}
9494

95-
static std::string generate(llama_context * ctx, const std::string & prompt, bool stream) {
95+
static std::string generate(llama_context * ctx, llama_sampling * smpl, const std::string & prompt, bool stream) {
9696
std::string result;
9797

9898
const llama_model * model = llama_get_model(ctx);
99-
llama_sampling * smpl = llama_get_sampling(ctx);
10099
llama_token eos_token = llama_token_eos(model);
101100

102101
llama_kv_cache_clear(ctx);
@@ -117,7 +116,7 @@ static std::string generate(llama_context * ctx, const std::string & prompt, boo
117116
inputs.clear();
118117

119118
llama_decode(ctx, bat);
120-
auto logits = llama_get_logits_ith(ctx, bat.n_tokens - 1);
119+
auto * logits = llama_get_logits_ith(ctx, bat.n_tokens - 1);
121120

122121
auto candidates = std::vector<llama_token_data>(llama_n_vocab(model));
123122
auto n_candidates = (int32_t)candidates.size();
@@ -173,6 +172,8 @@ int main(int argc, char * argv[]) {
173172
// create generation context
174173
llama_context * ctx = llama_new_context_with_model(model, cparams);
175174

175+
llama_sampling * smpl = llama_sampling_init(model, nullptr, nullptr);
176+
176177
// ### Embedding/Representation ###
177178
// samples taken from: https://github.com/ContextualAI/gritlm#basic
178179
{
@@ -209,9 +210,10 @@ int main(int argc, char * argv[]) {
209210
// GritLM models are not finetuned with system prompts, as you can just include system-like instructions together with your user instruction
210211
{
211212
const std::string prompt = "<|user|>\nPlease write me a poem about my recent hike of Mt. Fuji at midnight in the style of Shakespeare.\n<|assistant|>\n";
212-
std::string response = generate(ctx, prompt, true);
213+
std::string response = generate(ctx, smpl, prompt, true);
213214
}
214215

216+
llama_sampling_free(smpl);
215217
llama_free(ctx);
216218
llama_free_model(model);
217219
llama_backend_free();

examples/infill/infill.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,9 @@ int main(int argc, char ** argv) {
156156
LOG_TEE("%s: warning: scaling RoPE frequency by %g.\n", __func__, params.rope_freq_scale);
157157
}
158158

159-
LOG_TEE("%s: build = %d (%s)\n", __func__, LLAMA_BUILD_NUMBER, LLAMA_COMMIT);
160-
LOG_TEE("%s: built with %s for %s\n", __func__, LLAMA_COMPILER, LLAMA_BUILD_TARGET);
159+
print_build_info();
161160

162-
if (params.seed == LLAMA_DEFAULT_SEED) {
163-
params.seed = time(NULL);
164-
}
165-
166-
LOG_TEE("%s: seed = %u\n", __func__, params.seed);
167-
168-
std::mt19937 rng(params.seed);
161+
LOG_TEE("%s: seed = %u\n", __func__, params.sparams.seed);
169162

170163
LOG("%s: llama backend init\n", __func__);
171164
llama_backend_init();
@@ -351,7 +344,7 @@ int main(int argc, char ** argv) {
351344

352345
std::vector<llama_token> embd;
353346

354-
ctx_sampling = llama_sampling_init(sparams, llama_get_sampling(ctx));
347+
ctx_sampling = llama_sampling_init(sparams, model);
355348

356349
while (n_remain != 0 || params.interactive) {
357350
// predict

examples/llama.android/llama/src/main/cpp/llama-android.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ Java_android_llama_cpp_LLamaAndroid_new_1context(JNIEnv *env, jobject, jlong jmo
120120
LOGi("Using %d threads", n_threads);
121121

122122
llama_context_params ctx_params = llama_context_default_params();
123-
ctx_params.seed = 1234;
124123
ctx_params.n_ctx = 2048;
125124
ctx_params.n_threads = n_threads;
126125
ctx_params.n_threads_batch = n_threads;
@@ -380,12 +379,13 @@ Java_android_llama_cpp_LLamaAndroid_completion_1loop(
380379
JNIEnv * env,
381380
jobject,
382381
jlong context_pointer,
382+
jlong sampling_pointer,
383383
jlong batch_pointer,
384384
jint n_len,
385385
jobject intvar_ncur
386386
) {
387387
const auto context = reinterpret_cast<llama_context *>(context_pointer);
388-
const auto sampling = reinterpret_cast<llama_sampling *>(llama_get_sampling(context));
388+
const auto sampling = reinterpret_cast<llama_sampling *>(sampling_pointer);
389389
const auto batch = reinterpret_cast<llama_batch *>(batch_pointer);
390390
const auto model = llama_get_model(context);
391391

examples/llama.swiftui/llama.cpp.swift/LibLlama.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ actor LlamaContext {
4343
self.tokens_list = []
4444
self.batch = llama_batch_init(512, 0, 1)
4545
self.temporary_invalid_cchars = []
46-
self.sampling = llama_get_sampling(context)
46+
self.sampling = llama_sampling_init(context, nil, nil);
4747
}
4848

4949
deinit {
50+
llama_sampling_free(sampling)
5051
llama_batch_free(batch)
5152
llama_free(context)
5253
llama_free_model(model)
53-
llama_sampling_free(sampling)
5454
llama_backend_free()
5555
}
5656

@@ -72,7 +72,6 @@ actor LlamaContext {
7272
print("Using \(n_threads) threads")
7373

7474
var ctx_params = llama_context_default_params()
75-
ctx_params.seed = 1234
7675
ctx_params.n_ctx = 2048
7776
ctx_params.n_threads = UInt32(n_threads)
7877
ctx_params.n_threads_batch = UInt32(n_threads)

examples/llava/llava-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static void process_prompt(struct llava_context * ctx_llava, struct llava_image_
191191

192192
LOG_TEE("\n");
193193

194-
struct llama_sampling_context * ctx_sampling = llama_sampling_init(params->sparams, llama_get_sampling(ctx_llava->ctx_llama));
194+
struct llama_sampling_context * ctx_sampling = llama_sampling_init(params->sparams, ctx_llava->model);
195195
if (!ctx_sampling) {
196196
fprintf(stderr, "%s: failed to initialize sampling subsystem\n", __func__);
197197
exit(1);

examples/llava/minicpmv-cli.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static const char * sample(struct llama_sampling_context * ctx_sampling,
161161
struct llama_context * ctx_llama,
162162
int * n_past) {
163163
const llama_token id = llama_sampling_sample(ctx_sampling, ctx_llama, NULL);
164-
llama_sampling_accept(ctx_sampling, ctx_llama, id, true);
164+
llama_sampling_accept(ctx_sampling, id, true);
165165
static std::string ret;
166166
if (llama_token_is_eog(llama_get_model(ctx_llama), id)) {
167167
ret = "</s>";
@@ -218,7 +218,7 @@ static struct llama_sampling_context * llama_init(struct llava_context * ctx_lla
218218

219219
LOG_TEE("\n");
220220

221-
struct llama_sampling_context * ctx_sampling = llama_sampling_init(params->sparams);
221+
struct llama_sampling_context * ctx_sampling = llama_sampling_init(params->sparams, ctx_llava->model);
222222
return ctx_sampling;
223223
}
224224

@@ -299,7 +299,7 @@ int main(int argc, char ** argv) {
299299
}
300300
}
301301
printf("\n");
302-
llama_print_timings(ctx_llava->ctx_llama);
302+
llama_print_timings(ctx_llava->ctx_llama, nullptr);
303303

304304
ctx_llava->model = NULL;
305305
llava_free(ctx_llava);

examples/lookahead/lookahead.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "common.h"
22
#include "llama.h"
33

4-
#include <cmath>
54
#include <cstdio>
65
#include <string>
76
#include <vector>
@@ -118,7 +117,7 @@ int main(int argc, char ** argv) {
118117
llama_batch batch = llama_batch_init(params.n_ctx, 0, W + G + 1);
119118

120119
// target model sampling context
121-
struct llama_sampling_context * ctx_sampling = llama_sampling_init(params.sparams, llama_get_sampling(ctx));
120+
struct llama_sampling_context * ctx_sampling = llama_sampling_init(params.sparams, model);
122121

123122
// verification n-grams
124123
std::vector<ngram_data> ngrams_cur(G);

examples/lookup/lookup.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
#include "common.h"
44
#include "ngram-cache.h"
55

6-
#include <cmath>
76
#include <cstdint>
87
#include <cstdio>
98
#include <fstream>
109
#include <string>
1110
#include <vector>
12-
#include <unordered_map>
1311

1412
int main(int argc, char ** argv){
1513
gpt_params params;
@@ -106,7 +104,7 @@ int main(int argc, char ** argv){
106104

107105
bool has_eos = false;
108106

109-
struct llama_sampling_context * ctx_sampling = llama_sampling_init(params.sparams, llama_get_sampling(ctx));
107+
struct llama_sampling_context * ctx_sampling = llama_sampling_init(params.sparams, model);
110108

111109
std::vector<llama_token> draft;
112110

0 commit comments

Comments
 (0)