Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
use code from ik
CJ Paise7b29e19119e
diff --git a/llama.cpp/llama.cpp b/llama.cpp/llama.cppindex 33eaae5d..841b8761 100644--- a/llama.cpp/llama.cpp+++ b/llama.cpp/llama.cpp@@ -10904,7 +10904,8 @@ struct llm_build_context {// inp_pos - contains the positionsggml_tensor * inp_pos = build_inp_pos();- ggml_tensor * inp_attn = build_inp_KQ_mask();+ // 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) {ggml_tensor * inpSA = inpL;@@ -10927,49 +10928,41 @@ struct llm_build_context {ggml_tensor * Vcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wv, cur);cb(Vcur, "Vcur", il);- Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);- Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);- Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);-- Qcur = llm_build_norm(ctx0, Qcur, hparams,- model.layers[il].attn_q_norm, NULL,- LLM_NORM_RMS, cb, il);+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);+ Qcur = llm_build_norm(ctx0, Qcur, hparams, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, cb, il);cb(Qcur, "Qcur_normed", il);Qcur = ggml_rope_ext(- ctx0, Qcur, inp_pos, nullptr,- n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,- ext_factor, attn_factor, beta_fast, beta_slow- );+ ctx0, Qcur, inp_pos, nullptr,+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,+ ext_factor, attn_factor, beta_fast, beta_slow+ );+ cb(Qcur, "Qcur", il);- Kcur = llm_build_norm(ctx0, Kcur, hparams,- model.layers[il].attn_k_norm, NULL,- LLM_NORM_RMS, cb, il);+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);+ Kcur = llm_build_norm(ctx0, Kcur, hparams, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, cb, il);cb(Kcur, "Kcur_normed", il);Kcur = ggml_rope_ext(- ctx0, Kcur, inp_pos, nullptr,- n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,- ext_factor, attn_factor, beta_fast, beta_slow- );-- cb(Qcur, "Qcur", il);+ ctx0, Kcur, inp_pos, nullptr,+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,+ ext_factor, attn_factor, beta_fast, beta_slow+ );cb(Kcur, "Kcur", il);- cb(Vcur, "Vcur", il);cur = llm_build_kv(ctx0, lctx, kv_self, gf,model.layers[il].wo, model.layers[il].bo,- Kcur, Vcur, Qcur, inp_attn, n_tokens, kv_head, n_kv, 1.0f/sqrtf(float(n_embd_head)), cb, il);+ Kcur, Vcur, Qcur, KQ_mask, n_tokens, kv_head, n_kv, 1.0f/sqrtf(float(n_embd_head)), cb, il);}if (il == n_layer - 1) {// skip computing output for unused tokens- 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);inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);}- ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);+ struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);cb(ffn_inp, "ffn_inp", il);// feed-forward network@@ -11016,8 +11009,10 @@ struct llm_build_context {struct ggml_cgraph * build_qwen3moe() {struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, llama_model_max_nodes(model), false);- const int64_t n_embd_head = hparams.n_embd_head_v;+ // mutable variable, needed during the last layer of the computation to skip unused tokens+ int32_t n_tokens = this->n_tokens;+ const int64_t n_embd_head = hparams.n_embd_head_v;GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);GGML_ASSERT(n_embd_head == hparams.n_rot);@@ -11029,7 +11024,8 @@ struct llm_build_context {// inp_pos - contains the positionsggml_tensor * inp_pos = build_inp_pos();- ggml_tensor * inp_attn = build_inp_KQ_mask();+ // 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) {ggml_tensor * inpSA = inpL;@@ -11052,45 +11048,37 @@ struct llm_build_context {ggml_tensor * Vcur = llm_build_lora_mm(lctx, ctx0, model.layers[il].wv, cur);cb(Vcur, "Vcur", il);- Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);- Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);- Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);-- Qcur = llm_build_norm(ctx0, Qcur, hparams,- model.layers[il].attn_q_norm, NULL,- LLM_NORM_RMS, cb, il);+ Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);+ Qcur = llm_build_norm(ctx0, Qcur, hparams, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, cb, il);cb(Qcur, "Qcur_normed", il);Qcur = ggml_rope_ext(- ctx0, Qcur, inp_pos, nullptr,- n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,- ext_factor, attn_factor, beta_fast, beta_slow- );+ ctx0, Qcur, inp_pos, nullptr,+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,+ ext_factor, attn_factor, beta_fast, beta_slow+ );+ cb(Qcur, "Qcur", il);- Kcur = llm_build_norm(ctx0, Kcur, hparams,- model.layers[il].attn_k_norm, NULL,- LLM_NORM_RMS, cb, il);+ Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);+ Kcur = llm_build_norm(ctx0, Kcur, hparams, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, cb, il);cb(Kcur, "Kcur_normed", il);Kcur = ggml_rope_ext(- ctx0, Kcur, inp_pos, nullptr,- n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,- ext_factor, attn_factor, beta_fast, beta_slow- );-- cb(Qcur, "Qcur", il);+ ctx0, Kcur, inp_pos, nullptr,+ n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,+ ext_factor, attn_factor, beta_fast, beta_slow+ );cb(Kcur, "Kcur", il);- cb(Vcur, "Vcur", il);- // inp_attncur = llm_build_kv(ctx0, lctx, kv_self, gf,model.layers[il].wo, model.layers[il].bo,- Kcur, Vcur, Qcur, inp_attn, n_tokens, kv_head, n_kv, 1.0f/sqrtf(float(n_embd_head)), cb, il);+ Kcur, Vcur, Qcur, KQ_mask, n_tokens, kv_head, n_kv, 1.0f/sqrtf(float(n_embd_head)), cb, il);}if (il == n_layer - 1) {// skip computing output for unused tokens- ggml_tensor * inp_out_ids = build_inp_out_ids();+ struct ggml_tensor * inp_out_ids = build_inp_out_ids();+ n_tokens = n_outputs;cur = ggml_get_rows(ctx0, cur, inp_out_ids);inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);}@@ -11104,8 +11092,8 @@ struct llm_build_context {LLM_NORM_RMS, cb, il);cb(cur, "ffn_norm", il);- ggml_tensor * moe_out =- llm_build_moe_ffn(ctx0, lctx, cur,+ cur =+ llm_build_moe_ffn(ctx0, lctx, cur,model.layers[il].ffn_gate_inp,model.layers[il].ffn_up_exps,model.layers[il].ffn_gate_exps,@@ -11113,10 +11101,8 @@ struct llm_build_context {n_expert, n_expert_used,LLM_FFN_SILU, true,false, 0.0,- cb,- il);- cb(moe_out, "ffn_moe_out", il);- cur = moe_out;+ cb, il);+ cb(cur, "ffn_moe_out", il);cur = ggml_add(ctx0, cur, ffn_inp);