Commit 7dca4a8da for llama.cpp

commit 7dca4a8daa01d1c040332dddd2cd236eab494e86
Author: Georgi Gerganov <ggerganov@gmail.com>
Date:   Tue Dec 1 09:26:03 2026 +0200

    qwen4exp : simplify the PLE conv graph (#33380)

    * qwen4exp : remove redundant cont in ple conv output

    conv_out is contiguous after ggml_silu, so the extra ggml_cont before the reshape is unnecessary.

    Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)

    * qwen4exp : avoid the permute copy in the PLE conv

    Fold the sequence axis into the channel axis so im2col writes the layout needed by the batched matmul directly, removing the cont(permute(x)) copy.

    Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)

    * qwen4exp : load the PLE conv kernel as 3D

    Use TENSOR_ALLOW_RESHAPE to load ple_conv1d as [kern, 1, hc_dim], so the graph uses it directly without a reshape or f32 cast. Update the tensor op mapping to GGML_OP_MUL_MAT for buffer selection with the 3D shape.

    Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)

    * qwen4exp : use a standalone shape tensor for im2col

    ggml_im2col only needs the kernel shape, so create the [kern, 1] tensor directly instead of viewing the PLE conv weight.

    Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4.1-Flash-1017-GGUF (Mac Studio, M5 Ultra, 256GB)

diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp
index a83210f20..ff03ca871 100644
--- a/src/llama-arch.cpp
+++ b/src/llama-arch.cpp
@@ -1083,7 +1083,7 @@ static const std::map<llm_tensor, llm_tensor_info> LLM_TENSOR_INFOS = {
     {LLM_TENSOR_PLE_NORM_KEY,               {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
     {LLM_TENSOR_PLE_NORM_QUERY,             {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
     {LLM_TENSOR_PLE_NORM_CONV,              {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL}},
-    {LLM_TENSOR_PLE_CONV1D,                 {LLM_TENSOR_LAYER_REPEATING, GGML_OP_SSM_CONV}},
+    {LLM_TENSOR_PLE_CONV1D,                 {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT}},
     {LLM_TENSOR_ATTN_COMPRESSOR_WKV,        {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT}},
     {LLM_TENSOR_ATTN_COMPRESSOR_WGATE,      {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT}},
     {LLM_TENSOR_ATTN_COMPRESSOR_APE,        {LLM_TENSOR_LAYER_REPEATING, GGML_OP_GET_ROWS}},
diff --git a/src/models/qwen4exp.cpp b/src/models/qwen4exp.cpp
index be02ac07a..a6bc485a9 100644
--- a/src/models/qwen4exp.cpp
+++ b/src/models/qwen4exp.cpp
@@ -294,7 +294,7 @@ void llama_model_qwen4exp::load_arch_tensors(llama_model_loader & ml) {
             layer.ple_norm_key   = create_tensor(tn(LLM_TENSOR_PLE_NORM_KEY,   "weight", il), { n_embd, hc }, flags | TENSOR_ALLOW_RESHAPE);
             layer.ple_norm_query = create_tensor(tn(LLM_TENSOR_PLE_NORM_QUERY, "weight", il), { n_embd, hc }, flags | TENSOR_ALLOW_RESHAPE);
             layer.ple_norm_conv  = create_tensor(tn(LLM_TENSOR_PLE_NORM_CONV,  "weight", il), { n_embd, hc }, flags | TENSOR_ALLOW_RESHAPE);
-            layer.ple_conv1d     = create_tensor(tn(LLM_TENSOR_PLE_CONV1D,     "weight", il), { hparams.ple_conv_kernel, hc_dim }, flags);
+            layer.ple_conv1d     = create_tensor(tn(LLM_TENSOR_PLE_CONV1D,     "weight", il), { hparams.ple_conv_kernel, 1, hc_dim }, flags | TENSOR_ALLOW_RESHAPE);
         }

         layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", il), { n_embd, n_expert }, flags);
@@ -1479,34 +1479,18 @@ ggml_tensor * llama_model_qwen4exp::graph::build_ple(
             ggml_reshape_3d(ctx0, normalized, hc_dim, n_seq_tokens, n_seqs),
             hist, hc_dim, il);

-    ggml_tensor * conv_out = nullptr;
-    for (int64_t k = 0; k < kern; ++k) {
-        // tap k reads (kern-1-k)*dilation positions back
-        const int64_t start = hist - (kern - 1 - k) * dil;
-
-        ggml_tensor * shifted = ggml_cont(ctx0,
-                ggml_transpose(ctx0,
-                        ggml_view_3d(ctx0, padded, n_seq_tokens, hc_dim, n_seqs,
-                                padded->nb[1], padded->nb[2],
-                                ggml_row_size(padded->type, start))));
-
-        // column k of the [kern, hc_dim] kernel is one weight per channel
-        ggml_tensor * wk = ggml_cont(ctx0,
-                ggml_view_2d(ctx0, model.layers[il].ple_conv1d, 1, hc_dim,
-                        model.layers[il].ple_conv1d->nb[1],
-                        k * model.layers[il].ple_conv1d->nb[0]));
-        // this kernel keeps the file type, so cast it before it multiplies an f32 activation
-        wk = ggml_reshape_1d(ctx0, wk, hc_dim);
-        if (wk->type != GGML_TYPE_F32) {
-            wk = ggml_cast(ctx0, wk, GGML_TYPE_F32);
-        }
+    ggml_tensor * w = model.layers[il].ple_conv1d;

-        ggml_tensor * term = ggml_mul(ctx0, shifted, wk);
-        conv_out = conv_out ? ggml_add(ctx0, conv_out, term) : term;
-    }
+    ggml_tensor * padded2 = ggml_reshape_3d(ctx0, padded, padded->ne[0], 1, hc_dim * n_seqs);
+    ggml_tensor * kshape = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, kern, 1);
+    ggml_tensor * im = ggml_im2col(ctx0, kshape, padded2, 1, 0, 0, 0, dil, 0, false, GGML_TYPE_F32);
+    ggml_tensor * x = ggml_reshape_4d(ctx0, im, kern, n_seq_tokens, hc_dim, n_seqs);
+    ggml_tensor * y = ggml_mul_mat(ctx0, w, x);
+    y = ggml_cont(ctx0, ggml_permute(ctx0, y, 2, 1, 0, 3));
+    ggml_tensor * conv_out = ggml_reshape_3d(ctx0, y, hc_dim, n_seq_tokens, n_seqs);

     conv_out = ggml_silu(ctx0, conv_out);
-    conv_out = ggml_reshape_3d(ctx0, ggml_cont(ctx0, conv_out), n_embd, hc, n_tokens);
+    conv_out = ggml_reshape_3d(ctx0, conv_out, n_embd, hc, n_tokens);
     cb(conv_out, "ple_conv_out", il);

     return ggml_add(ctx0, hidden, ggml_add(ctx0, gated, conv_out));