@@ -211,6 +211,7 @@ enum llm_arch {
211
211
LLM_ARCH_INTERNLM2,
212
212
LLM_ARCH_MINICPM,
213
213
LLM_ARCH_GEMMA,
214
+ LLM_ARCH_STARCODER2,
214
215
LLM_ARCH_UNKNOWN,
215
216
};
216
217
@@ -238,6 +239,7 @@ static std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
238
239
{ LLM_ARCH_INTERNLM2, "internlm2" },
239
240
{ LLM_ARCH_MINICPM, "minicpm" },
240
241
{ LLM_ARCH_GEMMA, "gemma" },
242
+ { LLM_ARCH_STARCODER2, "starcoder2" },
241
243
};
242
244
243
245
enum llm_kv {
@@ -779,6 +781,24 @@ static std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NAMES =
779
781
{ LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },
780
782
},
781
783
},
784
+ {
785
+ LLM_ARCH_STARCODER2,
786
+ {
787
+ { LLM_TENSOR_TOKEN_EMBD, "token_embd" },
788
+ { LLM_TENSOR_OUTPUT_NORM, "output_norm" },
789
+ { LLM_TENSOR_OUTPUT, "output" },
790
+ { LLM_TENSOR_ROPE_FREQS, "rope_freqs" },
791
+ { LLM_TENSOR_ATTN_NORM, "blk.%d.attn_norm" },
792
+ { LLM_TENSOR_ATTN_Q, "blk.%d.attn_q" },
793
+ { LLM_TENSOR_ATTN_K, "blk.%d.attn_k" },
794
+ { LLM_TENSOR_ATTN_V, "blk.%d.attn_v" },
795
+ { LLM_TENSOR_ATTN_OUT, "blk.%d.attn_output" },
796
+ { LLM_TENSOR_ATTN_ROT_EMBD, "blk.%d.attn_rot_embd" },
797
+ { LLM_TENSOR_FFN_NORM, "blk.%d.ffn_norm" },
798
+ { LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" },
799
+ { LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },
800
+ },
801
+ },
782
802
{
783
803
LLM_ARCH_UNKNOWN,
784
804
{
@@ -3320,6 +3340,16 @@ static void llm_load_hparams(
3320
3340
default: model.type = e_model::MODEL_UNKNOWN;
3321
3341
}
3322
3342
} break;
3343
+ case LLM_ARCH_STARCODER2:
3344
+ {
3345
+ ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
3346
+ switch (hparams.n_layer) {
3347
+ case 30: model.type = e_model::MODEL_3B; break;
3348
+ case 32: model.type = e_model::MODEL_7B; break;
3349
+ case 40: model.type = e_model::MODEL_15B; break;
3350
+ default: model.type = e_model::MODEL_UNKNOWN;
3351
+ }
3352
+ } break;
3323
3353
default: (void)0;
3324
3354
}
3325
3355
@@ -4490,6 +4520,56 @@ static bool llm_load_tensors(
4490
4520
layer.ffn_down = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd});
4491
4521
}
4492
4522
} break;
4523
+ case LLM_ARCH_STARCODER2:
4524
+ {
4525
+ model.tok_embd = ml.create_tensor(ctx_input, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});
4526
+
4527
+ // output
4528
+ {
4529
+ model.output_norm = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd});
4530
+ model.output_norm_b = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd});
4531
+
4532
+ model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, false);
4533
+ // if output is NULL, init from the input tok embed
4534
+ if (model.output == NULL) {
4535
+ model.output = ml.create_tensor(ctx_output, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});
4536
+ ml.n_created--; // artificial tensor
4537
+ ml.size_data += ggml_nbytes(model.output);
4538
+ }
4539
+
4540
+ }
4541
+
4542
+ for (int i = 0; i < n_layer; ++i) {
4543
+ ggml_context * ctx_layer = ctx_for_layer(i);
4544
+ ggml_context * ctx_split = ctx_for_layer_split(i);
4545
+
4546
+ auto & layer = model.layers[i];
4547
+
4548
+ layer.attn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd});
4549
+ layer.attn_norm_b = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd});
4550
+
4551
+ layer.wq = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd});
4552
+ layer.wk = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa});
4553
+ layer.wv = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa});
4554
+ layer.wo = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd});
4555
+
4556
+ // optional bias tensors
4557
+ layer.bq = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd});
4558
+ layer.bk = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa});
4559
+ layer.bv = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa});
4560
+ layer.bo = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd});
4561
+
4562
+ layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd});
4563
+ layer.ffn_norm_b = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd});
4564
+
4565
+ layer.ffn_down = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd});
4566
+ layer.ffn_up = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff});
4567
+
4568
+ // optional bias tensors
4569
+ layer.ffn_down_b = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd});
4570
+ layer.ffn_up_b = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_UP , "bias", i), { n_ff});
4571
+ }
4572
+ } break;
4493
4573
default:
4494
4574
throw std::runtime_error("unknown architecture");
4495
4575
}
@@ -7559,6 +7639,120 @@ struct llm_build_context {
7559
7639
7560
7640
return gf;
7561
7641
}
7642
+
7643
+ struct ggml_cgraph * build_starcoder2() {
7644
+ struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, LLAMA_MAX_NODES, false);
7645
+
7646
+ const int64_t n_embd_head = hparams.n_embd_head_v;
7647
+ GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
7648
+ GGML_ASSERT(n_embd_head == hparams.n_rot);
7649
+
7650
+ struct ggml_tensor * cur;
7651
+ struct ggml_tensor * inpL;
7652
+
7653
+ inpL = llm_build_inp_embd(ctx0, hparams, batch, model.tok_embd, lctx.inp_tokens, lctx.inp_embd, cb);
7654
+ cb(inpL, "inp_embd", -1);
7655
+
7656
+ // inp_pos - contains the positions
7657
+ struct ggml_tensor * inp_pos = ggml_view_1d(ctx0, lctx.inp_pos, n_tokens, 0);
7658
+ cb(inp_pos, "inp_pos", -1);
7659
+
7660
+ // KQ_mask (mask for 1 head, it will be broadcasted to all heads)
7661
+ struct ggml_tensor * KQ_mask = ggml_view_2d(ctx0, lctx.inp_KQ_mask, n_kv, n_tokens, n_kv*ggml_type_size(lctx.inp_KQ_mask->type), 0);
7662
+ cb(KQ_mask, "KQ_mask", -1);
7663
+
7664
+ for (int il = 0; il < n_layer; ++il) {
7665
+ struct ggml_tensor * inpSA = inpL;
7666
+
7667
+ // norm
7668
+ cur = llm_build_norm(ctx0, inpL, hparams,
7669
+ model.layers[il].attn_norm, model.layers[il].attn_norm_b,
7670
+ LLM_NORM, cb, il);
7671
+ cb(cur, "attn_norm", il);
7672
+
7673
+ // self-attention
7674
+ {
7675
+ // compute Q and K and RoPE them
7676
+ struct ggml_tensor * Qcur = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
7677
+ cb(Qcur, "Qcur", il);
7678
+ if (model.layers[il].bq) {
7679
+ Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
7680
+ cb(Qcur, "Qcur", il);
7681
+ }
7682
+
7683
+ struct ggml_tensor * Kcur = ggml_mul_mat(ctx0, model.layers[il].wk, cur);
7684
+ cb(Kcur, "Kcur", il);
7685
+ if (model.layers[il].bk) {
7686
+ Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
7687
+ cb(Kcur, "Kcur", il);
7688
+ }
7689
+
7690
+ struct ggml_tensor * Vcur = ggml_mul_mat(ctx0, model.layers[il].wv, cur);
7691
+ cb(Vcur, "Vcur", il);
7692
+ if (model.layers[il].bv) {
7693
+ Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
7694
+ cb(Vcur, "Vcur", il);
7695
+ }
7696
+
7697
+ Qcur = ggml_rope_custom(
7698
+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos,
7699
+ n_rot, rope_type, 0, n_orig_ctx, freq_base, freq_scale,
7700
+ ext_factor, attn_factor, beta_fast, beta_slow
7701
+ );
7702
+ cb(Qcur, "Qcur", il);
7703
+
7704
+ Kcur = ggml_rope_custom(
7705
+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos,
7706
+ n_rot, rope_type, 0, n_orig_ctx, freq_base, freq_scale,
7707
+ ext_factor, attn_factor, beta_fast, beta_slow
7708
+ );
7709
+ cb(Kcur, "Kcur", il);
7710
+
7711
+ cur = llm_build_kv(ctx0, model, hparams, kv_self, gf,
7712
+ model.layers[il].wo, model.layers[il].bo,
7713
+ Kcur, Vcur, Qcur, KQ_mask, nullptr, n_ctx, n_tokens, kv_head, n_kv, 1.0f/sqrtf(float(n_embd_head)), cb, il);
7714
+ cb(cur, "kqv_out", il);
7715
+ }
7716
+
7717
+ struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
7718
+ cb(ffn_inp, "ffn_inp", il);
7719
+
7720
+ // feed-forward network
7721
+
7722
+ cur = llm_build_norm(ctx0, ffn_inp, hparams,
7723
+ model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
7724
+ LLM_NORM, cb, il);
7725
+ cb(cur, "ffn_norm", il);
7726
+
7727
+ cur = llm_build_ffn(ctx0, cur,
7728
+ model.layers[il].ffn_up, model.layers[il].ffn_up_b,
7729
+ NULL, NULL,
7730
+ model.layers[il].ffn_down, model.layers[il].ffn_down_b,
7731
+ NULL,
7732
+ LLM_FFN_GELU, LLM_FFN_SEQ, cb, il);
7733
+ cb(cur, "ffn_out", il);
7734
+ cur = ggml_add(ctx0, cur, ffn_inp);
7735
+ cb(cur, "l_out", il);
7736
+
7737
+ // input for next layer
7738
+ inpL = cur;
7739
+ }
7740
+
7741
+ cur = inpL;
7742
+
7743
+ cur = llm_build_norm(ctx0, cur, hparams,
7744
+ model.output_norm, model.output_norm_b,
7745
+ LLM_NORM, cb, -1);
7746
+ cb(cur, "result_norm", -1);
7747
+
7748
+ // lm_head
7749
+ cur = ggml_mul_mat(ctx0, model.output, cur);
7750
+ cb(cur, "result_output", -1);
7751
+
7752
+ ggml_build_forward_expand(gf, cur);
7753
+
7754
+ return gf;
7755
+ }
7562
7756
};
7563
7757
7564
7758
static struct ggml_cgraph * llama_build_graph_defrag(llama_context & lctx, const std::vector<uint32_t> & ids) {
@@ -7705,6 +7899,10 @@ static struct ggml_cgraph * llama_build_graph(
7705
7899
{
7706
7900
result = llm.build_gemma();
7707
7901
} break;
7902
+ case LLM_ARCH_STARCODER2:
7903
+ {
7904
+ result = llm.build_starcoder2();
7905
+ } break;
7708
7906
default:
7709
7907
GGML_ASSERT(false);
7710
7908
}
@@ -12084,6 +12282,7 @@ enum llama_rope_type llama_rope_type(const struct llama_model * model) {
12084
12282
case LLM_ARCH_QWEN2:
12085
12283
case LLM_ARCH_PHI2:
12086
12284
case LLM_ARCH_GEMMA:
12285
+ case LLM_ARCH_STARCODER2:
12087
12286
return LLAMA_ROPE_TYPE_NEOX;
12088
12287
12089
12288
// all model arches should be listed explicitly here
0 commit comments