Skip to content

Commit bbef282

Browse files
LostRuinsggerganov
andauthored
Possible solution to allow K-quants on models with n_vocab!=32000 (#2148)
* This allows LLAMA models that were previously incompatible with K quants to function mostly as normal. This happens when a model has a vocab != 32000, e.g 32001 which means it's not divisible by 256 or 64. Since the problematic dimensions only apply for `tok_embeddings.weight` and `output.weight` (dimentions 4096 x n_vocab), we can simply quantize these layers to Q8_0 whereas the majority of the hidden layers are still K-quanted since they have compatible dimensions. * Fix indentation Co-authored-by: Georgi Gerganov <[email protected]> * As an alternative, to avoid failing on Metal due to lack of Q8_0 support, instead quantize tok_embeddings.weight to Q4_0 and retain output.weight as F16. This results in a net gain of about 55mb for a 7B model compared to previous approach, but should minimize adverse impact to model quality. --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent 5656d10 commit bbef282

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

llama.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,15 +2454,14 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
24542454
} else {
24552455
new_type = quantized_type;
24562456
#ifdef GGML_USE_K_QUANTS
2457+
bool convert_incompatible_tensor = false;
24572458
if (quantized_type == GGML_TYPE_Q2_K || quantized_type == GGML_TYPE_Q3_K || quantized_type == GGML_TYPE_Q4_K ||
24582459
quantized_type == GGML_TYPE_Q5_K || quantized_type == GGML_TYPE_Q6_K) {
24592460
int nx = tensor.ne.at(0);
24602461
int ny = tensor.ne.at(1);
24612462
if (nx % QK_K != 0 || ny % QK_K != 0) {
2462-
fprintf(stderr, "\n\n========================= Tensor sizes %d x %d are not divisible by %d\n",nx,ny,QK_K);
2463-
fprintf(stderr, "This is required to be able to use k-quants for now!\n");
2464-
fprintf(stderr, "========================================================================================\n\n");
2465-
throw std::runtime_error("Unsupported tensor size encountered\n");
2463+
fprintf(stderr, "\n\nTensor sizes %d x %d are not divisible by %d, required for k-quants.\n",nx,ny,QK_K);
2464+
convert_incompatible_tensor = true;
24662465
}
24672466
}
24682467
if (tensor.name == "output.weight") {
@@ -2490,6 +2489,17 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s
24902489
if (ftype == LLAMA_FTYPE_MOSTLY_Q3_K_M || ftype == LLAMA_FTYPE_MOSTLY_Q2_K) new_type = GGML_TYPE_Q4_K;
24912490
else if (ftype == LLAMA_FTYPE_MOSTLY_Q3_K_L) new_type = GGML_TYPE_Q5_K;
24922491
}
2492+
if (convert_incompatible_tensor) {
2493+
if (tensor.name == "output.weight") {
2494+
new_type = GGML_TYPE_F16; //fall back to F16 instead of just failing.
2495+
fprintf(stderr, "F16 will be used for this tensor instead.\n");
2496+
} else if (tensor.name == "tok_embeddings.weight") {
2497+
new_type = GGML_TYPE_Q4_0; //fall back to Q4_0 instead of just failing.
2498+
fprintf(stderr, "Q4_0 will be used for this tensor instead.\n");
2499+
} else {
2500+
throw std::runtime_error("Unsupported tensor size encountered\n");
2501+
}
2502+
}
24932503
#endif
24942504

24952505
float * f32_data;

0 commit comments

Comments
 (0)