|
@@ -716,6 +716,8 @@ static const std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NA
|
|
|
{ LLM_TENSOR_FFN_GATE, "blk.%d.ffn_gate" },
|
|
{ LLM_TENSOR_FFN_GATE, "blk.%d.ffn_gate" },
|
|
|
{ LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" },
|
|
{ LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" },
|
|
|
{ LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },
|
|
{ LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },
|
|
|
|
|
+ { LLM_TENSOR_ATTN_Q_NORM, "blk.%d.attn_q_norm" },
|
|
|
|
|
+ { LLM_TENSOR_ATTN_K_NORM, "blk.%d.attn_k_norm" },
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
@@ -1744,6 +1746,7 @@ enum e_model {
|
|
|
MODEL_4B,
|
|
MODEL_4B,
|
|
|
MODEL_7B,
|
|
MODEL_7B,
|
|
|
MODEL_8B,
|
|
MODEL_8B,
|
|
|
|
|
+ MODEL_12B,
|
|
|
MODEL_13B,
|
|
MODEL_13B,
|
|
|
MODEL_14B,
|
|
MODEL_14B,
|
|
|
MODEL_15B,
|
|
MODEL_15B,
|
|
@@ -3607,6 +3610,7 @@ static const char * llama_model_type_name(e_model type) {
|
|
|
case MODEL_3B: return "3B";
|
|
case MODEL_3B: return "3B";
|
|
|
case MODEL_7B: return "7B";
|
|
case MODEL_7B: return "7B";
|
|
|
case MODEL_8B: return "8B";
|
|
case MODEL_8B: return "8B";
|
|
|
|
|
+ case MODEL_12B: return "12B";
|
|
|
case MODEL_13B: return "13B";
|
|
case MODEL_13B: return "13B";
|
|
|
case MODEL_14B: return "14B";
|
|
case MODEL_14B: return "14B";
|
|
|
case MODEL_15B: return "15B";
|
|
case MODEL_15B: return "15B";
|
|
@@ -3898,6 +3902,7 @@ static void llm_load_hparams(
|
|
|
switch (hparams.n_layer) {
|
|
switch (hparams.n_layer) {
|
|
|
case 24: model.type = e_model::MODEL_1B; break;
|
|
case 24: model.type = e_model::MODEL_1B; break;
|
|
|
case 32: model.type = e_model::MODEL_3B; break;
|
|
case 32: model.type = e_model::MODEL_3B; break;
|
|
|
|
|
+ case 40: model.type = e_model::MODEL_12B; break;
|
|
|
default: model.type = e_model::MODEL_UNKNOWN;
|
|
default: model.type = e_model::MODEL_UNKNOWN;
|
|
|
}
|
|
}
|
|
|
} break;
|
|
} break;
|
|
@@ -5128,8 +5133,13 @@ static bool llm_load_tensors(
|
|
|
layer.bk = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, false);
|
|
layer.bk = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, false);
|
|
|
layer.bv = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, false);
|
|
layer.bv = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, false);
|
|
|
|
|
|
|
|
- layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd});
|
|
|
|
|
- layer.ffn_norm_b = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd});
|
|
|
|
|
|
|
+ // optional q and k layernorms, present in StableLM 2 12B
|
|
|
|
|
+ layer.attn_q_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {hparams.n_embd_head_k, hparams.n_head}, false);
|
|
|
|
|
+ layer.attn_k_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {hparams.n_embd_head_k, hparams.n_head_kv}, false);
|
|
|
|
|
+
|
|
|
|
|
+ // optional FFN norm, not present in StableLM 2 12B which uses parallel residual
|
|
|
|
|
+ layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, false);
|
|
|
|
|
+ layer.ffn_norm_b = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, false);
|
|
|
|
|
|
|
|
layer.ffn_gate = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff});
|
|
layer.ffn_gate = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff});
|
|
|
layer.ffn_down = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd});
|
|
layer.ffn_down = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd});
|
|
@@ -8197,7 +8207,7 @@ struct llm_build_context {
|
|
|
struct ggml_tensor * KQ_mask = build_inp_KQ_mask();
|
|
struct ggml_tensor * KQ_mask = build_inp_KQ_mask();
|
|
|
|
|
|
|
|
for (int il = 0; il < n_layer; ++il) {
|
|
for (int il = 0; il < n_layer; ++il) {
|
|
|
- struct ggml_tensor * inpSA = inpL;
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// norm
|
|
// norm
|
|
|
cur = llm_build_norm(ctx0, inpL, hparams,
|
|
cur = llm_build_norm(ctx0, inpL, hparams,
|
|
@@ -8206,6 +8216,8 @@ struct llm_build_context {
|
|
|
LLM_NORM, cb, il);
|
|
LLM_NORM, cb, il);
|
|
|
cb(cur, "attn_norm", il);
|
|
cb(cur, "attn_norm", il);
|
|
|
|
|
|
|
|
|
|
+ struct ggml_tensor * inpSA = cur;
|
|
|
|
|
+
|
|
|
// self-attention
|
|
// self-attention
|
|
|
{
|
|
{
|
|
|
// compute Q and K and RoPE them
|
|
// compute Q and K and RoPE them
|
|
@@ -8230,15 +8242,36 @@ struct llm_build_context {
|
|
|
cb(Vcur, "Vcur", il);
|
|
cb(Vcur, "Vcur", il);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
|
|
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
|
|
+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
|
|
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
|
|
+
|
|
|
|
|
+ if (model.layers[il].attn_q_norm) {
|
|
|
|
|
+ Qcur = llm_build_norm(ctx0, Qcur, hparams,
|
|
|
|
|
+ model.layers[il].attn_q_norm,
|
|
|
|
|
+ NULL,
|
|
|
|
|
+ LLM_NORM, cb, il);
|
|
|
|
|
+ cb(Qcur, "Qcur", il);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (model.layers[il].attn_k_norm) {
|
|
|
|
|
+ Kcur = llm_build_norm(ctx0, Kcur, hparams,
|
|
|
|
|
+ model.layers[il].attn_k_norm,
|
|
|
|
|
+ NULL,
|
|
|
|
|
+ LLM_NORM, cb, il);
|
|
|
|
|
+ cb(Kcur, "Kcur", il);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
Qcur = ggml_rope_custom(
|
|
Qcur = ggml_rope_custom(
|
|
|
- ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos,
|
|
|
|
|
|
|
+ ctx0, Qcur, inp_pos,
|
|
|
n_rot, rope_type, 0, n_orig_ctx, freq_base, freq_scale,
|
|
n_rot, rope_type, 0, n_orig_ctx, freq_base, freq_scale,
|
|
|
ext_factor, attn_factor, beta_fast, beta_slow
|
|
ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
);
|
|
);
|
|
|
cb(Qcur, "Qcur", il);
|
|
cb(Qcur, "Qcur", il);
|
|
|
|
|
|
|
|
Kcur = ggml_rope_custom(
|
|
Kcur = ggml_rope_custom(
|
|
|
- ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos,
|
|
|
|
|
|
|
+ ctx0, Kcur, inp_pos,
|
|
|
n_rot, rope_type, 0, n_orig_ctx, freq_base, freq_scale,
|
|
n_rot, rope_type, 0, n_orig_ctx, freq_base, freq_scale,
|
|
|
ext_factor, attn_factor, beta_fast, beta_slow
|
|
ext_factor, attn_factor, beta_fast, beta_slow
|
|
|
);
|
|
);
|
|
@@ -8253,20 +8286,25 @@ struct llm_build_context {
|
|
|
// skip computing output for unused tokens
|
|
// skip computing output for unused tokens
|
|
|
struct ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
struct ggml_tensor * inp_out_ids = build_inp_out_ids();
|
|
|
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
|
|
|
|
|
+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
|
|
|
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
|
|
|
|
|
|
|
+ struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
|
|
|
cb(ffn_inp, "ffn_inp", il);
|
|
cb(ffn_inp, "ffn_inp", il);
|
|
|
|
|
|
|
|
// feed-forward network
|
|
// feed-forward network
|
|
|
{
|
|
{
|
|
|
- cur = llm_build_norm(ctx0, ffn_inp, hparams,
|
|
|
|
|
- model.layers[il].ffn_norm,
|
|
|
|
|
- model.layers[il].ffn_norm_b,
|
|
|
|
|
- LLM_NORM, cb, il);
|
|
|
|
|
- cb(cur, "ffn_norm", il);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if (model.layers[il].ffn_norm) {
|
|
|
|
|
+ cur = llm_build_norm(ctx0, ffn_inp, hparams,
|
|
|
|
|
+ model.layers[il].ffn_norm,
|
|
|
|
|
+ model.layers[il].ffn_norm_b,
|
|
|
|
|
+ LLM_NORM, cb, il);
|
|
|
|
|
+ cb(cur, "ffn_norm", il);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // parallel residual
|
|
|
|
|
+ cur = inpSA;
|
|
|
|
|
+ }
|
|
|
cur = llm_build_ffn(ctx0, cur,
|
|
cur = llm_build_ffn(ctx0, cur,
|
|
|
model.layers[il].ffn_up, NULL,
|
|
model.layers[il].ffn_up, NULL,
|
|
|
model.layers[il].ffn_gate, NULL,
|
|
model.layers[il].ffn_gate, NULL,
|