gpt_neox_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,246 @@
1
+ #include "common-ggml.h"
2
+
3
+ #include <regex>
4
+ #include <map>
5
+
6
+ static const std::map<std::string, enum ggml_ftype> GGML_FTYPE_MAP = {
7
+ {"q4_0", GGML_FTYPE_MOSTLY_Q4_0},
8
+ {"q4_1", GGML_FTYPE_MOSTLY_Q4_1},
9
+ {"q5_0", GGML_FTYPE_MOSTLY_Q5_0},
10
+ {"q5_1", GGML_FTYPE_MOSTLY_Q5_1},
11
+ {"q8_0", GGML_FTYPE_MOSTLY_Q8_0},
12
+ };
13
+
14
+ void ggml_print_ftypes(FILE * fp) {
15
+ for (auto it = GGML_FTYPE_MAP.begin(); it != GGML_FTYPE_MAP.end(); it++) {
16
+ fprintf(fp, " type = \"%s\" or %d\n", it->first.c_str(), it->second);
17
+ }
18
+ }
19
+
20
+ enum ggml_ftype ggml_parse_ftype(const char * str) {
21
+ enum ggml_ftype ftype;
22
+ if (str[0] == 'q') {
23
+ const auto it = GGML_FTYPE_MAP.find(str);
24
+ if (it == GGML_FTYPE_MAP.end()) {
25
+ fprintf(stderr, "%s: unknown ftype '%s'\n", __func__, str);
26
+ return GGML_FTYPE_UNKNOWN;
27
+ }
28
+ ftype = it->second;
29
+ } else {
30
+ ftype = (enum ggml_ftype) atoi(str);
31
+ }
32
+
33
+ return ftype;
34
+ }
35
+
36
+ bool ggml_common_quantize_0(
37
+ std::ifstream & finp,
38
+ std::ofstream & fout,
39
+ const ggml_ftype ftype,
40
+ const std::vector<std::string> & to_quant,
41
+ const std::vector<std::string> & to_skip) {
42
+
43
+ ggml_type qtype = GGML_TYPE_F32;
44
+
45
+ switch (ftype) {
46
+ case GGML_FTYPE_MOSTLY_Q4_0: qtype = GGML_TYPE_Q4_0; break;
47
+ case GGML_FTYPE_MOSTLY_Q4_1: qtype = GGML_TYPE_Q4_1; break;
48
+ case GGML_FTYPE_MOSTLY_Q5_0: qtype = GGML_TYPE_Q5_0; break;
49
+ case GGML_FTYPE_MOSTLY_Q5_1: qtype = GGML_TYPE_Q5_1; break;
50
+ case GGML_FTYPE_MOSTLY_Q8_0: qtype = GGML_TYPE_Q8_0; break;
51
+ case GGML_FTYPE_UNKNOWN:
52
+ case GGML_FTYPE_ALL_F32:
53
+ case GGML_FTYPE_MOSTLY_F16:
54
+ case GGML_FTYPE_MOSTLY_Q4_1_SOME_F16:
55
+ case GGML_FTYPE_MOSTLY_Q2_K:
56
+ case GGML_FTYPE_MOSTLY_Q3_K:
57
+ case GGML_FTYPE_MOSTLY_Q4_K:
58
+ case GGML_FTYPE_MOSTLY_Q5_K:
59
+ case GGML_FTYPE_MOSTLY_Q6_K:
60
+ {
61
+ fprintf(stderr, "%s: invalid model type %d\n", __func__, ftype);
62
+ return false;
63
+ }
64
+ };
65
+
66
+ if (!ggml_is_quantized(qtype)) {
67
+ fprintf(stderr, "%s: invalid quantization type %d (%s)\n", __func__, qtype, ggml_type_name(qtype));
68
+ return false;
69
+ }
70
+
71
+ size_t total_size_org = 0;
72
+ size_t total_size_new = 0;
73
+
74
+ std::vector<float> work;
75
+
76
+ std::vector<uint8_t> data_u8;
77
+ std::vector<ggml_fp16_t> data_f16;
78
+ std::vector<float> data_f32;
79
+
80
+ std::vector<int64_t> hist_all(1 << 4, 0);
81
+
82
+ while (true) {
83
+ int32_t n_dims;
84
+ int32_t length;
85
+ int32_t ttype;
86
+
87
+ finp.read(reinterpret_cast<char *>(&n_dims), sizeof(n_dims));
88
+ finp.read(reinterpret_cast<char *>(&length), sizeof(length));
89
+ finp.read(reinterpret_cast<char *>(&ttype), sizeof(ttype));
90
+
91
+ if (finp.eof()) {
92
+ break;
93
+ }
94
+
95
+ int32_t nelements = 1;
96
+ int32_t ne[4] = { 1, 1, 1, 1 };
97
+ for (int i = 0; i < n_dims; ++i) {
98
+ finp.read (reinterpret_cast<char *>(&ne[i]), sizeof(ne[i]));
99
+ nelements *= ne[i];
100
+ }
101
+
102
+ std::string name(length, 0);
103
+ finp.read (&name[0], length);
104
+
105
+ printf("%64s - [%5d, %5d, %5d], type = %6s ", name.data(), ne[0], ne[1], ne[2], ggml_type_name((ggml_type) ttype));
106
+
107
+ bool quantize = false;
108
+
109
+ // check if we should quantize this tensor
110
+ for (const auto & s : to_quant) {
111
+ if (std::regex_match(name, std::regex(s))) {
112
+ quantize = true;
113
+ break;
114
+ }
115
+ }
116
+
117
+ // check if we should skip this tensor
118
+ for (const auto & s : to_skip) {
119
+ if (std::regex_match(name, std::regex(s))) {
120
+ quantize = false;
121
+ break;
122
+ }
123
+ }
124
+
125
+ // quantize only 2D tensors
126
+ quantize &= (n_dims == 2);
127
+
128
+ if (quantize) {
129
+ if (ttype != GGML_TYPE_F32 && ttype != GGML_TYPE_F16) {
130
+ fprintf(stderr, "%s: unsupported ttype %d (%s) for integer quantization\n", __func__, ttype, ggml_type_name((ggml_type) ttype));
131
+ return false;
132
+ }
133
+
134
+ if (ttype == GGML_TYPE_F16) {
135
+ data_f16.resize(nelements);
136
+ finp.read(reinterpret_cast<char *>(data_f16.data()), nelements * sizeof(ggml_fp16_t));
137
+ data_f32.resize(nelements);
138
+ for (int i = 0; i < nelements; ++i) {
139
+ data_f32[i] = ggml_fp16_to_fp32(data_f16[i]);
140
+ }
141
+ } else {
142
+ data_f32.resize(nelements);
143
+ finp.read(reinterpret_cast<char *>(data_f32.data()), nelements * sizeof(float));
144
+ }
145
+
146
+ ttype = qtype;
147
+ } else {
148
+ const int bpe = (ttype == 0) ? sizeof(float) : sizeof(uint16_t);
149
+
150
+ data_u8.resize(nelements*bpe);
151
+ finp.read(reinterpret_cast<char *>(data_u8.data()), nelements * bpe);
152
+ }
153
+
154
+ fout.write(reinterpret_cast<char *>(&n_dims), sizeof(n_dims));
155
+ fout.write(reinterpret_cast<char *>(&length), sizeof(length));
156
+ fout.write(reinterpret_cast<char *>(&ttype), sizeof(ttype));
157
+ for (int i = 0; i < n_dims; ++i) {
158
+ fout.write(reinterpret_cast<char *>(&ne[i]), sizeof(ne[i]));
159
+ }
160
+ fout.write(&name[0], length);
161
+
162
+ if (quantize) {
163
+ work.resize(nelements); // for quantization
164
+
165
+ size_t cur_size = 0;
166
+ std::vector<int64_t> hist_cur(1 << 4, 0);
167
+
168
+ switch ((ggml_type) ttype) {
169
+ case GGML_TYPE_Q4_0:
170
+ {
171
+ cur_size = ggml_quantize_q4_0(data_f32.data(), work.data(), nelements, ne[0], hist_cur.data());
172
+ } break;
173
+ case GGML_TYPE_Q4_1:
174
+ {
175
+ cur_size = ggml_quantize_q4_1(data_f32.data(), work.data(), nelements, ne[0], hist_cur.data());
176
+ } break;
177
+ case GGML_TYPE_Q5_0:
178
+ {
179
+ cur_size = ggml_quantize_q5_0(data_f32.data(), work.data(), nelements, ne[0], hist_cur.data());
180
+ } break;
181
+ case GGML_TYPE_Q5_1:
182
+ {
183
+ cur_size = ggml_quantize_q5_1(data_f32.data(), work.data(), nelements, ne[0], hist_cur.data());
184
+ } break;
185
+ case GGML_TYPE_Q8_0:
186
+ {
187
+ cur_size = ggml_quantize_q8_0(data_f32.data(), work.data(), nelements, ne[0], hist_cur.data());
188
+ } break;
189
+ case GGML_TYPE_F32:
190
+ case GGML_TYPE_F16:
191
+ case GGML_TYPE_I8:
192
+ case GGML_TYPE_I16:
193
+ case GGML_TYPE_I32:
194
+ case GGML_TYPE_Q8_1:
195
+ case GGML_TYPE_Q2_K:
196
+ case GGML_TYPE_Q3_K:
197
+ case GGML_TYPE_Q4_K:
198
+ case GGML_TYPE_Q5_K:
199
+ case GGML_TYPE_Q6_K:
200
+ case GGML_TYPE_Q8_K:
201
+ case GGML_TYPE_COUNT:
202
+ {
203
+ fprintf(stderr, "%s: unsupported quantization type %d (%s)\n", __func__, ttype, ggml_type_name((ggml_type) ttype));
204
+ return false;
205
+ }
206
+ }
207
+
208
+ fout.write(reinterpret_cast<char *>(work.data()), cur_size);
209
+ total_size_new += cur_size;
210
+
211
+ printf("size = %8.2f MB -> %8.2f MB | hist: ", nelements * sizeof(float)/1024.0/1024.0, cur_size/1024.0/1024.0);
212
+ for (int i = 0; i < (int) hist_cur.size(); ++i) {
213
+ hist_all[i] += hist_cur[i];
214
+ }
215
+
216
+ for (int i = 0; i < (int) hist_cur.size(); ++i) {
217
+ printf("%5.3f ", hist_cur[i] / (float)nelements);
218
+ }
219
+ printf("\n");
220
+ } else {
221
+ printf("size = %8.3f MB\n", data_u8.size()/1024.0/1024.0);
222
+ fout.write(reinterpret_cast<char *>(data_u8.data()), data_u8.size());
223
+ total_size_new += data_u8.size();
224
+ }
225
+
226
+ total_size_org += nelements * sizeof(float);
227
+ }
228
+
229
+ printf("%s: model size = %8.2f MB\n", __func__, total_size_org/1024.0/1024.0);
230
+ printf("%s: quant size = %8.2f MB | ftype = %d (%s)\n", __func__, total_size_new/1024.0/1024.0, ftype, ggml_type_name(qtype));
231
+
232
+ {
233
+ int64_t sum_all = 0;
234
+ for (int i = 0; i < (int) hist_all.size(); ++i) {
235
+ sum_all += hist_all[i];
236
+ }
237
+
238
+ printf("%s: hist: ", __func__);
239
+ for (int i = 0; i < (int) hist_all.size(); ++i) {
240
+ printf("%5.3f ", hist_all[i] / (float)sum_all);
241
+ }
242
+ printf("\n");
243
+ }
244
+
245
+ return true;
246
+ }
@@ -0,0 +1,18 @@
1
+ #pragma once
2
+
3
+ #include "ggml.h"
4
+
5
+ #include <fstream>
6
+ #include <vector>
7
+ #include <string>
8
+
9
+ enum ggml_ftype ggml_parse_ftype(const char * str);
10
+
11
+ void ggml_print_ftypes(FILE * fp = stderr);
12
+
13
+ bool ggml_common_quantize_0(
14
+ std::ifstream & finp,
15
+ std::ofstream & fout,
16
+ const ggml_ftype ftype,
17
+ const std::vector<std::string> & to_quant,
18
+ const std::vector<std::string> & to_skip);