Skip to content

Commit 515f7d0

Browse files
authored
llama : fix quantization of shared token_embd (#5944)
1 parent 76e8688 commit 515f7d0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

llama.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -10973,6 +10973,9 @@ struct quantize_state_internal {
1097310973

1097410974
bool has_imatrix = false;
1097510975

10976+
// used to figure out if a model shares tok_embd with the output weight
10977+
bool has_output = false;
10978+
1097610979
quantize_state_internal(const llama_model & model, const llama_model_quantize_params * params)
1097710980
: model(model)
1097810981
, params(params)
@@ -11070,8 +11073,7 @@ static ggml_type get_k_quant_type(quantize_state_internal & qs, ggml_type new_ty
1107011073

1107111074
// for arches that share the same tensor between the token embeddings and the output, we quantize the token embeddings
1107211075
// with the quantization of the output tensor
11073-
if (name == tn(LLM_TENSOR_OUTPUT, "weight") ||
11074-
(LLM_TENSOR_NAMES.at(arch).find(LLM_TENSOR_OUTPUT) == LLM_TENSOR_NAMES.at(arch).end() && name == "token_embd.weight")) {
11076+
if (name == tn(LLM_TENSOR_OUTPUT, "weight") || (!qs.has_output && name == tn(LLM_TENSOR_TOKEN_EMBD, "weight"))) {
1107511077
int nx = tensor->ne[0];
1107611078
if (arch == LLM_ARCH_FALCON || nx % QK_K != 0) {
1107711079
new_type = GGML_TYPE_Q8_0;
@@ -11460,6 +11462,9 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
1146011462
else if (name.find("ffn_up") != std::string::npos) {
1146111463
++qs.n_ffn_up;
1146211464
}
11465+
else if (name == LLM_TN(model.arch)(LLM_TENSOR_OUTPUT, "weight")) {
11466+
qs.has_output = true;
11467+
}
1146311468
}
1146411469
if (qs.n_attention_wv != qs.n_ffn_down || (uint32_t)qs.n_attention_wv != model.hparams.n_layer) {
1146511470
LLAMA_LOG_WARN("%s ============ Strange model: n_attention_wv = %d, n_ffn_down = %d, hparams.n_layer = %d\n",

0 commit comments

Comments
 (0)