| 1 | #include "models.h" |
| 2 | |
| 3 | llm_build_jais::llm_build_jais(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(tok_embd: model.tok_embd); |
| 13 | |
| 14 | auto * inp_attn = build_attn_inp_kv(); |
| 15 | |
| 16 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 17 | |
| 18 | for (int il = 0; il < n_layer; ++il) { |
| 19 | cur = build_norm(cur: inpL, |
| 20 | mw: model.layers[il].attn_norm, |
| 21 | mb: model.layers[il].attn_norm_b, |
| 22 | type: LLM_NORM, il); |
| 23 | cb(cur, name: "attn_norm" , il); |
| 24 | |
| 25 | // self-attention |
| 26 | { |
| 27 | cur = build_lora_mm(w: model.layers[il].wqkv, cur); |
| 28 | cb(cur, name: "wqkv" , il); |
| 29 | |
| 30 | cur = ggml_add(ctx: ctx0, a: cur, b: model.layers[il].bqkv); |
| 31 | cb(cur, name: "bqkv" , il); |
| 32 | |
| 33 | 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), nb2: cur->nb[1], offset: 0*cur->nb[0]*(n_embd)); |
| 34 | 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), nb2: cur->nb[1], offset: 1*cur->nb[0]*(n_embd)); |
| 35 | 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), nb2: cur->nb[1], offset: 1*cur->nb[0]*(n_embd + n_embd_gqa)); |
| 36 | |
| 37 | cb(cur: Qcur, name: "Qcur" , il); |
| 38 | cb(cur: Kcur, name: "Kcur" , il); |
| 39 | cb(cur: Vcur, name: "Vcur" , il); |
| 40 | |
| 41 | cur = build_attn(inp: inp_attn, |
| 42 | wo: model.layers[il].wo, wo_b: model.layers[il].bo, |
| 43 | q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f/float(n_embd_head), il); |
| 44 | } |
| 45 | if (il == n_layer - 1 && inp_out_ids) { |
| 46 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 47 | inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids); |
| 48 | } |
| 49 | // add the input |
| 50 | ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpL); |
| 51 | cb(cur: ffn_inp, name: "ffn_inp" , il); |
| 52 | |
| 53 | // FF |
| 54 | { |
| 55 | cur = build_norm(cur: ffn_inp, |
| 56 | mw: model.layers[il].ffn_norm, |
| 57 | mb: model.layers[il].ffn_norm_b, |
| 58 | type: LLM_NORM, il); |
| 59 | cb(cur, name: "ffn_norm" , il); |
| 60 | |
| 61 | cur = build_ffn(cur, |
| 62 | up: model.layers[il].ffn_up, up_b: model.layers[il].ffn_up_b, NULL, |
| 63 | gate: model.layers[il].ffn_gate, gate_b: model.layers[il].ffn_gate_b, NULL, |
| 64 | down: model.layers[il].ffn_down, down_b: model.layers[il].ffn_down_b, NULL, |
| 65 | NULL, |
| 66 | type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il); |
| 67 | cb(cur, name: "ffn_out" , il); |
| 68 | } |
| 69 | inpL = ggml_add(ctx: ctx0, a: cur, b: ffn_inp); |
| 70 | cb(cur: inpL, name: "l_out" , il); |
| 71 | } |
| 72 | cur = build_norm(cur: inpL, |
| 73 | mw: model.output_norm, |
| 74 | mb: model.output_norm_b, |
| 75 | type: LLM_NORM, il: -1); |
| 76 | |
| 77 | cb(cur, name: "result_norm" , il: -1); |
| 78 | res->t_embd = cur; |
| 79 | |
| 80 | cur = build_lora_mm(w: model.output, cur); |
| 81 | |
| 82 | cb(cur, name: "result_output" , il: -1); |
| 83 | res->t_logits = cur; |
| 84 | |
| 85 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 86 | } |
| 87 | |