Felipe97/llama-cpp-compiled
01.1k
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 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);33 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);34 35 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);36 37 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);38 layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);39 40 // Jais-2 uses simple MLP (no gate) with biases41 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);42 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);43 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);44 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);45 }46}47 48std::unique_ptr<llm_graph_context> llama_model_jais2::build_arch_graph(const llm_graph_params & params) const {49 return std::make_unique<graph>(*this, params);50}51 52// JAIS-2 model graph builder53// Uses: LayerNorm (not RMSNorm), relu2 activation, separate Q/K/V, RoPE embeddings54llama_model_jais2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {55 const int64_t n_embd_head = hparams.n_embd_head_v();56 57 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());58 GGML_ASSERT(n_embd_head == n_rot);59 60 ggml_tensor * cur;61 ggml_tensor * inpL;62 63 inpL = build_inp_embd(model.tok_embd);64 65 // inp_pos - contains the positions66 ggml_tensor * inp_pos = build_inp_pos();67 68 // KV input for attention69 auto * inp_attn = build_attn_inp_kv();70 71 ggml_tensor * inp_out_ids = build_inp_out_ids();72 73 for (int il = 0; il < n_layer; ++il) {74 // Pre-attention LayerNorm75 cur = build_norm(inpL,76 model.layers[il].attn_norm,77 model.layers[il].attn_norm_b,78 LLM_NORM, il);79 cb(cur, "attn_norm", il);80 81 // Self-attention with separate Q, K, V projections82 {83 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,84 n_embd_head, n_head, n_head_kv, il);85 86 // Apply RoPE87 Qcur = ggml_rope_ext(88 ctx0, Qcur, inp_pos, nullptr,89 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,90 ext_factor, attn_factor, beta_fast, beta_slow91 );92 93 Kcur = ggml_rope_ext(94 ctx0, Kcur, 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 cb(Qcur, "Qcur_rope", il);100 cb(Kcur, "Kcur_rope", il);101 102 cur = build_attn(inp_attn,103 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,104 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);105 }106 107 if (il == n_layer - 1 && inp_out_ids) {108 cur = ggml_get_rows(ctx0, cur, inp_out_ids);109 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);110 }111 112 // Residual connection113 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);114 cb(ffn_inp, "ffn_inp", il);115 116 // Pre-FFN LayerNorm117 cur = build_norm(ffn_inp,118 model.layers[il].ffn_norm,119 model.layers[il].ffn_norm_b,120 LLM_NORM, il);121 cb(cur, "ffn_norm", il);122 123 // FFN with relu2 activation (ReLU squared) - no gate projection124 // up -> relu2 -> down125 cur = build_ffn(cur,126 model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,127 NULL, NULL, NULL, // no gate128 model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,129 NULL,130 LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);131 cb(cur, "ffn_out", il);132 133 // Residual connection134 inpL = ggml_add(ctx0, cur, ffn_inp);135 inpL = build_cvec(inpL, il);136 cb(inpL, "l_out", il);137 }138 139 // Final LayerNorm140 cur = build_norm(inpL,141 model.output_norm,142 model.output_norm_b,143 LLM_NORM, -1);144 cb(cur, "result_norm", -1);145 146 res->t_embd = cur;147 148 // Output projection149 cur = build_lora_mm(model.output, cur, model.output_s);150 cb(cur, "result_output", -1);151 152 res->t_logits = cur;153 154 ggml_build_forward_expand(gf, cur);155}156 