-
Notifications
You must be signed in to change notification settings - Fork 11.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update llama-quant.cpp llama_tensor_get_type with DeepSeek friendly modifications #12727
Open
bartowski1182
wants to merge
9
commits into
ggml-org:master
Choose a base branch
from
bartowski1182:quant_types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−10
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b1a60aa
Update llama-quant.cpp llama_tensor_get_type with DeepSeek friendly m…
bartowski1182 e51a5e5
Claw back a few of the changes for less dramatic file size increase
bartowski1182 4f90fac
Merge branch 'ggml-org:master' into quant_types
bartowski1182 c07f5d7
Few more changes and tweaks
bartowski1182 db2d562
Remove debug assert
bartowski1182 feae28b
Remove trailing whitespaces
bartowski1182 71ab742
A bit more weight to shared experts for larger sizes
bartowski1182 a82a8c1
Merge branch 'ggml-org:master' into quant_types
bartowski1182 ad6ab17
Merge branch 'ggml-org:master' into quant_types
bartowski1182 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,10 +28,22 @@ struct quantize_state_impl { | |
int n_ffn_down = 0; | ||
int n_ffn_gate = 0; | ||
int n_ffn_up = 0; | ||
int n_ffn_down_exp = 0; | ||
int n_ffn_gate_exp = 0; | ||
int n_ffn_up_exp = 0; | ||
int n_ffn_down_shexp = 0; | ||
int n_ffn_gate_shexp = 0; | ||
int n_ffn_up_shexp = 0; | ||
int i_attention_wv = 0; | ||
int i_ffn_down = 0; | ||
int i_ffn_gate = 0; | ||
int i_ffn_up = 0; | ||
int i_ffn_down_exp = 0; | ||
int i_ffn_gate_exp = 0; | ||
int i_ffn_up_exp = 0; | ||
int i_ffn_down_shexp = 0; | ||
int i_ffn_gate_shexp = 0; | ||
int i_ffn_up_shexp = 0; | ||
|
||
int n_k_quantized = 0; | ||
int n_fallback = 0; | ||
|
@@ -119,6 +131,23 @@ static void llama_tensor_dequantize_impl( | |
workers.clear(); | ||
} | ||
|
||
// Check if ftype is specifically IQ2_S or IQ2_M | ||
static inline bool is_iq2s_or_iq2m(llama_ftype ftype) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is used all over the place, made it an inline helper, happy to change it back if changes like these are unwanted (same below with is_iq1_group and get_expert_exps_type) |
||
return ftype == LLAMA_FTYPE_MOSTLY_IQ2_S || ftype == LLAMA_FTYPE_MOSTLY_IQ2_M; | ||
} | ||
|
||
// Check if ftype belongs to the IQ1 group | ||
static inline bool is_iq1_group(llama_ftype ftype) { | ||
return ftype == LLAMA_FTYPE_MOSTLY_IQ1_S || ftype == LLAMA_FTYPE_MOSTLY_IQ1_M; | ||
} | ||
|
||
// Returns the appropriate type for expert _exps tensors based on ftype | ||
static inline ggml_type get_expert_exps_type(llama_ftype ftype) { | ||
if (is_iq1_group(ftype)) return GGML_TYPE_IQ2_XXS; | ||
if (is_iq2s_or_iq2m(ftype)) return GGML_TYPE_IQ3_XXS; | ||
/* otherwise */ return GGML_TYPE_IQ2_XS; | ||
} | ||
|
||
static ggml_type llama_tensor_get_type(quantize_state_impl & qs, ggml_type new_type, const ggml_tensor * tensor, llama_ftype ftype) { | ||
const std::string name = ggml_get_name(tensor); | ||
|
||
|
@@ -175,7 +204,7 @@ static ggml_type llama_tensor_get_type(quantize_state_impl & qs, ggml_type new_t | |
ftype == LLAMA_FTYPE_MOSTLY_IQ1_S || ftype == LLAMA_FTYPE_MOSTLY_IQ1_M) { | ||
new_type = GGML_TYPE_Q2_K; | ||
} | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_IQ2_S || ftype == LLAMA_FTYPE_MOSTLY_IQ2_M) { | ||
else if (is_iq2s_or_iq2m(ftype)) { | ||
new_type = GGML_TYPE_IQ3_S; | ||
} | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_IQ3_XXS) { | ||
|
@@ -189,24 +218,105 @@ static ggml_type llama_tensor_get_type(quantize_state_impl & qs, ggml_type new_t | |
ftype == LLAMA_FTYPE_MOSTLY_IQ2_S || ftype == LLAMA_FTYPE_MOSTLY_IQ2_M || ftype == LLAMA_FTYPE_MOSTLY_IQ1_M) { | ||
if (name.find("attn_v.weight") != std::string::npos) { | ||
if (qs.model.hparams.n_gqa() >= 4 || qs.model.hparams.n_expert >= 4) new_type = GGML_TYPE_Q4_K; | ||
else new_type = ftype == LLAMA_FTYPE_MOSTLY_IQ2_S || ftype == LLAMA_FTYPE_MOSTLY_IQ2_M ? GGML_TYPE_IQ3_S : GGML_TYPE_Q2_K; | ||
else new_type = is_iq2s_or_iq2m(ftype) ? GGML_TYPE_IQ3_S : GGML_TYPE_Q2_K; | ||
++qs.i_attention_wv; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("attn_k.weight") != std::string::npos) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("attn_kv_a_mqa.weight") != std::string::npos) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("attn_kv_b.weight") != std::string::npos) { | ||
if (qs.i_attention_wv < qs.n_attention_wv/8) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
else if (use_more_bits(qs.i_attention_wv, qs.n_attention_wv)) { | ||
new_type = is_iq2s_or_iq2m(ftype) ? GGML_TYPE_IQ3_S : GGML_TYPE_Q2_K; | ||
} | ||
++qs.i_attention_wv; | ||
} | ||
else if (qs.model.hparams.n_expert == 8 && name.find("attn_k.weight") != std::string::npos) { | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("attn_q_a.weight") != std::string::npos) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("attn_q_b.weight") != std::string::npos) { | ||
new_type = is_iq2s_or_iq2m(ftype) ? GGML_TYPE_IQ3_S : GGML_TYPE_Q2_K; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_down.weight") != std::string::npos) { | ||
if (qs.i_ffn_down < qs.n_ffn_down/16) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
else if (qs.i_ffn_down < qs.n_ffn_down/8) { | ||
new_type = is_iq2s_or_iq2m(ftype) ? GGML_TYPE_IQ3_S : GGML_TYPE_Q2_K; | ||
} | ||
++qs.i_ffn_down; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_gate.weight") != std::string::npos) { | ||
if (qs.i_ffn_gate < qs.n_ffn_gate/16) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
else if (qs.i_ffn_gate < qs.n_ffn_gate/8) { | ||
new_type = is_iq2s_or_iq2m(ftype) ? GGML_TYPE_IQ3_S : GGML_TYPE_Q2_K; | ||
} | ||
++qs.i_ffn_gate; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_up.weight") != std::string::npos) { | ||
if (qs.i_ffn_up < qs.n_ffn_up/16) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
else if (qs.i_ffn_up < qs.n_ffn_up/8) { | ||
new_type = is_iq2s_or_iq2m(ftype) ? GGML_TYPE_IQ3_S : GGML_TYPE_Q2_K; | ||
} | ||
++qs.i_ffn_up; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_down_exps.weight") != std::string::npos) { | ||
if (qs.i_ffn_down_exp < qs.n_ffn_down_exp/8) { | ||
new_type = get_expert_exps_type(ftype); | ||
} | ||
++qs.i_ffn_down_exp; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_gate_exps.weight") != std::string::npos) { | ||
if (qs.i_ffn_gate_exp < qs.n_ffn_gate_exp/8) { | ||
new_type = get_expert_exps_type(ftype); | ||
} | ||
++qs.i_ffn_gate_exp; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_up_exps.weight") != std::string::npos) { | ||
if (qs.i_ffn_up_exp < qs.n_ffn_up_exp/8) { | ||
new_type = get_expert_exps_type(ftype); | ||
} | ||
++qs.i_ffn_up_exp; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_down_shexp.weight") != std::string::npos) { | ||
if (use_more_bits(qs.i_ffn_down_shexp, qs.n_ffn_down_shexp)) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
++qs.i_ffn_down_shexp; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_gate_shexp.weight") != std::string::npos) { | ||
if (use_more_bits(qs.i_ffn_gate_shexp, qs.n_ffn_gate_shexp)) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
++qs.i_ffn_gate_shexp; | ||
} | ||
else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_up_shexp.weight") != std::string::npos) { | ||
if (use_more_bits(qs.i_ffn_up_shexp, qs.n_ffn_up_shexp)) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
++qs.i_ffn_up_shexp; | ||
} | ||
else if (name.find("ffn_down") != std::string::npos) { | ||
if (qs.i_ffn_down < qs.n_ffn_down/8) { | ||
new_type = ftype == LLAMA_FTYPE_MOSTLY_IQ2_S || ftype == LLAMA_FTYPE_MOSTLY_IQ2_M ? GGML_TYPE_IQ3_S : GGML_TYPE_Q2_K; | ||
new_type = is_iq2s_or_iq2m(ftype) ? GGML_TYPE_IQ3_S : GGML_TYPE_Q2_K; | ||
} | ||
++qs.i_ffn_down; | ||
} | ||
else if (name.find("attn_output.weight") != std::string::npos) { | ||
if (qs.model.hparams.n_expert == 8) { | ||
new_type = GGML_TYPE_Q5_K; | ||
if (qs.model.hparams.n_expert >= 8) { | ||
new_type = is_iq2s_or_iq2m(ftype) ? GGML_TYPE_Q5_K : GGML_TYPE_Q4_K; | ||
} else { | ||
if (ftype == LLAMA_FTYPE_MOSTLY_IQ1_S || ftype == LLAMA_FTYPE_MOSTLY_IQ1_M) new_type = GGML_TYPE_IQ2_XXS; | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_IQ2_S || ftype == LLAMA_FTYPE_MOSTLY_IQ2_M) new_type = GGML_TYPE_IQ3_S; | ||
if (is_iq1_group(ftype)) new_type = GGML_TYPE_IQ2_XXS; | ||
else if (is_iq2s_or_iq2m(ftype)) new_type = GGML_TYPE_IQ3_S; | ||
} | ||
} | ||
} else if (name.find("attn_v.weight") != std::string::npos) { | ||
|
@@ -266,6 +376,30 @@ static ggml_type llama_tensor_get_type(quantize_state_impl & qs, ggml_type new_t | |
else if (ftype == LLAMA_FTYPE_MOSTLY_IQ3_XXS) { | ||
new_type = GGML_TYPE_IQ2_S; | ||
} | ||
} else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_down_shexp.weight") != std::string::npos) { | ||
new_type = GGML_TYPE_Q5_K; | ||
if (use_more_bits(qs.i_ffn_down_shexp, qs.n_ffn_down_shexp)) { | ||
new_type = GGML_TYPE_Q8_0; | ||
} | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q4_K_M || ftype == LLAMA_FTYPE_MOSTLY_Q5_K_S) new_type = GGML_TYPE_Q6_K; | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q5_K_M) new_type = GGML_TYPE_Q8_0; | ||
++qs.i_ffn_down_shexp; | ||
} else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_gate_shexp.weight") != std::string::npos) { | ||
new_type = GGML_TYPE_Q5_K; | ||
if (use_more_bits(qs.i_ffn_gate_shexp, qs.n_ffn_gate_shexp)) { | ||
new_type = GGML_TYPE_Q8_0; | ||
} | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q4_K_M || ftype == LLAMA_FTYPE_MOSTLY_Q5_K_S) new_type = GGML_TYPE_Q6_K; | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q5_K_M) new_type = GGML_TYPE_Q8_0; | ||
++qs.i_ffn_gate_shexp; | ||
} else if (qs.model.hparams.n_expert >= 8 && name.find("ffn_up_shexp.weight") != std::string::npos) { | ||
new_type = GGML_TYPE_Q5_K; | ||
if (use_more_bits(qs.i_ffn_up_shexp, qs.n_ffn_up_shexp)) { | ||
new_type = GGML_TYPE_Q8_0; | ||
} | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q4_K_M || ftype == LLAMA_FTYPE_MOSTLY_Q5_K_S) new_type = GGML_TYPE_Q6_K; | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q5_K_M) new_type = GGML_TYPE_Q8_0; | ||
++qs.i_ffn_up_shexp; | ||
} else if (name.find("ffn_down") != std::string::npos) { | ||
auto info = layer_info(qs.i_ffn_down, qs.n_ffn_down, name.c_str()); | ||
int i_layer = info.first, n_layer = info.second; | ||
|
@@ -313,7 +447,7 @@ static ggml_type llama_tensor_get_type(quantize_state_impl & qs, ggml_type new_t | |
++qs.i_ffn_down; | ||
} else if (name.find("attn_output.weight") != std::string::npos) { | ||
if (arch != LLM_ARCH_FALCON) { | ||
if (qs.model.hparams.n_expert == 8) { | ||
if (qs.model.hparams.n_expert >= 8) { | ||
if (ftype == LLAMA_FTYPE_MOSTLY_Q2_K || ftype == LLAMA_FTYPE_MOSTLY_IQ3_XS || ftype == LLAMA_FTYPE_MOSTLY_IQ3_XXS || | ||
ftype == LLAMA_FTYPE_MOSTLY_Q3_K_S || ftype == LLAMA_FTYPE_MOSTLY_Q3_K_M || ftype == LLAMA_FTYPE_MOSTLY_IQ4_NL || | ||
ftype == LLAMA_FTYPE_MOSTLY_Q4_K_S || ftype == LLAMA_FTYPE_MOSTLY_Q4_K_M || ftype == LLAMA_FTYPE_MOSTLY_IQ3_S || | ||
|
@@ -353,6 +487,28 @@ static ggml_type llama_tensor_get_type(quantize_state_impl & qs, ggml_type new_t | |
new_type = GGML_TYPE_IQ3_XXS; | ||
} | ||
++qs.i_ffn_up; | ||
} else if (qs.model.hparams.n_expert >= 8 && name.find("attn_kv_a_mqa.weight") != std::string::npos) { | ||
new_type = GGML_TYPE_Q8_0; | ||
} else if (qs.model.hparams.n_expert >= 8 && name.find("attn_kv_b.weight") != std::string::npos) { | ||
new_type = GGML_TYPE_Q4_K; | ||
if (qs.i_attention_wv < qs.n_attention_wv/16) { | ||
new_type = GGML_TYPE_Q8_0; | ||
} else if (use_more_bits(qs.i_attention_wv, qs.n_attention_wv)) { | ||
new_type = GGML_TYPE_Q6_K; | ||
} | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q4_K_M || ftype == LLAMA_FTYPE_MOSTLY_Q3_K_L || ftype == LLAMA_FTYPE_MOSTLY_IQ4_NL || ftype == LLAMA_FTYPE_MOSTLY_IQ4_XS) new_type = GGML_TYPE_Q5_K; | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q5_K_M) new_type = GGML_TYPE_Q6_K; | ||
++qs.i_attention_wv; | ||
} else if (qs.model.hparams.n_expert >= 8 &&name.find("attn_q_b.weight") != std::string::npos) { | ||
if (ftype == LLAMA_FTYPE_MOSTLY_Q3_K_M || ftype == LLAMA_FTYPE_MOSTLY_Q3_K_L || ftype == LLAMA_FTYPE_MOSTLY_IQ3_M) { | ||
new_type = GGML_TYPE_Q4_K; | ||
} | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q4_K_M || ftype == LLAMA_FTYPE_MOSTLY_Q5_K_S) new_type = GGML_TYPE_Q5_K; | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q5_K_M) new_type = GGML_TYPE_Q6_K; | ||
} else if (qs.model.hparams.n_expert >= 8 && name.find("attn_q_a.weight") != std::string::npos) { | ||
new_type = GGML_TYPE_Q5_K; | ||
if (ftype == LLAMA_FTYPE_MOSTLY_Q4_K_M || ftype == LLAMA_FTYPE_MOSTLY_Q5_K_S) new_type = GGML_TYPE_Q6_K; | ||
else if (ftype == LLAMA_FTYPE_MOSTLY_Q5_K_M) new_type = GGML_TYPE_Q8_0; | ||
} | ||
|
||
// if (ftype == LLAMA_FTYPE_MOSTLY_Q2_K) new_type = GGML_TYPE_Q3_K; | ||
|
@@ -618,6 +774,18 @@ static void llama_model_quantize_impl(const std::string & fname_inp, const std:: | |
++qs.n_attention_wv; | ||
} else if (name == LLM_TN(model.arch)(LLM_TENSOR_OUTPUT, "weight")) { | ||
qs.has_output = true; | ||
} else if (name.find("ffn_gate_exps.weight") != std::string::npos) { | ||
++qs.n_ffn_gate_exp; | ||
} else if (name.find("ffn_gate_shexp.weight") != std::string::npos) { | ||
++qs.n_ffn_gate_shexp; | ||
} else if (name.find("ffn_down_exps.weight") != std::string::npos) { | ||
++qs.n_ffn_down_exp; | ||
} else if (name.find("ffn_down_shexp.weight") != std::string::npos) { | ||
++qs.n_ffn_down_shexp; | ||
} else if (name.find("ffn_up_exps.weight") != std::string::npos) { | ||
++qs.n_ffn_up_exp; | ||
} else if (name.find("ffn_up_shexp.weight") != std::string::npos) { | ||
++qs.n_ffn_up_shexp; | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was bothering me that my IDE couldn't see the BPW from the docstring