| 1 | #include "models.h" |
| 2 | |
| 3 | |
| 4 | |
| 5 | llm_build_gemma_embedding::llm_build_gemma_embedding(const llama_model & model, const llm_graph_params & params) : |
| 6 | llm_graph_context(params) { |
| 7 | const int64_t n_embd_head = hparams.n_embd_head_k; |
| 8 | |
| 9 | ggml_tensor * cur; |
| 10 | ggml_tensor * inpL; |
| 11 | |
| 12 | inpL = build_inp_embd(tok_embd: model.tok_embd); |
| 13 | |
| 14 | // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings) |
| 15 | if (ubatch.token) { |
| 16 | inpL = ggml_scale(ctx: ctx0, a: inpL, s: sqrtf(x: n_embd)); |
| 17 | cb(cur: inpL, name: "inp_scaled" , il: -1); |
| 18 | } |
| 19 | |
| 20 | // inp_pos - contains the positions |
| 21 | ggml_tensor * inp_pos = build_inp_pos(); |
| 22 | |
| 23 | auto * inp_attn = build_attn_inp_no_cache(); |
| 24 | |
| 25 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 26 | |
| 27 | for (int il = 0; il < n_layer; ++il) { |
| 28 | const float freq_base_l = model.get_rope_freq_base(cparams, il); |
| 29 | const float freq_scale_l = model.get_rope_freq_scale(cparams, il); |
| 30 | |
| 31 | // norm |
| 32 | cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il); |
| 33 | cb(cur, name: "attn_norm" , il); |
| 34 | |
| 35 | // self-attention |
| 36 | { |
| 37 | // compute Q and K and RoPE them |
| 38 | ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur); |
| 39 | cb(cur: Qcur, name: "Qcur" , il); |
| 40 | |
| 41 | ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur); |
| 42 | cb(cur: Kcur, name: "Kcur" , il); |
| 43 | |
| 44 | ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur); |
| 45 | cb(cur: Vcur, name: "Vcur" , il); |
| 46 | |
| 47 | Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens); |
| 48 | Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 49 | Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 50 | |
| 51 | Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL, type: LLM_NORM_RMS, il); |
| 52 | cb(cur: Qcur, name: "Qcur_normed" , il); |
| 53 | |
| 54 | Qcur = ggml_rope_ext(ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base: freq_base_l, freq_scale: freq_scale_l, |
| 55 | ext_factor, attn_factor, beta_fast, beta_slow); |
| 56 | |
| 57 | Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL, type: LLM_NORM_RMS, il); |
| 58 | cb(cur: Kcur, name: "Kcur_normed" , il); |
| 59 | |
| 60 | Kcur = ggml_rope_ext(ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base: freq_base_l, freq_scale: freq_scale_l, |
| 61 | ext_factor, attn_factor, beta_fast, beta_slow); |
| 62 | |
| 63 | cb(cur: Qcur, name: "Qcur" , il); |
| 64 | cb(cur: Kcur, name: "Kcur" , il); |
| 65 | cb(cur: Vcur, name: "Vcur" , il); |
| 66 | |
| 67 | // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315 |
| 68 | Qcur = ggml_scale(ctx: ctx0, a: Qcur, s: hparams.f_attention_scale); |
| 69 | |
| 70 | cur = |
| 71 | build_attn(inp: inp_attn, |
| 72 | wo: model.layers[il].wo, NULL, |
| 73 | q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f, il); |
| 74 | } |
| 75 | |
| 76 | if (il == n_layer - 1 && inp_out_ids) { |
| 77 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 78 | inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids); |
| 79 | } |
| 80 | |
| 81 | cur = build_norm(cur, mw: model.layers[il].attn_post_norm, NULL, type: LLM_NORM_RMS, il); |
| 82 | cb(cur, name: "attn_post_norm" , il); |
| 83 | |
| 84 | ggml_tensor * sa_out = ggml_add(ctx: ctx0, a: cur, b: inpL); |
| 85 | cb(cur: sa_out, name: "sa_out" , il); |
| 86 | |
| 87 | cur = build_norm(cur: sa_out, mw: model.layers[il].ffn_norm, NULL, type: LLM_NORM_RMS, il); |
| 88 | cb(cur, name: "ffn_norm" , il); |
| 89 | |
| 90 | // feed-forward network |
| 91 | { |
| 92 | cur = build_ffn(cur, |
| 93 | up: model.layers[il].ffn_up, NULL, NULL, |
| 94 | gate: model.layers[il].ffn_gate, NULL, NULL, |
| 95 | down: model.layers[il].ffn_down, NULL, NULL, |
| 96 | NULL, type_op: LLM_FFN_GELU, type_gate: LLM_FFN_PAR, il); |
| 97 | cb(cur, name: "ffn_out" , il); |
| 98 | } |
| 99 | |
| 100 | cur = build_norm(cur, mw: model.layers[il].ffn_post_norm, NULL, type: LLM_NORM_RMS, il: -1); |
| 101 | cb(cur, name: "ffn_post_norm" , il: -1); |
| 102 | |
| 103 | cur = ggml_add(ctx: ctx0, a: cur, b: sa_out); |
| 104 | |
| 105 | cur = build_cvec(cur, il); |
| 106 | cb(cur, name: "l_out" , il); |
| 107 | |
| 108 | // input for next layer |
| 109 | inpL = cur; |
| 110 | } |
| 111 | |
| 112 | cur = inpL; |
| 113 | |
| 114 | cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1); |
| 115 | |
| 116 | cb(cur, name: "result_norm" , il: -1); |
| 117 | res->t_embd = cur; |
| 118 | |
| 119 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 120 | } |
| 121 | |