/src/llama.cpp/src/models/jais2.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_jais2::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
5 | |
|
6 | 0 | switch (hparams.n_layer()) { |
7 | 0 | case 32: type = LLM_TYPE_8B; break; |
8 | 0 | case 68: type = LLM_TYPE_70B; break; |
9 | 0 | default: type = LLM_TYPE_UNKNOWN; |
10 | 0 | } |
11 | 0 | } |
12 | | |
13 | 0 | void llama_model_jais2::load_arch_tensors(llama_model_loader &) { |
14 | 0 | LLAMA_LOAD_LOCALS; |
15 | |
|
16 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
17 | | |
18 | | // output |
19 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
20 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
21 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
22 | 0 | if (!output) { |
23 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
24 | 0 | } |
25 | |
|
26 | 0 | for (int i = 0; i < n_layer; ++i) { |
27 | 0 | auto & layer = layers[i]; |
28 | |
|
29 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
30 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
31 | |
|
32 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
33 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
34 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
35 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
36 | | |
37 | | // attention biases - all have shape n_embd (output dimension of projections) |
38 | 0 | layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0); |
39 | 0 | layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd}, 0); |
40 | 0 | layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd}, 0); |
41 | 0 | layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
42 | |
|
43 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
44 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
45 | | |
46 | | // Jais-2 uses simple MLP (no gate) with biases |
47 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
48 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
49 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
50 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | 0 | std::unique_ptr<llm_graph_context> llama_model_jais2::build_arch_graph(const llm_graph_params & params) const { |
55 | 0 | return std::make_unique<graph>(*this, params); |
56 | 0 | } |
57 | | |
58 | | // JAIS-2 model graph builder |
59 | | // Uses: LayerNorm (not RMSNorm), relu2 activation, separate Q/K/V, RoPE embeddings |
60 | 0 | llama_model_jais2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
61 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
62 | |
|
63 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
64 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
65 | |
|
66 | 0 | ggml_tensor * cur; |
67 | 0 | ggml_tensor * inpL; |
68 | |
|
69 | 0 | inpL = build_inp_embd(model.tok_embd); |
70 | | |
71 | | // inp_pos - contains the positions |
72 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
73 | | |
74 | | // KV input for attention |
75 | 0 | auto * inp_attn = build_attn_inp_kv(); |
76 | |
|
77 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
78 | |
|
79 | 0 | for (int il = 0; il < n_layer; ++il) { |
80 | | // Pre-attention LayerNorm |
81 | 0 | cur = build_norm(inpL, |
82 | 0 | model.layers[il].attn_norm, |
83 | 0 | model.layers[il].attn_norm_b, |
84 | 0 | LLM_NORM, il); |
85 | 0 | cb(cur, "attn_norm", il); |
86 | | |
87 | | // Self-attention with separate Q, K, V projections |
88 | 0 | { |
89 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
90 | 0 | n_embd_head, n_head, n_head_kv, il); |
91 | | |
92 | | // Apply RoPE |
93 | 0 | Qcur = ggml_rope_ext( |
94 | 0 | ctx0, Qcur, inp_pos, nullptr, |
95 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
96 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
97 | 0 | ); |
98 | |
|
99 | 0 | Kcur = ggml_rope_ext( |
100 | 0 | ctx0, Kcur, inp_pos, nullptr, |
101 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
102 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
103 | 0 | ); |
104 | |
|
105 | 0 | cb(Qcur, "Qcur_rope", il); |
106 | 0 | cb(Kcur, "Kcur_rope", il); |
107 | |
|
108 | 0 | cur = build_attn(inp_attn, |
109 | 0 | model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
110 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
111 | 0 | } |
112 | |
|
113 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
114 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
115 | 0 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); |
116 | 0 | } |
117 | | |
118 | | // Residual connection |
119 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL); |
120 | 0 | cb(ffn_inp, "ffn_inp", il); |
121 | | |
122 | | // Pre-FFN LayerNorm |
123 | 0 | cur = build_norm(ffn_inp, |
124 | 0 | model.layers[il].ffn_norm, |
125 | 0 | model.layers[il].ffn_norm_b, |
126 | 0 | LLM_NORM, il); |
127 | 0 | cb(cur, "ffn_norm", il); |
128 | | |
129 | | // FFN with relu2 activation (ReLU squared) - no gate projection |
130 | | // up -> relu2 -> down |
131 | 0 | cur = build_ffn(cur, |
132 | 0 | model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL, |
133 | 0 | NULL, NULL, NULL, // no gate |
134 | 0 | model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, |
135 | 0 | NULL, |
136 | 0 | LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il); |
137 | 0 | cb(cur, "ffn_out", il); |
138 | | |
139 | | // Residual connection |
140 | 0 | inpL = ggml_add(ctx0, cur, ffn_inp); |
141 | 0 | inpL = build_cvec(inpL, il); |
142 | 0 | cb(inpL, "l_out", il); |
143 | 0 | } |
144 | | |
145 | | // Final LayerNorm |
146 | 0 | cur = build_norm(inpL, |
147 | 0 | model.output_norm, |
148 | 0 | model.output_norm_b, |
149 | 0 | LLM_NORM, -1); |
150 | 0 | cb(cur, "result_norm", -1); |
151 | |
|
152 | 0 | res->t_embd = cur; |
153 | | |
154 | | // Output projection |
155 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
156 | 0 | cb(cur, "result_output", -1); |
157 | |
|
158 | 0 | res->t_logits = cur; |
159 | |
|
160 | 0 | ggml_build_forward_expand(gf, cur); |
161 | 0 | } |