ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / gpt-2 / quantize.cpp
| 1 | #include "ggml.h" |
| 2 | |
| 3 | #include "common.h" |
| 4 | #include "common-ggml.h" |
| 5 | |
| 6 | #include <cassert> |
| 7 | #include <cmath> |
| 8 | #include <cstdio> |
| 9 | #include <cstring> |
| 10 | #include <fstream> |
| 11 | #include <map> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | #include <regex> |
| 15 | |
| 16 | // default hparams (GPT-2 117M) |
| 17 | struct gpt2_hparams { |
| 18 | int32_t n_vocab = 50257; |
| 19 | int32_t n_ctx = 1024; |
| 20 | int32_t n_embd = 768; |
| 21 | int32_t n_head = 12; |
| 22 | int32_t n_layer = 12; |
| 23 | int32_t ftype = 1; |
| 24 | }; |
| 25 | |
| 26 | // quantize a model |
| 27 | bool gpt2_model_quantize(const std::string & fname_inp, const std::string & fname_out, ggml_ftype ftype) { |
| 28 | gpt_vocab vocab; |
| 29 | |
| 30 | printf("%s: loading model from '%s'\n", __func__, fname_inp.c_str()); |
| 31 | |
| 32 | auto finp = std::ifstream(fname_inp, std::ios::binary); |
| 33 | if (!finp) { |
| 34 | fprintf(stderr, "%s: failed to open '%s' for reading\n", __func__, fname_inp.c_str()); |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | auto fout = std::ofstream(fname_out, std::ios::binary); |
| 39 | if (!fout) { |
| 40 | fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname_out.c_str()); |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | // verify magic |
| 45 | { |
| 46 | uint32_t magic; |
| 47 | finp.read((char *) &magic, sizeof(magic)); |
| 48 | if (magic != GGML_FILE_MAGIC) { |
| 49 | fprintf(stderr, "%s: invalid model file '%s' (bad magic)\n", __func__, fname_inp.c_str()); |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | fout.write((char *) &magic, sizeof(magic)); |
| 54 | } |
| 55 | |
| 56 | gpt2_hparams hparams; |
| 57 | |
| 58 | // load hparams |
| 59 | { |
| 60 | finp.read((char *) &hparams.n_vocab, sizeof(hparams.n_vocab)); |
| 61 | finp.read((char *) &hparams.n_ctx, sizeof(hparams.n_ctx)); |
| 62 | finp.read((char *) &hparams.n_embd, sizeof(hparams.n_embd)); |
| 63 | finp.read((char *) &hparams.n_head, sizeof(hparams.n_head)); |
| 64 | finp.read((char *) &hparams.n_layer, sizeof(hparams.n_layer)); |
| 65 | finp.read((char *) &hparams.ftype, sizeof(hparams.ftype)); |
| 66 | |
| 67 | const int32_t qntvr_src = hparams.ftype / GGML_QNT_VERSION_FACTOR; |
| 68 | const int32_t ftype_dst = GGML_QNT_VERSION * GGML_QNT_VERSION_FACTOR + ftype; |
| 69 | |
| 70 | printf("%s: n_vocab = %d\n", __func__, hparams.n_vocab); |
| 71 | printf("%s: n_ctx = %d\n", __func__, hparams.n_ctx); |
| 72 | printf("%s: n_embd = %d\n", __func__, hparams.n_embd); |
| 73 | printf("%s: n_head = %d\n", __func__, hparams.n_head); |
| 74 | printf("%s: n_layer = %d\n", __func__, hparams.n_layer); |
| 75 | printf("%s: ftype (src) = %d\n", __func__, hparams.ftype); |
| 76 | printf("%s: qntvr (src) = %d\n", __func__, qntvr_src); |
| 77 | printf("%s: ftype (dst) = %d\n", __func__, ftype_dst); |
| 78 | printf("%s: qntvr (dst) = %d\n", __func__, GGML_QNT_VERSION); |
| 79 | |
| 80 | fout.write((char *) &hparams.n_vocab, sizeof(hparams.n_vocab)); |
| 81 | fout.write((char *) &hparams.n_ctx, sizeof(hparams.n_ctx)); |
| 82 | fout.write((char *) &hparams.n_embd, sizeof(hparams.n_embd)); |
| 83 | fout.write((char *) &hparams.n_head, sizeof(hparams.n_head)); |
| 84 | fout.write((char *) &hparams.n_layer, sizeof(hparams.n_layer)); |
| 85 | fout.write((char *) &ftype_dst, sizeof(ftype_dst)); |
| 86 | } |
| 87 | |
| 88 | // load vocab |
| 89 | { |
| 90 | int32_t n_vocab = 0; |
| 91 | finp.read ((char *) &n_vocab, sizeof(n_vocab)); |
| 92 | fout.write((char *) &n_vocab, sizeof(n_vocab)); |
| 93 | |
| 94 | if (n_vocab != hparams.n_vocab) { |
| 95 | fprintf(stderr, "%s: invalid model file '%s' (bad vocab size %d != %d)\n", |
| 96 | __func__, fname_inp.c_str(), n_vocab, hparams.n_vocab); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | std::string word; |
| 101 | for (int i = 0; i < n_vocab; i++) { |
| 102 | uint32_t len; |
| 103 | finp.read ((char *) &len, sizeof(len)); |
| 104 | fout.write((char *) &len, sizeof(len)); |
| 105 | |
| 106 | word.resize(len); |
| 107 | finp.read ((char *) word.data(), len); |
| 108 | fout.write((char *) word.data(), len); |
| 109 | |
| 110 | vocab.token_to_id[word] = i; |
| 111 | vocab.id_to_token[i] = word; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // regexes of tensor names to be quantized |
| 116 | const std::vector<std::string> to_quant = { |
| 117 | "model/wte", |
| 118 | "model/lm_head", |
| 119 | "model/h.*/attn/c_attn/w", |
| 120 | "model/h.*/attn/c_proj/w", |
| 121 | "model/h.*/mlp/c_fc/w", |
| 122 | "model/h.*/mlp/c_proj/w", |
| 123 | }; |
| 124 | |
| 125 | if (!ggml_common_quantize_0(finp, fout, ftype, to_quant, {})) { |
| 126 | fprintf(stderr, "%s: failed to quantize model '%s'\n", __func__, fname_inp.c_str()); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | finp.close(); |
| 131 | fout.close(); |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | // usage: |
| 137 | // ./gpt-2-quantize models/gpt-2-117M/ggml-model.bin models/gpt-2-117M/ggml-model-quant.bin type |
| 138 | // |
| 139 | int main(int argc, char ** argv) { |
| 140 | if (argc != 4) { |
| 141 | fprintf(stderr, "usage: %s model-f32.bin model-quant.bin type\n", argv[0]); |
| 142 | ggml_print_ftypes(stderr); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | // needed to initialize f16 tables |
| 147 | { |
| 148 | struct ggml_init_params params = { 0, NULL, false }; |
| 149 | struct ggml_context * ctx = ggml_init(params); |
| 150 | ggml_free(ctx); |
| 151 | } |
| 152 | |
| 153 | const std::string fname_inp = argv[1]; |
| 154 | const std::string fname_out = argv[2]; |
| 155 | |
| 156 | const ggml_ftype ftype = ggml_parse_ftype(argv[3]); |
| 157 | |
| 158 | const int64_t t_main_start_us = ggml_time_us(); |
| 159 | |
| 160 | int64_t t_quantize_us = 0; |
| 161 | |
| 162 | // load the model |
| 163 | { |
| 164 | const int64_t t_start_us = ggml_time_us(); |
| 165 | |
| 166 | if (!gpt2_model_quantize(fname_inp, fname_out, ggml_ftype(ftype))) { |
| 167 | fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str()); |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | t_quantize_us = ggml_time_us() - t_start_us; |
| 172 | } |
| 173 | |
| 174 | // report timing |
| 175 | { |
| 176 | const int64_t t_main_end_us = ggml_time_us(); |
| 177 | |
| 178 | printf("\n"); |
| 179 | printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0f); |
| 180 | printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0f); |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |