interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / examples / gpt-j / quantize.cpp

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