ilintar commited on
Commit
f83a16c
·
verified ·
1 Parent(s): a17cc09

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. test_problematic_tensors.cpp +236 -3
test_problematic_tensors.cpp CHANGED
@@ -4,12 +4,96 @@
4
  #include "llama.h"
5
  #include "ggml.h"
6
  #include "gguf.h"
 
 
7
 
8
  #include <cstdio>
9
  #include <string>
10
  #include <vector>
11
  #include <numeric>
12
  #include <fstream>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  int main(int argc, char ** argv) {
15
 
@@ -70,7 +154,7 @@ int main(int argc, char ** argv) {
70
  max_cpu = elt > max_cpu ? elt : max_cpu;
71
  min_cpu = elt < min_cpu ? elt : min_cpu;
72
  }
73
- printf("\n CPU sum of matmul: %.8f, max: %.8f, min: %.8f, nelements: %lu\n\n", sum_cpu, max_cpu, min_cpu, ggml_nelements(mul_mat_id_cpu));
74
 
75
  struct ggml_cgraph * gf = ggml_new_graph(gctx);
76
 
@@ -92,7 +176,6 @@ int main(int argc, char ** argv) {
92
 
93
  ggml_gallocr_t cuda_allocr = ggml_gallocr_new(ggml_backend_get_default_buffer_type(cuda));
94
  ggml_gallocr_alloc_graph(cuda_allocr, gf_cuda);
95
-
96
  ggml_backend_graph_compute(cuda, gf_cuda);
97
 
98
  std::vector<float> vec;
@@ -117,6 +200,156 @@ int main(int argc, char ** argv) {
117
  max = elt > max ? elt : max;
118
  min = elt < min ? elt : min;
119
  }
120
- printf("\n CUDA sum of matmul: %.8f, max: %.8f, min: %.8f, max diff: %.8f at pos %lu, nelements: %lu\n\n", sum, max, min, maxdiff, maxdiff_pos, ggml_nelements(mul_mat_id_cuda));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  return 0;
122
  }
 
4
  #include "llama.h"
5
  #include "ggml.h"
6
  #include "gguf.h"
7
+ #include "math.h"
8
+ #include "string.h"
9
 
10
  #include <cstdio>
11
  #include <string>
12
  #include <vector>
13
  #include <numeric>
14
  #include <fstream>
15
+ #include <thread>
16
+ #include <future>
17
+ #include <cmath>
18
+
19
+ static void set_tensor_type(ggml_tensor * tensor, ggml_type type) { // adapted from gguf_set_tensor_type
20
+ const size_t type_size = ggml_type_size(type);
21
+ const int64_t blck_size = ggml_blck_size(type);
22
+
23
+ tensor->type = type;
24
+ GGML_ASSERT(tensor->ne[0] % blck_size == 0 && "tensor row size not divisible by block size of new type");
25
+
26
+ tensor->nb[0] = type_size;
27
+ tensor->nb[1] = tensor->nb[0]*(tensor->ne[0]/blck_size);
28
+ for (int i = 2; i < GGML_MAX_DIMS; i++) {
29
+ tensor->nb[i] = tensor->nb[i - 1]*tensor->ne[i - 1];
30
+ }
31
+ }
32
+
33
+ static void dequantize(ggml_tensor * tensor) { // adapted from llama_tensor_dequantize_impl
34
+ int64_t nelements = ggml_nelements(tensor);
35
+ std::vector<float> output(nelements);
36
+ float * f32_output = (float *) output.data();
37
+
38
+ const ggml_type_traits * qtype = ggml_get_type_traits(tensor->type);
39
+
40
+ uint8_t * data = (uint8_t *) tensor->data;
41
+ std::vector<float> cdata;
42
+ if ((tensor->buffer && !ggml_backend_buffer_is_host(tensor->buffer))) {
43
+ auto n_bytes = ggml_nbytes(tensor);
44
+ cdata.resize(n_bytes);
45
+ ggml_backend_tensor_get(tensor, cdata.data(), 0, n_bytes);
46
+ data = (uint8_t *) cdata.data();
47
+ }
48
+
49
+ if (tensor->type == GGML_TYPE_F16) {
50
+ ggml_fp16_to_fp32_row((ggml_fp16_t *) data, f32_output, nelements);
51
+ } else if (tensor->type == GGML_TYPE_BF16) {
52
+ ggml_bf16_to_fp32_row((ggml_bf16_t *) data, f32_output, nelements);
53
+ } else if (ggml_is_quantized(tensor->type)) {
54
+ qtype->to_float(data, f32_output, nelements);
55
+ } else {
56
+ GGML_ABORT("fatal error"); // unreachable
57
+ }
58
+
59
+ set_tensor_type(tensor, GGML_TYPE_F32);
60
+ float * new_data = (float *) malloc(output.size() * sizeof(float));
61
+ memcpy(new_data, output.data(), output.size() * sizeof(float));
62
+ tensor->data = new_data;
63
+ double sum = 0.0f;
64
+ float min = ((float *) tensor->data)[0];
65
+ float max = ((float *) tensor->data)[0];
66
+ for (int64_t i = 0; i < ggml_nelements(tensor); i++) {
67
+ float elt = ((float *) tensor->data)[i];
68
+ if (isnan(elt) || isinf(elt)) {
69
+ GGML_ABORT("NaN or Inf found at position %ld", i);
70
+ }
71
+ sum += elt;
72
+ if (elt < min) min = elt;
73
+ if (elt > max) max = elt;
74
+ }
75
+ printf("\nSanity check: dequantized tensor has sum = %.8f, min = %.8f, max = %.8f\n", sum, min, max);
76
+ }
77
+
78
+ static void quantize(ggml_tensor * tensor, const float * source_data, ggml_type type) {
79
+ int64_t nelements = ggml_nelements(tensor);
80
+
81
+ size_t blck_size = tensor->ne[0];
82
+ size_t n_blocks = tensor->ne[1];
83
+ size_t n_experts = tensor->ne[2];
84
+
85
+ size_t expert_size = ggml_row_size(type, n_blocks * blck_size);
86
+ std::vector<uint8_t> dataq(ggml_row_size(type, n_blocks * blck_size * n_experts));
87
+
88
+ printf("Quantizing to %s", ggml_type_name(type));
89
+ for (size_t i = 0; i < n_experts; i++) {
90
+ printf(".");
91
+ ggml_quantize_chunk(type, source_data + (n_blocks * blck_size) * i, dataq.data() + expert_size * i, 0, n_blocks, blck_size, nullptr);
92
+ }
93
+ printf(" DONE\n");
94
+ set_tensor_type(tensor, type);
95
+ ggml_backend_tensor_set(tensor, dataq.data(), 0, dataq.size());
96
+ }
97
 
98
  int main(int argc, char ** argv) {
99
 
 
154
  max_cpu = elt > max_cpu ? elt : max_cpu;
155
  min_cpu = elt < min_cpu ? elt : min_cpu;
156
  }
157
+ printf("\nCPU sum of matmul: %.8f, max: %.8f, min: %.8f, nelements: %lu\n\n", sum_cpu, max_cpu, min_cpu, ggml_nelements(mul_mat_id_cpu));
158
 
159
  struct ggml_cgraph * gf = ggml_new_graph(gctx);
160
 
 
176
 
177
  ggml_gallocr_t cuda_allocr = ggml_gallocr_new(ggml_backend_get_default_buffer_type(cuda));
178
  ggml_gallocr_alloc_graph(cuda_allocr, gf_cuda);
 
179
  ggml_backend_graph_compute(cuda, gf_cuda);
180
 
181
  std::vector<float> vec;
 
200
  max = elt > max ? elt : max;
201
  min = elt < min ? elt : min;
202
  }
203
+ printf("CUDA sum of matmul: %.8f, max: %.8f, min: %.8f, max diff: %.8f at pos %lu, nelements: %lu\n\n", sum, max, min, maxdiff, maxdiff_pos, ggml_nelements(mul_mat_id_cuda));
204
+ ggml_gallocr_free(cuda_allocr);
205
+
206
+ dequantize(weights);
207
+
208
+ ggml_context * gctx_cpu_comp_deq = ggml_init(params);
209
+ struct ggml_cgraph * gf_cpu_deq = ggml_new_graph(gctx_cpu_comp_deq);
210
+ ggml_tensor * mul_mat_id_cpu_deq = ggml_mul_mat_id(gctx_cpu_comp_deq, weights, norm, ids);
211
+ ggml_build_forward_expand(gf_cpu_deq, mul_mat_id_cpu_deq);
212
+
213
+ ggml_gallocr_t allocr_deq = ggml_gallocr_new(ggml_backend_get_default_buffer_type(cpu));
214
+ ggml_gallocr_alloc_graph(allocr_deq, gf_cpu_deq);
215
+
216
+ ggml_backend_graph_compute(cpu, gf_cpu_deq);
217
+
218
+ double sum_cpu_deq = 0.0f;
219
+ float max_cpu_deq = ((float *) mul_mat_id_cpu_deq->data)[0];
220
+ float min_cpu_deq = ((float *) mul_mat_id_cpu_deq->data)[0];
221
+ for (uint64_t i = 0; i < ggml_nelements(mul_mat_id_cpu_deq); i++) {
222
+ float elt = ((float *) mul_mat_id_cpu_deq->data)[i];
223
+ sum_cpu_deq += elt;
224
+ max_cpu_deq = elt > max_cpu_deq ? elt : max_cpu_deq;
225
+ min_cpu_deq = elt < min_cpu_deq ? elt : min_cpu_deq;
226
+ }
227
+ printf("\nCPU sum of matmul (dequantized): %.8f, max: %.8f, min: %.8f, nelements: %lu\n\n", sum_cpu_deq, max_cpu_deq, min_cpu_deq, ggml_nelements(mul_mat_id_cpu_deq));
228
+ ggml_gallocr_free(allocr_deq);
229
+
230
+ ggml_context * gctx_cuda_comp_deq = ggml_init(params);
231
+ ggml_context * gctx_cuda_dequant = ggml_init(params);
232
+
233
+ struct ggml_cgraph * gf_cuda_deq = ggml_new_graph(gctx_cuda_comp_deq);
234
+ ggml_tensor * w_cuda_deq = ggml_new_tensor_4d(gctx_cuda_comp_deq, GGML_TYPE_F32, weights->ne[0], weights->ne[1], weights->ne[2], weights->ne[3]);
235
+ ggml_backend_alloc_ctx_tensors(gctx_cuda_comp_deq, cuda);
236
+ ggml_backend_tensor_set(w_cuda_deq, weights->data, 0, ggml_nbytes(weights));
237
+
238
+ ggml_tensor * mul_mat_id_cuda_deq = ggml_mul_mat_id(gctx_cuda_comp_deq, w_cuda_deq, n_cuda, i_cuda);
239
+ ggml_build_forward_expand(gf_cuda_deq, mul_mat_id_cuda_deq);
240
+
241
+ ggml_gallocr_t allocr_cuda_deq = ggml_gallocr_new(ggml_backend_get_default_buffer_type(cuda));
242
+ ggml_gallocr_alloc_graph(allocr_cuda_deq, gf_cuda_deq);
243
+
244
+ ggml_backend_graph_compute(cuda, gf_cuda_deq);
245
+
246
+ std::vector<float> vec_deq(ggml_nbytes(mul_mat_id_cuda_deq));
247
+ ggml_backend_tensor_get(mul_mat_id_cuda_deq, vec_deq.data(), 0, n_bytes);
248
+
249
+ double sum_cuda_deq = 0.0f;
250
+ float max_cuda_deq = vec_deq[0];
251
+ float min_cuda_deq = vec_deq[0];
252
+ for (uint64_t i = 0; i < vec_deq.size(); i++) {
253
+ float elt = vec_deq[i];
254
+ sum_cuda_deq += elt;
255
+ max_cuda_deq = elt > max_cpu_deq ? elt : max_cpu_deq;
256
+ min_cuda_deq = elt < min_cpu_deq ? elt : min_cpu_deq;
257
+ }
258
+ printf("\nCUDA sum of matmul (dequantized): %.8f, max: %.8f, min: %.8f, nelements: %lu\n\n", sum_cuda_deq, max_cuda_deq, min_cuda_deq, ggml_nelements(mul_mat_id_cuda_deq));
259
+ ggml_gallocr_free(allocr_cuda_deq);
260
+ ggml_free(gctx_cuda_comp_deq);
261
+ ggml_free(gctx_cuda_dequant);
262
+
263
+ /*ggml_type test_quantizations[] = { GGML_TYPE_IQ2_S, GGML_TYPE_IQ3_XXS, GGML_TYPE_IQ3_S, GGML_TYPE_IQ4_NL, GGML_TYPE_Q4_1 };
264
+ for (int i = 0; i < 6; i++) {
265
+ std::vector<float> qdata;
266
+ {
267
+ ggml_context * gctx_cuda_quant = ggml_init(params);
268
+ ggml_context * gctx_cuda_req_comp = ggml_init(params);
269
+ ggml_tensor * w_cuda_qt = ggml_new_tensor_4d(gctx_cuda_quant, test_quantizations[i], weights->ne[0], weights->ne[1], weights->ne[2], weights->ne[3]);
270
+ ggml_backend_alloc_ctx_tensors(gctx_cuda_quant, cuda);
271
+ quantize(w_cuda_qt, (const float *) weights->data, test_quantizations[i]);
272
+ qdata.resize(ggml_nbytes(w_cuda_qt));
273
+ ggml_backend_tensor_get(w_cuda_qt, qdata.data(), 0, qdata.size());
274
+
275
+ struct ggml_cgraph * gf_cuda_req = ggml_new_graph(gctx_cuda_req_comp);
276
+ ggml_tensor * mul_mat_id_cuda_req = ggml_mul_mat_id(gctx_cuda_comp, w_cuda_qt, n_cuda, i_cuda);
277
+ ggml_build_forward_expand(gf_cuda_req, mul_mat_id_cuda_req);
278
+
279
+ ggml_gallocr_t cuda_allocr_req = ggml_gallocr_new(ggml_backend_get_default_buffer_type(cuda));
280
+ ggml_gallocr_alloc_graph(cuda_allocr_req, gf_cuda_req);
281
+ ggml_backend_graph_compute(cuda, gf_cuda_req);
282
+
283
+ std::vector<float> vec;
284
+
285
+ auto n_bytes = ggml_nbytes(mul_mat_id_cuda_req);
286
+ vec.resize(n_bytes);
287
+ ggml_backend_tensor_get(mul_mat_id_cuda_req, vec.data(), 0, n_bytes);
288
+ double sum = 0.0f;
289
+ float max = vec[0];
290
+ float min = vec[0];
291
+ float maxdiff = 0;
292
+ uint64_t maxdiff_pos = -1;
293
+ for (uint64_t i = 0; i < ggml_nelements(mul_mat_id_cuda_req); i++) {
294
+ float elt = vec[i];
295
+ float org_elt = ((float *) mul_mat_id_cpu->data)[i];
296
+ float diff = fabs(elt - org_elt);
297
+ if (diff > maxdiff) {
298
+ maxdiff = diff;
299
+ maxdiff_pos = i;
300
+ }
301
+ sum += elt;
302
+ max = elt > max ? elt : max;
303
+ min = elt < min ? elt : min;
304
+ }
305
+ printf("CUDA sum of quant %s matmul: %.8f, max: %.8f, min: %.8f, max diff: %.8f at pos %lu, nelements: %lu\n\n", ggml_type_name(test_quantizations[i]), sum, max, min, maxdiff, maxdiff_pos, ggml_nelements(mul_mat_id_cuda_req));
306
+ ggml_gallocr_free(cuda_allocr_req);
307
+ ggml_free(gctx_cuda_quant);
308
+ ggml_free(gctx_cuda_req_comp);
309
+ }
310
+ {
311
+ ggml_context * gctx_cpu_quant = ggml_init(params);
312
+ ggml_context * gctx_cpu_req_comp = ggml_init(params);
313
+ ggml_tensor * w_cpu_qt = ggml_new_tensor_4d(gctx_cpu_quant, test_quantizations[i], weights->ne[0], weights->ne[1], weights->ne[2], weights->ne[3]);
314
+ ggml_backend_alloc_ctx_tensors(gctx_cpu_quant, cpu);
315
+ set_tensor_type(w_cpu_qt, test_quantizations[i]);
316
+ w_cpu_qt->data = qdata.data();
317
+
318
+ struct ggml_cgraph * gf_cpu_req = ggml_new_graph(gctx_cpu_req_comp);
319
+ ggml_tensor * mul_mat_id_cpu_req = ggml_mul_mat_id(gctx_cpu_comp, w_cpu_qt, norm, ids);
320
+ ggml_build_forward_expand(gf_cpu_req, mul_mat_id_cpu_req);
321
+
322
+ ggml_gallocr_t cpu_allocr_req = ggml_gallocr_new(ggml_backend_get_default_buffer_type(cpu));
323
+ ggml_gallocr_alloc_graph(cpu_allocr_req, gf_cpu_req);
324
+ ggml_backend_graph_compute(cuda, gf_cpu_req);
325
+
326
+ std::vector<float> vec;
327
+
328
+ auto n_bytes = ggml_nbytes(mul_mat_id_cpu_req);
329
+ vec.resize(n_bytes);
330
+ ggml_backend_tensor_get(mul_mat_id_cpu_req, vec.data(), 0, n_bytes);
331
+ double sum = 0.0f;
332
+ float max = vec[0];
333
+ float min = vec[0];
334
+ float maxdiff = 0;
335
+ uint64_t maxdiff_pos = -1;
336
+ for (uint64_t i = 0; i < ggml_nelements(mul_mat_id_cpu_req); i++) {
337
+ float elt = vec[i];
338
+ float org_elt = ((float *) mul_mat_id_cpu->data)[i];
339
+ float diff = fabs(elt - org_elt);
340
+ if (diff > maxdiff) {
341
+ maxdiff = diff;
342
+ maxdiff_pos = i;
343
+ }
344
+ sum += elt;
345
+ max = elt > max ? elt : max;
346
+ min = elt < min ? elt : min;
347
+ }
348
+ printf("CPU sum of quant %s matmul: %.8f, max: %.8f, min: %.8f, max diff: %.8f at pos %lu, nelements: %lu\n\n", ggml_type_name(test_quantizations[i]), sum, max, min, maxdiff, maxdiff_pos, ggml_nelements(mul_mat_id_cpu_req));
349
+ ggml_gallocr_free(cpu_allocr_req);
350
+ ggml_free(gctx_cpu_quant);
351
+ ggml_free(gctx_cpu_req_comp);
352
+ }
353
+ }*/
354
  return 0;
355
  }