1#include "models.h"
2
3
4llm_build_phi2::llm_build_phi2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
5 const int64_t n_embd_head = hparams.n_embd_head_v;
6 const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
7
8 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
9
10 ggml_tensor * cur;
11 ggml_tensor * attn_norm_output;
12 ggml_tensor * ffn_output;
13 ggml_tensor * inpL;
14
15 inpL = build_inp_embd(tok_embd: model.tok_embd);
16
17 // inp_pos - contains the positions
18 ggml_tensor * inp_pos = build_inp_pos();
19
20 auto * inp_attn = build_attn_inp_kv();
21
22 ggml_tensor * inp_out_ids = build_inp_out_ids();
23
24 for (int il = 0; il < n_layer; ++il) {
25 attn_norm_output = 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: attn_norm_output, name: "attn_norm", il);
30
31 // self-attention
32 {
33 ggml_tensor * Qcur = nullptr;
34 ggml_tensor * Kcur = nullptr;
35 ggml_tensor * Vcur = nullptr;
36
37 if (model.layers[il].wqkv) {
38 cur = build_lora_mm(w: model.layers[il].wqkv, cur: attn_norm_output);
39 cb(cur, name: "wqkv", il);
40
41 cur = ggml_add(ctx: ctx0, a: cur, b: model.layers[il].bqkv);
42 cb(cur, name: "bqkv", il);
43
44 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));
45 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));
46 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));
47 } else {
48 Qcur = ggml_add(ctx: ctx0, a: build_lora_mm(w: model.layers[il].wq, cur: attn_norm_output), b: model.layers[il].bq);
49 Kcur = ggml_add(ctx: ctx0, a: build_lora_mm(w: model.layers[il].wk, cur: attn_norm_output), b: model.layers[il].bk);
50 Vcur = ggml_add(ctx: ctx0, a: build_lora_mm(w: model.layers[il].wv, cur: attn_norm_output), b: model.layers[il].bv);
51
52 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
53 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
54 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
55 }
56 Qcur = ggml_rope_ext(
57 ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr,
58 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
59 ext_factor, attn_factor, beta_fast, beta_slow
60 );
61
62 Kcur = ggml_rope_ext(
63 ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr,
64 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
65 ext_factor, attn_factor, beta_fast, beta_slow
66 );
67
68 cb(cur: Qcur, name: "Qcur", il);
69 cb(cur: Kcur, name: "Kcur", il);
70 cb(cur: Vcur, name: "Vcur", il);
71
72 // with phi2, we scale the Q to avoid precision issues
73 // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66
74 Qcur = ggml_scale(ctx: ctx0, a: Qcur, s: 1.0f/sqrtf(x: float(n_embd_head)));
75
76 cur = build_attn(inp: inp_attn,
77 wo: model.layers[il].wo, wo_b: model.layers[il].bo,
78 q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f, il);
79 }
80 if (il == n_layer - 1 && inp_out_ids) {
81 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
82 inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids);
83 attn_norm_output = ggml_get_rows(ctx: ctx0, a: attn_norm_output, b: inp_out_ids);
84 }
85 // FF
86 {
87 ffn_output = build_ffn(cur: attn_norm_output,
88 up: model.layers[il].ffn_up, up_b: model.layers[il].ffn_up_b, NULL,
89 NULL, NULL, NULL,
90 down: model.layers[il].ffn_down, down_b: model.layers[il].ffn_down_b, NULL,
91 NULL,
92 type_op: LLM_FFN_GELU, type_gate: LLM_FFN_SEQ, il);
93 cb(cur: ffn_output, name: "ffn_out", il);
94 }
95 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_output);
96 cur = ggml_add(ctx: ctx0, a: cur, b: inpL);
97
98 cur = build_cvec(cur, il);
99 cb(cur, name: "l_out", il);
100
101 // input for next layer
102 inpL = cur;
103 }
104 cur = build_norm(cur: inpL,
105 mw: model.output_norm,
106 mb: model.output_norm_b,
107 type: LLM_NORM, il: -1);
108
109 cb(cur, name: "result_norm", il: -1);
110 res->t_embd = cur;
111
112 cur = build_lora_mm(w: model.output, cur);
113 cb(cur, name: "result_output_no_bias", il: -1);
114
115 cur = ggml_add(ctx: ctx0, a: cur, b: model.output_b);
116
117 cb(cur, name: "result_output", il: -1);
118 res->t_logits = cur;
119
120 ggml_build_forward_expand(cgraph: gf, tensor: cur);
121}
122