Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Add Google Gemma v2 support
Justine Tunney7692b8505869
diff --git a/llama.cpp/llama.cpp b/llama.cpp/llama.cppindex 9ed7b34b..4fb0cab9 100644--- a/llama.cpp/llama.cpp+++ b/llama.cpp/llama.cpp@@ -172,6 +172,7 @@ enum llm_arch {LLM_ARCH_INTERNLM2,LLM_ARCH_MINICPM,LLM_ARCH_GEMMA,+ LLM_ARCH_GEMMA2,LLM_ARCH_STARCODER2,LLM_ARCH_MAMBA,LLM_ARCH_XVERSE,@@ -208,6 +209,7 @@ static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {{ LLM_ARCH_INTERNLM2, "internlm2" },{ LLM_ARCH_MINICPM, "minicpm" },{ LLM_ARCH_GEMMA, "gemma" },+ { LLM_ARCH_GEMMA2, "gemma2" },{ LLM_ARCH_STARCODER2, "starcoder2" },{ LLM_ARCH_MAMBA, "mamba" },{ LLM_ARCH_XVERSE, "xverse" },@@ -401,10 +403,12 @@ enum llm_tensor {LLM_TENSOR_ATTN_NORM,LLM_TENSOR_ATTN_NORM_2,LLM_TENSOR_ATTN_OUT_NORM,+ LLM_TENSOR_ATTN_POST_NORM,LLM_TENSOR_ATTN_ROT_EMBD,LLM_TENSOR_FFN_GATE_INP,LLM_TENSOR_FFN_GATE_INP_SHEXP,LLM_TENSOR_FFN_NORM,+ LLM_TENSOR_FFN_POST_NORM,LLM_TENSOR_FFN_GATE,LLM_TENSOR_FFN_DOWN,LLM_TENSOR_FFN_UP,@@ -889,6 +893,24 @@ static const std::map<llm_arch, std::map<llm_tensor, std::string>> LLM_TENSOR_NA{ LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },},},+ {+ LLM_ARCH_GEMMA2,+ {+ { LLM_TENSOR_TOKEN_EMBD, "token_embd" },+ { LLM_TENSOR_OUTPUT_NORM, "output_norm" },+ { LLM_TENSOR_ATTN_NORM, "blk.%d.attn_norm" },+ { LLM_TENSOR_ATTN_Q, "blk.%d.attn_q" },+ { LLM_TENSOR_ATTN_K, "blk.%d.attn_k" },+ { LLM_TENSOR_ATTN_V, "blk.%d.attn_v" },+ { LLM_TENSOR_ATTN_OUT, "blk.%d.attn_output" },+ { LLM_TENSOR_ATTN_POST_NORM, "blk.%d.post_attention_norm" },+ { LLM_TENSOR_FFN_NORM, "blk.%d.ffn_norm" },+ { LLM_TENSOR_FFN_GATE, "blk.%d.ffn_gate" },+ { LLM_TENSOR_FFN_DOWN, "blk.%d.ffn_down" },+ { LLM_TENSOR_FFN_UP, "blk.%d.ffn_up" },+ { LLM_TENSOR_FFN_POST_NORM, "blk.%d.post_ffw_norm" },+ },+ },{LLM_ARCH_STARCODER2,{@@ -1473,6 +1495,8 @@ enum e_model {MODEL_8x7B,MODEL_8x22B,MODEL_16x12B,+ MODEL_9B,+ MODEL_27B,};static const size_t kiB = 1024;@@ -1627,6 +1651,7 @@ struct llama_layer {struct ggml_tensor * attn_k_norm_b;struct ggml_tensor * attn_out_norm;struct ggml_tensor * attn_out_norm_b;+ struct ggml_tensor * attn_post_norm;// attentionstruct ggml_tensor * wq;@@ -1645,6 +1670,7 @@ struct llama_layer {// normalizationstruct ggml_tensor * ffn_norm;struct ggml_tensor * ffn_norm_b;+ struct ggml_tensor * ffn_post_norm;struct ggml_tensor * layer_out_norm;struct ggml_tensor * layer_out_norm_b;@@ -3185,11 +3211,15 @@ struct llama_model_loader {return get_tensor_meta(get_tensor_name(i));}- struct ggml_tensor * create_tensor_for(struct ggml_context * ctx, const struct ggml_tensor * cur) {+ struct ggml_tensor * create_tensor_for(struct ggml_context * ctx, const struct ggml_tensor * cur, bool duplicated) {struct ggml_tensor * tensor = ggml_dup_tensor(ctx, cur);ggml_set_name(tensor, ggml_get_name(cur));- n_created++;+ if (duplicated) {+ size_data += ggml_nbytes(cur);+ } else {+ n_created++;+ }return tensor;}@@ -3224,14 +3254,17 @@ struct llama_model_loader {return cur;}- struct ggml_tensor * create_tensor(struct ggml_context * ctx, const std::string & name, const std::vector<int64_t> & ne, bool required = true) {- const struct ggml_tensor * cur = check_tensor_dims(name, ne, required);+ static const int TENSOR_NOT_REQUIRED = 1;+ static const int TENSOR_DUPLICATED = 2;++ struct ggml_tensor * create_tensor(struct ggml_context * ctx, const std::string & name, const std::vector<int64_t> & ne, int flags = 0) {+ const struct ggml_tensor * cur = check_tensor_dims(name, ne, !(flags & TENSOR_NOT_REQUIRED));if (cur == NULL) {return NULL;}- return create_tensor_for(ctx, cur);+ return create_tensor_for(ctx, cur, flags & TENSOR_DUPLICATED);}struct ggml_tensor * create_tensor_as_view(struct ggml_context * ctx, struct ggml_tensor * base, const std::string & name, const std::vector<int64_t> & ne, size_t offset, bool required = true) {@@ -3955,6 +3988,16 @@ static void llm_load_hparams(default: model.type = e_model::MODEL_UNKNOWN;}} break;+ case LLM_ARCH_GEMMA2:+ {+ ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);++ switch (hparams.n_layer) {+ case 18: model.type = e_model::MODEL_9B; break;+ case 28: model.type = e_model::MODEL_27B; break;+ default: model.type = e_model::MODEL_UNKNOWN;+ }+ } break;case LLM_ARCH_STARCODER2:{ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);@@ -4722,7 +4765,7 @@ static bool llm_load_tensors({model.output_norm = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd});if (model.arch != LLM_ARCH_MINICPM){- model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, false);+ model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_NOT_REQUIRED);// if output is NULL, init from the input tok embedif (model.output == NULL) {model.output = ml.create_tensor(ctx_output, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});@@ -4746,10 +4789,10 @@ static bool llm_load_tensors(layer.wo = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd});// optional bias tensors- layer.bq = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 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.bo = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, false);+ layer.bq = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, llama_model_loader::TENSOR_NOT_REQUIRED);+ layer.bk = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, llama_model_loader::TENSOR_NOT_REQUIRED);+ layer.bv = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, llama_model_loader::TENSOR_NOT_REQUIRED);+ layer.bo = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, llama_model_loader::TENSOR_NOT_REQUIRED);layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd});@@ -5215,17 +5258,17 @@ static bool llm_load_tensors(layer.wo = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd});// optional bias tensors, present in Stable LM 2 1.6B- layer.bq = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 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.bq = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, llama_model_loader::TENSOR_NOT_REQUIRED);+ layer.bk = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, llama_model_loader::TENSOR_NOT_REQUIRED);+ layer.bv = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, llama_model_loader::TENSOR_NOT_REQUIRED);// 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);+ 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}, llama_model_loader::TENSOR_NOT_REQUIRED);+ 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}, llama_model_loader::TENSOR_NOT_REQUIRED);// 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_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, llama_model_loader::TENSOR_NOT_REQUIRED);+ layer.ffn_norm_b = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, llama_model_loader::TENSOR_NOT_REQUIRED);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});@@ -5268,7 +5311,7 @@ static bool llm_load_tensors(// output{model.output_norm = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd});- model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, false);+ model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_NOT_REQUIRED);// if output is NULL, init from the input tok embedif (model.output == NULL) {model.output = ml.create_tensor(ctx_output, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});@@ -5371,8 +5414,8 @@ static bool llm_load_tensors(layer.attn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd});layer.attn_norm_b = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd});- layer.wqkv = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, false);- layer.bqkv = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, false);+ layer.wqkv = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, llama_model_loader::TENSOR_NOT_REQUIRED);+ layer.bqkv = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, llama_model_loader::TENSOR_NOT_REQUIRED);if (layer.wqkv == nullptr) {layer.wq = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd});@@ -5399,8 +5442,8 @@ static bool llm_load_tensors({model.tok_embd = ml.create_tensor(ctx_input, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab });- model.rope_long = ml.create_tensor(ctx_input, tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight"), { n_embd_head/2 }, false);- model.rope_short = ml.create_tensor(ctx_input, tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight"), { n_embd_head/2 }, false);+ model.rope_long = ml.create_tensor(ctx_input, tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight"), { n_embd_head/2 }, llama_model_loader::TENSOR_NOT_REQUIRED);+ model.rope_short = ml.create_tensor(ctx_input, tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight"), { n_embd_head/2 }, llama_model_loader::TENSOR_NOT_REQUIRED);// output{@@ -5416,7 +5459,7 @@ static bool llm_load_tensors(layer.attn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd });- layer.wqkv = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, false);+ layer.wqkv = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, llama_model_loader::TENSOR_NOT_REQUIRED);layer.wo = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd });layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd });@@ -5619,6 +5662,40 @@ static bool llm_load_tensors(layer.ffn_down = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd});}} break;+ case LLM_ARCH_GEMMA2:+ {+ model.tok_embd = ml.create_tensor(ctx_input, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});++ // output+ model.output_norm = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd});+ model.output = ml.create_tensor(ctx_output, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading++ const int64_t n_ff = hparams.n_ff;+ const int64_t n_embd_head_k = hparams.n_embd_head_k;+ const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa();+ const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa();++ for (uint32_t i = 0; i < n_layer; ++i) {+ ggml_context * ctx_layer = ctx_for_layer(i);+ ggml_context * ctx_split = ctx_for_layer_split(i);++ auto & layer = model.layers[i];++ layer.attn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd});++ layer.wq = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * hparams.n_head});+ layer.wk = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa});+ layer.wv = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa});+ layer.wo = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * hparams.n_head, n_embd});+ layer.attn_post_norm = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd});++ layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd});+ layer.ffn_gate = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff});+ layer.ffn_up = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_UP, "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_post_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd});+ }+ } break;case LLM_ARCH_STARCODER2:{model.tok_embd = ml.create_tensor(ctx_input, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});@@ -5628,7 +5705,7 @@ static bool llm_load_tensors(model.output_norm = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd});model.output_norm_b = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd});- model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, false);+ model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_NOT_REQUIRED);// if output is NULL, init from the input tok embedif (model.output == NULL) {model.output = ml.create_tensor(ctx_output, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});@@ -5684,7 +5761,7 @@ static bool llm_load_tensors({model.output_norm = ml.create_tensor(ctx_output, tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd});- model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, false);+ model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_NOT_REQUIRED);// if output is NULL, init from the input tok embed, duplicated to allow offloadingif (model.output == NULL) {model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});@@ -5784,7 +5861,7 @@ static bool llm_load_tensors(// output{- model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, false);+ model.output = ml.create_tensor(ctx_output_split, tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, llama_model_loader::TENSOR_NOT_REQUIRED);// if output is NULL, init from the input tok embedif (model.output == NULL) {model.output = ml.create_tensor(ctx_output, tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab});@@ -9792,6 +9869,123 @@ struct llm_build_context {return gf;}+ struct ggml_cgraph * build_gemma2() {+ struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, LLAMA_MAX_NODES, false);++ const int64_t n_embd_head_k = hparams.n_embd_head_k;++ struct ggml_tensor * cur;+ struct ggml_tensor * inpL;++ inpL = llm_build_inp_embd(ctx0, lctx, hparams, batch, model.tok_embd, cb);++ inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));+ cb(inpL, "inp_scaled", -1);++ // inp_pos - contains the positions+ struct ggml_tensor * inp_pos = build_inp_pos();++ // KQ_mask (mask for 1 head, it will be broadcasted to all heads)+ struct ggml_tensor * KQ_mask = build_inp_KQ_mask();++ for (int il = 0; il < n_layer; ++il) {+ // norm+ cur = llm_build_norm(ctx0, inpL, hparams,+ model.layers[il].attn_norm, NULL,+ LLM_NORM_RMS, cb, il);+ cb(cur, "attn_norm", il);++ // self-attention+ {+ // compute Q and K and RoPE them+ struct ggml_tensor * Qcur = ggml_mul_mat(ctx0, model.layers[il].wq, cur);+ cb(Qcur, "Qcur", il);++ struct ggml_tensor * Kcur = ggml_mul_mat(ctx0, model.layers[il].wk, cur);+ cb(Kcur, "Kcur", il);++ struct ggml_tensor * Vcur = ggml_mul_mat(ctx0, model.layers[il].wv, cur);+ cb(Vcur, "Vcur", il);++ Qcur = ggml_rope_ext(+ ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head_k, n_head, n_tokens), inp_pos, nullptr,+ n_embd_head_k, rope_type, 0, n_orig_ctx, freq_base, freq_scale,+ ext_factor, attn_factor, beta_fast, beta_slow);+ cb(Qcur, "Qcur", il);++ Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head_k)));+ cb(Qcur, "Qcur_scaled", il);++ Kcur = ggml_rope_ext(+ ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head_k, n_head_kv, n_tokens), inp_pos, nullptr,+ n_embd_head_k, rope_type, 0, n_orig_ctx, freq_base, freq_scale,+ ext_factor, attn_factor, beta_fast, beta_slow);+ cb(Kcur, "Kcur", il);++ cur = llm_build_kv(ctx0, model, hparams, cparams, kv_self, gf,+ model.layers[il].wo, NULL,+ Kcur, Vcur, Qcur, KQ_mask, n_tokens, kv_head, n_kv, 1.0f, cb, il);+ }++ if (il == n_layer - 1) {+ // skip computing output for unused tokens+ struct ggml_tensor * inp_out_ids = build_inp_out_ids();+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);+ inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);+ }++ cur = llm_build_norm(ctx0, cur, hparams,+ model.layers[il].attn_post_norm, NULL,+ LLM_NORM_RMS, cb, il);+ cb(cur, "attn_post_norm", il);++ struct ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);+ cb(sa_out, "sa_out", il);++ cur = llm_build_norm(ctx0, sa_out, hparams,+ model.layers[il].ffn_norm, NULL,+ LLM_NORM_RMS, cb, il);+ cb(cur, "ffn_norm", il);++ // feed-forward network+ {+ cur = llm_build_ffn(ctx0, cur,+ model.layers[il].ffn_up, NULL,+ model.layers[il].ffn_gate, NULL,+ model.layers[il].ffn_down, NULL,+ NULL,+ LLM_FFN_GELU, LLM_FFN_PAR, cb, il);+ cb(cur, "ffn_out", il);+ }++ cur = llm_build_norm(ctx0, cur, hparams,+ model.layers[il].ffn_post_norm, NULL,+ LLM_NORM_RMS, cb, -1);+ cb(cur, "ffn_post_norm", -1);++ cur = ggml_add(ctx0, cur, sa_out);+ cb(cur, "l_out", il);++ // input for next layer+ inpL = cur;+ }++ cur = inpL;++ cur = llm_build_norm(ctx0, cur, hparams,+ model.output_norm, NULL,+ LLM_NORM_RMS, cb, -1);+ cb(cur, "result_norm", -1);++ // lm_head+ cur = ggml_mul_mat(ctx0, model.output, cur);+ cb(cur, "result_output", -1);++ ggml_build_forward_expand(gf, cur);++ return gf;+ }+struct ggml_cgraph * build_starcoder2() {struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, LLAMA_MAX_NODES, false);@@ -10522,6 +10716,10 @@ static struct ggml_cgraph * llama_build_graph({result = llm.build_gemma();} break;+ case LLM_ARCH_GEMMA2:+ {+ result = llm.build_gemma2();+ } break;case LLM_ARCH_STARCODER2:{result = llm.build_starcoder2();@@ -15584,6 +15782,7 @@ enum llama_rope_type llama_rope_type(const struct llama_model * model) {case LLM_ARCH_PHI2:case LLM_ARCH_PHI3:case LLM_ARCH_GEMMA:+ case LLM_ARCH_GEMMA2:case LLM_ARCH_STARCODER2:return LLAMA_ROPE_TYPE_NEOX;@@ -17413,7 +17612,7 @@ static int32_t llama_chat_apply_template_internal(if (add_ass) {ss << "<s>assistant\n";}- } else if (tmpl == "gemma" || tmpl.find("<start_of_turn>") != std::string::npos) {+ } else if (tmpl == "gemma" || tmpl == "gemma2" || tmpl.find("<start_of_turn>") != std::string::npos) {// google/gemma-7b-itstd::string system_prompt = "";for (auto message : chat) {diff --git a/llamafile/tinyblas_cpu_sgemm.inc b/llamafile/tinyblas_cpu_sgemm.incindex db01abc4..21915383 100644--- a/llamafile/tinyblas_cpu_sgemm.inc+++ b/llamafile/tinyblas_cpu_sgemm.inc@@ -82,9 +82,7 @@ bool llamafile_sgemm_impl(long m, long n, long k, const void *A, long lda, constcase GGML_TYPE_BF16: {#if defined(__AVX512BF16__)- if (k % 32)- return NOT_SUPPORTED;- if (Btype == GGML_TYPE_F32 && n < 2) {+ if (Btype == GGML_TYPE_F32 && n < 2 && !(k % 16)) {tinyBLAS<0, 16, __m512, __m512, ggml_bf16_t, float, TC> tb{k, (const ggml_bf16_t *)A, lda, (const float *)B, ldb, C, ldc, ith, nth};tb.matmul(m, n, task);@@ -94,12 +92,14 @@ bool llamafile_sgemm_impl(long m, long n, long k, const void *A, long lda, constreturn WANT_QUANTIZATION;if (Btype != GGML_TYPE_BF16)return NOT_SUPPORTED;- if (!FLAG_precise) {+ if (n > 1 && !FLAG_precise && !(k % 32)) {tinyBLAS<0, 32, __m512, __m512bh, ggml_bf16_t, ggml_bf16_t, TC> tb{k, (const ggml_bf16_t *)A, lda, (const ggml_bf16_t *)B, ldb, C, ldc, ith, nth};tb.matmul(m, n, task);return true;} else {+ if (k % 16)+ return NOT_SUPPORTED;tinyBLAS<0, 16, __m512, __m512, ggml_bf16_t, ggml_bf16_t, TC> tb{k, (const ggml_bf16_t *)A, lda, (const ggml_bf16_t *)B, ldb, C, ldc, ith, nth};tb.matmul(m, n, task);