Skip to content

Commit 3dd9591

Browse files
EugeoSynthesisThirtyTwoYour Nametaronaeo
authored
quantize: add option --tensor-type-file to llama-quantize (ggml-org#18572)
* add option --tensor-type-file to llama-quantize, but it raises an error. * add error message when file not found * quantize: update help menu, fix CI Signed-off-by: Aaron Teo <aaron.teo1@ibm.com> --------- Signed-off-by: Aaron Teo <aaron.teo1@ibm.com> Co-authored-by: Your Name <you@example.com> Co-authored-by: Aaron Teo <aaron.teo1@ibm.com>
1 parent ec6c742 commit 3dd9591

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

tools/quantize/quantize.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static bool try_parse_ftype(const std::string & ftype_str_in, llama_ftype & ftyp
119119
[[noreturn]]
120120
static void usage(const char * executable) {
121121
printf("usage: %s [--help] [--allow-requantize] [--leave-output-tensor] [--pure] [--imatrix] [--include-weights]\n", executable);
122-
printf(" [--exclude-weights] [--output-tensor-type] [--token-embedding-type] [--tensor-type] [--prune-layers] [--keep-split] [--override-kv]\n");
122+
printf(" [--exclude-weights] [--output-tensor-type] [--token-embedding-type] [--tensor-type] [--tensor-type-file] [--prune-layers] [--keep-split] [--override-kv]\n");
123123
printf(" model-f32.gguf [model-quant.gguf] type [nthreads]\n\n");
124124
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");
125125
printf(" --leave-output-tensor: Will leave output.weight un(re)quantized. Increases model size but may also increase quality, especially when requantizing\n");
@@ -131,6 +131,8 @@ static void usage(const char * executable) {
131131
printf(" --token-embedding-type ggml_type: use this ggml_type for the token embeddings tensor\n");
132132
printf(" --tensor-type TENSOR=TYPE: quantize this tensor to this ggml_type. example: --tensor-type attn_q=q8_0\n");
133133
printf(" Advanced option to selectively quantize tensors. May be specified multiple times.\n");
134+
printf(" --tensor-type-file tensor_type.txt: list of tensors to quantize to specific ggml_type. example: --tensor-type-file tensor_type_list.txt\n");
135+
printf(" Advanced option to selectively quantize a long list of tensors. Format to be tensor_name=ggml_type, separated by spaces/newline.\n");
134136
printf(" --prune-layers L0,L1,L2...comma-separated list of layer numbers to prune from the model\n");
135137
printf(" Advanced option to remove all tensors from the given layers\n");
136138
printf(" --keep-split: will generate quantized model in the same shards as input\n");
@@ -415,6 +417,23 @@ static bool parse_tensor_type(const char * data, std::vector<tensor_quantization
415417
return true;
416418
}
417419

420+
static bool parse_tensor_type_file(const char * filename, std::vector<tensor_quantization> & tensor_type) {
421+
std::ifstream file(filename);
422+
if (!file) {
423+
printf("\n%s: failed to open file '%s': %s\n\n", __func__, filename, std::strerror(errno));
424+
return false;
425+
}
426+
427+
std::string arg;
428+
while (file >> arg) {
429+
if (!parse_tensor_type(arg.c_str(), tensor_type)) {
430+
return false;
431+
}
432+
}
433+
434+
return true;
435+
}
436+
418437
static bool parse_layer_prune(const char * data, std::vector<int> & prune_layers) {
419438
if (!data) {
420439
printf("\n%s: no layer pruning ids provided\n\n", __func__);
@@ -480,6 +499,10 @@ int main(int argc, char ** argv) {
480499
if (arg_idx == argc-1 || !parse_tensor_type(argv[++arg_idx], tensor_types)) {
481500
usage(argv[0]);
482501
}
502+
} else if (strcmp(argv[arg_idx], "--tensor-type-file") == 0) {
503+
if (arg_idx == argc-1 || !parse_tensor_type_file(argv[++arg_idx], tensor_types)) {
504+
usage(argv[0]);
505+
}
483506
} else if (strcmp(argv[arg_idx], "--prune-layers") == 0) {
484507
if (arg_idx == argc-1 || !parse_layer_prune(argv[++arg_idx], prune_layers)) {
485508
usage(argv[0]);
@@ -686,3 +709,4 @@ int main(int argc, char ** argv) {
686709

687710
return 0;
688711
}
712+

0 commit comments

Comments
 (0)