echodict/llama.cpp
version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786
0479
1#include "models.h"2 3#include "llama-memory-recurrent.h"4 5llm_build_qwen3next::llm_build_qwen3next(const llama_model & model, const llm_graph_params & params) :6 llm_build_delta_net_base(params), model(model) {7 ggml_tensor * cur;8 ggml_tensor * inpL;9 10 inpL = build_inp_embd(model.tok_embd);11 cb(inpL, "model.embed_tokens", -1);12 13 auto * inp = build_inp_mem_hybrid();14 15 ggml_tensor * inp_pos = build_inp_pos();16 ggml_tensor * inp_out_ids = build_inp_out_ids();17 18 for (int il = 0; il < n_layer; ++il) {19 ggml_tensor * inpSA = inpL;20 21 cur = build_norm(inpL, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il);22 cb(cur, "attn_norm", il);23 24 ggml_build_forward_expand(gf, cur);25 26 // Determine layer type and build appropriate attention mechanism27 if (hparams.is_recurrent(il)) {28 // Linear attention layer (gated delta net)29 cur = build_layer_attn_linear(inp->get_recr(), cur, il);30 } else {31 // Full attention layer32 cur = build_layer_attn(inp->get_attn(), cur, inp_pos, il);33 }34 35 if (il == n_layer - 1 && inp_out_ids) {36 cur = ggml_get_rows(ctx0, cur, inp_out_ids);37 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);38 }39 40 // Residual connection41 cur = ggml_add(ctx0, cur, inpSA);42 cb(cur, "attn_residual", il);43 44 // Save the tensor before post-attention norm for residual connection45 ggml_tensor * ffn_residual = cur;46 47 // Post-attention norm48 ggml_tensor * attn_post_norm = build_norm(cur, model.layers[il].attn_post_norm, nullptr, LLM_NORM_RMS, il);49 cb(attn_post_norm, "attn_post_norm", il);50 51 // FFN layer (MoE or dense) - without residual connection52 cur = build_layer_ffn(attn_post_norm, il);53 cb(cur, "ffn_out", il);54 55 // Residual connection for FFN - add to the tensor from before post_attention_layernorm56 cur = ggml_add(ctx0, cur, ffn_residual);57 cb(cur, "post_moe", il);58 59 cur = build_cvec(cur, il);60 cb(cur, "l_out", il);61 62 // Input for next layer63 inpL = cur;64 }65 cur = inpL;66 67 // Final norm68 cur = build_norm(cur, model.output_norm, nullptr, LLM_NORM_RMS, -1);69 70 cb(cur, "result_norm", -1);71 res->t_embd = cur;72 73 // LM head74 cur = build_lora_mm(model.output, cur);75 76 cb(cur, "result_output", -1);77 res->t_logits = cur;78 79 ggml_build_forward_expand(gf, cur);80}81 82// utility to get one slice from the third dimension83// input dim: [x, y, c, b]84// output dim: [x, y, 1, b]85static ggml_tensor * get_slice_2d(ggml_context * ctx0, ggml_tensor * t, int64_t c) {86 return ggml_view_4d(ctx0, t, t->ne[0], t->ne[1], 1, t->ne[3],87 t->nb[1], t->nb[2], t->nb[3], t->nb[2] * c);88}89 90ggml_tensor * llm_build_qwen3next::build_norm_gated(91 ggml_tensor * input,92 ggml_tensor * weights,93 ggml_tensor * gate,94 int layer) {95 ggml_tensor * normalized = build_norm(input, weights, nullptr, LLM_NORM_RMS, layer);96 ggml_tensor * gated_silu = ggml_silu(ctx0, gate);97 98 return ggml_mul(ctx0, normalized, gated_silu);99}100 101ggml_tensor * llm_build_qwen3next::build_layer_attn(102 llm_graph_input_attn_kv * inp,103 ggml_tensor * cur,104 ggml_tensor * inp_pos,105 int il) {106 const int64_t n_embd_head = hparams.n_embd_head_v();107 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());108 109 // Order: joint QG projection, QG split, Q norm, KV projection, K norm, RoPE, attention110 111 // Qwen3Next uses a single Q projection that outputs query + gate112 ggml_tensor * Qcur_full = build_lora_mm(model.layers[il].wq, cur);113 cb(Qcur_full, "Qcur_full", il);114 115 Qcur_full = ggml_reshape_4d(ctx0, Qcur_full, n_embd_head * 2, n_head, n_tokens, 1);116 117 // Split Q projection into query and gate118 // The split should be along dimension 0 (the feature dimension)119 ggml_tensor * Qcur = ggml_view_4d(ctx0, Qcur_full, n_embd_head, n_head, n_tokens, 1,120 Qcur_full->nb[1], Qcur_full->nb[2], Qcur_full->nb[3], 0);121 cb(Qcur, "Qcur_view", il);122 123 ggml_tensor * gate =124 ggml_view_4d(ctx0, Qcur_full, n_embd_head, n_head, n_tokens, 1,125 Qcur_full->nb[1], Qcur_full->nb[2], Qcur_full->nb[3], n_embd_head * ggml_element_size(Qcur_full));126 cb(gate, "gate", il);127 128 ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);129 cb(Kcur, "Kcur", il);130 131 ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);132 cb(Vcur, "Vcur", il);133 134 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);135 Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);136 137 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, nullptr, LLM_NORM_RMS, il);138 cb(Qcur, "Qcur_normed", il);139 140 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, nullptr, LLM_NORM_RMS, il);141 cb(Kcur, "Kcur_normed", il);142 143 Qcur = ggml_rope_ext(144 ctx0, Qcur, inp_pos, nullptr,145 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,146 ext_factor, attn_factor, beta_fast, beta_slow);147 148 Kcur = ggml_rope_ext(149 ctx0, Kcur, inp_pos, nullptr,150 n_rot, rope_type, n_ctx_orig, freq_base,151 freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);152 153 cb(Qcur, "Qcur", il);154 cb(Kcur, "Kcur", il);155 cb(Vcur, "Vcur", il);156 157 const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;158 159 cur = build_attn(inp,160 nullptr, nullptr, nullptr,161 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);162 cb(cur, "attn_pregate", il);163 164 // TODO: CUDA is missing non-contiguous unary ops. when implemented: remove this cont165 gate = ggml_cont_2d(ctx0, gate, n_embd_head * n_head, n_tokens);166 167 gate = ggml_sigmoid(ctx0, gate);168 cb(gate, "gate_sigmoid", il);169 170 gate = ggml_reshape_2d(ctx0, gate, n_embd_head * n_head, n_tokens);171 172 cur = ggml_mul(ctx0, cur, gate);173 cb(cur, "attn_gated", il);174 175 cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);176 cb(cur, "attn_output", il);177 178 return cur;179}180 181std::pair<ggml_tensor *, ggml_tensor *> llm_build_qwen3next::build_qkvz(182 ggml_tensor * input,183 int il) {184 const int64_t d_inner = hparams.ssm_d_inner;185 const int64_t n_seqs = ubatch.n_seqs;186 const int64_t head_k_dim = hparams.ssm_d_state;187 const int64_t num_k_heads = hparams.ssm_n_group;188 const int64_t num_v_heads = hparams.ssm_dt_rank;189 const int64_t head_v_dim = d_inner / num_v_heads;190 const int64_t n_seq_tokens = ubatch.n_seq_tokens;191 192 if (model.layers[il].wqkv) {193 // optimized path194 ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input);195 qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_seq_tokens, n_seqs);196 cb(qkv_mixed, "linear_attn_qkv_mixed", il);197 198 ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input);199 cb(z, "z", il);200 201 return { qkv_mixed, z };202 } else {203 // legacy (slower) path204 ggml_tensor * mixed_qkvz = build_lora_mm(model.layers[il].ssm_in, input);205 cb(mixed_qkvz, "linear_attn_mixed_qkvz", il);206 207 int64_t qkvz_new_dim = 2 * head_k_dim + 2 * head_v_dim * (num_v_heads / num_k_heads);208 ggml_tensor * mixed_qkvz_reshaped = ggml_reshape_4d(ctx0, mixed_qkvz, qkvz_new_dim, num_k_heads, n_seq_tokens, n_seqs);209 210 // Split mixed_qkvz into query, key, value, z211 int64_t split_sizes_qkvz[4] = {212 head_k_dim, // query size213 head_k_dim, // key size214 head_v_dim * num_v_heads / num_k_heads, // value size215 head_v_dim * num_v_heads / num_k_heads // z size216 };217 218 ggml_tensor * query =219 ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[0], num_k_heads, n_seq_tokens, n_seqs,220 mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2], mixed_qkvz_reshaped->nb[3], 0);221 cb(query, "q", il);222 223 ggml_tensor * key = ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[1], num_k_heads, n_seq_tokens, n_seqs,224 mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2], mixed_qkvz_reshaped->nb[3],225 split_sizes_qkvz[0] * ggml_element_size(mixed_qkvz_reshaped));226 cb(key, "k", il);227 228 ggml_tensor * value =229 ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[2], num_k_heads, n_seq_tokens, n_seqs,230 mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2], mixed_qkvz_reshaped->nb[3],231 (split_sizes_qkvz[0] + split_sizes_qkvz[1]) * ggml_element_size(mixed_qkvz_reshaped));232 cb(value, "v", il);233 234 ggml_tensor * z = ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[3], num_k_heads, n_seq_tokens, n_seqs,235 mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2], mixed_qkvz_reshaped->nb[3],236 (split_sizes_qkvz[0] + split_sizes_qkvz[1] + split_sizes_qkvz[2]) * ggml_element_size(mixed_qkvz_reshaped));237 z = ggml_cont(ctx0, z);238 cb(z, "z", il);239 240 // After creating query, key, and value_reshaped, reshape each to flatten the head dimensions241 // query: [head_k_dim, num_k_heads, n_tokens, n_seqs] -> [head_k_dim * num_k_heads, n_tokens, n_seqs]242 ggml_tensor * query_flat = ggml_cont_3d(ctx0, query, head_k_dim * num_k_heads, n_seq_tokens, n_seqs);243 cb(query_flat, "query_flat", il);244 245 // key: [head_k_dim, num_k_heads, n_tokens, n_seqs] -> [head_k_dim * num_k_heads, n_tokens, n_seqs]246 ggml_tensor * key_flat = ggml_cont_3d(ctx0, key, head_k_dim * num_k_heads, n_seq_tokens, n_seqs);247 cb(key_flat, "key_flat", il);248 249 // value_reshaped: [head_v_dim, num_v_heads, n_tokens, n_seqs] -> [head_v_dim * num_v_heads, n_tokens, n_seqs]250 ggml_tensor * value_flat = ggml_cont_3d(ctx0, value, head_v_dim * num_v_heads, n_seq_tokens, n_seqs);251 cb(value_flat, "value_flat", il);252 253 // Now concatenate along the feature dimension (dim 0) to get [conv_dim, n_tokens, n_seqs]254 ggml_tensor * qkv_mixed = ggml_concat(ctx0, query_flat, key_flat, 0);255 qkv_mixed = ggml_concat(ctx0, qkv_mixed, value_flat, 0);256 cb(qkv_mixed, "qkv_mixed", il);257 258 return { qkv_mixed, z };259 }260}261 262ggml_tensor * llm_build_qwen3next::build_layer_attn_linear(263 llm_graph_input_rs * inp,264 ggml_tensor * cur,265 int il) {266 const auto * mctx_cur = inp->mctx;267 268 const int64_t d_inner = hparams.ssm_d_inner;269 const int64_t n_seqs = ubatch.n_seqs;270 const int64_t head_k_dim = hparams.ssm_d_state;271 const int64_t num_k_heads = hparams.ssm_n_group;272 const int64_t num_v_heads = hparams.ssm_dt_rank;273 const int64_t head_v_dim = d_inner / num_v_heads;274 const int64_t n_seq_tokens = ubatch.n_seq_tokens;275 276 const auto kv_head = mctx_cur->get_head();277 278 GGML_ASSERT(n_seqs != 0);279 GGML_ASSERT(ubatch.equal_seqs());280 GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);281 282 // Input projections283 auto qkvz = build_qkvz(cur, il);284 ggml_tensor * qkv_mixed = qkvz.first;285 ggml_tensor * z = qkvz.second;286 287 ggml_tensor * mixed_ba = build_lora_mm(model.layers[il].ssm_beta_alpha, cur);288 cb(mixed_ba, "linear_attn_mixed_ba", il);289 290 // Reshape mixed_ba: [batch, seq_len, hidden_size] -> [batch, seq_len, num_k_heads, 2*num_v_heads/num_k_heads]291 int64_t ba_new_dim = 2 * num_v_heads / num_k_heads;292 ggml_tensor * mixed_ba_reshaped = ggml_reshape_4d(ctx0, mixed_ba, ba_new_dim, num_k_heads, n_seq_tokens, n_seqs);293 294 // Split mixed_ba into b and a (beta and alpha parameters)295 int64_t split_sizes_ba[2] = {296 num_v_heads / num_k_heads, // beta size297 num_v_heads / num_k_heads // alpha size298 };299 300 ggml_tensor * b = ggml_view_4d(ctx0, mixed_ba_reshaped, split_sizes_ba[0], num_k_heads, n_seq_tokens, n_seqs,301 mixed_ba_reshaped->nb[1], mixed_ba_reshaped->nb[2], mixed_ba_reshaped->nb[3], 0);302 cb(b, "b", il);303 304 ggml_tensor * a = ggml_view_4d(ctx0, mixed_ba_reshaped, split_sizes_ba[1], num_k_heads, n_seq_tokens, n_seqs,305 mixed_ba_reshaped->nb[1], mixed_ba_reshaped->nb[2], mixed_ba_reshaped->nb[3],306 split_sizes_ba[0] * ggml_element_size(mixed_ba_reshaped));307 cb(a, "a", il);308 309 // TODO: CUDA is missing non-contiguous unary ops. when implemented: remove this cont310 b = ggml_cont(ctx0, b);311 312 ggml_tensor * beta = ggml_sigmoid(ctx0, b);313 314 // Reshape a to merge head dimensions: [batch, seq_len, num_k_heads, num_v_heads/num_k_heads] -> [batch, seq_len, num_v_heads]315 ggml_tensor * alpha = ggml_cont_3d(ctx0, a, num_v_heads, n_seq_tokens, n_seqs);316 317 ggml_tensor * alpha_biased = ggml_add(ctx0, alpha, model.layers[il].ssm_dt);318 ggml_tensor * alpha_softplus = ggml_softplus(ctx0, alpha_biased);319 cb(alpha_softplus, "a_softplus", il);320 321 ggml_tensor * gate = ggml_mul(ctx0, alpha_softplus, model.layers[il].ssm_a); // -A_log.exp() * softplus322 cb(gate, "gate", il);323 324 beta = ggml_reshape_4d(ctx0, beta, 1, num_v_heads, n_seq_tokens, n_seqs);325 gate = ggml_reshape_4d(ctx0, gate, 1, num_v_heads, n_seq_tokens, n_seqs);326 327 // Get convolution states from cache328 ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);329 ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);330 331 // Build the convolution states tensor332 ggml_tensor * conv_states = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);333 cb(conv_states, "conv_states", il);334 335 // Calculate convolution kernel size336 ggml_tensor * conv_kernel = model.layers[il].ssm_conv1d;337 const int64_t conv_kernel_size = conv_kernel->ne[0];338 const int64_t conv_channels = d_inner + 2 * hparams.ssm_n_group * hparams.ssm_d_state;339 340 conv_states = ggml_reshape_3d(ctx0, conv_states, conv_kernel_size - 1, conv_channels, n_seqs);341 cb(conv_states, "conv_states_reshaped", il);342 343 qkv_mixed = ggml_transpose(ctx0, qkv_mixed);344 cb(qkv_mixed, "qkv_mixed_transposed", il);345 346 ggml_tensor * conv_input = ggml_concat(ctx0, conv_states, qkv_mixed, 0);347 cb(conv_input, "conv_input", il);348 349 // Update convolution state cache350 // Extract the last (conv_kernel_size - 1) states from conv_input351 ggml_tensor * last_conv_states =352 ggml_view_3d(ctx0, conv_input, conv_kernel_size - 1, conv_channels, n_seqs, conv_input->nb[1],353 conv_input->nb[2], (conv_input->ne[0] - conv_states->ne[0]) * ggml_element_size(conv_input));354 cb(last_conv_states, "last_conv_states", il);355 356 ggml_tensor * state_update_target =357 ggml_view_2d(ctx0, conv_states_all, (conv_kernel_size - 1) * conv_channels, n_seqs, conv_states_all->nb[1],358 kv_head * (conv_kernel_size - 1) * conv_channels * ggml_element_size(conv_states_all));359 cb(state_update_target, "state_update_target", il);360 361 ggml_build_forward_expand(gf, ggml_cpy(ctx0, last_conv_states, state_update_target));362 363 ggml_tensor * state = build_rs(inp, ssm_states_all, hparams.n_embd_s(), n_seqs);364 state = ggml_reshape_4d(ctx0, state, head_v_dim, head_v_dim, num_v_heads, n_seqs);365 cb(state, "state_predelta", il);366 367 ggml_tensor * conv_output_proper = ggml_ssm_conv(ctx0, conv_input, conv_kernel);368 cb(conv_output_proper, "conv_output_raw", il);369 370 ggml_tensor * conv_output_silu = ggml_silu(ctx0, conv_output_proper);371 cb(conv_output_silu, "conv_output_silu", il);372 373 ggml_tensor * conv_qkv_mix = conv_output_silu;374 375 // Calculate the total conv dimension376 int64_t qkv_dim = head_k_dim * num_k_heads * 2 + head_v_dim * num_v_heads;377 int64_t nb1_qkv = ggml_row_size(conv_qkv_mix->type, qkv_dim);378 379 // Extract the convolved Q, K, V from conv_output380 ggml_tensor * q_conv = ggml_view_4d(ctx0, conv_qkv_mix, head_k_dim, num_k_heads, n_seq_tokens, n_seqs,381 ggml_row_size(conv_qkv_mix->type, head_k_dim),382 nb1_qkv,383 nb1_qkv * n_seq_tokens,384 0);385 386 ggml_tensor * k_conv = ggml_view_4d(ctx0, conv_qkv_mix, head_k_dim, num_k_heads, n_seq_tokens, n_seqs,387 ggml_row_size(conv_qkv_mix->type, head_k_dim),388 nb1_qkv,389 nb1_qkv * n_seq_tokens,390 head_k_dim * num_k_heads * ggml_element_size(conv_qkv_mix));391 392 ggml_tensor * v_conv = ggml_view_4d(ctx0, conv_qkv_mix, head_v_dim, num_v_heads, n_seq_tokens, n_seqs,393 ggml_row_size(conv_qkv_mix->type, head_v_dim),394 nb1_qkv,395 nb1_qkv * n_seq_tokens,396 ggml_row_size(conv_qkv_mix->type, 2 * head_k_dim * num_k_heads));397 398 cb(q_conv, "q_conv", il);399 cb(k_conv, "k_conv", il);400 cb(v_conv, "v_conv", il);401 402 const float eps_norm = hparams.f_norm_rms_eps;403 404 q_conv = ggml_l2_norm(ctx0, q_conv, eps_norm);405 k_conv = ggml_l2_norm(ctx0, k_conv, eps_norm);406 407 //q_conv = ggml_cont_4d(ctx0, q_conv, head_k_dim, num_k_heads, n_seq_tokens, n_seqs);408 //k_conv = ggml_cont_4d(ctx0, k_conv, head_k_dim, num_k_heads, n_seq_tokens, n_seqs);409 //v_conv = ggml_cont_4d(ctx0, v_conv, head_v_dim, num_v_heads, n_seq_tokens, n_seqs);410 411 // if head keys and value keys are different, repeat to force tensors into matching shapes412 // TODO: avoid repeats for fused GDN, needs broadcast configuration for GDN op [TAG_GGML_GDN_BCAST]413 if (num_k_heads != num_v_heads) {414 GGML_ASSERT(num_v_heads % num_k_heads == 0);415 int64_t repeat_factor = num_v_heads / num_k_heads;416 417 // repeat interleave: reshape to (repeat part, 1, remaining part...), do repeat, then reshape back418 ggml_tensor * q_reshaped = ggml_reshape_4d(ctx0, q_conv, head_k_dim, 1, num_k_heads, n_seq_tokens * n_seqs);419 ggml_tensor * k_reshaped = ggml_reshape_4d(ctx0, k_conv, head_k_dim, 1, num_k_heads, n_seq_tokens * n_seqs);420 421 // Repeat along the third dimension (the new dimension with size 1)422 ggml_tensor * q_repeated =423 ggml_repeat_4d(ctx0, q_reshaped, head_k_dim, repeat_factor, num_k_heads, n_seq_tokens * n_seqs);424 ggml_tensor * k_repeated =425 ggml_repeat_4d(ctx0, k_reshaped, head_k_dim, repeat_factor, num_k_heads, n_seq_tokens * n_seqs);426 427 // Reshape back to merge the head and repeat dimensions428 // From [head_dim, repeat_factor, num_k_heads, n_seq_tokens * n_seqs]429 // Back to [head_dim, repeat_factor * num_k_heads, n_seq_tokens, n_seqs]430 q_conv = ggml_reshape_4d(ctx0, q_repeated, head_k_dim, num_k_heads * repeat_factor, n_seq_tokens, n_seqs);431 k_conv = ggml_reshape_4d(ctx0, k_repeated, head_k_dim, num_k_heads * repeat_factor, n_seq_tokens, n_seqs);432 }433 434 cb(q_conv, "q_conv_predelta", il);435 cb(k_conv, "k_conv_predelta", il);436 cb(v_conv, "v_conv_predelta", il);437 438 auto attn_out = build_delta_net(q_conv, k_conv, v_conv, gate, beta, state, il);439 440 ggml_tensor * output = attn_out.first;441 ggml_tensor * new_state = attn_out.second;442 cb(output, "attn_output", il);443 cb(new_state, "new_state", il);444 445 // Update the recurrent states446 ggml_build_forward_expand(gf,447 ggml_cpy(ctx0, new_state,448 ggml_view_2d(ctx0, ssm_states_all, hparams.n_embd_s(), n_seqs, ssm_states_all->nb[1],449 kv_head * hparams.n_embd_s() * ggml_element_size(ssm_states_all))));450 451 // z: [head_dim, n_heads, n_tokens, n_seqs] -> [n_heads * n_tokens * n_seqs, head_dim]452 ggml_tensor * z_2d = ggml_reshape_4d(ctx0, z, head_v_dim, num_v_heads, n_seq_tokens, n_seqs);453 454 // Apply gated normalization: self.norm(core_attn_out, z)455 ggml_tensor * attn_out_norm = build_norm_gated(output, model.layers[il].ssm_norm, z_2d, il);456 457 // Final reshape: [head_dim, n_heads, n_tokens, n_seqs] -> [n_tokens, n_seqs, n_heads * head_dim]458 ggml_tensor * final_output = ggml_reshape_3d(ctx0, attn_out_norm, head_v_dim * num_v_heads, n_seq_tokens, n_seqs);459 cb(final_output, "final_output", il);460 461 // Output projection462 cur = build_lora_mm(model.layers[il].ssm_out, final_output);463 cb(cur, "linear_attn_out", il);464 465 // Reshape back to original dimensions466 cur = ggml_reshape_2d(ctx0, cur, n_embd, n_seq_tokens * n_seqs);467 468 return cur;469}470 471ggml_tensor * llm_build_qwen3next::build_layer_ffn(ggml_tensor * cur, const int il) {472 // Check if this is an MoE layer473 if (model.layers[il].ffn_gate_inp != nullptr) {474 // MoE branch475 ggml_tensor * moe_out =476 build_moe_ffn(cur,477 model.layers[il].ffn_gate_inp,478 model.layers[il].ffn_up_exps,479 model.layers[il].ffn_gate_exps,480 model.layers[il].ffn_down_exps,481 nullptr,482 n_expert, n_expert_used,483 LLM_FFN_SILU, true,484 hparams.expert_weights_scale,485 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, il,486 nullptr, model.layers[il].ffn_gate_up_exps);487 cb(moe_out, "ffn_moe_out", il);488 489 // Add shared experts if present - following Qwen3Next reference implementation490 if (model.layers[il].ffn_up_shexp != nullptr) {491 ggml_tensor * ffn_shexp =492 build_ffn(cur,493 model.layers[il].ffn_up_shexp, NULL, NULL,494 model.layers[il].ffn_gate_shexp, NULL, NULL,495 model.layers[il].ffn_down_shexp, NULL, NULL,496 NULL,497 LLM_FFN_SILU, LLM_FFN_PAR, il);498 cb(ffn_shexp, "ffn_shexp", il);499 500 // Apply shared expert gating as in the reference implementation501 // The shared expert has its own gate that is sigmoided502 // Note: ffn_gate_inp_shexp is the shared expert gate (outputs 1 value per token)503 ggml_tensor * shared_gate = build_lora_mm(model.layers[il].ffn_gate_inp_shexp, cur);504 cb(shared_gate, "shared_expert_gate", il);505 506 shared_gate = ggml_sigmoid(ctx0, shared_gate);507 cb(shared_gate, "shared_expert_gate_sigmoid", il);508 509 ffn_shexp = ggml_mul(ctx0, ffn_shexp, shared_gate);510 cb(ffn_shexp, "ffn_shexp_gated", il);511 512 cur = ggml_add(ctx0, moe_out, ffn_shexp);513 cb(cur, "ffn_out", il);514 } else {515 cur = moe_out;516 }517 } else {518 // Dense FFN branch (not currently used I believe)519 cur = build_ffn(cur,520 model.layers[il].ffn_up, NULL, NULL,521 model.layers[il].ffn_gate, NULL, NULL,522 model.layers[il].ffn_down, NULL, NULL,523 NULL,524 LLM_FFN_SILU, LLM_FFN_PAR, il);525 cb(cur, "ffn_out", il);526 }527 return cur;528}529 