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.
This commit is contained in:
林晗 2026-03-20 18:01:21 +08:00
parent c46583b86b
commit a79d977fbf
1 changed files with 10 additions and 1 deletions

View File

@ -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);
}