Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03k
1#include "models.h"2 3void llama_model_jais2::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);5 6 switch (hparams.n_layer()) {7 case 32: type = LLM_TYPE_8B; break;8 case 68: type = LLM_TYPE_70B; break;9 default: type = LLM_TYPE_UNKNOWN;10 }11}12 13void llama_model_jais2::load_arch_tensors(llama_model_loader &) {14 LLAMA_LOAD_LOCALS;15 16 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);17 18 // output19 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);20 output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);21 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);22 if (!output) {23 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);24 }25 26 for (int i = 0; i < n_layer; ++i) {27 auto & layer = layers[i];28 29 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);30 layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);31 32 layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);33 layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);34 layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);35 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);36 37 // attention biases - all have shape n_embd (output dimension of projections)38 layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);39 layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd}, 0);40 layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd}, 0);41 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);42 43 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);44 layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);45 46 // Jais-2 uses simple MLP (no gate) with biases47 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);48 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);49 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);50 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);51 }52}53 54std::unique_ptr<llm_graph_context> llama_model_jais2::build_arch_graph(const llm_graph_params & params) const {55 return std::make_unique<graph>(*this, params);56}57 58// JAIS-2 model graph builder59// Uses: LayerNorm (not RMSNorm), relu2 activation, separate Q/K/V, RoPE embeddings60llama_model_jais2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {61 const int64_t n_embd_head = hparams.n_embd_head_v();62 63 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());64 GGML_ASSERT(n_embd_head == n_rot);65 66 ggml_tensor * cur;67 ggml_tensor * inpL;68 69 inpL = build_inp_embd(model.tok_embd);70 71 // inp_pos - contains the positions72 ggml_tensor * inp_pos = build_inp_pos();73 74 // KV input for attention75 auto * inp_attn = build_attn_inp_kv();76 77 ggml_tensor * inp_out_ids = build_inp_out_ids();78 79 for (int il = 0; il < n_layer; ++il) {80 // Pre-attention LayerNorm81 cur = build_norm(inpL,82 model.layers[il].attn_norm,83 model.layers[il].attn_norm_b,84 LLM_NORM, il);85 cb(cur, "attn_norm", il);86 87 // Self-attention with separate Q, K, V projections88 {89 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,90 n_embd_head, n_head, n_head_kv, il);91 92 // Apply RoPE93 Qcur = ggml_rope_ext(94 ctx0, Qcur, inp_pos, nullptr,95 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,96 ext_factor, attn_factor, beta_fast, beta_slow97 );98 99 Kcur = ggml_rope_ext(100 ctx0, Kcur, inp_pos, nullptr,101 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,102 ext_factor, attn_factor, beta_fast, beta_slow103 );104 105 cb(Qcur, "Qcur_rope", il);106 cb(Kcur, "Kcur_rope", il);107 108 cur = build_attn(inp_attn,109 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,110 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);111 }112 113 if (il == n_layer - 1 && inp_out_ids) {114 cur = ggml_get_rows(ctx0, cur, inp_out_ids);115 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);116 }117 118 // Residual connection119 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);120 cb(ffn_inp, "ffn_inp", il);121 122 // Pre-FFN LayerNorm123 cur = build_norm(ffn_inp,124 model.layers[il].ffn_norm,125 model.layers[il].ffn_norm_b,126 LLM_NORM, il);127 cb(cur, "ffn_norm", il);128 129 // FFN with relu2 activation (ReLU squared) - no gate projection130 // up -> relu2 -> down131 cur = build_ffn(cur,132 model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,133 NULL, NULL, NULL, // no gate134 model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,135 NULL,136 LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);137 cb(cur, "ffn_out", il);138 139 // Residual connection140 inpL = ggml_add(ctx0, cur, ffn_inp);141 inpL = build_cvec(inpL, il);142 cb(inpL, "l_out", il);143 }144 145 // Final LayerNorm146 cur = build_norm(inpL,147 model.output_norm,148 model.output_norm_b,149 LLM_NORM, -1);150 cb(cur, "result_norm", -1);151 152 res->t_embd = cur;153 154 // Output projection155 cur = build_lora_mm(model.output, cur, model.output_s);156 cb(cur, "result_output", -1);157 158 res->t_logits = cur;159 160 ggml_build_forward_expand(gf, cur);161}162 