Commit 49f972e13 for llama.cpp

commit 49f972e13961cddbe7d0545945267574a4bc1a04
Author: neoli <cdeatr@gmail.com>
Date:   Tue Dec 1 02:09:20 2026 +0800

    llama: Fix the Clef-Flash model loading crash on arm cpus (#32962)

    * fix crash while loading clef model on arm cpu

    * Add has_lm_head() judgement in qwen35 for clef models

    * add comment for LLM_TENSOR_OUTPUT_LEXICAL tensor type

    * Update src/llama-arch.cpp

    ---------

    Co-authored-by: Xuan-Son Nguyen <thichthat@gmail.com>

diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp
index e2fb51cdd..a83210f20 100644
--- a/src/llama-arch.cpp
+++ b/src/llama-arch.cpp
@@ -966,6 +966,7 @@ static const std::map<llm_tensor, const char *> LLM_TENSOR_NAMES = {
     { LLM_TENSOR_ROUTER_OUT,                             "blk.%d.ffn_router_out" },
     { LLM_TENSOR_ATTN_V_GATE,                            "blk.%d.attn_v_gate" },
     { LLM_TENSOR_ATTN_V_EXPS,                            "blk.%d.attn_v_exps" },
+    { LLM_TENSOR_OUTPUT_LEXICAL,                         "output" },
 };

 // declare information about the model weight tensors:
@@ -1408,6 +1409,7 @@ static const std::map<llm_tensor, llm_tensor_info> LLM_TENSOR_INFOS = {

     {LLM_TENSOR_ATTN_V_GATE,                {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT}},
     {LLM_TENSOR_ATTN_V_EXPS,                {LLM_TENSOR_LAYER_REPEATING, GGML_OP_MUL_MAT_ID}},
+    {LLM_TENSOR_OUTPUT_LEXICAL,             {LLM_TENSOR_LAYER_OUTPUT,    GGML_OP_GET_ROWS}},
 };

 LLM_KV::LLM_KV(llm_arch arch, const char * suffix) : arch(arch), suffix(suffix) {}
diff --git a/src/llama-arch.h b/src/llama-arch.h
index 50e3bc839..2dd67435a 100644
--- a/src/llama-arch.h
+++ b/src/llama-arch.h
@@ -969,6 +969,7 @@ enum llm_tensor {
     LLM_TENSOR_ROUTER_OUT,
     LLM_TENSOR_ATTN_V_GATE,
     LLM_TENSOR_ATTN_V_EXPS,
+    LLM_TENSOR_OUTPUT_LEXICAL, // Clef: Maps to the same tensor as LLM_TENSOR_OUTPUT but uses GET_ROWS operation
 };


diff --git a/src/models/clef.cpp b/src/models/clef.cpp
index 0add0655b..d7d94f49b 100644
--- a/src/models/clef.cpp
+++ b/src/models/clef.cpp
@@ -82,6 +82,12 @@ void llama_model_clef::load_arch_tensors(llama_model_loader & ml) {
     load_norm(field_norm,          LLM_TENSOR_DECISION_FIELD_NORM,          n_embd_h);
     load_norm(option_norm,         LLM_TENSOR_DECISION_OPTION_NORM,         n_embd_h);

+    output_lexical = create_tensor(tn(LLM_TENSOR_OUTPUT_LEXICAL, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
+
+    if (output_lexical == nullptr) {
+        output_lexical = tok_embd;
+    }
+
     proj_memory          = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_MEMORY,          "weight"), {n_embd, n_embd_h}, 0);
     proj_question        = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_QUESTION,        "weight"), {n_embd, n_embd_h}, 0);
     proj_option_question = create_tensor(tn(LLM_TENSOR_DECISION_PROJ_OPTION_QUESTION, "weight"), {n_embd, n_embd_h}, 0);
@@ -559,7 +565,7 @@ ggml_tensor * llama_model_clef::graph::build_head(ggml_tensor * hidden, input_de
     ggml_tensor * opt_ctx  = ggml_mul_mat(ctx0, hidden_t, inp->pool_o); // [n_embd, n_options]

     // mean of the output embeddings of the tokens of each option
-    ggml_tensor * lexical = ggml_get_rows(ctx0, model.output, inp->tokens);
+    ggml_tensor * lexical = ggml_get_rows(ctx0, model.output_lexical, inp->tokens);
     lexical = ggml_mul_mat(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, lexical)), inp->pool_o); // [n_embd, n_options]

     // one query per option
diff --git a/src/models/models.h b/src/models/models.h
index 7c0b9ca2f..893e74251 100644
--- a/src/models/models.h
+++ b/src/models/models.h
@@ -2999,6 +2999,8 @@ struct llama_model_qwen35 : public llama_model_base {
         graph_mtp(const llm_graph_params & params);
     };

+    // if false, the derived model does not use the LM head and builds its own output
+    virtual bool has_lm_head() const { return true; }
     std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
 };

@@ -3060,6 +3062,7 @@ struct llama_model_clef : public llama_model_qwen35 {
     ggml_tensor * scorer_b             = nullptr;
     ggml_tensor * scorer_out           = nullptr;
     ggml_tensor * scorer_out_b         = nullptr;
+    ggml_tensor * output_lexical       = nullptr;

     class input_decision;

@@ -3090,6 +3093,7 @@ struct llama_model_clef : public llama_model_qwen35 {
         const llama_model_clef & model;
     };

+    bool has_lm_head() const override { return false; }
     std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
 };

diff --git a/src/models/qwen35.cpp b/src/models/qwen35.cpp
index 542e150cb..129f6ddc4 100644
--- a/src/models/qwen35.cpp
+++ b/src/models/qwen35.cpp
@@ -41,17 +41,20 @@ void llama_model_qwen35::load_arch_tensors(llama_model_loader & ml) {

     // output
     output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
-    output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
+
+    if (has_lm_head()) {
+        output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
+
+        // if output is NULL, init from the input tok embed
+        if (output == NULL) {
+            output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
+        }
+    }

     // optional projection of the embeddings output
     cls_out   = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), { n_embd, hparams.n_embd_out() }, TENSOR_NOT_REQUIRED);
     cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"),   { hparams.n_embd_out() },         TENSOR_NOT_REQUIRED);

-    // if output is NULL, init from the input tok embed
-    if (output == NULL) {
-        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
-    }
-
     auto load_block_trunk = [&](int il, int flags) {
         auto & layer = layers[il];