|
| 1 | +#include "models.h" |
| 2 | + |
| 3 | +llm_build_glm4v::llm_build_glm4v(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 4 | + const int64_t n_embd_head = hparams.n_embd_head_v; |
| 5 | + const int64_t n_embd_gqa = hparams.n_embd_v_gqa(); |
| 6 | + |
| 7 | + GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 8 | + |
| 9 | + ggml_tensor * cur; |
| 10 | + ggml_tensor * inpL; |
| 11 | + |
| 12 | + inpL = build_inp_embd(model.tok_embd); |
| 13 | + |
| 14 | + // inp_pos - contains the positions |
| 15 | + ggml_tensor * inp_pos = build_inp_pos(); |
| 16 | + |
| 17 | + auto * inp_attn = build_attn_inp_kv(); |
| 18 | + |
| 19 | + // M-RoPE sections from hparams |
| 20 | + int sections[4]; |
| 21 | + std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections); |
| 22 | + |
| 23 | + ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 24 | + |
| 25 | + for (int il = 0; il < n_layer; ++il) { |
| 26 | + ggml_tensor * inpSA = inpL; |
| 27 | + |
| 28 | + // Pre-attention norm |
| 29 | + cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
| 30 | + cb(cur, "attn_norm", il); |
| 31 | + |
| 32 | + // self-attention |
| 33 | + { |
| 34 | + ggml_tensor * Qcur = nullptr; |
| 35 | + ggml_tensor * Kcur = nullptr; |
| 36 | + ggml_tensor * Vcur = nullptr; |
| 37 | + |
| 38 | + if (model.layers[il].wqkv == nullptr) { |
| 39 | + Qcur = build_lora_mm(model.layers[il].wq, cur); |
| 40 | + if (model.layers[il].bq) { |
| 41 | + Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq); |
| 42 | + } |
| 43 | + Kcur = build_lora_mm(model.layers[il].wk, cur); |
| 44 | + if (model.layers[il].bk) { |
| 45 | + Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk); |
| 46 | + } |
| 47 | + Vcur = build_lora_mm(model.layers[il].wv, cur); |
| 48 | + if (model.layers[il].bv) { |
| 49 | + Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv); |
| 50 | + } |
| 51 | + Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
| 52 | + Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
| 53 | + Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
| 54 | + } else { |
| 55 | + cur = build_lora_mm(model.layers[il].wqkv, cur); |
| 56 | + cb(cur, "wqkv", il); |
| 57 | + if (model.layers[il].bqkv) { |
| 58 | + cur = ggml_add(ctx0, cur, model.layers[il].bqkv); |
| 59 | + cb(cur, "bqkv", il); |
| 60 | + } |
| 61 | + Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), cur->nb[1], |
| 62 | + 0 * sizeof(float) * (n_embd)); |
| 63 | + Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
| 64 | + cur->nb[1], 1 * sizeof(float) * (n_embd)); |
| 65 | + Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
| 66 | + cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa)); |
| 67 | + } |
| 68 | + |
| 69 | + // GLM4V uses M-RoPE (multi-dimensional rotary position embeddings) |
| 70 | + Qcur = ggml_rope_multi(ctx0, Qcur, inp_pos, nullptr, |
| 71 | + n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
| 72 | + ext_factor, attn_factor, beta_fast, beta_slow); |
| 73 | + |
| 74 | + Kcur = ggml_rope_multi(ctx0, Kcur, inp_pos, nullptr, |
| 75 | + n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
| 76 | + ext_factor, attn_factor, beta_fast, beta_slow); |
| 77 | + |
| 78 | + cb(Qcur, "Qcur", il); |
| 79 | + cb(Kcur, "Kcur", il); |
| 80 | + cb(Vcur, "Vcur", il); |
| 81 | + |
| 82 | + cur = build_attn(inp_attn, |
| 83 | + model.layers[il].wo, NULL, |
| 84 | + Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il); |
| 85 | + } |
| 86 | + if (il == n_layer - 1 && inp_out_ids) { |
| 87 | + cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
| 88 | + inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
| 89 | + } |
| 90 | + // Post-attention norm |
| 91 | + cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il); |
| 92 | + cb(cur, "post_attn_norm", il); |
| 93 | + |
| 94 | + // Add the input (residual connection after post-attention norm) |
| 95 | + ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
| 96 | + cb(ffn_inp, "ffn_inp", il); |
| 97 | + |
| 98 | + // FF |
| 99 | + { |
| 100 | + // Pre-MLP norm |
| 101 | + cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
| 102 | + cb(cur, "ffn_norm", il); |
| 103 | + |
| 104 | + // MLP |
| 105 | + cur = build_ffn(cur, |
| 106 | + model.layers[il].ffn_up, NULL, NULL, |
| 107 | + NULL, NULL, NULL, |
| 108 | + model.layers[il].ffn_down, NULL, NULL, |
| 109 | + NULL, LLM_FFN_SWIGLU, LLM_FFN_SEQ, il); |
| 110 | + cb(cur, "ffn_out", il); |
| 111 | + |
| 112 | + // Post-MLP norm |
| 113 | + cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il); |
| 114 | + cb(cur, "post_mlp_norm", il); |
| 115 | + } |
| 116 | + // Add residual connection after post-MLP norm |
| 117 | + inpL = ggml_add(ctx0, cur, ffn_inp); |
| 118 | + cb(inpL, "l_out", il); |
| 119 | + } |
| 120 | + // Final norm |
| 121 | + cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1); |
| 122 | + |
| 123 | + cb(cur, "result_norm", -1); |
| 124 | + res->t_embd = cur; |
| 125 | + |
| 126 | + // Output projection |
| 127 | + cur = build_lora_mm(model.output, cur); |
| 128 | + |
| 129 | + cb(cur, "result_output", -1); |
| 130 | + res->t_logits = cur; |
| 131 | + |
| 132 | + ggml_build_forward_expand(gf, cur); |
| 133 | +} |
0 commit comments