| 1 | #include "models.h" |
| 2 | |
| 3 | |
| 4 | |
| 5 | llm_build_mpt::llm_build_mpt(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 6 | const int64_t n_embd_head = hparams.n_embd_head_v; |
| 7 | const int64_t n_embd_gqa = hparams.n_embd_v_gqa(); |
| 8 | |
| 9 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 10 | |
| 11 | ggml_tensor * cur; |
| 12 | ggml_tensor * pos; |
| 13 | ggml_tensor * inpL; |
| 14 | |
| 15 | inpL = build_inp_embd(tok_embd: model.tok_embd); |
| 16 | |
| 17 | auto * inp_attn = build_attn_inp_kv(); |
| 18 | |
| 19 | if (model.pos_embd) { |
| 20 | // inp_pos - contains the positions |
| 21 | ggml_tensor * inp_pos = build_inp_pos(); |
| 22 | pos = ggml_get_rows(ctx: ctx0, a: model.pos_embd, b: inp_pos); |
| 23 | cb(cur: pos, name: "pos_embd" , il: -1); |
| 24 | |
| 25 | inpL = ggml_add(ctx: ctx0, a: inpL, b: pos); |
| 26 | cb(cur: inpL, name: "inpL" , il: -1); |
| 27 | } |
| 28 | |
| 29 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 30 | |
| 31 | for (int il = 0; il < n_layer; ++il) { |
| 32 | ggml_tensor * attn_norm; |
| 33 | |
| 34 | attn_norm = build_norm(cur: inpL, mw: model.layers[il].attn_norm, mb: model.layers[il].attn_norm_b, type: LLM_NORM, il); |
| 35 | cb(cur: attn_norm, name: "attn_norm" , il); |
| 36 | |
| 37 | // self-attention |
| 38 | { |
| 39 | cur = attn_norm; |
| 40 | |
| 41 | cur = build_lora_mm(w: model.layers[il].wqkv, cur); |
| 42 | cb(cur, name: "wqkv" , il); |
| 43 | |
| 44 | if (model.layers[il].bqkv) { |
| 45 | cur = ggml_add(ctx: ctx0, a: cur, b: model.layers[il].bqkv); |
| 46 | cb(cur, name: "bqkv" , il); |
| 47 | } |
| 48 | |
| 49 | if (hparams.f_clamp_kqv > 0.0f) { |
| 50 | cur = ggml_clamp(ctx: ctx0, a: cur, min: -hparams.f_clamp_kqv, max: hparams.f_clamp_kqv); |
| 51 | cb(cur, name: "wqkv_clamped" , il); |
| 52 | } |
| 53 | |
| 54 | ggml_tensor * Qcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens, nb1: n_embd_head * sizeof(float), |
| 55 | nb2: cur->nb[1], offset: 0 * sizeof(float) * (n_embd)); |
| 56 | ggml_tensor * Kcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head * sizeof(float), |
| 57 | nb2: cur->nb[1], offset: 1 * sizeof(float) * (n_embd)); |
| 58 | ggml_tensor * Vcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head * sizeof(float), |
| 59 | nb2: cur->nb[1], offset: 1 * sizeof(float) * (n_embd + n_embd_gqa)); |
| 60 | |
| 61 | // Q/K Layernorm |
| 62 | if (model.layers[il].attn_q_norm) { |
| 63 | Qcur = ggml_reshape_2d(ctx: ctx0, a: Qcur, ne0: n_embd_head * n_head, ne1: n_tokens); |
| 64 | Kcur = ggml_reshape_2d(ctx: ctx0, a: Kcur, ne0: n_embd_head * n_head_kv, ne1: n_tokens); |
| 65 | |
| 66 | Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, mb: model.layers[il].attn_q_norm_b, type: LLM_NORM, il); |
| 67 | |
| 68 | Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, mb: model.layers[il].attn_k_norm_b, type: LLM_NORM, il); |
| 69 | |
| 70 | Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens); |
| 71 | Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 72 | } |
| 73 | |
| 74 | cb(cur: Qcur, name: "Qcur" , il); |
| 75 | cb(cur: Kcur, name: "Kcur" , il); |
| 76 | cb(cur: Vcur, name: "Vcur" , il); |
| 77 | |
| 78 | cur = build_attn(inp: inp_attn, |
| 79 | wo: model.layers[il].wo, wo_b: model.layers[il].bo, |
| 80 | q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f / sqrtf(x: float(n_embd_head)), il); |
| 81 | } |
| 82 | |
| 83 | if (il == n_layer - 1 && inp_out_ids) { |
| 84 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 85 | inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids); |
| 86 | } |
| 87 | |
| 88 | // Add the input |
| 89 | ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpL); |
| 90 | cb(cur: ffn_inp, name: "ffn_inp" , il); |
| 91 | |
| 92 | // feed forward |
| 93 | { |
| 94 | cur = build_norm(cur: ffn_inp, mw: model.layers[il].ffn_norm, mb: model.layers[il].ffn_norm_b, type: LLM_NORM, il); |
| 95 | cb(cur, name: "ffn_norm" , il); |
| 96 | cur = build_ffn(cur, |
| 97 | up: model.layers[il].ffn_up, up_b: model.layers[il].ffn_up_b, NULL, |
| 98 | NULL, NULL, NULL, |
| 99 | down: model.layers[il].ffn_down, down_b: model.layers[il].ffn_down_b, NULL, |
| 100 | act_scales: model.layers[il].ffn_act, type_op: LLM_FFN_GELU, type_gate: LLM_FFN_SEQ, il); |
| 101 | cb(cur, name: "ffn_out" , il); |
| 102 | } |
| 103 | |
| 104 | cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp); |
| 105 | |
| 106 | cur = build_cvec(cur, il); |
| 107 | cb(cur, name: "l_out" , il); |
| 108 | |
| 109 | // input for next layer |
| 110 | inpL = cur; |
| 111 | } |
| 112 | |
| 113 | cur = inpL; |
| 114 | |
| 115 | cur = build_norm(cur, mw: model.output_norm, mb: model.output_norm_b, type: LLM_NORM, il: -1); |
| 116 | |
| 117 | cb(cur, name: "result_norm" , il: -1); |
| 118 | res->t_embd = cur; |
| 119 | |
| 120 | cur = build_lora_mm(w: model.output, cur); |
| 121 | |
| 122 | cb(cur, name: "result_output" , il: -1); |
| 123 | res->t_logits = cur; |
| 124 | |
| 125 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 126 | } |
| 127 | |