MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / build_qkvz

Method build_qkvz

subprojects/llama.cpp/src/models/qwen3next.cpp:536–616  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

534}
535
536std::pair<ggml_tensor *, ggml_tensor *> llm_build_qwen3next::build_qkvz(
537 ggml_tensor * input,
538 int il) {
539 const int64_t d_inner = hparams.ssm_d_inner;
540 const int64_t n_seqs = ubatch.n_seqs;
541 const int64_t head_k_dim = hparams.ssm_d_state;
542 const int64_t num_k_heads = hparams.ssm_n_group;
543 const int64_t num_v_heads = hparams.ssm_dt_rank;
544 const int64_t head_v_dim = d_inner / num_v_heads;
545 const int64_t n_seq_tokens = ubatch.n_seq_tokens;
546
547 if (model.layers[il].wqkv) {
548 // optimized path
549 ggml_tensor * qkv_mixed = build_lora_mm(model.layers[il].wqkv, input);
550 qkv_mixed = ggml_reshape_3d(ctx0, qkv_mixed, qkv_mixed->ne[0], n_seq_tokens, n_seqs);
551 cb(qkv_mixed, "linear_attn_qkv_mixed", il);
552
553 ggml_tensor * z = build_lora_mm(model.layers[il].wqkv_gate, input);
554 cb(z, "z", il);
555
556 return { qkv_mixed, z };
557
558 } else {
559 // legacy (slower) path
560 ggml_tensor * mixed_qkvz = build_lora_mm(model.layers[il].ssm_in, input);
561 cb(mixed_qkvz, "linear_attn_mixed_qkvz", il);
562
563 int64_t qkvz_new_dim = 2 * head_k_dim + 2 * head_v_dim * (num_v_heads / num_k_heads);
564 ggml_tensor * mixed_qkvz_reshaped = ggml_reshape_4d(ctx0, mixed_qkvz, qkvz_new_dim, num_k_heads, n_seq_tokens, n_seqs);
565
566 // Split mixed_qkvz into query, key, value, z
567 int64_t split_sizes_qkvz[4] = {
568 head_k_dim, // query size
569 head_k_dim, // key size
570 head_v_dim * num_v_heads / num_k_heads, // value size
571 head_v_dim * num_v_heads / num_k_heads // z size
572 };
573
574 ggml_tensor * query =
575 ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[0], num_k_heads, n_seq_tokens, n_seqs,
576 mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2], mixed_qkvz_reshaped->nb[3], 0);
577 cb(query, "q", il);
578
579 ggml_tensor * key = ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[1], num_k_heads, n_seq_tokens, n_seqs,
580 mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2], mixed_qkvz_reshaped->nb[3],
581 split_sizes_qkvz[0] * ggml_element_size(mixed_qkvz_reshaped));
582 cb(key, "k", il);
583
584 ggml_tensor * value =
585 ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[2], num_k_heads, n_seq_tokens, n_seqs,
586 mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2], mixed_qkvz_reshaped->nb[3],
587 (split_sizes_qkvz[0] + split_sizes_qkvz[1]) * ggml_element_size(mixed_qkvz_reshaped));
588 cb(value, "v", il);
589
590 ggml_tensor * z = ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[3], num_k_heads, n_seq_tokens, n_seqs,
591 mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2], mixed_qkvz_reshaped->nb[3],
592 (split_sizes_qkvz[0] + split_sizes_qkvz[1] + split_sizes_qkvz[2]) * ggml_element_size(mixed_qkvz_reshaped));
593 z = ggml_cont(ctx0, z);

Callers

nothing calls this directly

Calls 7

ggml_reshape_3dFunction · 0.85
ggml_reshape_4dFunction · 0.85
ggml_view_4dFunction · 0.85
ggml_element_sizeFunction · 0.85
ggml_contFunction · 0.85
ggml_cont_3dFunction · 0.85
ggml_concatFunction · 0.85

Tested by

no test coverage detected