/src/llama.cpp/src/models/plamo3.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | template <bool iswa> |
4 | | llm_build_plamo3<iswa>::llm_build_plamo3(const llama_model & model, const llm_graph_params & params) : |
5 | 0 | llm_graph_context(params) { |
6 | 0 | const int64_t head_dim_q = hparams.n_embd_head_k; |
7 | 0 | const int64_t head_dim_v = hparams.n_embd_head_v; |
8 | |
|
9 | 0 | ggml_tensor * cur; |
10 | 0 | ggml_tensor * inpL = build_inp_embd(model.tok_embd); |
11 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
12 | |
|
13 | 0 | using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>; |
14 | 0 | inp_attn_type * inp_attn = nullptr; |
15 | |
|
16 | 0 | if constexpr (iswa) { |
17 | 0 | inp_attn = build_attn_inp_kv_iswa(); |
18 | 0 | } else { |
19 | 0 | inp_attn = build_attn_inp_kv(); |
20 | 0 | } |
21 | |
|
22 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
23 | |
|
24 | 0 | for (int il = 0; il < n_layer; ++il) { |
25 | 0 | ggml_tensor * residual = inpL; |
26 | |
|
27 | 0 | float freq_base_l = 0.0f; |
28 | 0 | float freq_scale_l = 0.0f; |
29 | 0 | if constexpr (iswa) { |
30 | 0 | freq_base_l = model.get_rope_freq_base (cparams, il); |
31 | 0 | freq_scale_l = model.get_rope_freq_scale(cparams, il); |
32 | 0 | } else { |
33 | 0 | freq_base_l = freq_base; |
34 | 0 | freq_scale_l = freq_scale; |
35 | 0 | } |
36 | |
|
37 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
38 | 0 | cb(cur, "attn_norm", il); |
39 | |
|
40 | 0 | ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur); |
41 | 0 | cb(cur, "wqkv", il); |
42 | |
|
43 | 0 | const int32_t n_head = hparams.n_head(il); |
44 | 0 | const int32_t n_head_kv = hparams.n_head_kv(il); |
45 | |
|
46 | 0 | const int64_t q_offset = 0; |
47 | 0 | const int64_t k_offset = head_dim_q * n_head; |
48 | 0 | const int64_t v_offset = k_offset + head_dim_q * n_head_kv; |
49 | |
|
50 | 0 | ggml_tensor * Qcur = ggml_view_3d(ctx0, qkv, head_dim_q, n_head, n_tokens, |
51 | 0 | head_dim_q * sizeof(float), qkv->nb[1], q_offset * ggml_element_size(qkv)); |
52 | 0 | ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, head_dim_q, n_head_kv, n_tokens, |
53 | 0 | head_dim_q * sizeof(float), qkv->nb[1], k_offset * ggml_element_size(qkv)); |
54 | 0 | ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, head_dim_v, n_head_kv, n_tokens, |
55 | 0 | head_dim_v * sizeof(float), qkv->nb[1], v_offset * ggml_element_size(qkv)); |
56 | |
|
57 | 0 | cb(Qcur, "Qcur", il); |
58 | 0 | cb(Kcur, "Kcur", il); |
59 | 0 | cb(Vcur, "Vcur", il); |
60 | |
|
61 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
62 | 0 | cb(Qcur, "attn_q_norm", il); |
63 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
64 | 0 | cb(Kcur, "attn_k_norm", il); |
65 | |
|
66 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, |
67 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
68 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
69 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, |
70 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
71 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
72 | |
|
73 | 0 | const float attn_scale = 1.0f / sqrtf(float(head_dim_q)); |
74 | |
|
75 | 0 | cur = build_attn(inp_attn, |
76 | 0 | model.layers[il].wo, NULL, |
77 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, attn_scale, il); |
78 | 0 | cb(cur, "attn_out", il); |
79 | |
|
80 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
81 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
82 | 0 | residual = ggml_get_rows(ctx0, residual, inp_out_ids); |
83 | 0 | } |
84 | |
|
85 | 0 | cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il); |
86 | 0 | cb(cur, "attn_post_norm", il); |
87 | |
|
88 | 0 | cur = ggml_add(ctx0, cur, residual); |
89 | 0 | cb(cur, "attn_residual", il); |
90 | |
|
91 | 0 | residual = cur; |
92 | |
|
93 | 0 | cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
94 | 0 | cb(cur, "ffn_norm", il); |
95 | |
|
96 | 0 | cur = build_ffn(cur, |
97 | 0 | model.layers[il].ffn_up, NULL, NULL, |
98 | 0 | NULL, NULL, NULL, |
99 | 0 | model.layers[il].ffn_down, NULL, NULL, |
100 | 0 | NULL, |
101 | 0 | LLM_FFN_SWIGLU, LLM_FFN_SEQ, il); |
102 | 0 | cb(cur, "ffn_out", il); |
103 | |
|
104 | 0 | cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il); |
105 | 0 | cb(cur, "ffn_post_norm", il); |
106 | |
|
107 | 0 | cur = ggml_add(ctx0, cur, residual); |
108 | 0 | cb(cur, "ffn_residual", il); |
109 | |
|
110 | 0 | cur = build_cvec(cur, il); |
111 | 0 | cb(cur, "l_out", il); |
112 | 0 | inpL = cur; |
113 | 0 | } |
114 | |
|
115 | 0 | cur = inpL; |
116 | |
|
117 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
118 | 0 | res->t_embd = cur; |
119 | |
|
120 | 0 | cur = build_lora_mm(model.output, cur); |
121 | 0 | res->t_logits = cur; |
122 | |
|
123 | 0 | ggml_build_forward_expand(gf, cur); |
124 | 0 | } Unexecuted instantiation: llm_build_plamo3<false>::llm_build_plamo3(llama_model const&, llm_graph_params const&) Unexecuted instantiation: llm_build_plamo3<true>::llm_build_plamo3(llama_model const&, llm_graph_params const&) |
125 | | |
126 | | // Explicit template instantiations |
127 | | template struct llm_build_plamo3<false>; |
128 | | template struct llm_build_plamo3<true>; |