From a79d977fbf7cbeefb786f62ad58a4aa424f64a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=99=97?= Date: Fri, 20 Mar 2026 18:01:21 +0800 Subject: [PATCH] model-loader : return nullptr for not-required tensors not found in GGUF This is needed for tied embeddings where output.weight shares token_embd.weight data, so output.weight is not present in the GGUF file. --- src/llama-model-loader.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/llama-model-loader.cpp b/src/llama-model-loader.cpp index 413f34c226..ec804888d0 100644 --- a/src/llama-model-loader.cpp +++ b/src/llama-model-loader.cpp @@ -1174,8 +1174,17 @@ struct ggml_tensor * llama_model_loader::create_tensor( if (flags & TENSOR_SKIP_IF_VIRTUAL) { return nullptr; } - ggml_type type = GGML_TYPE_F32; const int64_t tid = gguf_find_tensor(metadata, tn.str().c_str()); + + // when tensor is not found in metadata and not required, return nullptr + // this is needed for tied embeddings: output.weight shares token_embd.weight data, + // so output.weight is not present in the GGUF file. returning nullptr here allows + // the caller to fall through to the TENSOR_DUPLICATED branch. + if (tid == -1 && (flags & TENSOR_NOT_REQUIRED)) { + return nullptr; + } + + ggml_type type = GGML_TYPE_F32; if (tid != -1) { type = gguf_get_tensor_type(metadata, tid); }