Skip to content

Commit e0de09d

Browse files
committed
shorten code using a variable
1 parent 49d6daa commit e0de09d

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

examples/baby-llama/baby-llama.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,7 @@ int main(int argc, char ** argv) {
11791179

11801180
int n_examples = 128;
11811181
int n_tokens = model.hparams.n_ctx;
1182+
int n_vocab = model.hparams.n_vocab;
11821183

11831184
for (int ex=0; ex<n_examples; ++ex) {
11841185
struct ggml_init_params params = {
@@ -1190,18 +1191,18 @@ int main(int argc, char ** argv) {
11901191
struct ggml_context * ctx0 = ggml_init(params);
11911192

11921193

1193-
// struct ggml_tensor * before_opt_best_samples = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
1194-
// struct ggml_tensor * before_opt_probs = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, model.hparams.n_vocab, n_tokens);
1194+
struct ggml_tensor * before_opt_best_samples = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
1195+
struct ggml_tensor * before_opt_probs = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_vocab, n_tokens);
11951196
struct ggml_tensor * after_opt_best_samples = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
1196-
struct ggml_tensor * after_opt_probs = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, model.hparams.n_vocab, n_tokens);
1197+
struct ggml_tensor * after_opt_probs = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_vocab, n_tokens);
11971198
struct ggml_tensor * tokens_input1 = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
11981199
struct ggml_tensor * tokens_input2 = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
11991200
// struct ggml_tensor * tokens_input3 = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
12001201
// struct ggml_tensor * tokens_input4 = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);
1201-
struct ggml_tensor * targets1 = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, model.hparams.n_vocab, n_tokens);
1202-
struct ggml_tensor * targets2 = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, model.hparams.n_vocab, n_tokens);
1203-
// struct ggml_tensor * targets3 = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, model.hparams.n_vocab, n_tokens);
1204-
// struct ggml_tensor * targets4 = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, model.hparams.n_vocab, n_tokens);
1202+
struct ggml_tensor * targets1 = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_vocab, n_tokens);
1203+
struct ggml_tensor * targets2 = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_vocab, n_tokens);
1204+
// struct ggml_tensor * targets3 = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_vocab, n_tokens);
1205+
// struct ggml_tensor * targets4 = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_vocab, n_tokens);
12051206

12061207
int n_past = 0;
12071208

@@ -1213,7 +1214,7 @@ int main(int argc, char ** argv) {
12131214
// get_example_targets(64*ex+32, tokens_input3, targets3);
12141215
// get_example_targets(64*ex+48, tokens_input4, targets4);
12151216
// print_probs(targets);
1216-
// print_tokens(tokens_input, model.hparams.n_vocab);
1217+
// print_tokens(tokens_input, n_vocab);
12171218

12181219
struct ggml_tensor * logits1 = forward(&model, &kv_self, ctx0, &gf, tokens_input1, n_tokens, n_past);
12191220
struct ggml_tensor * logits2 = forward(&model, &kv_self, ctx0, &gf, tokens_input2, n_tokens, n_past);
@@ -1246,7 +1247,7 @@ int main(int argc, char ** argv) {
12461247
// printf("probabilities before optimization:\n");
12471248
// print_probs(before_opt_probs);
12481249
// printf("best samples before optimization:\n");
1249-
// print_tokens(before_opt_best_samples, model.hparams.n_vocab);
1250+
// print_tokens(before_opt_best_samples, n_vocab);
12501251

12511252
struct ggml_opt_params opt_params_adam = ggml_opt_default_params(GGML_OPT_ADAM);
12521253
struct ggml_opt_params opt_params_lbfgs = ggml_opt_default_params(GGML_OPT_LBFGS);
@@ -1276,28 +1277,28 @@ int main(int argc, char ** argv) {
12761277
// printf("probabilities after optimization:\n");
12771278
// print_probs(after_opt_probs);
12781279
printf("best samples after optimization:\n");
1279-
print_tokens(after_opt_best_samples, model.hparams.n_vocab);
1280+
print_tokens(after_opt_best_samples, n_vocab);
12801281
}
12811282

12821283
ggml_free(ctx0);
12831284
}
12841285

12851286
{
1286-
int n_gen = 64;
1287-
int sample_ctx = n_tokens/2;
1287+
int n_gen = 128;
1288+
int sample_ctx = n_tokens/2-n_tokens/16;
12881289

12891290
printf("Generating %d tokens.\n", n_gen);
12901291

12911292
struct ggml_tensor * tokens_input = ggml_new_tensor_1d(model.ctx, GGML_TYPE_I32, n_tokens);
1292-
struct ggml_tensor * targets = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, model.hparams.n_vocab, n_tokens);
1293+
struct ggml_tensor * targets = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, n_vocab, n_tokens);
12931294

12941295
get_example_targets(137, tokens_input, targets);
12951296
for (int i=sample_ctx; i<n_tokens; ++i) {
1296-
ggml_set_i32_1d(tokens_input, i, model.hparams.n_vocab/2);
1297+
ggml_set_i32_1d(tokens_input, i, n_vocab/2);
12971298
}
12981299

12991300
for (int i=0; i<sample_ctx-1; ++i) {
1300-
print_token(ggml_get_i32_1d(tokens_input, i), model.hparams.n_vocab);
1301+
print_token(ggml_get_i32_1d(tokens_input, i), n_vocab);
13011302
}
13021303
printf("---\n");
13031304
for (int i=0; i<n_gen; ++i) {
@@ -1318,15 +1319,15 @@ int main(int argc, char ** argv) {
13181319
ggml_graph_compute(ctx0, &gf);
13191320

13201321
struct ggml_tensor * best_samples = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, sample_ctx);
1321-
struct ggml_tensor * probs = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, model.hparams.n_vocab, sample_ctx);
1322+
struct ggml_tensor * probs = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_vocab, sample_ctx);
13221323

13231324
sample_softmax(logits, probs, best_samples);
13241325

13251326
// int sample_at = n_tokens-1;
13261327
int token = ggml_get_i32_1d(best_samples, sample_ctx-1);
13271328

13281329
// print_probs1(probs, sample_at);
1329-
print_token(token, model.hparams.n_vocab);
1330+
print_token(token, n_vocab);
13301331

13311332
lshift_examples(tokens_input, targets, 1);
13321333
ggml_set_i32_1d(tokens_input, 0, 0);

0 commit comments

Comments
 (0)