diff --git a/examples/quantize/quantize.cpp b/examples/quantize/quantize.cpp index 2e2c62bf..9e4bcd6d 100644 --- a/examples/quantize/quantize.cpp +++ b/examples/quantize/quantize.cpp @@ -151,7 +151,7 @@ static bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftyp // [[noreturn]] static void usage(const char * executable) { - printf("usage: %s [--help] [--allow-requantize] [--leave-output-tensor] [--pure] [--imatrix] [--hide-imatrix] [--include-weights] [--exclude-weights] [--output-tensor-type] [--token-embedding-type] [--attn-q-type] [--attn-k-type] [--attn-v-type] [--attn-qkv-type] [--attn-output-type] [--ffn-gate-type] [--ffn-down-type] [--ffn-up-type] [--keep-split] [--override-kv] model-f32.gguf [model-quant.gguf] type [nthreads]\n\n", executable); + printf("usage: %s [--help] [--allow-requantize] [--leave-output-tensor] [--pure] [--imatrix] [--hide-imatrix] [--include-weights] [--exclude-weights] [--output-tensor-type] [--token-embedding-type] [--ffn-gate-inp-type] [--attn-q-type] [--attn-k-type] [--attn-v-type] [--attn-qkv-type] [--attn-output-type] [--ffn-gate-type] [--ffn-down-type] [--ffn-up-type] [--keep-split] [--override-kv] model-f32.gguf [model-quant.gguf] type [nthreads]\n\n", executable); printf(" --allow-requantize: Allows requantizing tensors that have already been quantized. Warning: This can severely reduce quality compared to quantizing from 16bit or 32bit\n"); printf(" --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing\n"); printf(" --pure: Disable k-quant mixtures and quantize all tensors to the same type\n"); @@ -161,6 +161,7 @@ static void usage(const char * executable) { printf(" --exclude-weights tensor_name: use importance matrix for this/these tensor(s)\n"); printf(" --output-tensor-type ggml_type: use this ggml_type for the output.weight tensor.\n"); printf(" --token-embedding-type ggml_type: use this ggml_type for the token_embd.weight tensor.\n\n"); + printf(" --ffn-gate-inp-type ggml_type: use this ggml_type for the ffn_gate_inp tensors.\n\n"); printf(" --custom-q regex1=type1,regex2=type2...: use this to specify custom quantization type rules.\n\n"); printf(" --repack Repack all tensors to the corresponding _r4/8 variant if available.\n\n"); printf(" --repack-pattern Comma separated list of regexs to use for matching tensor names to be repacked.\n\n"); @@ -375,6 +376,12 @@ int main(int argc, char ** argv) { } else { usage(argv[0]); } + } else if (strcmp(argv[arg_idx], "--ffn-gate-inp-type") == 0) { + if (arg_idx < argc-1) { + params.ffn_gate_inp_type = parse_ggml_type(argv[++arg_idx]); + } else { + usage(argv[0]); + } } else if (strcmp(argv[arg_idx], "--attn-q-type") == 0) { if (arg_idx < argc-1) { params.attn_q_type = parse_ggml_type(argv[++arg_idx]); diff --git a/include/llama.h b/include/llama.h index d6759f16..31d82949 100644 --- a/include/llama.h +++ b/include/llama.h @@ -455,6 +455,7 @@ extern "C" { enum ggml_type ffn_gate_type; // feedforward network gate type enum ggml_type ffn_down_type; // feedforward network down type enum ggml_type ffn_up_type; // feedforward network up type + enum ggml_type ffn_gate_inp_type; // routed experts probabilities typy (relevant for MoE models only) bool allow_requantize; // allow quantizing non-f32/f16 tensors bool quantize_output_tensor; // quantize output.weight bool only_copy; // only copy tensors - ftype, allow_requantize and quantize_output_tensor are ignored diff --git a/src/llama-quantize.cpp b/src/llama-quantize.cpp index 14d61971..d35f89e5 100644 --- a/src/llama-quantize.cpp +++ b/src/llama-quantize.cpp @@ -1245,7 +1245,12 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s // do not quantize expert gating tensors // NOTE: can't use LLM_TN here because the layer number is not known - quantize &= name.find("ffn_gate_inp.weight") == std::string::npos; + if (name.find("ffn_gate_inp.weight") != std::string::npos) { + if (params->ffn_gate_inp_type == GGML_TYPE_COUNT || params->ffn_gate_inp_type == tensor->type) { + quantize = false; + } + } + //quantize &= name.find("ffn_gate_inp.weight") == std::string::npos; // do not quantize positional embeddings and token types (BERT) quantize &= name != LLM_TN(model.arch)(LLM_TENSOR_POS_EMBD, "weight"); @@ -1328,6 +1333,9 @@ static void llama_model_quantize_internal(const std::string & fname_inp, const s if (params->output_tensor_type < GGML_TYPE_COUNT && strcmp(tensor->name, "output.weight") == 0) { new_type = params->output_tensor_type; } + if (params->ffn_gate_inp_type < GGML_TYPE_COUNT && name.find("ffn_gate_inp.weight") != std::string::npos) { + new_type = params->ffn_gate_inp_type; + } if (params->attn_q_type < GGML_TYPE_COUNT && strcmp(tensor->name, "attn_q.weight") == 0) { new_type = params->attn_q_type; } diff --git a/src/llama.cpp b/src/llama.cpp index da90b19c..b78adf90 100644 --- a/src/llama.cpp +++ b/src/llama.cpp @@ -3863,6 +3863,7 @@ struct llama_model_quantize_params llama_model_quantize_default_params() { /*.ffn_gate_type =*/ GGML_TYPE_COUNT, /*.ffn_down_type =*/ GGML_TYPE_COUNT, /*.ffn_up_type =*/ GGML_TYPE_COUNT, + /*.ffn_gat_inp_type =*/ GGML_TYPE_COUNT, /*.allow_requantize =*/ false, /*.quantize_output_tensor =*/ true, /*.only_copy =*/ false,