| 1 | #include "models.h" |
| 2 | |
| 3 | llm_build_bloom::llm_build_bloom(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 | inpL = build_norm(cur: inpL, |
| 17 | mw: model.tok_norm, |
| 18 | mb: model.tok_norm_b, |
| 19 | type: LLM_NORM, il: -1); |
| 20 | cb(cur: inpL, name: "inp_norm" , il: -1); |
| 21 | |
| 22 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 23 | |
| 24 | for (int il = 0; il < n_layer; ++il) { |
| 25 | cur = build_norm(cur: inpL, |
| 26 | mw: model.layers[il].attn_norm, |
| 27 | mb: model.layers[il].attn_norm_b, |
| 28 | type: LLM_NORM, il); |
| 29 | cb(cur, name: "attn_norm" , il); |
| 30 | |
| 31 | // self-attention |
| 32 | { |
| 33 | cur = build_lora_mm(w: model.layers[il].wqkv, cur); |
| 34 | cb(cur, name: "wqkv" , il); |
| 35 | |
| 36 | cur = ggml_add(ctx: ctx0, a: cur, b: model.layers[il].bqkv); |
| 37 | cb(cur, name: "bqkv" , il); |
| 38 | |
| 39 | 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*sizeof(float)*(n_embd)); |
| 40 | 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*sizeof(float)*(n_embd)); |
| 41 | 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*sizeof(float)*(n_embd + n_embd_gqa)); |
| 42 | |
| 43 | cb(cur: Qcur, name: "Qcur" , il); |
| 44 | cb(cur: Kcur, name: "Kcur" , il); |
| 45 | cb(cur: Vcur, name: "Vcur" , il); |
| 46 | |
| 47 | cur = build_attn(inp: inp_attn, |
| 48 | wo: model.layers[il].wo, wo_b: model.layers[il].bo, |
| 49 | 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); |
| 50 | } |
| 51 | |
| 52 | if (il == n_layer - 1 && inp_out_ids) { |
| 53 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 54 | inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids); |
| 55 | } |
| 56 | |
| 57 | // Add the input |
| 58 | ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpL); |
| 59 | cb(cur: ffn_inp, name: "ffn_inp" , il); |
| 60 | |
| 61 | // FF |
| 62 | { |
| 63 | cur = build_norm(cur: ffn_inp, |
| 64 | mw: model.layers[il].ffn_norm, |
| 65 | mb: model.layers[il].ffn_norm_b, |
| 66 | type: LLM_NORM, il); |
| 67 | cb(cur, name: "ffn_norm" , il); |
| 68 | |
| 69 | cur = build_ffn(cur, |
| 70 | up: model.layers[il].ffn_up, up_b: model.layers[il].ffn_up_b, NULL, |
| 71 | NULL, NULL, NULL, |
| 72 | down: model.layers[il].ffn_down, down_b: model.layers[il].ffn_down_b, NULL, |
| 73 | NULL, |
| 74 | type_op: LLM_FFN_GELU, type_gate: LLM_FFN_SEQ, il); |
| 75 | cb(cur, name: "ffn_out" , il); |
| 76 | } |
| 77 | |
| 78 | cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp); |
| 79 | |
| 80 | cur = build_cvec(cur, il); |
| 81 | cb(cur, name: "l_out" , il); |
| 82 | |
| 83 | // input for next layer |
| 84 | inpL = cur; |
| 85 | } |
| 86 | |
| 87 | cur = build_norm(cur: inpL, |
| 88 | mw: model.output_norm, |
| 89 | mb: model.output_norm_b, |
| 90 | type: LLM_NORM, il: -1); |
| 91 | |
| 92 | cb(cur, name: "result_norm" , il: -1); |
| 93 | res->t_embd = cur; |
| 94 | |
| 95 | cur = build_lora_mm(w: model.output, cur); |
| 96 | |
| 97 | cb(cur, name: "result_output" , il: -1); |
| 98 | res->t_logits = cur; |
| 99 | |
| 100 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 101 | } |
| 102 | |